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
34 _ENV_DOES_NOT_EXIST = "GANETI_TEST_DOES_NOT_EXIST"
35 _ENV_TEST = "GANETI_TESTVAR"
38 class _EnvVarTest(testutils.GanetiTestCase):
40 testutils.GanetiTestCase.setUp(self)
42 os.environ.pop(_ENV_DOES_NOT_EXIST, None)
43 os.environ.pop(_ENV_TEST, None)
46 class TestGetRootDirectory(_EnvVarTest):
48 assert os.getenv(_ENV_TEST) is None
50 self.assertEqual(vcluster._GetRootDirectory(_ENV_DOES_NOT_EXIST), "")
51 self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "")
54 os.environ[_ENV_TEST] = "/tmp/xy11"
55 self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "/tmp/xy11")
58 os.environ[_ENV_TEST] = "foobar"
59 self.assertRaises(RuntimeError, vcluster._GetRootDirectory, _ENV_TEST)
63 class TestGetHostname(_EnvVarTest):
65 assert os.getenv(_ENV_TEST) is None
67 self.assertEqual(vcluster._GetRootDirectory(_ENV_DOES_NOT_EXIST), "")
68 self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "")
70 os.environ[_ENV_TEST] = "some.host.example.com"
71 self.assertEqual(vcluster._GetHostname(_ENV_TEST), "some.host.example.com")
74 class TestCheckHostname(_EnvVarTest):
76 for i in ["/", "/tmp"]:
77 self.assertRaises(RuntimeError, vcluster._CheckHostname, i)
80 class TestPreparePaths(_EnvVarTest):
81 def testInvalidParameters(self):
82 self.assertRaises(RuntimeError, vcluster._PreparePaths,
83 None, "host.example.com")
84 self.assertRaises(RuntimeError, vcluster._PreparePaths,
87 def testNonNormalizedRootDir(self):
88 self.assertRaises(AssertionError, vcluster._PreparePaths,
89 "/tmp////xyz//", "host.example.com")
91 def testInvalidHostname(self):
92 self.assertRaises(RuntimeError, vcluster._PreparePaths, "/tmp", "/")
94 def testPathHostnameMismatch(self):
95 self.assertRaises(RuntimeError, vcluster._PreparePaths,
96 "/tmp/host.example.com", "server.example.com")
98 def testNoVirtCluster(self):
100 self.assertEqual(vcluster._PreparePaths(i, i), ("", "", None))
102 def testVirtCluster(self):
103 self.assertEqual(vcluster._PreparePaths("/tmp/host.example.com",
105 ("/tmp", "/tmp/host.example.com", "host.example.com"))
108 class TestMakeNodeRoot(unittest.TestCase):
110 self.assertRaises(RuntimeError, vcluster._MakeNodeRoot, "/tmp", "/")
112 for i in ["/tmp", "/tmp/", "/tmp///"]:
113 self.assertEqual(vcluster._MakeNodeRoot(i, "other.example.com"),
114 "/tmp/other.example.com")
117 class TestEnvironmentForHost(unittest.TestCase):
119 self.assertEqual(vcluster.EnvironmentForHost("host.example.com",
122 for i in ["host.example.com", "other.example.com"]:
123 self.assertEqual(vcluster.EnvironmentForHost(i, _basedir="/tmp"), {
124 vcluster._ROOTDIR_ENVNAME: "/tmp/%s" % i,
125 vcluster._HOSTNAME_ENVNAME: i,
129 class TestExchangeNodeRoot(unittest.TestCase):
131 result = vcluster.ExchangeNodeRoot("node1.example.com", "/tmp/file",
132 _basedir=None, _noderoot=None)
133 self.assertEqual(result, "/tmp/file")
135 self.assertRaises(RuntimeError, vcluster.ExchangeNodeRoot,
136 "node1.example.com", "/tmp/node1.example.com",
138 _noderoot="/tmp/nodeZZ.example.com")
140 result = vcluster.ExchangeNodeRoot("node2.example.com",
141 "/tmp/node1.example.com/file",
143 _noderoot="/tmp/node1.example.com")
144 self.assertEqual(result, "/tmp/node2.example.com/file")
147 class TestAddNodePrefix(unittest.TestCase):
148 def testRelativePath(self):
149 self.assertRaises(AssertionError, vcluster.AddNodePrefix,
150 "foobar", _noderoot=None)
152 def testRelativeNodeRoot(self):
153 self.assertRaises(AssertionError, vcluster.AddNodePrefix,
154 "/tmp", _noderoot="foobar")
157 path = vcluster.AddNodePrefix("/file/path",
158 _noderoot="/tmp/node1.example.com/")
159 self.assertEqual(path, "/tmp/node1.example.com/file/path")
161 self.assertEqual(vcluster.AddNodePrefix("/file/path", _noderoot=""),
165 class TestRemoveNodePrefix(unittest.TestCase):
166 def testRelativePath(self):
167 self.assertRaises(AssertionError, vcluster._RemoveNodePrefix,
168 "foobar", _noderoot=None)
170 def testOutsideNodeRoot(self):
171 self.assertRaises(RuntimeError, vcluster._RemoveNodePrefix,
172 "/file/path", _noderoot="/tmp/node1.example.com")
173 self.assertRaises(RuntimeError, vcluster._RemoveNodePrefix,
174 "/tmp/xyzfile", _noderoot="/tmp/xyz")
177 path = vcluster._RemoveNodePrefix("/tmp/node1.example.com/file/path",
178 _noderoot="/tmp/node1.example.com")
179 self.assertEqual(path, "/file/path")
181 path = vcluster._RemoveNodePrefix("/file/path", _noderoot=None)
182 self.assertEqual(path, "/file/path")
185 class TestMakeVirtualPath(unittest.TestCase):
186 def testRelativePath(self):
187 self.assertRaises(AssertionError, vcluster.MakeVirtualPath,
188 "foobar", _noderoot=None)
190 def testOutsideNodeRoot(self):
191 self.assertRaises(RuntimeError, vcluster.MakeVirtualPath,
192 "/file/path", _noderoot="/tmp/node1.example.com")
194 def testWithNodeRoot(self):
195 path = vcluster.MakeVirtualPath("/tmp/node1.example.com/tmp/file",
196 _noderoot="/tmp/node1.example.com")
197 self.assertEqual(path, "%s/tmp/file" % vcluster._VIRT_PATH_PREFIX)
199 def testNormal(self):
200 self.assertEqual(vcluster.MakeVirtualPath("/tmp/file", _noderoot=None),
204 class TestLocalizeVirtualPath(unittest.TestCase):
205 def testWrongPrefix(self):
206 self.assertRaises(RuntimeError, vcluster.LocalizeVirtualPath,
207 "/tmp/some/path", _noderoot="/tmp/node1.example.com")
209 def testCorrectPrefixRelativePath(self):
210 self.assertRaises(AssertionError, vcluster.LocalizeVirtualPath,
211 vcluster._VIRT_PATH_PREFIX + "foobar",
212 _noderoot="/tmp/node1.example.com")
214 def testWithNodeRoot(self):
215 lvp = vcluster.LocalizeVirtualPath
217 virtpath1 = "%s/tmp/file" % vcluster._VIRT_PATH_PREFIX
218 virtpath2 = "%s////tmp////file" % vcluster._VIRT_PATH_PREFIX
220 for i in [virtpath1, virtpath2]:
221 result = lvp(i, _noderoot="/tmp/node1.example.com")
222 self.assertEqual(result, "/tmp/node1.example.com/tmp/file")
224 def testNormal(self):
225 self.assertEqual(vcluster.LocalizeVirtualPath("/tmp/file", _noderoot=None),
229 class TestVirtualPathPrefix(unittest.TestCase):
231 self.assertTrue(os.path.isabs(vcluster._VIRT_PATH_PREFIX))
232 self.assertEqual(os.path.normcase(vcluster._VIRT_PATH_PREFIX),
233 vcluster._VIRT_PATH_PREFIX)
236 if __name__ == "__main__":
237 testutils.GanetiTestProgram()