/usr/lib/python2.7/dist-packages/senlinclient/tests/unit/osc/v1/test_node.py is in python-senlinclient 0.4.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | # Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import copy
import mock
import six
from openstack.cluster.v1 import node as sdk_node
from openstack import exceptions as sdk_exc
from openstackclient.common import exceptions as exc
from senlinclient.osc.v1 import node as osc_node
from senlinclient.tests.unit.osc.v1 import fakes
class TestNode(fakes.TestClusteringv1):
def setUp(self):
super(TestNode, self).setUp()
self.mock_client = self.app.client_manager.clustering
class TestNodeList(TestNode):
columns = ['id', 'name', 'index', 'status', 'cluster_id',
'physical_id', 'profile_name', 'created_at', 'updated_at']
response = {"nodes": [
{
"cluster_id": None,
"created_at": "2015-02-27T04:39:21",
"data": {},
"details": {},
"domain": None,
"id": "573aa1ba-bf45-49fd-907d-6b5d6e6adfd3",
"index": -1,
"init_at": "2015-02-27T04:39:18",
"metadata": {},
"name": "node00a",
"physical_id": "cc028275-d078-4729-bf3e-154b7359814b",
"profile_id": "edc63d0a-2ca4-48fa-9854-27926da76a4a",
"profile_name": "mystack",
"project": "6e18cc2bdbeb48a5b3cad2dc499f6804",
"role": None,
"status": "ACTIVE",
"status_reason": "Creation succeeded",
"updated_at": None,
"user": "5e5bf8027826429c96af157f68dc9072"
}
]}
defaults = {
'cluster_id': None,
'global_project': False,
'marker': None,
'limit': None,
'sort': None,
}
def setUp(self):
super(TestNodeList, self).setUp()
self.cmd = osc_node.ListNode(self.app, None)
self.mock_client.nodes = mock.Mock(return_value=self.response)
def test_node_list_defaults(self):
arglist = []
parsed_args = self.check_parser(self.cmd, arglist, [])
columns, data = self.cmd.take_action(parsed_args)
self.mock_client.nodes.assert_called_with(**self.defaults)
self.assertEqual(self.columns, columns)
def test_node_list_full_id(self):
arglist = ['--full-id']
parsed_args = self.check_parser(self.cmd, arglist, [])
columns, data = self.cmd.take_action(parsed_args)
self.mock_client.nodes.assert_called_with(**self.defaults)
self.assertEqual(self.columns, columns)
def test_node_list_limit(self):
kwargs = copy.deepcopy(self.defaults)
kwargs['limit'] = '3'
arglist = ['--limit', '3']
parsed_args = self.check_parser(self.cmd, arglist, [])
columns, data = self.cmd.take_action(parsed_args)
self.mock_client.nodes.assert_called_with(**kwargs)
self.assertEqual(self.columns, columns)
def test_node_list_sort(self):
kwargs = copy.deepcopy(self.defaults)
kwargs['sort'] = 'name:asc'
arglist = ['--sort', 'name:asc']
parsed_args = self.check_parser(self.cmd, arglist, [])
columns, data = self.cmd.take_action(parsed_args)
self.mock_client.nodes.assert_called_with(**kwargs)
self.assertEqual(self.columns, columns)
def test_node_list_sort_invalid_key(self):
self.mock_client.nodes = mock.Mock(
return_value=self.response)
kwargs = copy.deepcopy(self.defaults)
kwargs['sort'] = 'bad_key'
arglist = ['--sort', 'bad_key']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.mock_client.nodes.side_effect = sdk_exc.HttpException()
self.assertRaises(sdk_exc.HttpException,
self.cmd.take_action, parsed_args)
def test_node_list_sort_invalid_direction(self):
self.mock_client.nodes = mock.Mock(
return_value=self.response)
kwargs = copy.deepcopy(self.defaults)
kwargs['sort'] = 'name:bad_direction'
arglist = ['--sort', 'name:bad_direction']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.mock_client.nodes.side_effect = sdk_exc.HttpException()
self.assertRaises(sdk_exc.HttpException,
self.cmd.take_action, parsed_args)
def test_node_list_filter(self):
kwargs = copy.deepcopy(self.defaults)
kwargs['name'] = 'my_node'
arglist = ['--filter', 'name=my_node']
parsed_args = self.check_parser(self.cmd, arglist, [])
columns, data = self.cmd.take_action(parsed_args)
self.mock_client.nodes.assert_called_with(**kwargs)
self.assertEqual(self.columns, columns)
def test_node_list_marker(self):
kwargs = copy.deepcopy(self.defaults)
kwargs['marker'] = 'a9448bf6'
arglist = ['--marker', 'a9448bf6']
parsed_args = self.check_parser(self.cmd, arglist, [])
columns, data = self.cmd.take_action(parsed_args)
self.mock_client.nodes.assert_called_with(**kwargs)
self.assertEqual(self.columns, columns)
class TestNodeShow(TestNode):
get_response = {"node": {
"cluster_id": None,
"created_at": "2015-02-10T12:03:16",
"data": {},
"details": {
"OS-DCF:diskConfig": "MANUAL"},
"domain": None,
"id": "d5779bb0-f0a0-49c9-88cc-6f078adb5a0b",
"index": -1,
"init_at": "2015-02-10T12:03:13",
"metadata": {},
"name": "my_node",
"physical_id": "f41537fa-22ab-4bea-94c0-c874e19d0c80",
"profile_id": "edc63d0a-2ca4-48fa-9854-27926da76a4a",
"profile_name": "mystack",
"project": "6e18cc2bdbeb48a5b3cad2dc499f6804",
"role": None,
"status": "ACTIVE",
"status_reason": "Creation succeeded",
"updated_at": "2015-03-04T04:58:27",
"user": "5e5bf8027826429c96af157f68dc9072"
}}
def setUp(self):
super(TestNodeShow, self).setUp()
self.cmd = osc_node.ShowNode(self.app, None)
self.mock_client.get_node = mock.Mock(
return_value=sdk_node.Node(attrs=self.get_response['node']))
def test_node_show(self):
arglist = ['my_node']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.get_node.assert_called_with('my_node', args=None)
def test_node_show_with_details(self):
arglist = ['my_node', '--details']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.get_node.assert_called_with(
'my_node', args={'show_details': True})
def test_node_show_not_found(self):
arglist = ['my_node']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.mock_client.get_node.side_effect = sdk_exc.ResourceNotFound()
error = self.assertRaises(exc.CommandError, self.cmd.take_action,
parsed_args)
self.assertEqual('Node not found: my_node', str(error))
class TestNodeCreate(TestNode):
response = {"node": {
"action": "2366d440-c73e-4961-9254-6d1c3af7c167",
"cluster_id": None,
"created_at": None,
"data": {},
"domain": None,
"id": "0df0931b-e251-4f2e-8719-4ebfda3627ba",
"index": -1,
"init_time": "2015-03-05T08:53:15",
"metadata": {},
"name": "my_node",
"physical_id": "",
"profile_id": "edc63d0a-2ca4-48fa-9854-27926da76a4a",
"profile_name": "mystack",
"project": "6e18cc2bdbeb48a5b3cad2dc499f6804",
"role": "master",
"status": "INIT",
"status_reason": "Initializing",
"updated_at": None,
"user": "5e5bf8027826429c96af157f68dc9072"
}}
defaults = {
"cluster_id": None,
"metadata": {},
"name": "my_node",
"profile_id": "mystack",
"role": None
}
def setUp(self):
super(TestNodeCreate, self).setUp()
self.cmd = osc_node.CreateNode(self.app, None)
self.mock_client.create_node = mock.Mock(
return_value=sdk_node.Node(attrs=self.response['node']))
self.mock_client.get_node = mock.Mock(
return_value=sdk_node.Node(attrs=self.response['node']))
def test_node_create_defaults(self):
arglist = ['my_node', '--profile', 'mystack']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.create_node.assert_called_with(**self.defaults)
def test_node_create_with_metadata(self):
arglist = ['my_node', '--profile', 'mystack',
'--metadata', 'key1=value1;key2=value2']
kwargs = copy.deepcopy(self.defaults)
kwargs['metadata'] = {'key1': 'value1', 'key2': 'value2'}
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.create_node.assert_called_with(**kwargs)
def test_node_create_with_cluster(self):
arglist = ['my_node', '--profile', 'mystack',
'--cluster', 'mycluster']
kwargs = copy.deepcopy(self.defaults)
kwargs['cluster_id'] = 'mycluster'
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.create_node.assert_called_with(**kwargs)
def test_node_create_with_role(self):
arglist = ['my_node', '--profile', 'mystack',
'--role', 'master']
kwargs = copy.deepcopy(self.defaults)
kwargs['role'] = 'master'
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.create_node.assert_called_with(**kwargs)
class TestNodeUpdate(TestNode):
response = {"node": {
"action": "2366d440-c73e-4961-9254-6d1c3af7c167",
"cluster_id": None,
"created_at": None,
"data": {},
"domain": None,
"id": "0df0931b-e251-4f2e-8719-4ebfda3627ba",
"index": -1,
"init_time": "2015-03-05T08:53:15",
"metadata": {},
"name": "my_node",
"physical_id": "",
"profile_id": "edc63d0a-2ca4-48fa-9854-27926da76a4a",
"profile_name": "mystack",
"project": "6e18cc2bdbeb48a5b3cad2dc499f6804",
"role": "master",
"status": "INIT",
"status_reason": "Initializing",
"updated_at": None,
"user": "5e5bf8027826429c96af157f68dc9072"
}}
defaults = {
"name": "new_node",
"metadata": {
"nk1": "nv1",
"nk2": "nv2",
},
"profile_id": "new_profile",
"role": "new_role"
}
def setUp(self):
super(TestNodeUpdate, self).setUp()
self.cmd = osc_node.UpdateNode(self.app, None)
self.mock_client.update_node = mock.Mock(
return_value=sdk_node.Node(attrs=self.response['node']))
self.mock_client.get_node = mock.Mock(
return_value=sdk_node.Node(attrs=self.response['node']))
self.mock_client.find_node = mock.Mock(
return_value=sdk_node.Node(attrs=self.response['node']))
def test_node_update_defaults(self):
arglist = ['--name', 'new_node', '--metadata', 'nk1=nv1;nk2=nv2',
'--profile', 'new_profile', '--role', 'new_role',
'0df0931b']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.update_node.assert_called_with(
'0df0931b-e251-4f2e-8719-4ebfda3627ba', **self.defaults)
def test_node_update_not_found(self):
arglist = ['--name', 'new_node', '--metadata', 'nk1=nv1;nk2=nv2',
'--profile', 'new_profile', '--role', 'new_role',
'c6b8b252']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.mock_client.find_node.return_value = None
error = self.assertRaises(exc.CommandError, self.cmd.take_action,
parsed_args)
self.assertIn('Node not found: c6b8b252', str(error))
class TestNodeDelete(TestNode):
def setUp(self):
super(TestNodeDelete, self).setUp()
self.cmd = osc_node.DeleteNode(self.app, None)
self.mock_client.delete_node = mock.Mock()
def test_node_delete(self):
arglist = ['node1', 'node2', 'node3']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.delete_node.assert_has_calls(
[mock.call('node1', False), mock.call('node2', False),
mock.call('node3', False)]
)
def test_node_delete_force(self):
arglist = ['node1', 'node2', 'node3', '--force']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
self.mock_client.delete_node.assert_has_calls(
[mock.call('node1', False), mock.call('node2', False),
mock.call('node3', False)]
)
def test_node_delete_not_found(self):
arglist = ['my_node']
self.mock_client.delete_node.side_effect = sdk_exc.ResourceNotFound
parsed_args = self.check_parser(self.cmd, arglist, [])
error = self.assertRaises(exc.CommandError, self.cmd.take_action,
parsed_args)
self.assertIn('Failed to delete 1 of the 1 specified node(s).',
str(error))
def test_node_delete_one_found_one_not_found(self):
arglist = ['node1', 'node2']
self.mock_client.delete_node.side_effect = (
[None, sdk_exc.ResourceNotFound]
)
parsed_args = self.check_parser(self.cmd, arglist, [])
error = self.assertRaises(exc.CommandError,
self.cmd.take_action, parsed_args)
self.mock_client.delete_node.assert_has_calls(
[mock.call('node1', False), mock.call('node2', False)]
)
self.assertEqual('Failed to delete 1 of the 2 specified node(s).',
str(error))
@mock.patch('sys.stdin', spec=six.StringIO)
def test_node_delete_prompt_yes(self, mock_stdin):
arglist = ['my_node']
mock_stdin.isatty.return_value = True
mock_stdin.readline.return_value = 'y'
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
mock_stdin.readline.assert_called_with()
self.mock_client.delete_node.assert_called_with('my_node',
False)
@mock.patch('sys.stdin', spec=six.StringIO)
def test_node_delete_prompt_no(self, mock_stdin):
arglist = ['my_node']
mock_stdin.isatty.return_value = True
mock_stdin.readline.return_value = 'n'
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
mock_stdin.readline.assert_called_with()
self.mock_client.delete_node.assert_not_called()
|