Revision 2d6cfa31

b/lib/utils.py
815 815
    raise
816 816

  
817 817

  
818
def _SplitKnownHostsHosts(hosts):
819
  """Parses the first field of a known_hosts file.
820

  
821
  TODO: Support other formats.
822
  """
823
  return hosts.split(',')
824

  
825

  
826
def AddKnownHost(file_name, hostname, pubkey):
827
  """Adds a new known host to a known_hosts file.
828

  
829
  """
830
  f = open(file_name, 'a+')
831
  try:
832
    nl = True
833
    for line in f:
834
      fields = line.split()
835
      if (len(fields) < 3 or
836
          fields[0].startswith('#') or
837
          fields[1] != 'ssh-rsa'):
838
        continue
839
      hosts = _SplitKnownHostsHosts(fields[0])
840
      if hostname in hosts and fields[2] == pubkey:
841
        break
842
      nl = line.endswith('\n')
843
    else:
844
      if not nl:
845
        f.write("\n")
846
      f.write(hostname)
847
      f.write(' ssh-rsa ')
848
      f.write(pubkey)
849
      f.write("\n")
850
      f.flush()
851
  finally:
852
    f.close()
853

  
854

  
855
def RemoveKnownHost(file_name, hostname):
856
  pass
857

  
858

  
818 859
def CreateBackup(file_name):
819 860
  """Creates a backup of a file.
820 861

  
b/test/ganeti.utils_unittest.py
38 38
     RemoveFile, CheckDict, MatchNameComponent, FormatUnit, \
39 39
     ParseUnit, AddAuthorizedKey, RemoveAuthorizedKey, \
40 40
     ShellQuote, ShellQuoteArgs, TcpPing, ListVisibleFiles, \
41
     AddEtcHostsEntry, RemoveEtcHostsEntry
41
     AddEtcHostsEntry, RemoveEtcHostsEntry, \
42
     AddKnownHost, RemoveKnownHost
42 43
from ganeti.errors import LockError, UnitParseError
43 44

  
44 45

  
......
518 519
      os.unlink(tmpname)
519 520

  
520 521

  
522
class TestKnownHosts(unittest.TestCase):
523
  """Test functions modifying known_hosts files"""
524

  
525
  def writeTestFile(self):
526
    (fd, tmpname) = tempfile.mkstemp(prefix = 'ganeti-test')
527
    f = os.fdopen(fd, 'w')
528
    try:
529
      f.write('node1.tld,node1\tssh-rsa AAAA1234567890=\n')
530
      f.write('node2,node2.tld ssh-rsa AAAA1234567890=\n')
531
    finally:
532
      f.close()
533

  
534
    return tmpname
535

  
536
  def testAddingNewHost(self):
537
    tmpname = self.writeTestFile()
538
    try:
539
      AddKnownHost(tmpname, 'node3.tld', 'AAAA0987654321=')
540

  
541
      f = open(tmpname, 'r')
542
      try:
543
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
544
                         '86cf3c7c7983a3bd5c475c4c1a3e5678')
545
      finally:
546
        f.close()
547
    finally:
548
      os.unlink(tmpname)
549

  
550
  def testAddingOldHost(self):
551
    tmpname = self.writeTestFile()
552
    try:
553
      AddKnownHost(tmpname, 'node2.tld', 'AAAA0987654321=')
554

  
555
      f = open(tmpname, 'r')
556
      try:
557
        os.system("vim %s" % tmpname)
558
        self.assertEqual(md5.new(f.read(8192)).hexdigest(),
559
                         '86cf3c7c7983a3bd5c475c4c1a3e5678')
560
      finally:
561
        f.close()
562
    finally:
563
      os.unlink(tmpname)
564

  
565

  
566

  
521 567
class TestShellQuoting(unittest.TestCase):
522 568
  """Test case for shell quoting functions"""
523 569

  

Also available in: Unified diff