4 # Copyright (C) 2012 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 testing ganeti.vcluster"""
27 from ganeti import utils
28 from ganeti import compat
29 from ganeti import vcluster
30 from ganeti import pathutils
35 _ENV_DOES_NOT_EXIST = "GANETI_TEST_DOES_NOT_EXIST"
36 _ENV_TEST = "GANETI_TESTVAR"
39 class _EnvVarTest(testutils.GanetiTestCase):
41 testutils.GanetiTestCase.setUp(self)
43 os.environ.pop(_ENV_DOES_NOT_EXIST, None)
44 os.environ.pop(_ENV_TEST, None)
47 class TestGetRootDirectory(_EnvVarTest):
49 assert os.getenv(_ENV_TEST) is None
51 self.assertEqual(vcluster._GetRootDirectory(_ENV_DOES_NOT_EXIST), "")
52 self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "")
55 os.environ[_ENV_TEST] = "/tmp/xy11"
56 self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "/tmp/xy11")
59 os.environ[_ENV_TEST] = "foobar"
60 self.assertRaises(RuntimeError, vcluster._GetRootDirectory, _ENV_TEST)
64 class TestGetHostname(_EnvVarTest):
66 assert os.getenv(_ENV_TEST) is None
68 self.assertEqual(vcluster._GetRootDirectory(_ENV_DOES_NOT_EXIST), "")
69 self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "")
71 os.environ[_ENV_TEST] = "some.host.example.com"
72 self.assertEqual(vcluster._GetHostname(_ENV_TEST), "some.host.example.com")
75 class TestCheckHostname(_EnvVarTest):
77 for i in ["/", "/tmp"]:
78 self.assertRaises(RuntimeError, vcluster._CheckHostname, i)
81 class TestPreparePaths(_EnvVarTest):
82 def testInvalidParameters(self):
83 self.assertRaises(RuntimeError, vcluster._PreparePaths,
84 None, "host.example.com")
85 self.assertRaises(RuntimeError, vcluster._PreparePaths,
88 def testNonNormalizedRootDir(self):
89 self.assertRaises(AssertionError, vcluster._PreparePaths,
90 "/tmp////xyz//", "host.example.com")
92 def testInvalidHostname(self):
93 self.assertRaises(RuntimeError, vcluster._PreparePaths, "/tmp", "/")
95 def testPathHostnameMismatch(self):
96 self.assertRaises(RuntimeError, vcluster._PreparePaths,
97 "/tmp/host.example.com", "server.example.com")
99 def testNoVirtCluster(self):
101 self.assertEqual(vcluster._PreparePaths(i, i), ("", "", None))
103 def testVirtCluster(self):
104 self.assertEqual(vcluster._PreparePaths("/tmp/host.example.com",
106 ("/tmp", "/tmp/host.example.com", "host.example.com"))
109 class TestMakeNodeRoot(unittest.TestCase):
111 self.assertRaises(RuntimeError, vcluster._MakeNodeRoot, "/tmp", "/")
113 for i in ["/tmp", "/tmp/", "/tmp///"]:
114 self.assertEqual(vcluster._MakeNodeRoot(i, "other.example.com"),
115 "/tmp/other.example.com")
118 class TestEnvironmentForHost(unittest.TestCase):
120 self.assertEqual(vcluster.EnvironmentForHost("host.example.com",
123 for i in ["host.example.com", "other.example.com"]:
124 self.assertEqual(vcluster.EnvironmentForHost(i, _basedir="/tmp"), {
125 vcluster._ROOTDIR_ENVNAME: "/tmp/%s" % i,
126 vcluster._HOSTNAME_ENVNAME: i,
130 class TestExchangeNodeRoot(unittest.TestCase):
132 result = vcluster.ExchangeNodeRoot("node1.example.com", "/tmp/file",
133 _basedir=None, _noderoot=None)
134 self.assertEqual(result, "/tmp/file")
136 self.assertRaises(RuntimeError, vcluster.ExchangeNodeRoot,
137 "node1.example.com", "/tmp/node1.example.com",
139 _noderoot="/tmp/nodeZZ.example.com")
141 result = vcluster.ExchangeNodeRoot("node2.example.com",
142 "/tmp/node1.example.com/file",
144 _noderoot="/tmp/node1.example.com")
145 self.assertEqual(result, "/tmp/node2.example.com/file")
148 class TestAddNodePrefix(unittest.TestCase):
149 def testRelativePath(self):
150 self.assertRaises(AssertionError, vcluster.AddNodePrefix,
151 "foobar", _noderoot=None)
153 def testRelativeNodeRoot(self):
154 self.assertRaises(AssertionError, vcluster.AddNodePrefix,
155 "/tmp", _noderoot="foobar")
158 path = vcluster.AddNodePrefix("/file/path",
159 _noderoot="/tmp/node1.example.com/")
160 self.assertEqual(path, "/tmp/node1.example.com/file/path")
162 self.assertEqual(vcluster.AddNodePrefix("/file/path", _noderoot=""),
166 class TestRemoveNodePrefix(unittest.TestCase):
167 def testRelativePath(self):
168 self.assertRaises(AssertionError, vcluster._RemoveNodePrefix,
169 "foobar", _noderoot=None)
171 def testOutsideNodeRoot(self):
172 self.assertRaises(RuntimeError, vcluster._RemoveNodePrefix,
173 "/file/path", _noderoot="/tmp/node1.example.com")
174 self.assertRaises(RuntimeError, vcluster._RemoveNodePrefix,
175 "/tmp/xyzfile", _noderoot="/tmp/xyz")
178 path = vcluster._RemoveNodePrefix("/tmp/node1.example.com/file/path",
179 _noderoot="/tmp/node1.example.com")
180 self.assertEqual(path, "/file/path")
182 path = vcluster._RemoveNodePrefix("/file/path", _noderoot=None)
183 self.assertEqual(path, "/file/path")
186 class TestMakeVirtualPath(unittest.TestCase):
187 def testRelativePath(self):
188 self.assertRaises(AssertionError, vcluster.MakeVirtualPath,
189 "foobar", _noderoot=None)
191 def testOutsideNodeRoot(self):
192 self.assertRaises(RuntimeError, vcluster.MakeVirtualPath,
193 "/file/path", _noderoot="/tmp/node1.example.com")
195 def testWithNodeRoot(self):
196 path = vcluster.MakeVirtualPath("/tmp/node1.example.com/tmp/file",
197 _noderoot="/tmp/node1.example.com")
198 self.assertEqual(path, "%s/tmp/file" % vcluster._VIRT_PATH_PREFIX)
200 def testNormal(self):
201 self.assertEqual(vcluster.MakeVirtualPath("/tmp/file", _noderoot=None),
204 def testWhitelisted(self):
205 mvp = vcluster.MakeVirtualPath
206 for path in vcluster._VPATH_WHITELIST:
207 self.assertEqual(mvp(path), path)
208 self.assertEqual(mvp(path, _noderoot=None), path)
209 self.assertEqual(mvp(path, _noderoot="/tmp"), path)
212 class TestLocalizeVirtualPath(unittest.TestCase):
213 def testWrongPrefix(self):
214 self.assertRaises(RuntimeError, vcluster.LocalizeVirtualPath,
215 "/tmp/some/path", _noderoot="/tmp/node1.example.com")
217 def testCorrectPrefixRelativePath(self):
218 self.assertRaises(AssertionError, vcluster.LocalizeVirtualPath,
219 vcluster._VIRT_PATH_PREFIX + "foobar",
220 _noderoot="/tmp/node1.example.com")
222 def testWithNodeRoot(self):
223 lvp = vcluster.LocalizeVirtualPath
225 virtpath1 = "%s/tmp/file" % vcluster._VIRT_PATH_PREFIX
226 virtpath2 = "%s////tmp////file" % vcluster._VIRT_PATH_PREFIX
228 for i in [virtpath1, virtpath2]:
229 result = lvp(i, _noderoot="/tmp/node1.example.com")
230 self.assertEqual(result, "/tmp/node1.example.com/tmp/file")
232 def testNormal(self):
233 self.assertEqual(vcluster.LocalizeVirtualPath("/tmp/file", _noderoot=None),
236 def testWhitelisted(self):
237 lvp = vcluster.LocalizeVirtualPath
238 for path in vcluster._VPATH_WHITELIST:
239 self.assertEqual(lvp(path), path)
240 self.assertEqual(lvp(path, _noderoot=None), path)
241 self.assertEqual(lvp(path, _noderoot="/tmp"), path)
244 class TestVirtualPathPrefix(unittest.TestCase):
246 self.assertTrue(os.path.isabs(vcluster._VIRT_PATH_PREFIX))
247 self.assertEqual(os.path.normcase(vcluster._VIRT_PATH_PREFIX),
248 vcluster._VIRT_PATH_PREFIX)
251 if __name__ == "__main__":
252 testutils.GanetiTestProgram()