|
| 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 copy |
| 16 | +import datetime |
| 17 | +import mock |
| 18 | +import pyVmomi |
| 19 | + |
| 20 | +from get_properties import GetProperties |
| 21 | +from pyVmomi import vim # pylint: disable-msg=E0611 |
| 22 | +from vsphere_base_action_test_case import VsphereBaseActionTestCase |
| 23 | + |
| 24 | +__all__ = [ |
| 25 | + 'GetPropertiesTestCase' |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +class GetPropertiesTestCase(VsphereBaseActionTestCase): |
| 30 | + __test__ = True |
| 31 | + action_cls = GetProperties |
| 32 | + |
| 33 | + transformed = { |
| 34 | + "vm-22": { |
| 35 | + "config.hardware.memoryMB": 10240, |
| 36 | + "dataobj-dyn": { |
| 37 | + "arguments": "arguments", |
| 38 | + 'dynamicType': "Hello", |
| 39 | + 'dynamicProperty': [], |
| 40 | + "envVariables": ["A=B", "C=D"], |
| 41 | + "programPath": "cmd.exe", |
| 42 | + "workingDirectory": "/tmp" |
| 43 | + }, |
| 44 | + "guest.ipAddress": "10.99.0.4", |
| 45 | + "name": "VCSA", |
| 46 | + "network": ["network-15"], |
| 47 | + "time": "1992-01-12T01:01:22Z" |
| 48 | + }, |
| 49 | + "vm-46": { |
| 50 | + "config.hardware.memoryMB": 2048, |
| 51 | + "dataobj-nodyn": { |
| 52 | + "arguments": "arguments", |
| 53 | + "envVariables": ["A=B", "C=D"], |
| 54 | + "programPath": "cmd.exe", |
| 55 | + "workingDirectory": "/tmp" |
| 56 | + }, |
| 57 | + "guest.ipAddress": "fe80::250:56ff:feb4:cfb9", |
| 58 | + "name": "testvm", |
| 59 | + "network": [], |
| 60 | + "time": "1992-01-17T01:01:46Z" |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + def setUp(self): |
| 65 | + def mockDynamicProperty(name, val): |
| 66 | + result = mock.Mock() |
| 67 | + result.name = name |
| 68 | + result.val = val |
| 69 | + return result |
| 70 | + super(GetPropertiesTestCase, self).setUp() |
| 71 | + self._action = self.get_action_instance(self.new_config) |
| 72 | + self._action.establish_connection = mock.Mock() |
| 73 | + self._action.si_content = mock.Mock() |
| 74 | + self._action.si_content.rootFolder = mock.Mock() |
| 75 | + self._action.si_content.viewManager = mock.Mock() |
| 76 | + self._action.si_content.viewManager.CreateContainerView = mock.Mock() |
| 77 | + self._action.si_content.viewManager.CreateListView = mock.Mock() |
| 78 | + self._action.si_content.propertyCollector = mock.Mock() |
| 79 | + cmdspec22 = vim.vm.guest.ProcessManager.ProgramSpec( |
| 80 | + arguments="arguments", |
| 81 | + envVariables=["A=B", "C=D"], |
| 82 | + programPath="cmd.exe", |
| 83 | + workingDirectory='/tmp') |
| 84 | + cmdspec46 = copy.deepcopy(cmdspec22) |
| 85 | + cmdspec22.__dict__['dynamicType'] = 'Hello' |
| 86 | + # mock up the raw objects used to test transform |
| 87 | + vm22 = mock.Mock() |
| 88 | + vm22.obj = 'vim.VirtualMachine:vm-22' |
| 89 | + vm22.propSet = [mockDynamicProperty("config.hardware.memoryMB", 10240), |
| 90 | + mockDynamicProperty("dataobj-dyn", cmdspec22), |
| 91 | + mockDynamicProperty("guest.ipAddress", "10.99.0.4"), |
| 92 | + mockDynamicProperty("name", "VCSA"), |
| 93 | + mockDynamicProperty("network", [ |
| 94 | + pyVmomi.VmomiSupport.GetWsdlType( |
| 95 | + 'urn:vim25', 'Network')( |
| 96 | + 'network-15')]), |
| 97 | + mockDynamicProperty("time", |
| 98 | + datetime.datetime( |
| 99 | + 1992, 1, 12, 1, 1, 22))] |
| 100 | + vm46 = mock.Mock() |
| 101 | + vm46.obj = 'vim.VirtualMachine:vm-46' |
| 102 | + vm46.propSet = [mockDynamicProperty("config.hardware.memoryMB", 2048), |
| 103 | + mockDynamicProperty("dataobj-nodyn", cmdspec46), |
| 104 | + mockDynamicProperty("guest.ipAddress", |
| 105 | + "fe80::250:56ff:feb4:cfb9"), |
| 106 | + mockDynamicProperty("name", "testvm"), |
| 107 | + mockDynamicProperty("network", []), |
| 108 | + mockDynamicProperty("time", |
| 109 | + datetime.datetime( |
| 110 | + 1992, 1, 17, 1, 1, 46))] |
| 111 | + self.raw = [vm22, vm46] |
| 112 | + |
| 113 | + @mock.patch('pyVmomi.vmodl.query.PropertyCollector') |
| 114 | + def test_simple_property_by_id(self, pc): |
| 115 | + with mock.patch.object( |
| 116 | + self._action.si_content.propertyCollector, 'RetrieveContents', |
| 117 | + return_value=self.raw): |
| 118 | + result = self._action.run(type='VirtualMachine', |
| 119 | + id=['vm-22', 'vm-46'], |
| 120 | + property=['does.not.exist'], raw=False) |
| 121 | + assert self._action.si_content.viewManager.CreateListView.called |
| 122 | + # status True because every object was found |
| 123 | + self.assertEqual(result, (True, self.transformed)) |
| 124 | + with mock.patch.object( |
| 125 | + self._action.si_content.propertyCollector, 'RetrieveContents', |
| 126 | + return_value=self.raw): |
| 127 | + result = self._action.run(type='VirtualMachine', |
| 128 | + id=['vm-22', 'vm-46', 'vm-47'], |
| 129 | + property=None, raw=False) |
| 130 | + # status False because not every object was found |
| 131 | + self.assertEqual(result, (False, self.transformed)) |
| 132 | + |
| 133 | + @mock.patch('pyVmomi.vmodl.query.PropertyCollector') |
| 134 | + def test_simple_by_type(self, pc): |
| 135 | + with mock.patch.object( |
| 136 | + self._action.si_content.propertyCollector, 'RetrieveContents', |
| 137 | + return_value=self.raw): |
| 138 | + result = self._action.run(type='VirtualMachine', id=None, |
| 139 | + property=None, raw=False) |
| 140 | + assert\ |
| 141 | + self._action.si_content.viewManager.CreateContainerView.called |
| 142 | + self.assertEqual(result, (True, self.transformed)) |
| 143 | + |
| 144 | + @mock.patch('pyVmomi.vmodl.query.PropertyCollector') |
| 145 | + def test_raw_output(self, pc): |
| 146 | + with mock.patch.object( |
| 147 | + self._action.si_content.propertyCollector, 'RetrieveContents', |
| 148 | + return_value=self.raw): |
| 149 | + result = self._action.run(type='VirtualMachine', id=None, |
| 150 | + property=None, raw=True) |
| 151 | + self.assertNotEqual(result, self.transformed) |
0 commit comments