Revision 17b97ab3 test/ganeti.utils_unittest.py

b/test/ganeti.utils_unittest.py
45 45
from ganeti import errors
46 46
from ganeti.utils import RunCmd, \
47 47
     FirstFree, \
48
     RunParts, \
49
     SetEtcHostsEntry, RemoveEtcHostsEntry
48
     RunParts
50 49

  
51 50

  
52 51
class TestIsProcessAlive(unittest.TestCase):
......
575 574
      self.assertRaises(errors.ParseError, utils.ParseCpuMask, data)
576 575

  
577 576

  
578
class TestEtcHosts(testutils.GanetiTestCase):
579
  """Test functions modifying /etc/hosts"""
580

  
581
  def setUp(self):
582
    testutils.GanetiTestCase.setUp(self)
583
    self.tmpname = self._CreateTempFile()
584
    handle = open(self.tmpname, 'w')
585
    try:
586
      handle.write('# This is a test file for /etc/hosts\n')
587
      handle.write('127.0.0.1\tlocalhost\n')
588
      handle.write('192.0.2.1 router gw\n')
589
    finally:
590
      handle.close()
591

  
592
  def testSettingNewIp(self):
593
    SetEtcHostsEntry(self.tmpname, '198.51.100.4', 'myhost.example.com',
594
                     ['myhost'])
595

  
596
    self.assertFileContent(self.tmpname,
597
      "# This is a test file for /etc/hosts\n"
598
      "127.0.0.1\tlocalhost\n"
599
      "192.0.2.1 router gw\n"
600
      "198.51.100.4\tmyhost.example.com myhost\n")
601
    self.assertFileMode(self.tmpname, 0644)
602

  
603
  def testSettingExistingIp(self):
604
    SetEtcHostsEntry(self.tmpname, '192.0.2.1', 'myhost.example.com',
605
                     ['myhost'])
606

  
607
    self.assertFileContent(self.tmpname,
608
      "# This is a test file for /etc/hosts\n"
609
      "127.0.0.1\tlocalhost\n"
610
      "192.0.2.1\tmyhost.example.com myhost\n")
611
    self.assertFileMode(self.tmpname, 0644)
612

  
613
  def testSettingDuplicateName(self):
614
    SetEtcHostsEntry(self.tmpname, '198.51.100.4', 'myhost', ['myhost'])
615

  
616
    self.assertFileContent(self.tmpname,
617
      "# This is a test file for /etc/hosts\n"
618
      "127.0.0.1\tlocalhost\n"
619
      "192.0.2.1 router gw\n"
620
      "198.51.100.4\tmyhost\n")
621
    self.assertFileMode(self.tmpname, 0644)
622

  
623
  def testRemovingExistingHost(self):
624
    RemoveEtcHostsEntry(self.tmpname, 'router')
625

  
626
    self.assertFileContent(self.tmpname,
627
      "# This is a test file for /etc/hosts\n"
628
      "127.0.0.1\tlocalhost\n"
629
      "192.0.2.1 gw\n")
630
    self.assertFileMode(self.tmpname, 0644)
631

  
632
  def testRemovingSingleExistingHost(self):
633
    RemoveEtcHostsEntry(self.tmpname, 'localhost')
634

  
635
    self.assertFileContent(self.tmpname,
636
      "# This is a test file for /etc/hosts\n"
637
      "192.0.2.1 router gw\n")
638
    self.assertFileMode(self.tmpname, 0644)
639

  
640
  def testRemovingNonExistingHost(self):
641
    RemoveEtcHostsEntry(self.tmpname, 'myhost')
642

  
643
    self.assertFileContent(self.tmpname,
644
      "# This is a test file for /etc/hosts\n"
645
      "127.0.0.1\tlocalhost\n"
646
      "192.0.2.1 router gw\n")
647
    self.assertFileMode(self.tmpname, 0644)
648

  
649
  def testRemovingAlias(self):
650
    RemoveEtcHostsEntry(self.tmpname, 'gw')
651

  
652
    self.assertFileContent(self.tmpname,
653
      "# This is a test file for /etc/hosts\n"
654
      "127.0.0.1\tlocalhost\n"
655
      "192.0.2.1 router\n")
656
    self.assertFileMode(self.tmpname, 0644)
657

  
658

  
659 577
class TestGetMounts(unittest.TestCase):
660 578
  """Test case for GetMounts()."""
661 579

  

Also available in: Unified diff