def test_with(self, fake_connect): """vCenter - supports use of the ``with`` statement""" with vcenter.vCenter(host='localhost', user='******', password='******') as vc: pass self.assertTrue(fake_connect.Disconnect.called)
def _verify_read_only(self, the_property, fake_connect): """Reduce boiler plate in testing read-only properites""" fake_entity = MagicMock() vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) with self.assertRaises(AttributeError): setattr(vc, the_property, 'woot')
def test_get_vm_folder_raises(self, fake_connect, fake_get_by_name): """vCenter - ``get_vm_folder`` raises FileNotFoundError when the folder doesn't exist""" fake_entity = MagicMock() vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) with self.assertRaises(FileNotFoundError): vc.get_vm_folder('/foo/bar')
def test_ovf_manager_property(self, fake_connect): """vCenter - ``ovf_manager`` pulls the ovfManager property off the content""" fake_entity = MagicMock() vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) result = vc.ovf_manager self.assertEqual(vc._conn.content.ovfManager, result)
def test_content_property(self, fake_connect): """vCenter - ``content`` property returns RetrieveContent""" fake_entity = MagicMock() vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) vc.content self.assertTrue(vc._conn.RetrieveContent.called)
def test_create_vm_folder(self, fake_connect): """vCenter - ``create_vm_folder`` can create a root folder""" fake_entity = MagicMock() vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) result = vc.create_vm_folder('/foo') self.assertEqual(result, None)
def test_get_by_name(self, fake_connect): """vCenter - get_by_name when param ``parent`` is None""" fake_obj = FakeObj(name='foo') fake_entity = MagicMock() fake_entity.view = [fake_obj] vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) result = vc.get_by_name(vimtype=vcenter.vim.Network, name='foo') self.assertEqual(result, fake_obj)
def test_get_by_type(self, fake_connect): """vCenter - get_by_type works when given an iterable object""" fake_entity = MagicMock() fake_entity.view = 'woot' vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) result = vc.get_by_type([vcenter.vim.Network]) expected = 'woot' self.assertEqual(result, expected)
def test_create_vm_folder_runtime_error(self, fake_connect, fake_get_by_name): """vCenter - ``create_vm_folder`` raises RuntimeError if no datacenter found""" fake_entity = MagicMock() fake_entity.name = 'someOtherDC' vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) vc.content.rootFolder.childEntity = [fake_entity] with self.assertRaises(RuntimeError): vc.create_vm_folder('/foo/bar', datacenter='someDC')
def test_create_vm_folder_datacenter(self, fake_connect, fake_get_by_name): """vCenter - ``create_vm_folder`` accepts the datacenter as a param""" fake_entity = MagicMock() fake_entity.name = 'someDC' vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) vc.content.rootFolder.childEntity = [fake_entity] result = vc.create_vm_folder('/foo/bar', datacenter='someDC') self.assertEqual(result, None)
def _verify_mapped_objects(self, the_property, fake_connect): """Reduce boiler plate in testing properties that return a dict of objects""" fake_entity = MagicMock() obj = FakeObj(name='bar') fake_entity.view = [obj] vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) result = getattr(vc, the_property) expected = {'bar': obj} self.assertEqual(result, expected)
def test_get_by_name_value_error(self, fake_connect): """vCenter - get_by_name raises ValueError when the object is not found""" fake_obj = FakeObj(name='foo') fake_entity = MagicMock() fake_entity.view = [fake_obj] vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) self.assertRaises(ValueError, vc.get_by_name, vimtype=vcenter.vim.Network, name='bar')
def test_resource_pools_property(self, fake_connect): """vCenter - ``resource_pools`` returns a dictionary""" fake_entity = MagicMock() obj = FakeObj(name='bar') obj.resourcePool = 'baz' fake_entity.view = [obj] vc = vcenter.vCenter(host='localhost', user='******', password='******') vc._conn = self._fake_conn_factory(fake_entity) result = vc.resource_pools expected = {'bar': 'baz'} self.assertEqual(result, expected)