def test_exception_unassign_member(self): set_module_args(args={ 'id': 'user-id', 'type': 'User', 'parent_id': 'group-id', 'parent_type': 'Group', 'state': 'absent' }) def users_get(self, filter=None, order_by=None, group_by=None, page=None, page_size=None, query_parameters=None, commit=True, callback=None, **kwargs): group_by = [] if group_by is None else group_by return [vsdk.NUUser(id='user-id'), vsdk.NUUser(id='user-id-2')] def group_assign(self, objects, nurest_object_type, callback=None, commit=True, **kwargs): raise BambouHTTPError(MockNuageConnection(status_code='500', reason='Server exception', errors={'description': 'Unable to remove member'})) with self.assertRaises(AnsibleFailJson) as exc: with patch('vspk.v5_0.fetchers.NUUsersFetcher.get', users_get): with patch('vspk.v5_0.NUGroup.assign', new=group_assign): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "Unable to remove entity as a member: [HTTP 500(Server exception)] {'description': 'Unable to remove member'}")
def test_state_present_children_update(self): set_module_args(args={ 'type': 'Enterprise', 'state': 'present', 'properties': { 'name': 'test-enterprise' }, 'children': [ { 'id': 'user-id', 'type': 'User', 'match_filter': 'userName == "johndoe"', 'properties': { 'user_name': 'johndoe-changed' } } ] }) with self.assertRaises(AnsibleExitJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) self.assertEqual(len(result['entities']), 2)
def test_state_present_children_create(self): set_module_args(args={ 'type': 'Enterprise', 'state': 'present', 'properties': { 'name': 'test-enterprise-create' }, 'children': [ { 'type': 'User', 'properties': { 'user_name': 'johndoe-new' } } ] }) def users_get(self, filter=None, order_by=None, group_by=None, page=None, page_size=None, query_parameters=None, commit=True, callback=None, **kwargs): group_by = [] if group_by is None else group_by return [] with self.assertRaises(AnsibleExitJson) as exc: with patch('vspk.v5_0.fetchers.NUUsersFetcher.get', users_get): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['changed']) self.assertEqual(len(result['entities']), 2)
def test_fail_children_parent(self): set_module_args(args={ 'id': 'group-id', 'type': 'Group', 'state': 'present', 'children': [ { 'type': 'User', 'properties': { 'name': 'test-user' } } ] }) def users_get_first(self, filter=None, order_by=None, group_by=None, page=None, page_size=None, query_parameters=None, commit=True, callback=None, **kwargs): group_by = [] if group_by is None else group_by return None with self.assertRaises(AnsibleFailJson) as exc: with patch('vspk.v5_0.fetchers.NUUsersFetcher.get_first', users_get_first): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], 'Trying to assign a child that does not exist')
def test_certificate_auth(self): set_module_args( args={ 'type': 'Enterprise', 'state': 'present', 'properties': { 'name': 'test-enterprise' }, 'auth': { 'api_username': '******', 'api_certificate': '/dummy/location/certificate.pem', 'api_key': '/dummy/location/key.pem', 'api_enterprise': 'csp', 'api_url': 'https://localhost:8443', 'api_version': 'v5_0' } } ) with self.assertRaises(AnsibleExitJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertFalse(result['changed']) self.assertEqual(len(result['entities']), 1) self.assertEqual(result['id'], 'enterprise-id') self.assertEqual(result['entities'][0]['name'], 'test-enterprise')
def test_input_require_on_off(self): set_module_args(args={ 'type': 'Enterprise' }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "one of the following is required: command,state")
def test_input_require_if_absent(self): set_module_args(args={ 'type': 'Enterprise', 'state': 'absent', }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "state is absent but the following are missing: id,properties,match_filter")
def test_fail_type(self): set_module_args(args={ 'type': 'Unknown', 'command': 'find' }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], 'Unrecognised type specified')
def test_fail_no_parent(self): set_module_args(args={ 'type': 'Group', 'command': 'find' }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], 'No parent specified and root object is not a parent for the type')
def test_input_require_if_wait_for_job_id(self): set_module_args(args={ 'type': 'Job', 'command': 'wait_for_job' }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "command is wait_for_job but the following are missing: id")
def test_input_exclusive(self): set_module_args(args={ 'type': 'Enterprise', 'state': 'present', 'command': 'find' }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "parameters are mutually exclusive: ['command', 'state']")
def test_input_require_both_parent_type(self): set_module_args(args={ 'type': 'User', 'command': 'find', 'parent_id': 'enterprise-id' }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "parameters are required together: ['parent_id', 'parent_type']")
def test_input_require_if_change_password_properties(self): set_module_args(args={ 'type': 'User', 'command': 'change_password', 'id': 'user-id' }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "command is change_password but the following are missing: properties")
def test_state_absent(self): set_module_args(args={ 'type': 'Enterprise', 'state': 'absent', 'properties': { 'name': 'test-enterprise' } }) with self.assertRaises(AnsibleExitJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['changed'])
def test_fail_parent_child(self): set_module_args(args={ 'type': 'Enterprise', 'parent_id': 'user-id', 'parent_type': 'User', 'command': 'find' }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], 'Specified parent is not a valid parent for the specified type')
def test_command_get_csp_enterprise(self): set_module_args(args={ 'type': 'Enterprise', 'command': 'get_csp_enterprise' }) with self.assertRaises(AnsibleExitJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertFalse(result['changed']) self.assertEqual(len(result['entities']), 1) self.assertEqual(result['id'], 'enterprise-id') self.assertEqual(result['entities'][0]['name'], 'test-enterprise')
def test_state_present_existing_filter(self): set_module_args(args={ 'type': 'Enterprise', 'state': 'present', 'match_filter': 'name == "test-enterprise"' }) with self.assertRaises(AnsibleExitJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertFalse(result['changed']) self.assertEqual(len(result['entities']), 1) self.assertEqual(result['id'], 'enterprise-id') self.assertEqual(result['entities'][0]['name'], 'test-enterprise')
def test_command_wait_for_job(self): set_module_args(args={ 'id': 'job-id', 'type': 'Job', 'command': 'wait_for_job', }) with self.assertRaises(AnsibleExitJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) self.assertEqual(result['id'], 'job-id') self.assertEqual(result['entities'][0]['command'], 'EXPORT') self.assertEqual(result['entities'][0]['status'], 'SUCCESS')
def test_command_find_by_filter(self): set_module_args(args={ 'type': 'Enterprise', 'command': 'find', 'match_filter': 'name == "test%"' }) with self.assertRaises(AnsibleExitJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertFalse(result['changed']) self.assertEqual(len(result['entities']), 2) self.assertEqual(result['entities'][0]['name'], 'test-enterprise') self.assertEqual(result['entities'][1]['name'], 'test-enterprise-2')
def test_fail_command_find(self): set_module_args(args={ 'type': 'Enterprise', 'command': 'find', 'properties': { 'id': 'unknown-enterprise-id', 'name': 'unkown-enterprise' } }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], 'Unable to find matching entries')
def test_fail_change_password_non_user(self): set_module_args(args={ 'id': 'group-id', 'type': 'Group', 'command': 'change_password', 'properties': { 'password': '******' } }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], 'Entity does not have a password property')
def test_exception_get_csp_enterprise(self): set_module_args(args={ 'type': 'Enterprise', 'command': 'get_csp_enterprise' }) def enterprise_failed_fetch(self, callback=None, **kwargs): raise BambouHTTPError(MockNuageConnection(status_code='404', reason='Not Found', errors={'description': 'Entity not found'})) with self.assertRaises(AnsibleFailJson) as exc: with patch('vspk.v5_0.NUEnterprise.fetch', enterprise_failed_fetch): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "Unable to fetch CSP enterprise: [HTTP 404(Not Found)] {'description': 'Entity not found'}")
def test_state_present_create(self): set_module_args(args={ 'type': 'Enterprise', 'state': 'present', 'properties': { 'name': 'test-enterprise-create' } }) with self.assertRaises(AnsibleExitJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) self.assertEqual(len(result['entities']), 1) self.assertEqual(result['id'], 'enterprise-id-create') self.assertEqual(result['entities'][0]['name'], 'test-enterprise-create')
def test_exception_session(self): set_module_args(args={ 'id': 'enterprise-id', 'type': 'Enterprise', 'command': 'find' }) def failed_session_start(self): raise BambouHTTPError(MockNuageConnection(status_code='401', reason='Unauthorized', errors={})) with self.assertRaises(AnsibleFailJson) as exc: with patch('vspk.v5_0.NUVSDSession.start', new=failed_session_start): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], 'Unable to connect to the API URL with given username, password and enterprise: [HTTP 401(Unauthorized)] {}')
def test_exception_delete_entity(self): set_module_args(args={ 'id': 'enterprise-id', 'type': 'Enterprise', 'state': 'absent' }) def enterprise_delete(self, response_choice=1, callback=None, **kwargs): raise BambouHTTPError(MockNuageConnection(status_code='500', reason='Server exception', errors={'description': 'Unable to delete entity'})) with self.assertRaises(AnsibleFailJson) as exc: with patch('vspk.v5_0.NUEnterprise.delete', enterprise_delete): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "Unable to delete entity: [HTTP 500(Server exception)] {'description': 'Unable to delete entity'}")
def test_exception_find_entity_id(self): set_module_args(args={ 'id': 'enterprise-id', 'type': 'Enterprise', 'state': 'present' }) def enterprise_failed_fetch(self, callback=None, **kwargs): raise BambouHTTPError(MockNuageConnection(status_code='404', reason='Not Found', errors={'description': 'Entity not found'})) with self.assertRaises(AnsibleFailJson) as exc: with patch('vspk.v5_0.NUEnterprise.fetch', enterprise_failed_fetch): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "Failed to fetch the specified entity by ID: [HTTP 404(Not Found)] {'description': 'Entity not found'}")
def test_fail_has_changed(self): set_module_args(args={ 'id': 'user-id', 'type': 'User', 'state': 'present', 'properties': { 'user_name': 'changed-user', 'fake': 'invalid-property', 'password': '******' } }) with self.assertRaises(AnsibleFailJson) as exc: nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], 'Property fake is not valid for this type of entity')
def test_exception_find_parent(self): set_module_args(args={ 'type': 'User', 'parent_id': 'group-id', 'parent_type': 'Group', 'command': 'find' }) def group_failed_fetch(self, callback=None, **kwargs): raise BambouHTTPError(MockNuageConnection(status_code='404', reason='Not Found', errors={'description': 'Entity not found'})) with self.assertRaises(AnsibleFailJson) as exc: with patch('vspk.v5_0.NUGroup.fetch', group_failed_fetch): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "Failed to fetch the specified parent: [HTTP 404(Not Found)] {'description': 'Entity not found'}")
def test_exception_create_entity(self): set_module_args(args={ 'type': 'Enterprise', 'state': 'present', 'properties': { 'name': 'test-enterprise-create' } }) def me_create_child(self, nurest_object, response_choice=None, callback=None, commit=True, **kwargs): raise BambouHTTPError(MockNuageConnection(status_code='500', reason='Server exception', errors={'description': 'Unable to create entity'})) with self.assertRaises(AnsibleFailJson) as exc: with patch('vspk.v5_0.NUMe.create_child', me_create_child): nuage_vspk.main() result = exc.exception.args[0] self.assertTrue(result['failed']) self.assertEqual(result['msg'], "Unable to create entity: [HTTP 500(Server exception)] {'description': 'Unable to create entity'}")
def test_exception_find_entity_property(self): set_module_args(args={ 'type': 'Enterprise', 'match_filter': 'name == "enterprise-id"', 'state': 'absent' }) def enterprises_failed_get_first(self, filter=None, order_by=None, group_by=None, page=None, page_size=None, query_parameters=None, commit=True, callback=None, **kwargs): group_by = [] if group_by is None else group_by raise BambouHTTPError(MockNuageConnection(status_code='404', reason='Not Found', errors={'description': 'Entity not found'})) with self.assertRaises(AnsibleExitJson) as exc: with patch('vspk.v5_0.fetchers.NUEnterprisesFetcher.get_first', enterprises_failed_get_first): nuage_vspk.main() result = exc.exception.args[0] self.assertFalse(result['changed'])