Revision f0990e0c

b/test/ganeti.utils_unittest.py
42 42
from ganeti.errors import LockError, UnitParseError
43 43

  
44 44

  
45
class GanetiTestCase(unittest.TestCase):
46
  def assertFileContent(self, file_name, content):
47
    """Checks the content of a file.
48

  
49
    """
50
    handle = open(file_name, 'r')
51
    try:
52
      self.assertEqual(handle.read(), content)
53
    finally:
54
      handle.close()
55

  
56

  
45 57
class TestIsProcessAlive(unittest.TestCase):
46 58
  """Testing case for IsProcessAlive"""
47 59
  def setUp(self):
......
334 346
      self.assertRaises(UnitParseError, ParseUnit, '1,3' + suffix)
335 347

  
336 348

  
337
class TestSshKeys(unittest.TestCase):
349
class TestSshKeys(GanetiTestCase):
338 350
  """Test case for the AddAuthorizedKey function"""
339 351

  
340 352
  KEY_A = 'ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a'
341 353
  KEY_B = ('command="/usr/bin/fooserver -t --verbose",from="1.2.3.4" '
342 354
           'ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b')
343 355

  
344
  # NOTE: The MD5 sums below were calculated after manually
345
  #       checking the output files.
346

  
347 356
  def writeTestFile(self):
348 357
    (fd, tmpname) = tempfile.mkstemp(prefix = 'ganeti-test')
349 358
    f = os.fdopen(fd, 'w')
......
362 371
    try:
363 372
      AddAuthorizedKey(tmpname, 'ssh-dss AAAAB3NzaC1kc3MAAACB root@test')
364 373

  
365
      f = open(tmpname, 'r')
366
      try:
367
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
368
                         'ccc71523108ca6e9d0343797dc3e9f16')
369
      finally:
370
        f.close()
374
      self.assertFileContent(tmpname,
375
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
376
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
377
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n"
378
        "ssh-dss AAAAB3NzaC1kc3MAAACB root@test\n")
371 379
    finally:
372 380
      os.unlink(tmpname)
373 381

  
......
377 385
      AddAuthorizedKey(tmpname,
378 386
          'ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@test')
379 387

  
380
      f = open(tmpname, 'r')
381
      try:
382
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
383
                         'f2c939d57addb5b3a6846884be896b46')
384
      finally:
385
        f.close()
388
      self.assertFileContent(tmpname,
389
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
390
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
391
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n"
392
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@test\n")
386 393
    finally:
387 394
      os.unlink(tmpname)
388 395

  
......
392 399
      AddAuthorizedKey(tmpname,
393 400
          'ssh-dss  AAAAB3NzaC1w5256closdj32mZaQU   root@key-a')
394 401

  
395
      f = open(tmpname, 'r')
396
      try:
397
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
398
                         '4e612764808bd46337eb0f575415fc30')
399
      finally:
400
        f.close()
402
      self.assertFileContent(tmpname,
403
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
404
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
405
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
401 406
    finally:
402 407
      os.unlink(tmpname)
403 408

  
......
407 412
      RemoveAuthorizedKey(tmpname,
408 413
          'ssh-dss  AAAAB3NzaC1w5256closdj32mZaQU   root@key-a')
409 414

  
410
      f = open(tmpname, 'r')
411
      try:
412
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
413
                         '77516d987fca07f70e30b830b3e4f2ed')
414
      finally:
415
        f.close()
415
      self.assertFileContent(tmpname,
416
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
417
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
416 418
    finally:
417 419
      os.unlink(tmpname)
418 420

  
......
422 424
      RemoveAuthorizedKey(tmpname,
423 425
          'ssh-dss  AAAAB3Nsdfj230xxjxJjsjwjsjdjU   root@test')
424 426

  
425
      f = open(tmpname, 'r')
426
      try:
427
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
428
                         '4e612764808bd46337eb0f575415fc30')
429
      finally:
430
        f.close()
427
      self.assertFileContent(tmpname,
428
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
429
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
430
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
431 431
    finally:
432 432
      os.unlink(tmpname)
433 433

  
434 434

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

  
438 438
  def writeTestFile(self):
......
452 452
    try:
453 453
      SetEtcHostsEntry(tmpname, '1.2.3.4', 'myhost.domain.tld', ['myhost'])
454 454

  
455
      f = open(tmpname, 'r')
456
      try:
457
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
458
                         '284c3454c158d4c4284eeb59910ab00b')
459
      finally:
460
        f.close()
455
      self.assertFileContent(tmpname,
456
        "# This is a test file for /etc/hosts\n"
457
        "127.0.0.1\tlocalhost\n"
458
        "192.168.1.1 router gw\n"
459
        "1.2.3.4\tmyhost.domain.tld myhost\n")
461 460
    finally:
462 461
      os.unlink(tmpname)
463 462

  
......
466 465
    try:
467 466
      SetEtcHostsEntry(tmpname, '192.168.1.1', 'myhost.domain.tld', ['myhost'])
468 467

  
469
      f = open(tmpname, 'r')
470
      try:
471
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
472
                         '1486c19f1fcb626f893c243e3ce38c8d')
473
      finally:
474
        f.close()
468
      self.assertFileContent(tmpname,
469
        "# This is a test file for /etc/hosts\n"
470
        "127.0.0.1\tlocalhost\n"
471
        "192.168.1.1\tmyhost.domain.tld myhost\n")
475 472
    finally:
476 473
      os.unlink(tmpname)
477 474

  
......
480 477
    try:
481 478
      RemoveEtcHostsEntry(tmpname, 'router')
482 479

  
483
      f = open(tmpname, 'r')
484
      try:
485
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
486
                         '8b09207a23709d60240674601a3548b2')
487
      finally:
488
        f.close()
480
      self.assertFileContent(tmpname,
481
        "# This is a test file for /etc/hosts\n"
482
        "127.0.0.1\tlocalhost\n"
483
        "192.168.1.1 gw\n")
489 484
    finally:
490 485
      os.unlink(tmpname)
491 486

  
......
494 489
    try:
495 490
      RemoveEtcHostsEntry(tmpname, 'localhost')
496 491

  
497
      f = open(tmpname, 'r')
498
      try:
499
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
500
                         'ec4e4589b56f82fdb88db5675de011b1')
501
      finally:
502
        f.close()
492
      self.assertFileContent(tmpname,
493
        "# This is a test file for /etc/hosts\n"
494
        "192.168.1.1 router gw\n")
503 495
    finally:
504 496
      os.unlink(tmpname)
505 497

  
......
508 500
    try:
509 501
      RemoveEtcHostsEntry(tmpname, 'myhost')
510 502

  
511
      f = open(tmpname, 'r')
512
      try:
513
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
514
                         'aa005bddc6d9ee399c296953f194929e')
515
      finally:
516
        f.close()
503
      self.assertFileContent(tmpname,
504
        "# This is a test file for /etc/hosts\n"
505
        "127.0.0.1\tlocalhost\n"
506
        "192.168.1.1 router gw\n")
517 507
    finally:
518 508
      os.unlink(tmpname)
519 509

  
......
522 512
    try:
523 513
      RemoveEtcHostsEntry(tmpname, 'gw')
524 514

  
525
      f = open(tmpname, 'r')
526
      try:
527
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
528
                         '156dd3980a17b2ef934e2d0bf670aca2')
529
      finally:
530
        f.close()
515
      self.assertFileContent(tmpname,
516
        "# This is a test file for /etc/hosts\n"
517
        "127.0.0.1\tlocalhost\n"
518
        "192.168.1.1 router\n")
531 519
    finally:
532 520
      os.unlink(tmpname)
533 521

  

Also available in: Unified diff