Debian Codegraph @

heatclient/tests/unit/osc/v1/test_snapshot.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 from unittest import mock
14
15 import io
16 from osc_lib import exceptions as exc
17
18 from heatclient import exc as heat_exc
19 from heatclient.osc.v1 import snapshot
20 from heatclient.tests.unit.osc.v1 import fakes as orchestration_fakes
21
22
23 class TestStack(orchestration_fakes.TestOrchestrationv1):
24 def setUp(self):
25 super().setUp()
26 self.mock_client = self.app.client_manager.orchestration
27 self.stack_client = self.app.client_manager.orchestration.stacks
28
29
30 class TestListSnapshot(TestStack):
31 def setUp(self):
32 super().setUp()
33 self.cmd = snapshot.ListSnapshot(self.app, None)
34 self.stack_client.snapshot_list.return_value = {'snapshots': []}
35
36 def test_snapshot_list(self):
37 arglist = ['my_stack']
38 parsed_args = self.check_parser(self.cmd, arglist, [])
39 self.cmd.take_action(parsed_args)
40 self.stack_client.snapshot_list.assert_called_with(
41 stack_id='my_stack')
42
43 def test_snapshot_list_error(self):
44 self.stack_client.snapshot_list.side_effect = heat_exc.HTTPNotFound()
45 arglist = ['my_stack']
46 parsed_args = self.check_parser(self.cmd, arglist, [])
47 error = self.assertRaises(
48 exc.CommandError,
49 self.cmd.take_action,
50 parsed_args)
51 self.assertEqual('Stack not found: my_stack',
52 str(error))
53
54
55 class TestSnapshotShow(TestStack):
56 def setUp(self):
57 super().setUp()
58 self.cmd = snapshot.ShowSnapshot(self.app, None)
59
60 def test_snapshot_show(self):
61 arglist = ['my_stack', 'snapshot_id']
62 parsed_args = self.check_parser(self.cmd, arglist, [])
63 self.stack_client.snapshot_show.return_value = {}
64 self.cmd.take_action(parsed_args)
65 self.stack_client.snapshot_show.assert_called_with(
66 'my_stack', 'snapshot_id')
67
68 def test_snapshot_not_found(self):
69 arglist = ['my_stack', 'snapshot_id']
70 parsed_args = self.check_parser(self.cmd, arglist, [])
71 self.stack_client.snapshot_show.side_effect = heat_exc.HTTPNotFound()
72 self.assertRaises(
73 exc.CommandError,
74 self.cmd.take_action,
75 parsed_args)
76
77
78 class TestRestoreSnapshot(TestStack):
79 def setUp(self):
80 super().setUp()
81 self.cmd = snapshot.RestoreSnapshot(self.app, None)
82
83 def test_snapshot_restore(self):
84 arglist = ['my_stack', 'my_snapshot']
85 parsed_args = self.check_parser(self.cmd, arglist, [])
86 self.cmd.take_action(parsed_args)
87 self.stack_client.restore.assert_called_with(
88 snapshot_id='my_snapshot', stack_id='my_stack')
89
90 def test_snapshot_restore_error(self):
91 self.stack_client.restore.side_effect = heat_exc.HTTPNotFound()
92 arglist = ['my_stack', 'my_snapshot']
93 parsed_args = self.check_parser(self.cmd, arglist, [])
94 error = self.assertRaises(
95 exc.CommandError,
96 self.cmd.take_action,
97 parsed_args)
98 self.assertEqual('Stack my_stack or snapshot my_snapshot not found.',
99 str(error))
100
101
102 class TestSnapshotCreate(TestStack):
103 get_response = {
104 "action": "CREATE",
105 "status": "IN_PROGRESS",
106 "name": "test_snapshot",
107 "status_reason": None,
108 "creation_time": "2015-11-09T04:35:38.534130",
109 "data": None,
110 "id": "108604fe-6d13-41b7-aa3a-79b6cf60c4ff"
111 }
112
113 def setUp(self):
114 super().setUp()
115 self.cmd = snapshot.CreateSnapshot(self.app, None)
116
117 def test_snapshot_create(self):
118 arglist = ['my_stack', '--name', 'test_snapshot']
119 parsed_args = self.check_parser(self.cmd, arglist, [])
120 self.stack_client.snapshot.return_value = self.get_response
121 self.cmd.take_action(parsed_args)
122 self.stack_client.snapshot.assert_called_with(
123 'my_stack', 'test_snapshot')
124
125 def test_snapshot_create_no_name(self):
126 arglist = ['my_stack']
127 parsed_args = self.check_parser(self.cmd, arglist, [])
128 self.stack_client.snapshot.return_value = self.get_response
129 self.cmd.take_action(parsed_args)
130 self.stack_client.snapshot.assert_called_with(
131 'my_stack', None)
132
133 def test_snapshot_create_error(self):
134 arglist = ['my_stack', '--name', 'test_snapshot']
135 parsed_args = self.check_parser(self.cmd, arglist, [])
136 self.stack_client.snapshot.side_effect = heat_exc.HTTPNotFound
137 self.assertRaises(
138 exc.CommandError,
139 self.cmd.take_action,
140 parsed_args)
141
142
143 class TestSnapshotDelete(TestStack):
144 def setUp(self):
145 super().setUp()
146 self.cmd = snapshot.DeleteSnapshot(self.app, None)
147
148 def test_snapshot_delete(self):
149 arglist = ['my_stack', 'snapshot_id']
150 parsed_args = self.check_parser(self.cmd, arglist, [])
151 self.cmd.take_action(parsed_args)
152 self.stack_client.snapshot_delete.assert_called_with(
153 'my_stack', 'snapshot_id')
154
155 def test_snapshot_delete_not_found(self):
156 arglist = ['my_stack', 'snapshot_id']
157 parsed_args = self.check_parser(self.cmd, arglist, [])
158 self.stack_client.snapshot_delete.side_effect = heat_exc.HTTPNotFound()
159 self.assertRaises(
160 exc.CommandError,
161 self.cmd.take_action,
162 parsed_args)
163
164 @mock.patch('sys.stdin', spec=io.StringIO)
165 def test_snapshot_delete_prompt(self, mock_stdin):
166 arglist = ['my_stack', 'snapshot_id']
167 mock_stdin.isatty.return_value = True
168 mock_stdin.readline.return_value = 'y'
169 parsed_args = self.check_parser(self.cmd, arglist, [])
170
171 self.cmd.take_action(parsed_args)
172
173 mock_stdin.readline.assert_called_with()
174 self.stack_client.snapshot_delete.assert_called_with('my_stack',
175 'snapshot_id')
176
177 @mock.patch('sys.stdin', spec=io.StringIO)
178 def test_snapshot_delete_prompt_no(self, mock_stdin):
179 arglist = ['my_stack', 'snapshot_id']
180 mock_stdin.isatty.return_value = True
181 mock_stdin.readline.return_value = 'n'
182 parsed_args = self.check_parser(self.cmd, arglist, [])
183
184 self.cmd.take_action(parsed_args)
185
186 mock_stdin.readline.assert_called_with()
187 self.stack_client.snapshot_delete.assert_not_called()