blob: 7a6b57691aabaddbff0c118eb3c289c5c84d938e [file] [log] [blame]
step682980c14ec2016-02-23 14:53:52 -05001# Copyright 2016 Rackspace
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15import os
16import shutil
17import subprocess
18import tempfile
19
Anna Pankiewicz0691ee52018-06-19 15:04:22 -050020from mock import patch
21try:
22 from StringIO import StringIO
23except ImportError:
24 from io import StringIO
Cao Xuan Hoang36fe23c2016-08-25 16:11:14 +070025from tempest.cmd import workspace
step682980c14ec2016-02-23 14:53:52 -050026from tempest.lib.common.utils import data_utils
27from tempest.tests import base
28
29
30class TestTempestWorkspaceBase(base.TestCase):
31 def setUp(self):
32 super(TestTempestWorkspaceBase, self).setUp()
33 self.name = data_utils.rand_uuid()
34 self.path = tempfile.mkdtemp()
35 self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
36 store_dir = tempfile.mkdtemp()
37 self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
38 self.store_file = os.path.join(store_dir, 'workspace.yaml')
Cao Xuan Hoang36fe23c2016-08-25 16:11:14 +070039 self.workspace_manager = workspace.WorkspaceManager(
40 path=self.store_file)
step682980c14ec2016-02-23 14:53:52 -050041 self.workspace_manager.register_new_workspace(self.name, self.path)
42
43
44class TestTempestWorkspace(TestTempestWorkspaceBase):
45 def _run_cmd_gets_return_code(self, cmd, expected):
46 process = subprocess.Popen(cmd, stdout=subprocess.PIPE,
47 stderr=subprocess.PIPE)
48 stdout, stderr = process.communicate()
49 return_code = process.returncode
zhuflbedb2ad2016-06-20 11:39:01 +080050 msg = ("%s failed with:\nstdout: %s\nstderr: %s" % (' '.join(cmd),
afazekas40fcb9b2019-03-08 11:25:11 +010051 stdout, stderr))
step682980c14ec2016-02-23 14:53:52 -050052 self.assertEqual(return_code, expected, msg)
53
54 def test_run_workspace_list(self):
Masayuki Igawa00effc92016-07-25 12:28:26 +090055 cmd = ['tempest', 'workspace', 'list',
56 '--workspace-path', self.store_file]
step682980c14ec2016-02-23 14:53:52 -050057 self._run_cmd_gets_return_code(cmd, 0)
58
59 def test_run_workspace_register(self):
60 name = data_utils.rand_uuid()
61 path = tempfile.mkdtemp()
62 self.addCleanup(shutil.rmtree, path, ignore_errors=True)
Masayuki Igawa00effc92016-07-25 12:28:26 +090063 cmd = ['tempest', 'workspace', 'register',
64 '--workspace-path', self.store_file,
65 '--name', name, '--path', path]
step682980c14ec2016-02-23 14:53:52 -050066 self._run_cmd_gets_return_code(cmd, 0)
67 self.assertIsNotNone(self.workspace_manager.get_workspace(name))
68
69 def test_run_workspace_rename(self):
70 new_name = data_utils.rand_uuid()
Masayuki Igawa00effc92016-07-25 12:28:26 +090071 cmd = ['tempest', 'workspace', 'rename',
72 '--workspace-path', self.store_file,
73 '--old-name', self.name, '--new-name', new_name]
step682980c14ec2016-02-23 14:53:52 -050074 self._run_cmd_gets_return_code(cmd, 0)
75 self.assertIsNone(self.workspace_manager.get_workspace(self.name))
76 self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))
77
78 def test_run_workspace_move(self):
79 new_path = tempfile.mkdtemp()
80 self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
Masayuki Igawa00effc92016-07-25 12:28:26 +090081 cmd = ['tempest', 'workspace', 'move',
82 '--workspace-path', self.store_file,
83 '--name', self.name, '--path', new_path]
step682980c14ec2016-02-23 14:53:52 -050084 self._run_cmd_gets_return_code(cmd, 0)
85 self.assertEqual(
86 self.workspace_manager.get_workspace(self.name), new_path)
87
Chandan Kumarb78e2572017-06-26 19:34:34 +053088 def test_run_workspace_remove_entry(self):
Masayuki Igawa00effc92016-07-25 12:28:26 +090089 cmd = ['tempest', 'workspace', 'remove',
90 '--workspace-path', self.store_file,
91 '--name', self.name]
step682980c14ec2016-02-23 14:53:52 -050092 self._run_cmd_gets_return_code(cmd, 0)
93 self.assertIsNone(self.workspace_manager.get_workspace(self.name))
94
Chandan Kumarb78e2572017-06-26 19:34:34 +053095 def test_run_workspace_remove_directory(self):
96 cmd = ['tempest', 'workspace', 'remove',
97 '--workspace-path', self.store_file,
98 '--name', self.name, '--rmdir']
99 self._run_cmd_gets_return_code(cmd, 0)
100 self.assertIsNone(self.workspace_manager.get_workspace(self.name))
101
step682980c14ec2016-02-23 14:53:52 -0500102
103class TestTempestWorkspaceManager(TestTempestWorkspaceBase):
104 def setUp(self):
105 super(TestTempestWorkspaceManager, self).setUp()
106 self.name = data_utils.rand_uuid()
107 self.path = tempfile.mkdtemp()
108 self.addCleanup(shutil.rmtree, self.path, ignore_errors=True)
109 store_dir = tempfile.mkdtemp()
110 self.addCleanup(shutil.rmtree, store_dir, ignore_errors=True)
111 self.store_file = os.path.join(store_dir, 'workspace.yaml')
Cao Xuan Hoang36fe23c2016-08-25 16:11:14 +0700112 self.workspace_manager = workspace.WorkspaceManager(
113 path=self.store_file)
step682980c14ec2016-02-23 14:53:52 -0500114 self.workspace_manager.register_new_workspace(self.name, self.path)
115
116 def test_workspace_manager_get(self):
117 self.assertIsNotNone(self.workspace_manager.get_workspace(self.name))
118
119 def test_workspace_manager_rename(self):
120 new_name = data_utils.rand_uuid()
121 self.workspace_manager.rename_workspace(self.name, new_name)
122 self.assertIsNone(self.workspace_manager.get_workspace(self.name))
123 self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))
124
Manik Bindlish864f37e2018-09-06 06:30:51 +0000125 def test_workspace_manager_rename_no_name_exist(self):
126 no_name = ""
127 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
128 ex = self.assertRaises(SystemExit,
129 self.workspace_manager.rename_workspace,
130 self.name, no_name)
131 self.assertEqual(1, ex.code)
132 self.assertEqual(mock_stdout.getvalue(),
133 "None or empty name is specified."
134 " Please specify correct name for workspace.\n")
135
Manik Bindlishc8254242018-09-12 10:55:03 +0000136 def test_workspace_manager_rename_with_existing_name(self):
137 new_name = self.name
138 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
139 ex = self.assertRaises(SystemExit,
140 self.workspace_manager.rename_workspace,
141 self.name, new_name)
142 self.assertEqual(1, ex.code)
143 self.assertEqual(mock_stdout.getvalue(),
144 "A workspace already exists with name: %s.\n"
145 % new_name)
146
147 def test_workspace_manager_rename_no_exist_old_name(self):
148 old_name = ""
149 new_name = data_utils.rand_uuid()
150 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
151 ex = self.assertRaises(SystemExit,
152 self.workspace_manager.rename_workspace,
153 old_name, new_name)
154 self.assertEqual(1, ex.code)
155 self.assertEqual(mock_stdout.getvalue(),
156 "A workspace was not found with name: %s\n"
157 % old_name)
158
159 def test_workspace_manager_rename_integer_data(self):
160 old_name = self.name
161 new_name = 12345
162 self.workspace_manager.rename_workspace(old_name, new_name)
163 self.assertIsNone(self.workspace_manager.get_workspace(old_name))
164 self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))
165
166 def test_workspace_manager_rename_alphanumeric_data(self):
167 old_name = self.name
168 new_name = 'abc123'
169 self.workspace_manager.rename_workspace(old_name, new_name)
170 self.assertIsNone(self.workspace_manager.get_workspace(old_name))
171 self.assertIsNotNone(self.workspace_manager.get_workspace(new_name))
172
step682980c14ec2016-02-23 14:53:52 -0500173 def test_workspace_manager_move(self):
174 new_path = tempfile.mkdtemp()
175 self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
176 self.workspace_manager.move_workspace(self.name, new_path)
177 self.assertEqual(
178 self.workspace_manager.get_workspace(self.name), new_path)
Manik Bindlishc8254242018-09-12 10:55:03 +0000179 # NOTE(mbindlish): Also checking for the workspace that it
180 # shouldn't exist in old path
181 self.assertNotEqual(
182 self.workspace_manager.get_workspace(self.name), self.path)
183
184 def test_workspace_manager_move_wrong_path(self):
185 new_path = 'wrong/path'
186 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
187 ex = self.assertRaises(SystemExit,
188 self.workspace_manager.move_workspace,
189 self.name, new_path)
190 self.assertEqual(1, ex.code)
191 self.assertEqual(mock_stdout.getvalue(),
192 "Path does not exist.\n")
193
194 def test_workspace_manager_move_wrong_workspace(self):
195 workspace_name = "wrong_workspace_name"
196 new_path = tempfile.mkdtemp()
197 self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
198 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
199 ex = self.assertRaises(SystemExit,
200 self.workspace_manager.move_workspace,
201 workspace_name, new_path)
202 self.assertEqual(1, ex.code)
203 self.assertEqual(mock_stdout.getvalue(),
204 "A workspace was not found with name: %s\n"
205 % workspace_name)
206
207 def test_workspace_manager_move_no_workspace_name(self):
208 workspace_name = ""
209 new_path = tempfile.mkdtemp()
210 self.addCleanup(shutil.rmtree, new_path, ignore_errors=True)
211 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
212 ex = self.assertRaises(SystemExit,
213 self.workspace_manager.move_workspace,
214 workspace_name, new_path)
215 self.assertEqual(1, ex.code)
216 self.assertEqual(mock_stdout.getvalue(),
217 "A workspace was not found with name: %s\n"
218 % workspace_name)
step682980c14ec2016-02-23 14:53:52 -0500219
Manik Bindlish6c956782018-10-25 06:59:55 +0000220 def test_workspace_manager_move_no_workspace_path(self):
221 new_path = ""
222 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
223 ex = self.assertRaises(SystemExit,
224 self.workspace_manager.move_workspace,
225 self.name, new_path)
226 self.assertEqual(1, ex.code)
227 self.assertEqual(mock_stdout.getvalue(),
228 "None or empty path is specified for workspace."
229 " Please specify correct workspace path.\n")
230
Chandan Kumarb78e2572017-06-26 19:34:34 +0530231 def test_workspace_manager_remove_entry(self):
232 self.workspace_manager.remove_workspace_entry(self.name)
233 self.assertIsNone(self.workspace_manager.get_workspace(self.name))
234
Manik Bindlishc8254242018-09-12 10:55:03 +0000235 def test_workspace_manager_remove_entry_no_name(self):
236 no_name = ""
237 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
238 ex = self.assertRaises(SystemExit,
239 self.workspace_manager.
240 remove_workspace_entry,
241 no_name)
242 self.assertEqual(1, ex.code)
243 self.assertEqual(mock_stdout.getvalue(),
244 "A workspace was not found with name: %s\n"
245 % no_name)
246
247 def test_workspace_manager_remove_entry_wrong_name(self):
248 wrong_name = "wrong_name"
249 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
250 ex = self.assertRaises(SystemExit,
251 self.workspace_manager.
252 remove_workspace_entry,
253 wrong_name)
254 self.assertEqual(1, ex.code)
255 self.assertEqual(mock_stdout.getvalue(),
256 "A workspace was not found with name: %s\n"
257 % wrong_name)
258
Chandan Kumarb78e2572017-06-26 19:34:34 +0530259 def test_workspace_manager_remove_directory(self):
260 path = self.workspace_manager.remove_workspace_entry(self.name)
261 self.workspace_manager.remove_workspace_directory(path)
step682980c14ec2016-02-23 14:53:52 -0500262 self.assertIsNone(self.workspace_manager.get_workspace(self.name))
263
Manik Bindlish6c956782018-10-25 06:59:55 +0000264 def test_workspace_manager_remove_directory_no_path(self):
265 no_path = ""
266 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
267 ex = self.assertRaises(SystemExit,
268 self.workspace_manager.
269 remove_workspace_directory,
270 no_path)
271 self.assertEqual(1, ex.code)
272 self.assertEqual(mock_stdout.getvalue(),
273 "None or empty path is specified for workspace."
274 " Please specify correct workspace path.\n")
275
step682980c14ec2016-02-23 14:53:52 -0500276 def test_path_expansion(self):
277 name = data_utils.rand_uuid()
278 path = os.path.join("~", name)
279 os.makedirs(os.path.expanduser(path))
280 self.addCleanup(shutil.rmtree, path, ignore_errors=True)
281 self.workspace_manager.register_new_workspace(name, path)
282 self.assertIsNotNone(self.workspace_manager.get_workspace(name))
Anna Pankiewicz0691ee52018-06-19 15:04:22 -0500283
284 def test_workspace_name_not_exists(self):
285 nonexistent_name = data_utils.rand_uuid()
286 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
287 ex = self.assertRaises(SystemExit,
288 self.workspace_manager._name_exists,
289 nonexistent_name)
290 self.assertEqual(1, ex.code)
291 self.assertEqual(mock_stdout.getvalue(),
Manik Bindlishc8254242018-09-12 10:55:03 +0000292 "A workspace was not found with name: %s\n"
293 % nonexistent_name)
294
295 def test_workspace_name_exists(self):
296 self.assertIsNone(self.workspace_manager._name_exists(self.name))
Anna Pankiewicz0691ee52018-06-19 15:04:22 -0500297
298 def test_workspace_name_already_exists(self):
299 duplicate_name = self.name
300 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
301 ex = self.assertRaises(SystemExit,
302 self.workspace_manager.
303 _workspace_name_exists,
304 duplicate_name)
305 self.assertEqual(1, ex.code)
306 self.assertEqual(mock_stdout.getvalue(),
307 "A workspace already exists with name: %s.\n"
308 % duplicate_name)
309
Manik Bindlishc8254242018-09-12 10:55:03 +0000310 def test_workspace_name_exists_check_new_name(self):
311 new_name = "fake_name"
312 self.assertIsNone(self.workspace_manager.
313 _workspace_name_exists(new_name))
314
Anna Pankiewicz0691ee52018-06-19 15:04:22 -0500315 def test_workspace_manager_path_not_exist(self):
316 fake_path = "fake_path"
317 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
318 ex = self.assertRaises(SystemExit,
319 self.workspace_manager._validate_path,
320 fake_path)
321 self.assertEqual(1, ex.code)
322 self.assertEqual(mock_stdout.getvalue(),
323 "Path does not exist.\n")
324
Manik Bindlishc8254242018-09-12 10:55:03 +0000325 def test_validate_path_exists(self):
326 new_path = self.path
327 self.assertIsNone(self.workspace_manager.
328 _validate_path(new_path))
329
Anna Pankiewicz0691ee52018-06-19 15:04:22 -0500330 def test_workspace_manager_list_workspaces(self):
331 listed = self.workspace_manager.list_workspaces()
332 self.assertEqual(1, len(listed))
333 self.assertIn(self.name, listed)
334 self.assertEqual(self.path, listed.get(self.name))
Manik Bindlish864f37e2018-09-06 06:30:51 +0000335
336 def test_register_new_workspace_no_name(self):
337 no_name = ""
338 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
339 ex = self.assertRaises(SystemExit,
340 self.workspace_manager.
341 register_new_workspace,
342 no_name, self.path)
343 self.assertEqual(1, ex.code)
344 self.assertEqual(mock_stdout.getvalue(),
345 "None or empty name is specified."
346 " Please specify correct name for workspace.\n")
Manik Bindlish6c956782018-10-25 06:59:55 +0000347
348 def test_register_new_workspace_no_path(self):
349 no_path = ""
350 with patch('sys.stdout', new_callable=StringIO) as mock_stdout:
351 ex = self.assertRaises(SystemExit,
352 self.workspace_manager.
353 register_new_workspace,
354 self.name, no_path)
355 self.assertEqual(1, ex.code)
356 self.assertEqual(mock_stdout.getvalue(),
357 "None or empty path is specified for workspace."
358 " Please specify correct workspace path.\n")
Manik Bindlishc8254242018-09-12 10:55:03 +0000359
360 def test_register_new_workspace_integer_data(self):
361 workspace_name = 12345
362 self.workspace_manager.register_new_workspace(
363 workspace_name, self.path)
364 self.assertIsNotNone(
365 self.workspace_manager.get_workspace(workspace_name))
366 self.assertEqual(
367 self.workspace_manager.get_workspace(workspace_name), self.path)
368
369 def test_register_new_workspace_alphanumeric_data(self):
370 workspace_name = 'abc123'
371 self.workspace_manager.register_new_workspace(
372 workspace_name, self.path)
373 self.assertIsNotNone(
374 self.workspace_manager.get_workspace(workspace_name))
375 self.assertEqual(
376 self.workspace_manager.get_workspace(workspace_name), self.path)