Debian Codegraph @

mistralclient/tests/unit/v2/test_cli_environments.py

raw symbols SCIP
1 # Copyright 2015 - StackStorm, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain 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,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import copy
16 import os
17 import tempfile
18 from unittest import mock
19
20 from oslo_serialization import jsonutils
21 from oslo_utils import timeutils
22 import yaml
23
24 from mistralclient.api.v2 import environments
25 from mistralclient.commands.v2 import environments as environment_cmd
26 from mistralclient.tests.unit import base
27
28
29 ENVIRONMENT_DICT = {
30 'name': 'env1',
31 'description': 'Test Environment #1',
32 'scope': 'private',
33 'variables': {
34 'server': 'localhost',
35 'database': 'test',
36 'timeout': 600,
37 'verbose': True
38 },
39 'created_at': str(timeutils.utcnow()),
40 'updated_at': str(timeutils.utcnow())
41 }
42
43 ENVIRONMENT = environments.Environment(mock, ENVIRONMENT_DICT)
44 EXPECTED_RESULT = (ENVIRONMENT_DICT['name'],
45 ENVIRONMENT_DICT['description'],
46 jsonutils.dumps(ENVIRONMENT_DICT['variables'], indent=4),
47 ENVIRONMENT_DICT['scope'],
48 ENVIRONMENT_DICT['created_at'],
49 ENVIRONMENT_DICT['updated_at'])
50
51 EXPECTED_EXPORT_RESULT = (ENVIRONMENT_DICT['name'],
52 ENVIRONMENT_DICT['description'],
53 ENVIRONMENT_DICT['scope'],
54 jsonutils.dumps(ENVIRONMENT_DICT['variables']))
55
56
57 class TestCLIEnvironmentsV2(base.BaseCommandTest):
58
59 def _test_create(self, content):
60 self.client.environments.create.return_value = ENVIRONMENT
61
62 with tempfile.NamedTemporaryFile() as f:
63 f.write(content.encode('utf-8'))
64 f.flush()
65 file_path = os.path.abspath(f.name)
66 result = self.call(environment_cmd.Create, app_args=[file_path])
67 self.assertEqual(EXPECTED_RESULT, result[1])
68
69 def test_create_from_json(self):
70 self._test_create(jsonutils.dumps(ENVIRONMENT_DICT, indent=4))
71
72 def test_create_from_yaml(self):
73 yml = yaml.dump(ENVIRONMENT_DICT, default_flow_style=False)
74 self._test_create(yml)
75
76 def _test_update(self, content):
77 self.client.environments.update.return_value = ENVIRONMENT
78
79 with tempfile.NamedTemporaryFile() as f:
80 f.write(content.encode('utf-8'))
81 f.flush()
82 file_path = os.path.abspath(f.name)
83 result = self.call(environment_cmd.Update, app_args=[file_path])
84 self.assertEqual(EXPECTED_RESULT, result[1])
85
86 def test_update_from_json(self):
87 env = copy.deepcopy(ENVIRONMENT_DICT)
88 del env['created_at']
89 del env['updated_at']
90 self._test_update(jsonutils.dumps(env, indent=4))
91
92 def test_update_from_yaml(self):
93 env = copy.deepcopy(ENVIRONMENT_DICT)
94 del env['created_at']
95 del env['updated_at']
96 yml = yaml.dump(env, default_flow_style=False)
97 self._test_update(yml)
98
99 def test_list(self):
100 self.client.environments.list.return_value = [ENVIRONMENT]
101 expected = (ENVIRONMENT_DICT['name'],
102 ENVIRONMENT_DICT['description'],
103 ENVIRONMENT_DICT['scope'],
104 ENVIRONMENT_DICT['created_at'],
105 ENVIRONMENT_DICT['updated_at'])
106
107 result = self.call(environment_cmd.List)
108
109 self.assertListEqual([expected], result[1])
110
111 def test_get(self):
112 self.client.environments.get.return_value = ENVIRONMENT
113
114 result = self.call(environment_cmd.Get, app_args=['name'])
115
116 self.assertEqual(EXPECTED_RESULT, result[1])
117
118 def test_get_with_export(self):
119 self.client.environments.get.return_value = ENVIRONMENT
120
121 result = self.call(environment_cmd.Get, app_args=['--export', 'name'])
122
123 self.assertEqual(EXPECTED_EXPORT_RESULT, result[1])
124
125 def test_delete(self):
126 self.call(environment_cmd.Delete, app_args=['name'])
127
128 self.client.environments.delete.assert_called_once_with('name')
129
130 def test_delete_with_multi_names(self):
131 self.call(environment_cmd.Delete, app_args=['name1', 'name2'])
132
133 self.assertEqual(2, self.client.environments.delete.call_count)
134 self.assertEqual(
135 [mock.call('name1'), mock.call('name2')],
136 self.client.environments.delete.call_args_list
137 )