|
| 1 | +# Copyright 2016 Easystack. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# 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, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | +# |
| 15 | + |
| 16 | +from openstackclient.compute.v2 import agent |
| 17 | +from openstackclient.tests.compute.v2 import fakes as compute_fakes |
| 18 | + |
| 19 | + |
| 20 | +class TestAgent(compute_fakes.TestComputev2): |
| 21 | + |
| 22 | + fake_agent = compute_fakes.FakeAgent.create_one_agent() |
| 23 | + |
| 24 | + columns = ( |
| 25 | + 'agent_id', |
| 26 | + 'architecture', |
| 27 | + 'hypervisor', |
| 28 | + 'md5hash', |
| 29 | + 'os', |
| 30 | + 'url', |
| 31 | + 'version', |
| 32 | + ) |
| 33 | + |
| 34 | + data = ( |
| 35 | + fake_agent.agent_id, |
| 36 | + fake_agent.architecture, |
| 37 | + fake_agent.hypervisor, |
| 38 | + fake_agent.md5hash, |
| 39 | + fake_agent.os, |
| 40 | + fake_agent.url, |
| 41 | + fake_agent.version, |
| 42 | + ) |
| 43 | + |
| 44 | + def setUp(self): |
| 45 | + super(TestAgent, self).setUp() |
| 46 | + |
| 47 | + self.agents_mock = self.app.client_manager.compute.agents |
| 48 | + self.agents_mock.reset_mock() |
| 49 | + |
| 50 | + |
| 51 | +class TestAgentCreate(TestAgent): |
| 52 | + |
| 53 | + def setUp(self): |
| 54 | + super(TestAgentCreate, self).setUp() |
| 55 | + |
| 56 | + self.agents_mock.create.return_value = self.fake_agent |
| 57 | + self.cmd = agent.CreateAgent(self.app, None) |
| 58 | + |
| 59 | + def test_agent_create(self): |
| 60 | + arglist = [ |
| 61 | + self.fake_agent.os, |
| 62 | + self.fake_agent.architecture, |
| 63 | + self.fake_agent.version, |
| 64 | + self.fake_agent.url, |
| 65 | + self.fake_agent.md5hash, |
| 66 | + self.fake_agent.hypervisor, |
| 67 | + ] |
| 68 | + |
| 69 | + verifylist = [ |
| 70 | + ('os', self.fake_agent.os), |
| 71 | + ('architecture', self.fake_agent.architecture), |
| 72 | + ('version', self.fake_agent.version), |
| 73 | + ('url', self.fake_agent.url), |
| 74 | + ('md5hash', self.fake_agent.md5hash), |
| 75 | + ('hypervisor', self.fake_agent.hypervisor), |
| 76 | + ] |
| 77 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 78 | + columns, data = self.cmd.take_action(parsed_args) |
| 79 | + self.agents_mock.create.assert_called_with(parsed_args.os, |
| 80 | + parsed_args.architecture, |
| 81 | + parsed_args.version, |
| 82 | + parsed_args.url, |
| 83 | + parsed_args.md5hash, |
| 84 | + parsed_args.hypervisor) |
| 85 | + |
| 86 | + self.assertEqual(self.columns, columns) |
| 87 | + self.assertEqual(self.data, data) |
| 88 | + |
| 89 | + |
| 90 | +class TestAgentDelete(TestAgent): |
| 91 | + |
| 92 | + def setUp(self): |
| 93 | + super(TestAgentDelete, self).setUp() |
| 94 | + |
| 95 | + self.agents_mock.get.return_value = self.fake_agent |
| 96 | + self.cmd = agent.DeleteAgent(self.app, None) |
| 97 | + |
| 98 | + def test_one_agent_delete(self): |
| 99 | + arglist = [ |
| 100 | + 'test' |
| 101 | + ] |
| 102 | + |
| 103 | + verifylist = [ |
| 104 | + ('id', 'test'), |
| 105 | + ] |
| 106 | + |
| 107 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 108 | + result = self.cmd.take_action(parsed_args) |
| 109 | + self.agents_mock.delete.assert_called_with(parsed_args.id) |
| 110 | + self.assertIsNone(result) |
| 111 | + |
| 112 | + |
| 113 | +class TestAgentList(TestAgent): |
| 114 | + |
| 115 | + agents = compute_fakes.FakeAgent.create_agents(count=3) |
| 116 | + list_columns = ( |
| 117 | + "Agent ID", |
| 118 | + "Hypervisor", |
| 119 | + "OS", |
| 120 | + "Architecture", |
| 121 | + "Version", |
| 122 | + "Md5Hash", |
| 123 | + "URL", |
| 124 | + ) |
| 125 | + |
| 126 | + list_data = [] |
| 127 | + for _agent in agents: |
| 128 | + list_data.append(( |
| 129 | + _agent.agent_id, |
| 130 | + _agent.hypervisor, |
| 131 | + _agent.os, |
| 132 | + _agent.architecture, |
| 133 | + _agent.version, |
| 134 | + _agent.md5hash, |
| 135 | + _agent.url, |
| 136 | + )) |
| 137 | + |
| 138 | + def setUp(self): |
| 139 | + |
| 140 | + super(TestAgentList, self).setUp() |
| 141 | + |
| 142 | + self.agents_mock.list.return_value = self.agents |
| 143 | + self.cmd = agent.ListAgent(self.app, None) |
| 144 | + |
| 145 | + def test_agent_list(self): |
| 146 | + |
| 147 | + arglist = [] |
| 148 | + verifylist = [] |
| 149 | + |
| 150 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 151 | + columns, data = self.cmd.take_action(parsed_args) |
| 152 | + |
| 153 | + self.assertEqual(self.list_columns, columns) |
| 154 | + self.assertEqual(self.list_data, list(data)) |
| 155 | + |
| 156 | + def test_agent_list_with_hypervisor(self): |
| 157 | + |
| 158 | + arglist = [ |
| 159 | + '--hypervisor', |
| 160 | + 'hypervisor', |
| 161 | + ] |
| 162 | + verifylist = [ |
| 163 | + ('hypervisor', 'hypervisor'), |
| 164 | + ] |
| 165 | + |
| 166 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 167 | + columns, data = self.cmd.take_action(parsed_args) |
| 168 | + |
| 169 | + self.assertEqual(self.list_columns, columns) |
| 170 | + self.assertEqual(self.list_data, list(data)) |
| 171 | + |
| 172 | + |
| 173 | +class TestAgentSet(TestAgent): |
| 174 | + |
| 175 | + def setUp(self): |
| 176 | + super(TestAgentSet, self).setUp() |
| 177 | + |
| 178 | + self.agents_mock.update.return_value = self.fake_agent |
| 179 | + self.cmd = agent.SetAgent(self.app, None) |
| 180 | + |
| 181 | + def test_agent_set(self): |
| 182 | + arglist = [ |
| 183 | + 'id', |
| 184 | + 'new-version', |
| 185 | + 'new-url', |
| 186 | + 'new-md5hash', |
| 187 | + ] |
| 188 | + |
| 189 | + verifylist = [ |
| 190 | + ('id', 'id'), |
| 191 | + ('version', 'new-version'), |
| 192 | + ('url', 'new-url'), |
| 193 | + ('md5hash', 'new-md5hash'), |
| 194 | + ] |
| 195 | + |
| 196 | + parsed_args = self.check_parser(self.cmd, arglist, verifylist) |
| 197 | + result = self.cmd.take_action(parsed_args) |
| 198 | + |
| 199 | + self.agents_mock.update.assert_called_with(parsed_args.id, |
| 200 | + parsed_args.version, |
| 201 | + parsed_args.url, |
| 202 | + parsed_args.md5hash) |
| 203 | + self.assertIsNone(result) |
0 commit comments