4 # Copyright (C) 2006, 2007, 2008 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script for unittesting the ssh module"""
32 from ganeti import constants
33 from ganeti import utils
34 from ganeti import ssh
35 from ganeti import errors
38 class TestKnownHosts(testutils.GanetiTestCase):
39 """Test case for function writing the known_hosts file"""
42 testutils.GanetiTestCase.setUp(self)
43 self.tmpfile = self._CreateTempFile()
46 cfg = mocks.FakeConfig()
47 ssh.WriteKnownHostsFile(cfg, self.tmpfile)
48 self.assertFileContent(self.tmpfile,
49 "%s ssh-rsa %s\n" % (cfg.GetClusterName(),
50 mocks.FAKE_CLUSTER_KEY))
53 class TestGetUserFiles(unittest.TestCase):
55 self.tmpdir = tempfile.mkdtemp()
58 shutil.rmtree(self.tmpdir)
64 def _GetTempHomedir(self, _):
67 def testNonExistantUser(self):
68 for kind in constants.SSHK_ALL:
69 self.assertRaises(errors.OpExecError, ssh.GetUserFiles, "example",
70 kind=kind, _homedir_fn=self._GetNoHomedir)
72 def testUnknownKind(self):
73 kind = "something-else"
74 assert kind not in constants.SSHK_ALL
75 self.assertRaises(errors.ProgrammerError, ssh.GetUserFiles, "example4645",
76 kind=kind, _homedir_fn=self._GetTempHomedir)
78 self.assertEqual(os.listdir(self.tmpdir), [])
80 def testNoSshDirectory(self):
81 for kind in constants.SSHK_ALL:
82 self.assertRaises(errors.OpExecError, ssh.GetUserFiles, "example29694",
83 kind=kind, _homedir_fn=self._GetTempHomedir)
84 self.assertEqual(os.listdir(self.tmpdir), [])
86 def testSshIsFile(self):
87 utils.WriteFile(os.path.join(self.tmpdir, ".ssh"), data="")
88 for kind in constants.SSHK_ALL:
89 self.assertRaises(errors.OpExecError, ssh.GetUserFiles, "example26237",
90 kind=kind, _homedir_fn=self._GetTempHomedir)
91 self.assertEqual(os.listdir(self.tmpdir), [".ssh"])
93 def testMakeSshDirectory(self):
94 sshdir = os.path.join(self.tmpdir, ".ssh")
96 self.assertEqual(os.listdir(self.tmpdir), [])
98 for kind in constants.SSHK_ALL:
99 ssh.GetUserFiles("example20745", mkdir=True, kind=kind,
100 _homedir_fn=self._GetTempHomedir)
101 self.assertEqual(os.listdir(self.tmpdir), [".ssh"])
102 self.assertEqual(os.stat(sshdir).st_mode & 0777, 0700)
104 def testFilenames(self):
105 sshdir = os.path.join(self.tmpdir, ".ssh")
109 for kind in constants.SSHK_ALL:
110 result = ssh.GetUserFiles("example15103", mkdir=False, kind=kind,
111 _homedir_fn=self._GetTempHomedir)
112 self.assertEqual(result, [
113 os.path.join(self.tmpdir, ".ssh", "id_%s" % kind),
114 os.path.join(self.tmpdir, ".ssh", "id_%s.pub" % kind),
115 os.path.join(self.tmpdir, ".ssh", "authorized_keys"),
118 self.assertEqual(os.listdir(self.tmpdir), [".ssh"])
119 self.assertEqual(os.listdir(sshdir), [])
121 def testNoDirCheck(self):
122 self.assertEqual(os.listdir(self.tmpdir), [])
124 for kind in constants.SSHK_ALL:
125 ssh.GetUserFiles("example14528", mkdir=False, dircheck=False, kind=kind,
126 _homedir_fn=self._GetTempHomedir)
127 self.assertEqual(os.listdir(self.tmpdir), [])
129 def testGetAllUserFiles(self):
130 result = ssh.GetAllUserFiles("example7475", mkdir=False, dircheck=False,
131 _homedir_fn=self._GetTempHomedir)
132 self.assertEqual(result,
133 (os.path.join(self.tmpdir, ".ssh", "authorized_keys"), {
135 (os.path.join(self.tmpdir, ".ssh", "id_rsa"),
136 os.path.join(self.tmpdir, ".ssh", "id_rsa.pub")),
138 (os.path.join(self.tmpdir, ".ssh", "id_dsa"),
139 os.path.join(self.tmpdir, ".ssh", "id_dsa.pub")),
141 self.assertEqual(os.listdir(self.tmpdir), [])
143 def testGetAllUserFilesNoDirectoryNoMkdir(self):
144 self.assertRaises(errors.OpExecError, ssh.GetAllUserFiles,
145 "example17270", mkdir=False, dircheck=True,
146 _homedir_fn=self._GetTempHomedir)
147 self.assertEqual(os.listdir(self.tmpdir), [])
150 if __name__ == "__main__":
151 testutils.GanetiTestProgram()