|
| 1 | +# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | + |
| 15 | +import mock |
| 16 | + |
| 17 | +from get_properties import GetProperties |
| 18 | +from vsphere_base_action_test_case import VsphereBaseActionTestCase |
| 19 | + |
| 20 | +__all__ = [ |
| 21 | + 'GetPropertiesTestCase' |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +class GetPropertiesTestCase(VsphereBaseActionTestCase): |
| 26 | + __test__ = True |
| 27 | + action_cls = GetProperties |
| 28 | + |
| 29 | + transformed = { |
| 30 | + "vm-22": { |
| 31 | + "config.hardware.memoryMB": 10240, |
| 32 | + "guest.ipAddress": "10.99.0.4", |
| 33 | + "name": "VCSA" |
| 34 | + }, |
| 35 | + "vm-46": { |
| 36 | + "config.hardware.memoryMB": 2048, |
| 37 | + "guest.ipAddress": "fe80::250:56ff:feb4:cfb9", |
| 38 | + "name": "testvm" |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + def setUp(self): |
| 43 | + def mockDynamicProperty(name, val): |
| 44 | + result = mock.Mock() |
| 45 | + result.name = name |
| 46 | + result.val = val |
| 47 | + return result |
| 48 | + super(GetPropertiesTestCase, self).setUp() |
| 49 | + self._action = self.get_action_instance(self.new_config) |
| 50 | + self._action.establish_connection = mock.Mock() |
| 51 | + self._action.si_content = mock.Mock() |
| 52 | + self._action.si_content.rootFolder = mock.Mock() |
| 53 | + self._action.si_content.viewManager = mock.Mock() |
| 54 | + self._action.si_content.viewManager.CreateContainerView = mock.Mock() |
| 55 | + self._action.si_content.viewManager.CreateListView = mock.Mock() |
| 56 | + self._action.si_content.propertyCollector = mock.Mock() |
| 57 | + # mock up the raw objects used to test transform |
| 58 | + vm22 = mock.Mock() |
| 59 | + vm22.obj = 'vim.VirtualMachine:vm-22' |
| 60 | + vm22.propSet = [mockDynamicProperty("config.hardware.memoryMB", 10240), |
| 61 | + mockDynamicProperty("guest.ipAddress", "10.99.0.4"), |
| 62 | + mockDynamicProperty("name", "VCSA")] |
| 63 | + vm46 = mock.Mock() |
| 64 | + vm46.obj = 'vim.VirtualMachine:vm-46' |
| 65 | + vm46.propSet = [mockDynamicProperty("config.hardware.memoryMB", 2048), |
| 66 | + mockDynamicProperty("guest.ipAddress", |
| 67 | + "fe80::250:56ff:feb4:cfb9"), |
| 68 | + mockDynamicProperty("name", "testvm")] |
| 69 | + self.raw = [vm22, vm46] |
| 70 | + |
| 71 | + @mock.patch('pyVmomi.vmodl.query.PropertyCollector') |
| 72 | + def test_simple_property_by_id(self, pc): |
| 73 | + with mock.patch.object( |
| 74 | + self._action.si_content.propertyCollector, 'RetrieveContents', |
| 75 | + return_value=self.raw): |
| 76 | + result = self._action.run(type='VirtualMachine', |
| 77 | + id=['vm-22', 'vm-46'], |
| 78 | + property=['does.not.exist'], raw=False) |
| 79 | + assert self._action.si_content.viewManager.CreateListView.called |
| 80 | + # status True because every object was found |
| 81 | + self.assertEqual(result, (True, self.transformed)) |
| 82 | + with mock.patch.object( |
| 83 | + self._action.si_content.propertyCollector, 'RetrieveContents', |
| 84 | + return_value=self.raw): |
| 85 | + result = self._action.run(type='VirtualMachine', |
| 86 | + id=['vm-22', 'vm-46', 'vm-47'], |
| 87 | + property=None, raw=False) |
| 88 | + # status False because not every object was found |
| 89 | + self.assertEqual(result, (False, self.transformed)) |
| 90 | + |
| 91 | + @mock.patch('pyVmomi.vmodl.query.PropertyCollector') |
| 92 | + def test_simple_by_type(self, pc): |
| 93 | + with mock.patch.object( |
| 94 | + self._action.si_content.propertyCollector, 'RetrieveContents', |
| 95 | + return_value=self.raw): |
| 96 | + result = self._action.run(type='VirtualMachine', id=None, |
| 97 | + property=None, raw=False) |
| 98 | + assert\ |
| 99 | + self._action.si_content.viewManager.CreateContainerView.called |
| 100 | + self.assertEqual(result, (True, self.transformed)) |
| 101 | + |
| 102 | + @mock.patch('pyVmomi.vmodl.query.PropertyCollector') |
| 103 | + def test_raw_output(self, pc): |
| 104 | + with mock.patch.object( |
| 105 | + self._action.si_content.propertyCollector, 'RetrieveContents', |
| 106 | + return_value=self.raw): |
| 107 | + result = self._action.run(type='VirtualMachine', id=None, |
| 108 | + property=None, raw=True) |
| 109 | + self.assertNotEqual(result, self.transformed) |
0 commit comments