| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
|
| 18 |
from mistralclient.api.v2 import dynamic_actions |
| 19 |
from mistralclient.commands.v2 import dynamic_actions as dyn_actions_cmd |
| 20 |
from mistralclient.tests.unit import base |
| 21 |
|
| 22 |
|
| 23 |
DYN_ACTION_DICT = { |
| 24 |
'id': '1-2-3-4', |
| 25 |
'name': 'my_action', |
| 26 |
'class_name': 'MyAction', |
| 27 |
'code_source_name': 'my_module', |
| 28 |
'code_source_id': '2-3-4-5', |
| 29 |
'namespace': '', |
| 30 |
'project_id': '12345', |
| 31 |
'scope': 'private', |
| 32 |
'created_at': '1', |
| 33 |
'updated_at': '1' |
| 34 |
} |
| 35 |
|
| 36 |
DYN_ACTION = dynamic_actions.DynamicAction(mock, DYN_ACTION_DICT) |
| 37 |
|
| 38 |
|
| 39 |
class TestCLIDynamicActions(base.BaseCommandTest): |
| 40 |
@mock.patch('argparse.open', create=True) |
| 41 |
def test_create(self, mock_open): |
| 42 |
self.client.dynamic_actions.create.return_value = DYN_ACTION |
| 43 |
|
| 44 |
result = self.call( |
| 45 |
dyn_actions_cmd.Create, |
| 46 |
app_args=['my_action', 'MyAction', 'my_module'] |
| 47 |
) |
| 48 |
|
| 49 |
self.assertEqual( |
| 50 |
( |
| 51 |
'1-2-3-4', |
| 52 |
'my_action', |
| 53 |
'MyAction', |
| 54 |
'2-3-4-5', |
| 55 |
'my_module', |
| 56 |
'12345', |
| 57 |
'private', |
| 58 |
'1', |
| 59 |
'1' |
| 60 |
), |
| 61 |
result[1] |
| 62 |
) |
| 63 |
|
| 64 |
@mock.patch('argparse.open', create=True) |
| 65 |
def test_create_public(self, mock_open): |
| 66 |
dyn_action_dict = DYN_ACTION_DICT.copy() |
| 67 |
dyn_action_dict['scope'] = 'public' |
| 68 |
|
| 69 |
dyn_action = dynamic_actions.DynamicAction(mock, dyn_action_dict) |
| 70 |
|
| 71 |
self.client.dynamic_actions.create.return_value = dyn_action |
| 72 |
|
| 73 |
result = self.call( |
| 74 |
dyn_actions_cmd.Create, |
| 75 |
app_args=['my_action', 'MyAction', 'my_module', '--public'] |
| 76 |
) |
| 77 |
|
| 78 |
self.assertEqual( |
| 79 |
( |
| 80 |
'1-2-3-4', |
| 81 |
'my_action', |
| 82 |
'MyAction', |
| 83 |
'2-3-4-5', |
| 84 |
'my_module', |
| 85 |
'12345', |
| 86 |
'public', |
| 87 |
'1', |
| 88 |
'1' |
| 89 |
), |
| 90 |
result[1] |
| 91 |
) |
| 92 |
self.assertEqual( |
| 93 |
'public', |
| 94 |
self.client.dynamic_actions.create.call_args[1]['scope'] |
| 95 |
) |
| 96 |
|
| 97 |
@mock.patch('argparse.open', create=True) |
| 98 |
def test_update(self, mock_open): |
| 99 |
dyn_action_dict = DYN_ACTION_DICT.copy() |
| 100 |
dyn_action_dict['class_name'] = 'MyNewAction' |
| 101 |
|
| 102 |
dyn_action = dynamic_actions.DynamicAction(mock, dyn_action_dict) |
| 103 |
|
| 104 |
self.client.dynamic_actions.update.return_value = dyn_action |
| 105 |
|
| 106 |
result = self.call( |
| 107 |
dyn_actions_cmd.Update, |
| 108 |
app_args=['my_action', '--class-name', 'MyNewAction'] |
| 109 |
) |
| 110 |
|
| 111 |
self.assertEqual( |
| 112 |
( |
| 113 |
'1-2-3-4', |
| 114 |
'my_action', |
| 115 |
'MyNewAction', |
| 116 |
'2-3-4-5', |
| 117 |
'my_module', |
| 118 |
'12345', |
| 119 |
'private', |
| 120 |
'1', |
| 121 |
'1' |
| 122 |
), |
| 123 |
result[1] |
| 124 |
) |
| 125 |
|
| 126 |
@mock.patch('argparse.open', create=True) |
| 127 |
def test_update_with_id(self, mock_open): |
| 128 |
dyn_action_dict = DYN_ACTION_DICT.copy() |
| 129 |
dyn_action_dict['class_name'] = 'MyNewAction' |
| 130 |
|
| 131 |
dyn_action = dynamic_actions.DynamicAction(mock, dyn_action_dict) |
| 132 |
|
| 133 |
self.client.dynamic_actions.update.return_value = dyn_action |
| 134 |
|
| 135 |
result = self.call( |
| 136 |
dyn_actions_cmd.Update, |
| 137 |
app_args=['1-2-3-4', '--class-name', 'MyNewAction'] |
| 138 |
) |
| 139 |
|
| 140 |
self.assertEqual( |
| 141 |
( |
| 142 |
'1-2-3-4', |
| 143 |
'my_action', |
| 144 |
'MyNewAction', |
| 145 |
'2-3-4-5', |
| 146 |
'my_module', |
| 147 |
'12345', |
| 148 |
'private', |
| 149 |
'1', |
| 150 |
'1' |
| 151 |
), |
| 152 |
result[1] |
| 153 |
) |
| 154 |
|
| 155 |
def test_list(self): |
| 156 |
self.client.dynamic_actions.list.return_value = [DYN_ACTION] |
| 157 |
|
| 158 |
result = self.call(dyn_actions_cmd.List) |
| 159 |
|
| 160 |
self.assertEqual( |
| 161 |
[ |
| 162 |
( |
| 163 |
'1-2-3-4', |
| 164 |
'my_action', |
| 165 |
'MyAction', |
| 166 |
'2-3-4-5', |
| 167 |
'my_module', |
| 168 |
'12345', |
| 169 |
'private', |
| 170 |
'1', |
| 171 |
'1' |
| 172 |
) |
| 173 |
], |
| 174 |
result[1] |
| 175 |
) |
| 176 |
|
| 177 |
def test_get(self): |
| 178 |
self.client.dynamic_actions.get.return_value = DYN_ACTION |
| 179 |
|
| 180 |
result = self.call(dyn_actions_cmd.Get, app_args=['name']) |
| 181 |
|
| 182 |
self.assertEqual( |
| 183 |
( |
| 184 |
'1-2-3-4', |
| 185 |
'my_action', |
| 186 |
'MyAction', |
| 187 |
'2-3-4-5', |
| 188 |
'my_module', |
| 189 |
'12345', |
| 190 |
'private', |
| 191 |
'1', |
| 192 |
'1' |
| 193 |
), |
| 194 |
result[1] |
| 195 |
) |
| 196 |
|
| 197 |
def test_delete(self): |
| 198 |
self.call(dyn_actions_cmd.Delete, app_args=['my_action']) |
| 199 |
|
| 200 |
self.client.dynamic_actions.delete.assert_called_once_with( |
| 201 |
'my_action', |
| 202 |
namespace='' |
| 203 |
) |
| 204 |
|
| 205 |
def test_delete_with_multi_names(self): |
| 206 |
self.call(dyn_actions_cmd.Delete, app_args=['name1', 'name2']) |
| 207 |
|
| 208 |
self.assertEqual(2, self.client.dynamic_actions.delete.call_count) |
| 209 |
self.assertEqual( |
| 210 |
[ |
| 211 |
mock.call('name1', namespace=''), |
| 212 |
mock.call('name2', namespace='') |
| 213 |
], |
| 214 |
self.client.dynamic_actions.delete.call_args_list |
| 215 |
) |