Debian Codegraph @

mistralclient/tests/unit/v2/test_cli_actions.py

raw symbols SCIP
1 # Copyright 2014 Mirantis, Inc.
2 # Copyright 2020 Nokia Software.
3 # All Rights Reserved
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16 #
17
18 from unittest import mock
19
20
21 from mistralclient.api.v2 import actions
22 from mistralclient.commands.v2 import actions as action_cmd
23 from mistralclient.commands.v2 import base as cmd_base
24 from mistralclient.tests.unit import base
25
26
27 ACTION_DICT = {
28 'id': '1234-4567-7894-7895',
29 'name': 'a',
30 'is_system': True,
31 'input': "param1",
32 'description': 'My cool action',
33 'tags': ['test'],
34 'created_at': '1',
35 'updated_at': '1',
36 'namespace': 'test_namespace'
37 }
38
39 ACTION_DEF = """
40 ---
41 version: '2.0'
42
43 base: std.echo
44 base-parameters:
45 output: "<% $.str1 %><% $.str2 %>"
46 output: "<% $ %><% $ %>"
47 """
48
49 ACTION_WITH_DEF_DICT = ACTION_DICT.copy()
50 ACTION_WITH_DEF_DICT.update({'definition': ACTION_DEF})
51 ACTION = actions.Action(mock, ACTION_DICT)
52 ACTION_WITH_DEF = actions.Action(mock, ACTION_WITH_DEF_DICT)
53
54
55 class TestCLIActionsV2(base.BaseCommandTest):
56 @mock.patch('argparse.open', create=True)
57 def test_create(self, mock_open):
58 self.client.actions.create.return_value = [ACTION]
59
60 result = self.call(action_cmd.Create, app_args=['1.txt'])
61
62 self.assertEqual(
63 [('1234-4567-7894-7895', 'a', True, "param1",
64 'My cool action', 'test', '1', '1')],
65 result[1]
66 )
67
68 @mock.patch('argparse.open', create=True)
69 def test_create_public(self, mock_open):
70 self.client.actions.create.return_value = [ACTION]
71
72 result = self.call(
73 action_cmd.Create,
74 app_args=['1.txt', '--public']
75 )
76
77 self.assertEqual(
78 [('1234-4567-7894-7895', 'a', True, "param1",
79 'My cool action', 'test', '1', '1')],
80 result[1]
81 )
82
83 self.assertEqual(
84 'public',
85 self.client.actions.create.call_args[1]['scope']
86 )
87
88 @mock.patch('argparse.open', create=True)
89 def test_create_long_input(self, mock_open):
90 action_long_input_dict = ACTION_DICT.copy()
91 long_input = ', '.join(
92 ['var%s' % i for i in range(10)]
93 )
94 action_long_input_dict['input'] = long_input
95 workflow_long_input = actions.Action(
96 mock.Mock(),
97 action_long_input_dict
98 )
99 self.client.actions.create.return_value = [workflow_long_input]
100
101 result = self.call(action_cmd.Create, app_args=['1.txt'])
102
103 self.assertEqual(
104 [('1234-4567-7894-7895', 'a',
105 True, cmd_base.cut(long_input), 'My cool action',
106 'test', '1', '1')],
107 result[1]
108 )
109
110 @mock.patch('argparse.open', create=True)
111 def test_update(self, mock_open):
112 self.client.actions.update.return_value = [ACTION]
113
114 result = self.call(action_cmd.Update, app_args=['my_action.yaml'])
115
116 self.assertEqual(
117 [('1234-4567-7894-7895', 'a', True, "param1",
118 'My cool action', 'test', '1', '1')],
119 result[1]
120 )
121
122 @mock.patch('argparse.open', create=True)
123 def test_update_public(self, mock_open):
124 self.client.actions.update.return_value = [ACTION]
125
126 result = self.call(
127 action_cmd.Update,
128 app_args=['my_action.yaml', '--public']
129 )
130
131 self.assertEqual(
132 [('1234-4567-7894-7895', 'a', True, "param1",
133 'My cool action', 'test', '1', '1')],
134 result[1]
135 )
136
137 self.assertEqual(
138 'public',
139 self.client.actions.update.call_args[1]['scope']
140 )
141
142 def test_list(self):
143 self.client.actions.list.return_value = [ACTION]
144
145 result = self.call(action_cmd.List)
146
147 self.assertEqual(
148 [('1234-4567-7894-7895', 'a', True, "param1",
149 'My cool action', 'test', '1', '1')],
150 result[1]
151 )
152
153 def test_get(self):
154 self.client.actions.get.return_value = ACTION
155
156 result = self.call(action_cmd.Get, app_args=['name'])
157
158 self.assertEqual(
159 ('1234-4567-7894-7895', 'a', True, "param1",
160 'My cool action', 'test', '1', '1'),
161 result[1]
162 )
163
164 def test_delete(self):
165 self.call(action_cmd.Delete, app_args=['name'])
166
167 self.client.actions.delete.assert_called_once_with('name',
168 namespace='')
169
170 def test_delete_with_namespace(self):
171 self.call(action_cmd.Delete, app_args=['name',
172 '--namespace',
173 'test_namespace']
174 )
175
176 self.client.actions.delete.assert_called_once_with(
177 'name',
178 namespace='test_namespace')
179
180 def test_delete_with_multi_names_and_namespace(self):
181 self.call(action_cmd.Delete, app_args=['name1',
182 'name2',
183 '--namespace',
184 'test_namespace'])
185
186 self.assertEqual(2, self.client.actions.delete.call_count)
187 self.assertEqual(
188 [mock.call('name1', namespace='test_namespace'),
189 mock.call('name2', namespace='test_namespace')],
190 self.client.actions.delete.call_args_list
191 )
192
193 def test_delete_with_multi_names(self):
194 self.call(action_cmd.Delete, app_args=['name1', 'name2'])
195
196 self.assertEqual(2, self.client.actions.delete.call_count)
197 self.assertEqual(
198 [mock.call('name1', namespace=''),
199 mock.call('name2', namespace='')],
200 self.client.actions.delete.call_args_list
201 )
202
203 def test_get_definition(self):
204 self.client.actions.get.return_value = ACTION_WITH_DEF
205
206 self.call(action_cmd.GetDefinition, app_args=['name'])
207
208 self.app.stdout.write.assert_called_with(ACTION_DEF)
209
210 @mock.patch('argparse.open', create=True)
211 def test_validate(self, mock_open):
212 self.client.actions.validate.return_value = {'valid': True}
213
214 result = self.call(action_cmd.Validate, app_args=['action.yaml'])
215
216 self.assertEqual((True, None), result[1])
217
218 @mock.patch('argparse.open', create=True)
219 def test_validate_failed(self, mock_open):
220 self.client.actions.validate.return_value = {
221 'valid': False,
222 'error': 'Invalid DSL...'
223 }
224
225 result = self.call(action_cmd.Validate, app_args=['action.yaml'])
226
227 self.assertEqual((False, 'Invalid DSL...'), result[1])