Revision 899d2a81 test/ganeti.utils_unittest.py

b/test/ganeti.utils_unittest.py
37 37
from ganeti.utils import IsProcessAlive, Lock, Unlock, RunCmd, \
38 38
     RemoveFile, CheckDict, MatchNameComponent, FormatUnit, \
39 39
     ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \
40
     ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles
40
     ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \
41
     AddEtcHostsEntry, RemoveEtcHostsEntry
41 42
from ganeti.errors import LockError, UnitParseError
42 43

  
43 44

  
......
431 432
      os.unlink(tmpname)
432 433

  
433 434

  
435
class TestEtcHosts(unittest.TestCase):
436
  """Test functions modifying /etc/hosts"""
437

  
438
  def writeTestFile(self):
439
    (fd, tmpname) = tempfile.mkstemp(prefix = 'ganeti-test')
440
    f = os.fdopen(fd, 'w')
441
    try:
442
      f.write('# This is a test file for /etc/hosts\n')
443
      f.write('127.0.0.1\tlocalhost\n')
444
      f.write('192.168.1.1 router gw\n')
445
    finally:
446
      f.close()
447

  
448
    return tmpname
449

  
450
  def testAddingNewIp(self):
451
    tmpname = self.writeTestFile()
452
    try:
453
      AddEtcHostsEntry(tmpname, 'myhost.domain.tld', '1.2.3.4')
454

  
455
      f = open(tmpname, 'r')
456
      try:
457
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
458
                         '00e0e88250482e7449743c89a49e9349')
459
      finally:
460
        f.close()
461
    finally:
462
      os.unlink(tmpname)
463

  
464
  def testAddingExistingIp(self):
465
    tmpname = self.writeTestFile()
466
    try:
467
      AddEtcHostsEntry(tmpname, 'myhost.domain.tld', '192.168.1.1')
468

  
469
      f = open(tmpname, 'r')
470
      try:
471
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
472
                         '4dc04c0acdd247175e0b980c6beea822')
473
      finally:
474
        f.close()
475
    finally:
476
      os.unlink(tmpname)
477

  
478
  def testRemovingExistingHost(self):
479
    tmpname = self.writeTestFile()
480
    try:
481
      RemoveEtcHostsEntry(tmpname, 'router')
482

  
483
      f = open(tmpname, 'r')
484
      try:
485
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
486
                         '7d1e7a559eedbc25e0dff67d33ccac84')
487
      finally:
488
        f.close()
489
    finally:
490
      os.unlink(tmpname)
491

  
492
  def testRemovingSingleExistingHost(self):
493
    tmpname = self.writeTestFile()
494
    try:
495
      RemoveEtcHostsEntry(tmpname, 'localhost')
496

  
497
      f = open(tmpname, 'r')
498
      try:
499
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
500
                         'ec4e4589b56f82fdb88db5675de011b1')
501
      finally:
502
        f.close()
503
    finally:
504
      os.unlink(tmpname)
505

  
506
  def testRemovingNonExistingHost(self):
507
    tmpname = self.writeTestFile()
508
    try:
509
      RemoveEtcHostsEntry(tmpname, 'myhost')
510

  
511
      f = open(tmpname, 'r')
512
      try:
513
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
514
                         'aa005bddc6d9ee399c296953f194929e')
515
      finally:
516
        f.close()
517
    finally:
518
      os.unlink(tmpname)
519

  
520

  
434 521
class TestShellQuoting(unittest.TestCase):
435 522
  """Test case for shell quoting functions"""
436 523

  

Also available in: Unified diff