def test_constructor(self, mock_g, mock_n, mock_k): # Fail if incomplete auth supplied with self.assertRaises(ValueError): c = ProcClient({'username': '******'}) # Normal creation c = ProcClient(test_auth) self.assertIsNone(c.certificate_file_location) self.assertIsNotNone(c.basic_auth) self.assertTrue(c.is_authorized) self.assertEqual(c.protocol, 'http')
def test_server_response(self, mock_g, mock_n, mock_k, mock_session_post): # Mock OpenStack clients and response to POST mock_image = {"name": test_image_name, "id": test_image_id} mock_gclient = Mock() mock_gclient.images = Mock() mock_im_list = Mock(return_value=[mock_image]) mock_gclient.images.list = mock_im_list mock_gclient.image_members = Mock() mock_gclient.image_members.create = Mock() mock_g.return_value = mock_gclient mock_flavor = Mock() mock_flavor.name = test_flavor_name mock_flavor.id = test_flavor_id mock_fl_list = Mock(return_value=[mock_flavor]) mock_nclient = Mock() mock_nclient.flavors = Mock() mock_nclient.flavors.list = mock_fl_list mock_n.return_value = mock_nclient mock_response = Mock() mock_response.text = test_job_id mock_response.status_code = 200 mock_session_post.return_value = mock_response c = ProcClient(test_auth, host=test_host) # Failure on server side when trying to validate job mock_response_validate = Mock() mock_response_validate.text = "Failed to validate job." mock_response_validate.status_code = 400 mock_response_validate.raise_for_status = Mock(side_effect=Exception("Failed.")) mock_session_post.return_value = mock_response_validate with self.assertRaises(Exception): c.submit_job(test_job_works, test_image_name, test_flavor_name) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_session_post.return_value = mock_response mock_session_post.reset_mock() # glance client needs to be refreshed. First time it tries to obtain # an image list it fails. Second time it works. mock_gclient.images.list.side_effect = [Exception("Failed"), mock_im_list()] mock_gclient.images.list.reset_mock() mock_g.reset_mock() c.submit_job(test_job_works, test_image_name, test_flavor_name) self.assertEqual(mock_gclient.images.list.call_count, 2) # only one additional call to glance client constructor since it # was originally created with the ProcClient instance self.assertEqual(mock_g.call_count, 1) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_gclient.images.list.side_effect = None mock_session_post.reset_mock() # nova client needs to be refreshed. First time it tries to obtain # a flavor list it fails. Second time it works. mock_nclient.flavors.list.side_effect = [Exception("Failed"), mock_fl_list()] mock_nclient.flavors.list.reset_mock() mock_n.reset_mock() c.submit_job(test_job_works, test_image_name, test_flavor_name) self.assertEqual(mock_nclient.flavors.list.call_count, 2) self.assertEqual(mock_n.call_count, 1) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_nclient.flavors.list.side_effect = None mock_session_post.reset_mock() # glance client needs to be refreshed. First time it tries to # share an image it fails. Second time it works. mock_gclient.image_members.create.side_effect = [Exception("Failed"), Mock()] mock_gclient.image_members.create.reset_mock() mock_g.reset_mock() c.submit_job(test_job_works, test_image_name, test_flavor_name) self.assertEqual(mock_gclient.image_members.create.call_count, 2) self.assertEqual(mock_g.call_count, 1) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works)
def test_submit_job(self, mock_g, mock_n, mock_k, mock_session_post): # Mock OpenStack clients and response to POST mock_image = {"name": test_image_name, "id": test_image_id} mock_gclient = Mock() mock_gclient.images = Mock() mock_gclient.images.list = Mock(return_value=[mock_image]) mock_g.return_value = mock_gclient mock_flavor = Mock() mock_flavor.name = test_flavor_name mock_flavor.id = test_flavor_id mock_nclient = Mock() mock_nclient.flavors = Mock() mock_nclient.flavors.list = Mock(return_value=[mock_flavor]) mock_n.return_value = mock_nclient mock_response = Mock() mock_response.text = test_job_id mock_response.status_code = 200 mock_session_post.return_value = mock_response c = ProcClient(test_auth, host=test_host) # good submission using image and flavor names c.submit_job(test_job_works, test_image_name, test_flavor_name) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_session_post.reset_mock() # good submission using image and flavor UUIDs c.submit_job(test_job_works, test_image_id, test_flavor_id) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_session_post.reset_mock() # With nopost we should not do a POST c.submit_job(test_job_works, test_image_name, test_flavor_name, nopost=True) self.assertEqual(mock_session_post.call_count, 0) # failed submission due to bad image name with self.assertRaises(ValueError): c.submit_job(test_job_works, "bad_image", test_flavor_name) self.assertEqual(mock_session_post.call_count, 0) # failed submission due to bad flavor name with self.assertRaises(ValueError): c.submit_job(test_job_works, test_image_name, "yucky") self.assertEqual(mock_session_post.call_count, 0) # failed submission because no Executable line in job file with self.assertRaises(ValueError): c.submit_job(test_job_fails, test_image_name, test_flavor_name) self.assertEqual(mock_session_post.call_count, 0) # works without Executable by specifying jobscriptonvm c.submit_job(test_job_fails, test_image_name, test_flavor_name, jobscriptonvm=test_script_on_vm) mock_session_post.assert_called_once_with(test_job_url, params=test_params_on_vm, files=test_files_on_vm) mock_session_post.reset_mock()
def test_server_response(self, mock_g, mock_n, mock_k, mock_session_post): # Mock OpenStack clients and response to POST mock_image = {'name': test_image_name, 'id': test_image_id} mock_gclient = Mock() mock_gclient.images = Mock() mock_im_list = Mock(return_value=[mock_image]) mock_gclient.images.list = mock_im_list mock_gclient.image_members = Mock() mock_gclient.image_members.create = Mock() mock_g.return_value = mock_gclient mock_flavor = Mock() mock_flavor.name = test_flavor_name mock_flavor.id = test_flavor_id mock_fl_list = Mock(return_value=[mock_flavor]) mock_nclient = Mock() mock_nclient.flavors = Mock() mock_nclient.flavors.list = mock_fl_list mock_n.return_value = mock_nclient mock_response = Mock() mock_response.text = test_job_id mock_response.status_code = 200 mock_session_post.return_value = mock_response c = ProcClient(test_auth, host=test_host) # Failure on server side when trying to validate job mock_response_validate = Mock() mock_response_validate.text = 'Failed to validate job.' mock_response_validate.status_code = 400 mock_response_validate.raise_for_status = Mock( side_effect=Exception('Failed.')) mock_session_post.return_value = mock_response_validate with self.assertRaises(Exception): c.submit_job(test_job_works, test_image_name, test_flavor_name) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_session_post.return_value = mock_response mock_session_post.reset_mock() # glance client needs to be refreshed. First time it tries to obtain # an image list it fails. Second time it works. mock_gclient.images.list.side_effect = [ Exception('Failed'), mock_im_list() ] mock_gclient.images.list.reset_mock() mock_g.reset_mock() c.submit_job(test_job_works, test_image_name, test_flavor_name) self.assertEqual(mock_gclient.images.list.call_count, 2) # only one additional call to glance client constructor since it # was originally created with the ProcClient instance self.assertEqual(mock_g.call_count, 1) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_gclient.images.list.side_effect = None mock_session_post.reset_mock() # nova client needs to be refreshed. First time it tries to obtain # a flavor list it fails. Second time it works. mock_nclient.flavors.list.side_effect = [ Exception('Failed'), mock_fl_list() ] mock_nclient.flavors.list.reset_mock() mock_n.reset_mock() c.submit_job(test_job_works, test_image_name, test_flavor_name) self.assertEqual(mock_nclient.flavors.list.call_count, 2) self.assertEqual(mock_n.call_count, 1) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_nclient.flavors.list.side_effect = None mock_session_post.reset_mock() # glance client needs to be refreshed. First time it tries to # share an image it fails. Second time it works. mock_gclient.image_members.create.side_effect = [ Exception('Failed'), Mock() ] mock_gclient.image_members.create.reset_mock() mock_g.reset_mock() c.submit_job(test_job_works, test_image_name, test_flavor_name) self.assertEqual(mock_gclient.image_members.create.call_count, 2) self.assertEqual(mock_g.call_count, 1) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works)
def test_submit_job(self, mock_g, mock_n, mock_k, mock_session_post): # Mock OpenStack clients and response to POST mock_image = {'name': test_image_name, 'id': test_image_id} mock_gclient = Mock() mock_gclient.images = Mock() mock_gclient.images.list = Mock(return_value=[mock_image]) mock_g.return_value = mock_gclient mock_flavor = Mock() mock_flavor.name = test_flavor_name mock_flavor.id = test_flavor_id mock_nclient = Mock() mock_nclient.flavors = Mock() mock_nclient.flavors.list = Mock(return_value=[mock_flavor]) mock_n.return_value = mock_nclient mock_response = Mock() mock_response.text = test_job_id mock_response.status_code = 200 mock_session_post.return_value = mock_response c = ProcClient(test_auth, host=test_host) # good submission using image and flavor names c.submit_job(test_job_works, test_image_name, test_flavor_name) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_session_post.reset_mock() # good submission using image and flavor UUIDs c.submit_job(test_job_works, test_image_id, test_flavor_id) mock_session_post.assert_called_once_with(test_job_url, params=test_params_works, files=test_files_works) mock_session_post.reset_mock() # With nopost we should not do a POST c.submit_job(test_job_works, test_image_name, test_flavor_name, nopost=True) self.assertEqual(mock_session_post.call_count, 0) # failed submission due to bad image name with self.assertRaises(ValueError): c.submit_job(test_job_works, 'bad_image', test_flavor_name) self.assertEqual(mock_session_post.call_count, 0) # failed submission due to bad flavor name with self.assertRaises(ValueError): c.submit_job(test_job_works, test_image_name, 'yucky') self.assertEqual(mock_session_post.call_count, 0) # failed submission because no Executable line in job file with self.assertRaises(ValueError): c.submit_job(test_job_fails, test_image_name, test_flavor_name) self.assertEqual(mock_session_post.call_count, 0) # works without Executable by specifying jobscriptonvm c.submit_job(test_job_fails, test_image_name, test_flavor_name, jobscriptonvm=test_script_on_vm) mock_session_post.assert_called_once_with(test_job_url, params=test_params_on_vm, files=test_files_on_vm) mock_session_post.reset_mock()