locking: Change locking order, move NAL after instances
[ganeti-local] / test / ganeti.vcluster_unittest.py
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2012 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 """Script for testing ganeti.vcluster"""
23
24 import os
25 import unittest
26
27 from ganeti import utils
28 from ganeti import compat
29 from ganeti import vcluster
30 from ganeti import pathutils
31
32 import testutils
33
34
35 _ENV_DOES_NOT_EXIST = "GANETI_TEST_DOES_NOT_EXIST"
36 _ENV_TEST = "GANETI_TESTVAR"
37
38
39 class _EnvVarTest(testutils.GanetiTestCase):
40   def setUp(self):
41     testutils.GanetiTestCase.setUp(self)
42
43     os.environ.pop(_ENV_DOES_NOT_EXIST, None)
44     os.environ.pop(_ENV_TEST, None)
45
46
47 class TestGetRootDirectory(_EnvVarTest):
48   def test(self):
49     assert os.getenv(_ENV_TEST) is None
50
51     self.assertEqual(vcluster._GetRootDirectory(_ENV_DOES_NOT_EXIST), "")
52     self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "")
53
54     # Absolute path
55     os.environ[_ENV_TEST] = "/tmp/xy11"
56     self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "/tmp/xy11")
57
58     # Relative path
59     os.environ[_ENV_TEST] = "foobar"
60     self.assertRaises(RuntimeError, vcluster._GetRootDirectory, _ENV_TEST)
61
62
63
64 class TestGetHostname(_EnvVarTest):
65   def test(self):
66     assert os.getenv(_ENV_TEST) is None
67
68     self.assertEqual(vcluster._GetRootDirectory(_ENV_DOES_NOT_EXIST), "")
69     self.assertEqual(vcluster._GetRootDirectory(_ENV_TEST), "")
70
71     os.environ[_ENV_TEST] = "some.host.example.com"
72     self.assertEqual(vcluster._GetHostname(_ENV_TEST), "some.host.example.com")
73
74
75 class TestCheckHostname(_EnvVarTest):
76   def test(self):
77     for i in ["/", "/tmp"]:
78       self.assertRaises(RuntimeError, vcluster._CheckHostname, i)
79
80
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,
86                       "/tmp/", "")
87
88   def testNonNormalizedRootDir(self):
89     self.assertRaises(AssertionError, vcluster._PreparePaths,
90                       "/tmp////xyz//", "host.example.com")
91
92   def testInvalidHostname(self):
93     self.assertRaises(RuntimeError, vcluster._PreparePaths, "/tmp", "/")
94
95   def testPathHostnameMismatch(self):
96     self.assertRaises(RuntimeError, vcluster._PreparePaths,
97                       "/tmp/host.example.com", "server.example.com")
98
99   def testNoVirtCluster(self):
100     for i in ["", None]:
101       self.assertEqual(vcluster._PreparePaths(i, i), ("", "", None))
102
103   def testVirtCluster(self):
104     self.assertEqual(vcluster._PreparePaths("/tmp/host.example.com",
105                                             "host.example.com"),
106                      ("/tmp", "/tmp/host.example.com", "host.example.com"))
107
108
109 class TestMakeNodeRoot(unittest.TestCase):
110   def test(self):
111     self.assertRaises(RuntimeError, vcluster._MakeNodeRoot, "/tmp", "/")
112
113     for i in ["/tmp", "/tmp/", "/tmp///"]:
114       self.assertEqual(vcluster._MakeNodeRoot(i, "other.example.com"),
115                        "/tmp/other.example.com")
116
117
118 class TestEnvironmentForHost(unittest.TestCase):
119   def test(self):
120     self.assertEqual(vcluster.EnvironmentForHost("host.example.com",
121                                                  _basedir=None),
122                      {})
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,
127         })
128
129
130 class TestExchangeNodeRoot(unittest.TestCase):
131   def test(self):
132     result = vcluster.ExchangeNodeRoot("node1.example.com", "/tmp/file",
133                                        _basedir=None, _noderoot=None)
134     self.assertEqual(result, "/tmp/file")
135
136     self.assertRaises(RuntimeError, vcluster.ExchangeNodeRoot,
137                       "node1.example.com", "/tmp/node1.example.com",
138                       _basedir="/tmp",
139                       _noderoot="/tmp/nodeZZ.example.com")
140
141     result = vcluster.ExchangeNodeRoot("node2.example.com",
142                                        "/tmp/node1.example.com/file",
143                                        _basedir="/tmp",
144                                        _noderoot="/tmp/node1.example.com")
145     self.assertEqual(result, "/tmp/node2.example.com/file")
146
147
148 class TestAddNodePrefix(unittest.TestCase):
149   def testRelativePath(self):
150     self.assertRaises(AssertionError, vcluster.AddNodePrefix,
151                       "foobar", _noderoot=None)
152
153   def testRelativeNodeRoot(self):
154     self.assertRaises(AssertionError, vcluster.AddNodePrefix,
155                       "/tmp", _noderoot="foobar")
156
157   def test(self):
158     path = vcluster.AddNodePrefix("/file/path",
159                                   _noderoot="/tmp/node1.example.com/")
160     self.assertEqual(path, "/tmp/node1.example.com/file/path")
161
162     self.assertEqual(vcluster.AddNodePrefix("/file/path", _noderoot=""),
163                      "/file/path")
164
165
166 class TestRemoveNodePrefix(unittest.TestCase):
167   def testRelativePath(self):
168     self.assertRaises(AssertionError, vcluster._RemoveNodePrefix,
169                       "foobar", _noderoot=None)
170
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")
176
177   def test(self):
178     path = vcluster._RemoveNodePrefix("/tmp/node1.example.com/file/path",
179                                       _noderoot="/tmp/node1.example.com")
180     self.assertEqual(path, "/file/path")
181
182     path = vcluster._RemoveNodePrefix("/file/path", _noderoot=None)
183     self.assertEqual(path, "/file/path")
184
185
186 class TestMakeVirtualPath(unittest.TestCase):
187   def testRelativePath(self):
188     self.assertRaises(AssertionError, vcluster.MakeVirtualPath,
189                       "foobar", _noderoot=None)
190
191   def testOutsideNodeRoot(self):
192     self.assertRaises(RuntimeError, vcluster.MakeVirtualPath,
193                       "/file/path", _noderoot="/tmp/node1.example.com")
194
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)
199
200   def testNormal(self):
201     self.assertEqual(vcluster.MakeVirtualPath("/tmp/file", _noderoot=None),
202                      "/tmp/file")
203
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)
210
211
212 class TestLocalizeVirtualPath(unittest.TestCase):
213   def testWrongPrefix(self):
214     self.assertRaises(RuntimeError, vcluster.LocalizeVirtualPath,
215                       "/tmp/some/path", _noderoot="/tmp/node1.example.com")
216
217   def testCorrectPrefixRelativePath(self):
218     self.assertRaises(AssertionError, vcluster.LocalizeVirtualPath,
219                       vcluster._VIRT_PATH_PREFIX + "foobar",
220                       _noderoot="/tmp/node1.example.com")
221
222   def testWithNodeRoot(self):
223     lvp = vcluster.LocalizeVirtualPath
224
225     virtpath1 = "%s/tmp/file" % vcluster._VIRT_PATH_PREFIX
226     virtpath2 = "%s////tmp////file" % vcluster._VIRT_PATH_PREFIX
227
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")
231
232   def testNormal(self):
233     self.assertEqual(vcluster.LocalizeVirtualPath("/tmp/file", _noderoot=None),
234                      "/tmp/file")
235
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)
242
243
244 class TestVirtualPathPrefix(unittest.TestCase):
245   def test(self):
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)
249
250
251 if __name__ == "__main__":
252   testutils.GanetiTestProgram()