def test_init(self): requests = Mock() requests.request = Mock(return_value=self.response_login) with patch("cloudify_vsphere.contentlibrary.requests", requests): # correct session id cl = contentlibrary.ContentLibrary({ 'host': 'host', 'username': '******', 'password': '******', 'allow_insecure': True }) cl.__del__() # wrong session id response = Mock() response.json = Mock(return_value={"value": 'other_id'}) response.cookies = {'vmware-api-session-id': 'session_id'} requests.request = Mock(return_value=response) with self.assertRaises(NonRecoverableError): contentlibrary.ContentLibrary({ 'host': 'host', 'username': '******', 'password': '******', 'allow_insecure': True }) # no response response = Mock() response.json = Mock(return_value={}) response.cookies = {} requests.request = Mock(return_value=response) with self.assertRaises(NonRecoverableError): contentlibrary.ContentLibrary({ 'host': 'host', 'username': '******', 'password': '******', 'allow_insecure': True })
def test_content_item_deploy(self): response_deployment = Mock() response_deployment.cookies = {} response_deployment.json = Mock(return_value={ "value": { 'name': 'def', 'succeeded': True, 'id': 'id' } }) response_deployment_state = Mock() response_deployment_state.cookies = {} response_deployment_state.json = Mock( return_value={"value": { 'name': 'def', 'id': 'id' }}) _responses = [ self.response_logout, response_deployment, response_deployment_state, self.response_login ] def _fake_response(*argc, **kwargs): return _responses.pop() requests = Mock() requests.request = _fake_response # check correct deployment with patch("cloudify_vsphere.contentlibrary.requests", requests): cl = contentlibrary.ContentLibrary({ 'host': 'host', 'username': '******', 'password': '******', 'allow_insecure': True }) self.assertEqual( cl.content_item_deploy("abc", {'target': '_target'}, {'param': '_param'}), { 'name': 'def', 'succeeded': True, 'id': 'id' }) cl.__del__()
def test_content_library_get_fail(self): _responses = [ self.response_logout, self.response_empty, self.response_login ] def _fake_response(*argc, **kwargs): return _responses.pop() requests = Mock() requests.request = _fake_response # check empty content libraries list with patch("cloudify_vsphere.contentlibrary.requests", requests): cl = contentlibrary.ContentLibrary({ 'host': 'host', 'username': '******', 'password': '******', 'allow_insecure': True }) with self.assertRaises(NonRecoverableError): cl.content_library_get("abc") cl.__del__()
def test_cleanup_parmeters(self): _responses = [self.response_logout, self.response_login] def _fake_response(*argc, **kwargs): return _responses.pop() requests = Mock() requests.request = _fake_response # check correct deployment with patch("cloudify_vsphere.contentlibrary.requests", requests): cl = contentlibrary.ContentLibrary({ 'host': 'host', 'username': '******', 'password': '******', 'allow_insecure': True }) self.assertEqual( cl._cleanup_specs({ "additional_parameters": [{ "type": "DeploymentOptionParams", "@class": "com.vmware.vcenter.ovf.deployment_option_params" }] }), { 'additional_parameters': [ collections.OrderedDict([ ( # class should be always first in list '@class', 'com.vmware.vcenter.ovf.deployment_option_params' ), ('type', 'DeploymentOptionParams') ]) ] }) cl.__del__()
def test_content_item_get(self): response_list = Mock() response_list.cookies = {} response_list.json = Mock(return_value={"value": ['abc']}) response_item = Mock() response_item.cookies = {} response_item.json = Mock( return_value={"value": { 'name': 'def', 'id': 'id' }}) _responses = [ self.response_logout, response_item, response_list, self.response_login ] def _fake_response(*argc, **kwargs): return _responses.pop() requests = Mock() requests.request = _fake_response # check content libraries list with patch("cloudify_vsphere.contentlibrary.requests", requests): cl = contentlibrary.ContentLibrary({ 'host': 'host', 'username': '******', 'password': '******', 'allow_insecure': True }) self.assertEqual(cl.content_item_get("abc", "def"), { 'name': 'def', 'id': 'id' }) cl.__del__()