| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import os.path |
| 20 |
from unittest import mock |
| 21 |
|
| 22 |
from oslo_serialization import jsonutils |
| 23 |
|
| 24 |
from mistralclient.api.v2 import executions |
| 25 |
from mistralclient.commands.v2 import executions as execution_cmd |
| 26 |
from mistralclient.tests.unit import base |
| 27 |
|
| 28 |
EXEC_DICT = { |
| 29 |
'id': '123', |
| 30 |
'workflow_id': '123e4567-e89b-12d3-a456-426655440000', |
| 31 |
'workflow_name': 'some', |
| 32 |
'workflow_namespace': '', |
| 33 |
'root_execution_id': '', |
| 34 |
'description': '', |
| 35 |
'state': 'SUCCESS', |
| 36 |
'state_info': None, |
| 37 |
'created_at': '2020-02-07 08:10:32', |
| 38 |
'updated_at': '2020-02-07 08:10:41', |
| 39 |
'task_execution_id': None |
| 40 |
} |
| 41 |
|
| 42 |
EXEC = executions.Execution(mock, EXEC_DICT) |
| 43 |
|
| 44 |
SUB_WF_EXEC = executions.Execution( |
| 45 |
mock, |
| 46 |
{ |
| 47 |
'id': '456', |
| 48 |
'workflow_id': '123e4567-e89b-12d3-a456-426655440000', |
| 49 |
'workflow_name': 'some_sub_wf', |
| 50 |
'workflow_namespace': '', |
| 51 |
'root_execution_id': 'ROOT_EXECUTION_ID', |
| 52 |
'description': '', |
| 53 |
'state': 'ERROR', |
| 54 |
'state_info': None, |
| 55 |
'created_at': '2020-02-07 08:10:32', |
| 56 |
'updated_at': '2020-02-07 08:10:41', |
| 57 |
'task_execution_id': 'abc' |
| 58 |
} |
| 59 |
) |
| 60 |
|
| 61 |
EX_RESULT = ( |
| 62 |
'123', |
| 63 |
'123e4567-e89b-12d3-a456-426655440000', |
| 64 |
'some', |
| 65 |
'', |
| 66 |
'', |
| 67 |
'<none>', |
| 68 |
'<none>', |
| 69 |
'SUCCESS', |
| 70 |
None, |
| 71 |
'2020-02-07 08:10:32', |
| 72 |
'2020-02-07 08:10:41', |
| 73 |
'0:00:09' |
| 74 |
) |
| 75 |
|
| 76 |
SUB_WF_EX_RESULT = ( |
| 77 |
'456', |
| 78 |
'123e4567-e89b-12d3-a456-426655440000', |
| 79 |
'some_sub_wf', |
| 80 |
'', |
| 81 |
'', |
| 82 |
'abc', |
| 83 |
'ROOT_EXECUTION_ID', |
| 84 |
'ERROR', |
| 85 |
None, |
| 86 |
'2020-02-07 08:10:32', |
| 87 |
'2020-02-07 08:10:41', |
| 88 |
'0:00:09' |
| 89 |
) |
| 90 |
|
| 91 |
EXECS_LIST = [EXEC, SUB_WF_EXEC] |
| 92 |
EXEC_PUBLISHED = {"bar1": "val1", "var2": 2} |
| 93 |
EXEC_WITH_PUBLISHED_DICT = EXEC_DICT.copy() |
| 94 |
EXEC_WITH_PUBLISHED_DICT.update( |
| 95 |
{'published_global': jsonutils.dumps(EXEC_PUBLISHED)}) |
| 96 |
EXEC_WITH_PUBLISHED = executions.Execution(mock, EXEC_WITH_PUBLISHED_DICT) |
| 97 |
|
| 98 |
|
| 99 |
class TestCLIExecutionsV2(base.BaseCommandTest): |
| 100 |
def setUp(self): |
| 101 |
super(TestCLIExecutionsV2, self).setUp() |
| 102 |
|
| 103 |
def tearDown(self): |
| 104 |
super(TestCLIExecutionsV2, self).tearDown() |
| 105 |
|
| 106 |
def test_create_wf_input_string(self): |
| 107 |
self.client.executions.create.return_value = EXEC |
| 108 |
|
| 109 |
result = self.call( |
| 110 |
execution_cmd.Create, |
| 111 |
app_args=['id', '{ "context": true }'] |
| 112 |
) |
| 113 |
|
| 114 |
self.assertEqual(EX_RESULT, result[1]) |
| 115 |
|
| 116 |
def test_create_wf_input_file(self): |
| 117 |
self.client.executions.create.return_value = EXEC |
| 118 |
|
| 119 |
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), |
| 120 |
'..', 'resources', 'ctx.json') |
| 121 |
|
| 122 |
result = self.call( |
| 123 |
execution_cmd.Create, |
| 124 |
app_args=['id', path] |
| 125 |
) |
| 126 |
|
| 127 |
self.assertEqual(EX_RESULT, result[1]) |
| 128 |
|
| 129 |
def test_create_with_description(self): |
| 130 |
self.client.executions.create.return_value = EXEC |
| 131 |
|
| 132 |
result = self.call( |
| 133 |
execution_cmd.Create, |
| 134 |
app_args=['id', '{ "context": true }', '-d', ''] |
| 135 |
) |
| 136 |
|
| 137 |
self.assertEqual(EX_RESULT, result[1]) |
| 138 |
|
| 139 |
def test_update_state(self): |
| 140 |
states = ['RUNNING', 'SUCCESS', 'PAUSED', 'ERROR', 'CANCELLED'] |
| 141 |
|
| 142 |
for state in states: |
| 143 |
self.client.executions.update.return_value = executions.Execution( |
| 144 |
mock, |
| 145 |
{ |
| 146 |
'id': '123', |
| 147 |
'workflow_id': '123e4567-e89b-12d3-a456-426655440000', |
| 148 |
'workflow_name': 'some', |
| 149 |
'workflow_namespace': '', |
| 150 |
'root_execution_id': '', |
| 151 |
'description': '', |
| 152 |
'state': state, |
| 153 |
'state_info': None, |
| 154 |
'created_at': '2020-02-07 08:10:32', |
| 155 |
'updated_at': '2020-02-07 08:10:41', |
| 156 |
'task_execution_id': None |
| 157 |
} |
| 158 |
) |
| 159 |
|
| 160 |
ex_result = list(EX_RESULT) |
| 161 |
ex_result[7] = state |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
del ex_result[11] |
| 166 |
ex_result = tuple(ex_result) |
| 167 |
|
| 168 |
result = self.call( |
| 169 |
execution_cmd.Update, |
| 170 |
app_args=['id', '-s', state] |
| 171 |
) |
| 172 |
|
| 173 |
result_ex = list(result[1]) |
| 174 |
del result_ex[11] |
| 175 |
result_ex = tuple(result_ex) |
| 176 |
|
| 177 |
self.assertEqual(ex_result, result_ex) |
| 178 |
|
| 179 |
def test_update_invalid_state(self): |
| 180 |
states = ['IDLE', 'WAITING', 'DELAYED'] |
| 181 |
|
| 182 |
for state in states: |
| 183 |
self.assertRaises( |
| 184 |
SystemExit, |
| 185 |
self.call, |
| 186 |
execution_cmd.Update, |
| 187 |
app_args=['id', '-s', state] |
| 188 |
) |
| 189 |
|
| 190 |
def test_resume_update_env(self): |
| 191 |
self.client.executions.update.return_value = EXEC |
| 192 |
|
| 193 |
result = self.call( |
| 194 |
execution_cmd.Update, |
| 195 |
app_args=['id', '-s', 'RUNNING', '--env', '{"k1": "foobar"}'] |
| 196 |
) |
| 197 |
|
| 198 |
self.assertEqual(EX_RESULT, result[1]) |
| 199 |
|
| 200 |
def test_update_description(self): |
| 201 |
self.client.executions.update.return_value = EXEC |
| 202 |
|
| 203 |
result = self.call( |
| 204 |
execution_cmd.Update, |
| 205 |
app_args=['id', '-d', 'foobar'] |
| 206 |
) |
| 207 |
|
| 208 |
self.assertEqual(EX_RESULT, result[1]) |
| 209 |
|
| 210 |
def test_list(self): |
| 211 |
self.client.executions.list.return_value = [SUB_WF_EXEC, EXEC] |
| 212 |
|
| 213 |
result = self.call(execution_cmd.List) |
| 214 |
|
| 215 |
self.assertEqual( |
| 216 |
[EX_RESULT, SUB_WF_EX_RESULT], |
| 217 |
result[1] |
| 218 |
) |
| 219 |
|
| 220 |
def test_sub_executions(self): |
| 221 |
self.client.executions.get_ex_sub_executions.return_value = \ |
| 222 |
EXECS_LIST |
| 223 |
|
| 224 |
result = self.call( |
| 225 |
execution_cmd.SubExecutionsLister, |
| 226 |
app_args=[EXEC_DICT['id']] |
| 227 |
) |
| 228 |
|
| 229 |
self.assertEqual([EX_RESULT, SUB_WF_EX_RESULT], result[1]) |
| 230 |
self.assertEqual( |
| 231 |
1, |
| 232 |
self.client.executions.get_ex_sub_executions.call_count |
| 233 |
) |
| 234 |
self.assertEqual( |
| 235 |
[mock.call(EXEC_DICT['id'], errors_only='', max_depth=-1)], |
| 236 |
self.client.executions.get_ex_sub_executions.call_args_list |
| 237 |
) |
| 238 |
|
| 239 |
def test_sub_executions_errors_only(self): |
| 240 |
self.client.executions.get_ex_sub_executions.return_value = \ |
| 241 |
EXECS_LIST |
| 242 |
|
| 243 |
self.call( |
| 244 |
execution_cmd.SubExecutionsLister, |
| 245 |
app_args=[EXEC_DICT['id'], '--errors-only'] |
| 246 |
) |
| 247 |
|
| 248 |
self.assertEqual( |
| 249 |
1, |
| 250 |
self.client.executions.get_ex_sub_executions.call_count |
| 251 |
) |
| 252 |
self.assertEqual( |
| 253 |
[mock.call(EXEC_DICT['id'], errors_only=True, max_depth=-1)], |
| 254 |
self.client.executions.get_ex_sub_executions.call_args_list |
| 255 |
) |
| 256 |
|
| 257 |
def test_sub_executions_with_max_depth(self): |
| 258 |
self.client.executions.get_ex_sub_executions.return_value = \ |
| 259 |
EXECS_LIST |
| 260 |
|
| 261 |
self.call( |
| 262 |
execution_cmd.SubExecutionsLister, |
| 263 |
app_args=[EXEC_DICT['id'], '--max-depth', '3'] |
| 264 |
) |
| 265 |
|
| 266 |
self.assertEqual( |
| 267 |
1, |
| 268 |
self.client.executions.get_ex_sub_executions.call_count |
| 269 |
) |
| 270 |
self.assertEqual( |
| 271 |
[mock.call(EXEC_DICT['id'], errors_only='', max_depth=3)], |
| 272 |
self.client.executions.get_ex_sub_executions.call_args_list |
| 273 |
) |
| 274 |
|
| 275 |
def test_list_with_pagination(self): |
| 276 |
self.client.executions.list.return_value = [EXEC] |
| 277 |
|
| 278 |
self.call(execution_cmd.List) |
| 279 |
self.client.executions.list.assert_called_once_with( |
| 280 |
fields=execution_cmd.ExecutionFormatter.fields(), |
| 281 |
limit=100, |
| 282 |
marker='', |
| 283 |
nulls='', |
| 284 |
sort_dirs='desc', |
| 285 |
sort_keys='created_at', |
| 286 |
task=None |
| 287 |
) |
| 288 |
|
| 289 |
self.call( |
| 290 |
execution_cmd.List, |
| 291 |
app_args=[ |
| 292 |
'--oldest' |
| 293 |
] |
| 294 |
) |
| 295 |
|
| 296 |
self.client.executions.list.assert_called_with( |
| 297 |
fields=execution_cmd.ExecutionFormatter.fields(), |
| 298 |
limit=100, |
| 299 |
marker='', |
| 300 |
nulls='', |
| 301 |
sort_keys='created_at', |
| 302 |
sort_dirs='asc', |
| 303 |
task=None |
| 304 |
) |
| 305 |
|
| 306 |
self.call( |
| 307 |
execution_cmd.List, |
| 308 |
app_args=[ |
| 309 |
'--limit', '5', |
| 310 |
'--sort_keys', 'id, Workflow', |
| 311 |
'--sort_dirs', 'desc', |
| 312 |
'--marker', 'abc' |
| 313 |
] |
| 314 |
) |
| 315 |
|
| 316 |
self.client.executions.list.assert_called_with( |
| 317 |
fields=execution_cmd.ExecutionFormatter.fields(), |
| 318 |
limit=5, |
| 319 |
marker='abc', |
| 320 |
nulls='', |
| 321 |
sort_keys='id, Workflow', |
| 322 |
sort_dirs='desc', |
| 323 |
task=None |
| 324 |
) |
| 325 |
|
| 326 |
def test_get(self): |
| 327 |
self.client.executions.get.return_value = EXEC |
| 328 |
|
| 329 |
result = self.call(execution_cmd.Get, app_args=['id']) |
| 330 |
|
| 331 |
self.assertEqual(EX_RESULT, result[1]) |
| 332 |
|
| 333 |
def test_get_sub_wf_ex(self): |
| 334 |
self.client.executions.get.return_value = SUB_WF_EXEC |
| 335 |
|
| 336 |
result = self.call(execution_cmd.Get, app_args=['id']) |
| 337 |
|
| 338 |
self.assertEqual(SUB_WF_EX_RESULT, result[1]) |
| 339 |
|
| 340 |
def test_delete(self): |
| 341 |
self.call(execution_cmd.Delete, app_args=['id']) |
| 342 |
|
| 343 |
self.client.executions.delete.assert_called_once_with( |
| 344 |
'id', |
| 345 |
force=False |
| 346 |
) |
| 347 |
|
| 348 |
def test_delete_with_force(self): |
| 349 |
self.call(execution_cmd.Delete, app_args=['id', '--force']) |
| 350 |
|
| 351 |
self.client.executions.delete.assert_called_once_with( |
| 352 |
'id', |
| 353 |
force=True |
| 354 |
) |
| 355 |
|
| 356 |
def test_delete_with_multi_names(self): |
| 357 |
self.call(execution_cmd.Delete, app_args=['id1', 'id2']) |
| 358 |
|
| 359 |
self.assertEqual(2, self.client.executions.delete.call_count) |
| 360 |
self.assertEqual( |
| 361 |
[mock.call('id1', force=False), mock.call('id2', force=False)], |
| 362 |
self.client.executions.delete.call_args_list |
| 363 |
) |
| 364 |
|
| 365 |
def test_get_published(self): |
| 366 |
self.client.executions.get.return_value = EXEC_WITH_PUBLISHED |
| 367 |
|
| 368 |
self.call(execution_cmd.GetPublished, app_args=['id']) |
| 369 |
|
| 370 |
self.assertDictEqual( |
| 371 |
EXEC_PUBLISHED, |
| 372 |
jsonutils.loads(self.app.stdout.write.call_args[0][0]) |
| 373 |
) |