Revision ebe8ef17

b/test/ganeti.utils_unittest.py
354 354
  KEY_B = ('command="/usr/bin/fooserver -t --verbose",from="1.2.3.4" '
355 355
           'ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b')
356 356

  
357
  def writeTestFile(self):
358
    (fd, tmpname) = tempfile.mkstemp(prefix = 'ganeti-test')
359
    f = os.fdopen(fd, 'w')
357
  def setUp(self):
358
    (fd, self.tmpname) = tempfile.mkstemp(prefix='ganeti-test')
360 359
    try:
361
      f.write(TestSshKeys.KEY_A)
362
      f.write("\n")
363
      f.write(TestSshKeys.KEY_B)
364
      f.write("\n")
365
    finally:
366
      f.close()
360
      handle = os.fdopen(fd, 'w')
361
      try:
362
        handle.write("%s\n" % TestSshKeys.KEY_A)
363
        handle.write("%s\n" % TestSshKeys.KEY_B)
364
      finally:
365
        handle.close()
366
    except:
367
      utils.RemoveFile(self.tmpname)
368
      raise
367 369

  
368
    return tmpname
370
  def tearDown(self):
371
    utils.RemoveFile(self.tmpname)
372
    del self.tmpname
369 373

  
370 374
  def testAddingNewKey(self):
371
    tmpname = self.writeTestFile()
372
    try:
373
      AddAuthorizedKey(tmpname, 'ssh-dss AAAAB3NzaC1kc3MAAACB root@test')
375
    AddAuthorizedKey(self.tmpname, 'ssh-dss AAAAB3NzaC1kc3MAAACB root@test')
374 376

  
375
      self.assertFileContent(tmpname,
376
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
377
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
378
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n"
379
        "ssh-dss AAAAB3NzaC1kc3MAAACB root@test\n")
380
    finally:
381
      os.unlink(tmpname)
377
    self.assertFileContent(self.tmpname,
378
      "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
379
      'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
380
      " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n"
381
      "ssh-dss AAAAB3NzaC1kc3MAAACB root@test\n")
382 382

  
383 383
  def testAddingAlmostButNotCompletlyTheSameKey(self):
384
    tmpname = self.writeTestFile()
385
    try:
386
      AddAuthorizedKey(tmpname,
387
          'ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@test')
388

  
389
      self.assertFileContent(tmpname,
390
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
391
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
392
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n"
393
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@test\n")
394
    finally:
395
      os.unlink(tmpname)
384
    AddAuthorizedKey(self.tmpname,
385
        'ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@test')
386

  
387
    self.assertFileContent(self.tmpname,
388
      "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
389
      'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
390
      " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n"
391
      "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@test\n")
396 392

  
397 393
  def testAddingExistingKeyWithSomeMoreSpaces(self):
398
    tmpname = self.writeTestFile()
399
    try:
400
      AddAuthorizedKey(tmpname,
401
          'ssh-dss  AAAAB3NzaC1w5256closdj32mZaQU   root@key-a')
394
    AddAuthorizedKey(self.tmpname,
395
        'ssh-dss  AAAAB3NzaC1w5256closdj32mZaQU   root@key-a')
402 396

  
403
      self.assertFileContent(tmpname,
404
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
405
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
406
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
407
    finally:
408
      os.unlink(tmpname)
397
    self.assertFileContent(self.tmpname,
398
      "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
399
      'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
400
      " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
409 401

  
410 402
  def testRemovingExistingKeyWithSomeMoreSpaces(self):
411
    tmpname = self.writeTestFile()
412
    try:
413
      RemoveAuthorizedKey(tmpname,
414
          'ssh-dss  AAAAB3NzaC1w5256closdj32mZaQU   root@key-a')
403
    RemoveAuthorizedKey(self.tmpname,
404
        'ssh-dss  AAAAB3NzaC1w5256closdj32mZaQU   root@key-a')
415 405

  
416
      self.assertFileContent(tmpname,
417
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
418
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
419
    finally:
420
      os.unlink(tmpname)
406
    self.assertFileContent(self.tmpname,
407
      'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
408
      " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
421 409

  
422 410
  def testRemovingNonExistingKey(self):
423
    tmpname = self.writeTestFile()
424
    try:
425
      RemoveAuthorizedKey(tmpname,
426
          'ssh-dss  AAAAB3Nsdfj230xxjxJjsjwjsjdjU   root@test')
411
    RemoveAuthorizedKey(self.tmpname,
412
        'ssh-dss  AAAAB3Nsdfj230xxjxJjsjwjsjdjU   root@test')
427 413

  
428
      self.assertFileContent(tmpname,
429
        "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
430
        'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
431
        " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
432
    finally:
433
      os.unlink(tmpname)
414
    self.assertFileContent(self.tmpname,
415
      "ssh-dss AAAAB3NzaC1w5256closdj32mZaQU root@key-a\n"
416
      'command="/usr/bin/fooserver -t --verbose",from="1.2.3.4"'
417
      " ssh-dss AAAAB3NzaC1w520smc01ms0jfJs22 root@key-b\n")
434 418

  
435 419

  
436 420
class TestEtcHosts(GanetiTestCase):
437 421
  """Test functions modifying /etc/hosts"""
438 422

  
439
  def writeTestFile(self):
440
    (fd, tmpname) = tempfile.mkstemp(prefix = 'ganeti-test')
441
    f = os.fdopen(fd, 'w')
423
  def setUp(self):
424
    (fd, self.tmpname) = tempfile.mkstemp(prefix='ganeti-test')
442 425
    try:
443
      f.write('# This is a test file for /etc/hosts\n')
444
      f.write('127.0.0.1\tlocalhost\n')
445
      f.write('192.168.1.1 router gw\n')
446
    finally:
447
      f.close()
426
      handle = os.fdopen(fd, 'w')
427
      try:
428
        handle.write('# This is a test file for /etc/hosts\n')
429
        handle.write('127.0.0.1\tlocalhost\n')
430
        handle.write('192.168.1.1 router gw\n')
431
      finally:
432
        handle.close()
433
    except:
434
      utils.RemoveFile(self.tmpname)
435
      raise
448 436

  
449
    return tmpname
437
  def tearDown(self):
438
    utils.RemoveFile(self.tmpname)
439
    del self.tmpname
450 440

  
451 441
  def testSettingNewIp(self):
452
    tmpname = self.writeTestFile()
453
    try:
454
      SetEtcHostsEntry(tmpname, '1.2.3.4', 'myhost.domain.tld', ['myhost'])
442
    SetEtcHostsEntry(self.tmpname, '1.2.3.4', 'myhost.domain.tld', ['myhost'])
455 443

  
456
      self.assertFileContent(tmpname,
457
        "# This is a test file for /etc/hosts\n"
458
        "127.0.0.1\tlocalhost\n"
459
        "192.168.1.1 router gw\n"
460
        "1.2.3.4\tmyhost.domain.tld myhost\n")
461
    finally:
462
      os.unlink(tmpname)
444
    self.assertFileContent(self.tmpname,
445
      "# This is a test file for /etc/hosts\n"
446
      "127.0.0.1\tlocalhost\n"
447
      "192.168.1.1 router gw\n"
448
      "1.2.3.4\tmyhost.domain.tld myhost\n")
463 449

  
464 450
  def testSettingExistingIp(self):
465
    tmpname = self.writeTestFile()
466
    try:
467
      SetEtcHostsEntry(tmpname, '192.168.1.1', 'myhost.domain.tld', ['myhost'])
451
    SetEtcHostsEntry(self.tmpname, '192.168.1.1', 'myhost.domain.tld',
452
                     ['myhost'])
468 453

  
469
      self.assertFileContent(tmpname,
470
        "# This is a test file for /etc/hosts\n"
471
        "127.0.0.1\tlocalhost\n"
472
        "192.168.1.1\tmyhost.domain.tld myhost\n")
473
    finally:
474
      os.unlink(tmpname)
454
    self.assertFileContent(self.tmpname,
455
      "# This is a test file for /etc/hosts\n"
456
      "127.0.0.1\tlocalhost\n"
457
      "192.168.1.1\tmyhost.domain.tld myhost\n")
475 458

  
476 459
  def testRemovingExistingHost(self):
477
    tmpname = self.writeTestFile()
478
    try:
479
      RemoveEtcHostsEntry(tmpname, 'router')
460
    RemoveEtcHostsEntry(self.tmpname, 'router')
480 461

  
481
      self.assertFileContent(tmpname,
482
        "# This is a test file for /etc/hosts\n"
483
        "127.0.0.1\tlocalhost\n"
484
        "192.168.1.1 gw\n")
485
    finally:
486
      os.unlink(tmpname)
462
    self.assertFileContent(self.tmpname,
463
      "# This is a test file for /etc/hosts\n"
464
      "127.0.0.1\tlocalhost\n"
465
      "192.168.1.1 gw\n")
487 466

  
488 467
  def testRemovingSingleExistingHost(self):
489
    tmpname = self.writeTestFile()
490
    try:
491
      RemoveEtcHostsEntry(tmpname, 'localhost')
468
    RemoveEtcHostsEntry(self.tmpname, 'localhost')
492 469

  
493
      self.assertFileContent(tmpname,
494
        "# This is a test file for /etc/hosts\n"
495
        "192.168.1.1 router gw\n")
496
    finally:
497
      os.unlink(tmpname)
470
    self.assertFileContent(self.tmpname,
471
      "# This is a test file for /etc/hosts\n"
472
      "192.168.1.1 router gw\n")
498 473

  
499 474
  def testRemovingNonExistingHost(self):
500
    tmpname = self.writeTestFile()
501
    try:
502
      RemoveEtcHostsEntry(tmpname, 'myhost')
475
    RemoveEtcHostsEntry(self.tmpname, 'myhost')
503 476

  
504
      self.assertFileContent(tmpname,
505
        "# This is a test file for /etc/hosts\n"
506
        "127.0.0.1\tlocalhost\n"
507
        "192.168.1.1 router gw\n")
508
    finally:
509
      os.unlink(tmpname)
477
    self.assertFileContent(self.tmpname,
478
      "# This is a test file for /etc/hosts\n"
479
      "127.0.0.1\tlocalhost\n"
480
      "192.168.1.1 router gw\n")
510 481

  
511 482
  def testRemovingAlias(self):
512
    tmpname = self.writeTestFile()
513
    try:
514
      RemoveEtcHostsEntry(tmpname, 'gw')
483
    RemoveEtcHostsEntry(self.tmpname, 'gw')
515 484

  
516
      self.assertFileContent(tmpname,
517
        "# This is a test file for /etc/hosts\n"
518
        "127.0.0.1\tlocalhost\n"
519
        "192.168.1.1 router\n")
520
    finally:
521
      os.unlink(tmpname)
485
    self.assertFileContent(self.tmpname,
486
      "# This is a test file for /etc/hosts\n"
487
      "127.0.0.1\tlocalhost\n"
488
      "192.168.1.1 router\n")
522 489

  
523 490

  
524 491
class TestShellQuoting(unittest.TestCase):

Also available in: Unified diff