|
| 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 io import StringIO |
| 14 | +from unittest import mock |
| 15 | + |
| 16 | +from openstackclient.common import project_cleanup |
| 17 | +from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes |
| 18 | +from openstackclient.tests.unit import utils as tests_utils |
| 19 | + |
| 20 | + |
| 21 | +class TestProjectCleanupBase(tests_utils.TestCommand): |
| 22 | + |
| 23 | + def setUp(self): |
| 24 | + super(TestProjectCleanupBase, self).setUp() |
| 25 | + |
| 26 | + self.app.client_manager.sdk_connection = mock.Mock() |
| 27 | + |
| 28 | + |
| 29 | +class TestProjectCleanup(TestProjectCleanupBase): |
| 30 | + |
| 31 | + project = identity_fakes.FakeProject.create_one_project() |
| 32 | + |
| 33 | + def setUp(self): |
| 34 | + super(TestProjectCleanup, self).setUp() |
| 35 | + self.cmd = project_cleanup.ProjectCleanup(self.app, None) |
| 36 | + |
| 37 | + self.project_cleanup_mock = mock.Mock() |
| 38 | + self.sdk_connect_as_project_mock = \ |
| 39 | + mock.Mock(return_value=self.app.client_manager.sdk_connection) |
| 40 | + self.app.client_manager.sdk_connection.project_cleanup = \ |
| 41 | + self.project_cleanup_mock |
| 42 | + self.app.client_manager.sdk_connection.identity.find_project = \ |
| 43 | + mock.Mock(return_value=self.project) |
| 44 | + self.app.client_manager.sdk_connection.connect_as_project = \ |
| 45 | + self.sdk_connect_as_project_mock |
| 46 | + |
| 47 | + def test_project_no_options(self): |
| 48 | + arglist = [] |
| 49 | + verifylist = [] |
| 50 | + |
| 51 | + self.assertRaises(tests_utils.ParserException, self.check_parser, |
| 52 | + self.cmd, arglist, verifylist) |
| 53 | + |
| 54 | + def test_project_cleanup_with_filters(self): |
| 55 | + arglist = [ |
| 56 | + '--project', self.project.id, |
| 57 | + '--created-before', '2200-01-01', |
| 58 | + '--updated-before', '2200-01-02' |
| 59 | + ] |
| 60 | + verifylist = [ |
| 61 | + ('dry_run', False), |
| 62 | + ('auth_project', False), |
| 63 | + ('project', self.project.id), |
| 64 | + ('created_before', '2200-01-01'), |
| 65 | + ('updated_before', '2200-01-02') |
| 66 | + ] |
| 67 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 68 | + result = None |
| 69 | + |
| 70 | + with mock.patch('sys.stdin', StringIO('y')): |
| 71 | + result = self.cmd.take_action(parsed_args) |
| 72 | + |
| 73 | + self.sdk_connect_as_project_mock.assert_called_with( |
| 74 | + self.project) |
| 75 | + filters = { |
| 76 | + 'created_at': '2200-01-01', |
| 77 | + 'updated_at': '2200-01-02' |
| 78 | + } |
| 79 | + |
| 80 | + calls = [ |
| 81 | + mock.call(dry_run=True, status_queue=mock.ANY, filters=filters), |
| 82 | + mock.call(dry_run=False, status_queue=mock.ANY, filters=filters) |
| 83 | + ] |
| 84 | + self.project_cleanup_mock.assert_has_calls(calls) |
| 85 | + |
| 86 | + self.assertIsNone(result) |
| 87 | + |
| 88 | + def test_project_cleanup_with_project(self): |
| 89 | + arglist = [ |
| 90 | + '--project', self.project.id, |
| 91 | + ] |
| 92 | + verifylist = [ |
| 93 | + ('dry_run', False), |
| 94 | + ('auth_project', False), |
| 95 | + ('project', self.project.id), |
| 96 | + ] |
| 97 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 98 | + result = None |
| 99 | + |
| 100 | + with mock.patch('sys.stdin', StringIO('y')): |
| 101 | + result = self.cmd.take_action(parsed_args) |
| 102 | + |
| 103 | + self.sdk_connect_as_project_mock.assert_called_with( |
| 104 | + self.project) |
| 105 | + calls = [ |
| 106 | + mock.call(dry_run=True, status_queue=mock.ANY, filters={}), |
| 107 | + mock.call(dry_run=False, status_queue=mock.ANY, filters={}) |
| 108 | + ] |
| 109 | + self.project_cleanup_mock.assert_has_calls(calls) |
| 110 | + |
| 111 | + self.assertIsNone(result) |
| 112 | + |
| 113 | + def test_project_cleanup_with_project_abort(self): |
| 114 | + arglist = [ |
| 115 | + '--project', self.project.id, |
| 116 | + ] |
| 117 | + verifylist = [ |
| 118 | + ('dry_run', False), |
| 119 | + ('auth_project', False), |
| 120 | + ('project', self.project.id), |
| 121 | + ] |
| 122 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 123 | + result = None |
| 124 | + |
| 125 | + with mock.patch('sys.stdin', StringIO('n')): |
| 126 | + result = self.cmd.take_action(parsed_args) |
| 127 | + |
| 128 | + self.sdk_connect_as_project_mock.assert_called_with( |
| 129 | + self.project) |
| 130 | + calls = [ |
| 131 | + mock.call(dry_run=True, status_queue=mock.ANY, filters={}), |
| 132 | + ] |
| 133 | + self.project_cleanup_mock.assert_has_calls(calls) |
| 134 | + |
| 135 | + self.assertIsNone(result) |
| 136 | + |
| 137 | + def test_project_cleanup_with_dry_run(self): |
| 138 | + arglist = [ |
| 139 | + '--dry-run', |
| 140 | + '--project', self.project.id, |
| 141 | + ] |
| 142 | + verifylist = [ |
| 143 | + ('dry_run', True), |
| 144 | + ('auth_project', False), |
| 145 | + ('project', self.project.id), |
| 146 | + ] |
| 147 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 148 | + result = None |
| 149 | + |
| 150 | + result = self.cmd.take_action(parsed_args) |
| 151 | + |
| 152 | + self.sdk_connect_as_project_mock.assert_called_with( |
| 153 | + self.project) |
| 154 | + self.project_cleanup_mock.assert_called_once_with( |
| 155 | + dry_run=True, status_queue=mock.ANY, filters={}) |
| 156 | + |
| 157 | + self.assertIsNone(result) |
| 158 | + |
| 159 | + def test_project_cleanup_with_auth_project(self): |
| 160 | + self.app.client_manager.auth_ref = mock.Mock() |
| 161 | + self.app.client_manager.auth_ref.project_id = self.project.id |
| 162 | + arglist = [ |
| 163 | + '--auth-project', |
| 164 | + ] |
| 165 | + verifylist = [ |
| 166 | + ('dry_run', False), |
| 167 | + ('auth_project', True), |
| 168 | + ('project', None), |
| 169 | + ] |
| 170 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 171 | + result = None |
| 172 | + |
| 173 | + with mock.patch('sys.stdin', StringIO('y')): |
| 174 | + result = self.cmd.take_action(parsed_args) |
| 175 | + |
| 176 | + self.sdk_connect_as_project_mock.assert_not_called() |
| 177 | + calls = [ |
| 178 | + mock.call(dry_run=True, status_queue=mock.ANY, filters={}), |
| 179 | + mock.call(dry_run=False, status_queue=mock.ANY, filters={}) |
| 180 | + ] |
| 181 | + self.project_cleanup_mock.assert_has_calls(calls) |
| 182 | + |
| 183 | + self.assertIsNone(result) |
0 commit comments