root / test / py / ganeti.ssh_unittest.py @ 13bb1b4c
History | View | Annotate | Download (5.6 kB)
1 | 75a5f456 | Michael Hanselmann | #!/usr/bin/python
|
---|---|---|---|
2 | 75a5f456 | Michael Hanselmann | #
|
3 | 75a5f456 | Michael Hanselmann | |
4 | 75a5f456 | Michael Hanselmann | # Copyright (C) 2006, 2007, 2008 Google Inc.
|
5 | fe42a791 | Klaus Aehlig | # All rights reserved.
|
6 | 75a5f456 | Michael Hanselmann | #
|
7 | fe42a791 | Klaus Aehlig | # Redistribution and use in source and binary forms, with or without
|
8 | fe42a791 | Klaus Aehlig | # modification, are permitted provided that the following conditions are
|
9 | fe42a791 | Klaus Aehlig | # met:
|
10 | 75a5f456 | Michael Hanselmann | #
|
11 | fe42a791 | Klaus Aehlig | # 1. Redistributions of source code must retain the above copyright notice,
|
12 | fe42a791 | Klaus Aehlig | # this list of conditions and the following disclaimer.
|
13 | 75a5f456 | Michael Hanselmann | #
|
14 | fe42a791 | Klaus Aehlig | # 2. Redistributions in binary form must reproduce the above copyright
|
15 | fe42a791 | Klaus Aehlig | # notice, this list of conditions and the following disclaimer in the
|
16 | fe42a791 | Klaus Aehlig | # documentation and/or other materials provided with the distribution.
|
17 | fe42a791 | Klaus Aehlig | #
|
18 | fe42a791 | Klaus Aehlig | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
19 | fe42a791 | Klaus Aehlig | # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
20 | fe42a791 | Klaus Aehlig | # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21 | fe42a791 | Klaus Aehlig | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
22 | fe42a791 | Klaus Aehlig | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
23 | fe42a791 | Klaus Aehlig | # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
24 | fe42a791 | Klaus Aehlig | # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
25 | fe42a791 | Klaus Aehlig | # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
26 | fe42a791 | Klaus Aehlig | # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
27 | fe42a791 | Klaus Aehlig | # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
28 | fe42a791 | Klaus Aehlig | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29 | 75a5f456 | Michael Hanselmann | |
30 | 75a5f456 | Michael Hanselmann | |
31 | 75a5f456 | Michael Hanselmann | """Script for unittesting the ssh module"""
|
32 | 75a5f456 | Michael Hanselmann | |
33 | 75a5f456 | Michael Hanselmann | import os |
34 | 75a5f456 | Michael Hanselmann | import tempfile |
35 | 75a5f456 | Michael Hanselmann | import unittest |
36 | 8a3c9e8a | Michael Hanselmann | import shutil |
37 | 75a5f456 | Michael Hanselmann | |
38 | 75a5f456 | Michael Hanselmann | import testutils |
39 | 75a5f456 | Michael Hanselmann | import mocks |
40 | 75a5f456 | Michael Hanselmann | |
41 | 75a5f456 | Michael Hanselmann | from ganeti import constants |
42 | 75a5f456 | Michael Hanselmann | from ganeti import utils |
43 | 75a5f456 | Michael Hanselmann | from ganeti import ssh |
44 | 8a3c9e8a | Michael Hanselmann | from ganeti import errors |
45 | 75a5f456 | Michael Hanselmann | |
46 | 75a5f456 | Michael Hanselmann | |
47 | 75a5f456 | Michael Hanselmann | class TestKnownHosts(testutils.GanetiTestCase): |
48 | 75a5f456 | Michael Hanselmann | """Test case for function writing the known_hosts file"""
|
49 | 75a5f456 | Michael Hanselmann | |
50 | 75a5f456 | Michael Hanselmann | def setUp(self): |
51 | 51596eb2 | Iustin Pop | testutils.GanetiTestCase.setUp(self)
|
52 | 51596eb2 | Iustin Pop | self.tmpfile = self._CreateTempFile() |
53 | 75a5f456 | Michael Hanselmann | |
54 | 75a5f456 | Michael Hanselmann | def test(self): |
55 | 75a5f456 | Michael Hanselmann | cfg = mocks.FakeConfig() |
56 | 51596eb2 | Iustin Pop | ssh.WriteKnownHostsFile(cfg, self.tmpfile)
|
57 | 51596eb2 | Iustin Pop | self.assertFileContent(self.tmpfile, |
58 | a9542a4f | Thomas Thrainer | "%s ssh-rsa %s\n%s ssh-dss %s\n" %
|
59 | a9542a4f | Thomas Thrainer | (cfg.GetClusterName(), mocks.FAKE_CLUSTER_KEY, |
60 | a9542a4f | Thomas Thrainer | cfg.GetClusterName(), mocks.FAKE_CLUSTER_KEY)) |
61 | 75a5f456 | Michael Hanselmann | |
62 | 75a5f456 | Michael Hanselmann | |
63 | 8a3c9e8a | Michael Hanselmann | class TestGetUserFiles(unittest.TestCase): |
64 | 8a3c9e8a | Michael Hanselmann | def setUp(self): |
65 | 8a3c9e8a | Michael Hanselmann | self.tmpdir = tempfile.mkdtemp()
|
66 | 8a3c9e8a | Michael Hanselmann | |
67 | 8a3c9e8a | Michael Hanselmann | def tearDown(self): |
68 | 8a3c9e8a | Michael Hanselmann | shutil.rmtree(self.tmpdir)
|
69 | 8a3c9e8a | Michael Hanselmann | |
70 | 8a3c9e8a | Michael Hanselmann | @staticmethod
|
71 | 8a3c9e8a | Michael Hanselmann | def _GetNoHomedir(_): |
72 | 8a3c9e8a | Michael Hanselmann | return None |
73 | 8a3c9e8a | Michael Hanselmann | |
74 | 8a3c9e8a | Michael Hanselmann | def _GetTempHomedir(self, _): |
75 | 8a3c9e8a | Michael Hanselmann | return self.tmpdir |
76 | 8a3c9e8a | Michael Hanselmann | |
77 | 8a3c9e8a | Michael Hanselmann | def testNonExistantUser(self): |
78 | 8a3c9e8a | Michael Hanselmann | for kind in constants.SSHK_ALL: |
79 | 8a3c9e8a | Michael Hanselmann | self.assertRaises(errors.OpExecError, ssh.GetUserFiles, "example", |
80 | 8a3c9e8a | Michael Hanselmann | kind=kind, _homedir_fn=self._GetNoHomedir)
|
81 | 8a3c9e8a | Michael Hanselmann | |
82 | 8a3c9e8a | Michael Hanselmann | def testUnknownKind(self): |
83 | 8a3c9e8a | Michael Hanselmann | kind = "something-else"
|
84 | 8a3c9e8a | Michael Hanselmann | assert kind not in constants.SSHK_ALL |
85 | 8a3c9e8a | Michael Hanselmann | self.assertRaises(errors.ProgrammerError, ssh.GetUserFiles, "example4645", |
86 | 8a3c9e8a | Michael Hanselmann | kind=kind, _homedir_fn=self._GetTempHomedir)
|
87 | 8a3c9e8a | Michael Hanselmann | |
88 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), []) |
89 | 8a3c9e8a | Michael Hanselmann | |
90 | 8a3c9e8a | Michael Hanselmann | def testNoSshDirectory(self): |
91 | 8a3c9e8a | Michael Hanselmann | for kind in constants.SSHK_ALL: |
92 | 8a3c9e8a | Michael Hanselmann | self.assertRaises(errors.OpExecError, ssh.GetUserFiles, "example29694", |
93 | 8a3c9e8a | Michael Hanselmann | kind=kind, _homedir_fn=self._GetTempHomedir)
|
94 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), []) |
95 | 8a3c9e8a | Michael Hanselmann | |
96 | 8a3c9e8a | Michael Hanselmann | def testSshIsFile(self): |
97 | 8a3c9e8a | Michael Hanselmann | utils.WriteFile(os.path.join(self.tmpdir, ".ssh"), data="") |
98 | 8a3c9e8a | Michael Hanselmann | for kind in constants.SSHK_ALL: |
99 | 8a3c9e8a | Michael Hanselmann | self.assertRaises(errors.OpExecError, ssh.GetUserFiles, "example26237", |
100 | 8a3c9e8a | Michael Hanselmann | kind=kind, _homedir_fn=self._GetTempHomedir)
|
101 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), [".ssh"]) |
102 | 8a3c9e8a | Michael Hanselmann | |
103 | 8a3c9e8a | Michael Hanselmann | def testMakeSshDirectory(self): |
104 | 8a3c9e8a | Michael Hanselmann | sshdir = os.path.join(self.tmpdir, ".ssh") |
105 | 8a3c9e8a | Michael Hanselmann | |
106 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), []) |
107 | 8a3c9e8a | Michael Hanselmann | |
108 | 8a3c9e8a | Michael Hanselmann | for kind in constants.SSHK_ALL: |
109 | 8a3c9e8a | Michael Hanselmann | ssh.GetUserFiles("example20745", mkdir=True, kind=kind, |
110 | 8a3c9e8a | Michael Hanselmann | _homedir_fn=self._GetTempHomedir)
|
111 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), [".ssh"]) |
112 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(os.stat(sshdir).st_mode & 0777, 0700) |
113 | 8a3c9e8a | Michael Hanselmann | |
114 | 8a3c9e8a | Michael Hanselmann | def testFilenames(self): |
115 | 8a3c9e8a | Michael Hanselmann | sshdir = os.path.join(self.tmpdir, ".ssh") |
116 | 8a3c9e8a | Michael Hanselmann | |
117 | 8a3c9e8a | Michael Hanselmann | os.mkdir(sshdir) |
118 | 8a3c9e8a | Michael Hanselmann | |
119 | 8a3c9e8a | Michael Hanselmann | for kind in constants.SSHK_ALL: |
120 | 8a3c9e8a | Michael Hanselmann | result = ssh.GetUserFiles("example15103", mkdir=False, kind=kind, |
121 | 8a3c9e8a | Michael Hanselmann | _homedir_fn=self._GetTempHomedir)
|
122 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(result, [
|
123 | 8a3c9e8a | Michael Hanselmann | os.path.join(self.tmpdir, ".ssh", "id_%s" % kind), |
124 | 8a3c9e8a | Michael Hanselmann | os.path.join(self.tmpdir, ".ssh", "id_%s.pub" % kind), |
125 | 8a3c9e8a | Michael Hanselmann | os.path.join(self.tmpdir, ".ssh", "authorized_keys"), |
126 | 8a3c9e8a | Michael Hanselmann | ]) |
127 | 8a3c9e8a | Michael Hanselmann | |
128 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), [".ssh"]) |
129 | 8a3c9e8a | Michael Hanselmann | self.assertEqual(os.listdir(sshdir), [])
|
130 | 8a3c9e8a | Michael Hanselmann | |
131 | 7bd70e6b | Michael Hanselmann | def testNoDirCheck(self): |
132 | 7bd70e6b | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), []) |
133 | 7bd70e6b | Michael Hanselmann | |
134 | 7bd70e6b | Michael Hanselmann | for kind in constants.SSHK_ALL: |
135 | 7bd70e6b | Michael Hanselmann | ssh.GetUserFiles("example14528", mkdir=False, dircheck=False, kind=kind, |
136 | 7bd70e6b | Michael Hanselmann | _homedir_fn=self._GetTempHomedir)
|
137 | 7bd70e6b | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), []) |
138 | 7bd70e6b | Michael Hanselmann | |
139 | 5484cda5 | Michael Hanselmann | def testGetAllUserFiles(self): |
140 | 5484cda5 | Michael Hanselmann | result = ssh.GetAllUserFiles("example7475", mkdir=False, dircheck=False, |
141 | 5484cda5 | Michael Hanselmann | _homedir_fn=self._GetTempHomedir)
|
142 | 5484cda5 | Michael Hanselmann | self.assertEqual(result,
|
143 | 5484cda5 | Michael Hanselmann | (os.path.join(self.tmpdir, ".ssh", "authorized_keys"), { |
144 | 5484cda5 | Michael Hanselmann | constants.SSHK_RSA: |
145 | 5484cda5 | Michael Hanselmann | (os.path.join(self.tmpdir, ".ssh", "id_rsa"), |
146 | 5484cda5 | Michael Hanselmann | os.path.join(self.tmpdir, ".ssh", "id_rsa.pub")), |
147 | 5484cda5 | Michael Hanselmann | constants.SSHK_DSA: |
148 | 5484cda5 | Michael Hanselmann | (os.path.join(self.tmpdir, ".ssh", "id_dsa"), |
149 | 5484cda5 | Michael Hanselmann | os.path.join(self.tmpdir, ".ssh", "id_dsa.pub")), |
150 | 5484cda5 | Michael Hanselmann | })) |
151 | 5484cda5 | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), []) |
152 | 5484cda5 | Michael Hanselmann | |
153 | 5484cda5 | Michael Hanselmann | def testGetAllUserFilesNoDirectoryNoMkdir(self): |
154 | 5484cda5 | Michael Hanselmann | self.assertRaises(errors.OpExecError, ssh.GetAllUserFiles,
|
155 | 5484cda5 | Michael Hanselmann | "example17270", mkdir=False, dircheck=True, |
156 | 5484cda5 | Michael Hanselmann | _homedir_fn=self._GetTempHomedir)
|
157 | 5484cda5 | Michael Hanselmann | self.assertEqual(os.listdir(self.tmpdir), []) |
158 | 5484cda5 | Michael Hanselmann | |
159 | 8a3c9e8a | Michael Hanselmann | |
160 | 8a3c9e8a | Michael Hanselmann | if __name__ == "__main__": |
161 | 25231ec5 | Michael Hanselmann | testutils.GanetiTestProgram() |