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