def test_get_propagation_results(self): rest = _RestProxyForTest() batches = Batches(rest) # Parts count not specified. No result retrieval is attempted. state = StateSummary({ 'uuid': 'aaa', 'calc_state': 'COMPLETED', }) self.assertIsNone(batches.get_propagation_results(state)) # Parts count is 0. No result retrieval is attempted. state = StateSummary({ 'uuid': 'aaa', 'calc_state': 'COMPLETED', 'parts_count': 0, }) self.assertIsNone(batches.get_propagation_results(state)) # Normal retrieval. state = StateSummary({ 'uuid': 'aaa', 'calc_state': 'COMPLETED', 'parts_count': 2, }) rest.expect_get('/batch/aaa/1', 200, {'part_index': 'a', 'calc_state': 'RUNNING'}) rest.expect_get('/batch/aaa/2', 200, {'part_index': 'z', 'calc_state': 'COMPLETED'}) results = batches.get_propagation_results(state) self.assertEqual(2, len(results.get_parts())) self.assertEqual('a', results.get_parts()[0].get_part_index()) self.assertEqual('z', results.get_parts()[1].get_part_index()) # Some parts could not be found. state = StateSummary({ 'uuid': 'aaa', 'calc_state': 'FAILED', 'parts_count': 3, }) rest.expect_get('/batch/aaa/1', 404, 'Not json') rest.expect_get('/batch/aaa/2', 200, {'part_index': 'z', 'calc_state': 'COMPLETED'}) rest.expect_get('/batch/aaa/3', 404, 'Not json') results = batches.get_propagation_results(state) self.assertEqual(3, len(results.get_parts())) self.assertIsNone(results.get_parts()[0]) self.assertEqual('z', results.get_parts()[1].get_part_index()) self.assertIsNone(results.get_parts()[2]) # Complete failure. state = StateSummary({ 'uuid': 'aaa', 'calc_state': 'COMPLETED', 'parts_count': 2, }) rest.expect_get('/batch/aaa/1', 403, 'irrelevant') with self.assertRaises(RuntimeError): batches.get_propagation_results(state)
def test_flow(self): batches = MockBatches() b1 = get_dummy_batch("p1") pending_state = StateSummary({'uuid': 'b1', 'calc_state': 'PENDING'}) running_state = StateSummary({'uuid': 'b1', 'calc_state': 'RUNNING'}) completed_state = StateSummary({ 'uuid': 'b1', 'calc_state': 'COMPLETED' }) failed_state = StateSummary({'uuid': 'b1', 'calc_state': 'FAILED'}) results = PropagationResults([None]) batches.expect_new_batch(b1, pending_state) batches.expect_get_summaries("p1", {"b1": completed_state}) batches.expect_get_results(completed_state, results) batch_runner = BatchRunManager(batches, [b1], "p1") batch_runner.run() self.assertEqual(b1.get_state_summary(), completed_state) self.assertEqual(b1.get_results(), results) batches.clear_expectations() batches.expect_new_batch(b1, pending_state) batches.expect_get_summaries("p1", {"b1": pending_state}) batches.expect_get_summaries("p1", {"b1": running_state}) batches.expect_get_summaries("p1", {"b1": running_state}) batches.expect_get_summaries("p1", {"b1": completed_state}) batches.expect_get_results(completed_state, results) batch_runner = BatchRunManager(batches, [b1]) batch_runner.run() self.assertEqual(b1.get_state_summary(), completed_state) self.assertEqual(b1.get_results(), results) batches.clear_expectations() batches.expect_new_batch(b1, pending_state) batches.expect_get_summaries("p1", {"b1": pending_state}) batches.expect_get_summaries("p1", {"b1": failed_state}) batches.expect_get_results(failed_state, results) batch_runner = BatchRunManager(batches, [b1]) batch_runner.run() self.assertEqual(b1.get_state_summary(), failed_state) self.assertEqual(b1.get_results(), results) batches.clear_expectations()
def new_batches(self, param_pairs): """ Expects a list of pairs of [propagation_params, opm_params]. Returns a list of batch summaries for the submitted batches in the same order. """ batch_dicts = [] for pair in param_pairs: batch_dicts.append( self._build_batch_creation_data(pair[0], pair[1])) code, response = self._rest.post('/batches', {'requests': batch_dicts}) # Check error code if code != 200: raise RuntimeError("Server status code: %s; Response: %s" % (code, response)) if len(param_pairs) != len(response['requests']): raise RuntimeError("Expected %s results, only got %s" % (len(param_pairs), len(response['requests']))) # Store response values summaries = [] for i in range(len(response['requests'])): summaries.append(StateSummary(response['requests'][i])) return summaries
def get_summary(self, uuid): code, response = self._rest.get('/batch/' + uuid) if code == 404: return None elif code != 200: raise RuntimeError("Server status code: %s; Response: %s" % (code, response)) return StateSummary(response)
def new_batch(self, propagation_params, opm_params): data = self._build_batch_creation_data(propagation_params, opm_params) code, response = self._rest.post('/batch', data) if code != 200: raise RuntimeError("Server status code: %s; Response: %s" % (code, response)) return StateSummary(response)
def test_get_methods(self): s = StateSummary({ 'uuid': 'a', 'calc_state': 'b', 'step_duration_sec': 1, 'create_time': 'c', 'execute_time': 'd', 'complete_time': 'e', 'project': 'f', 'parts_count': 2, }) self.assertEqual('a', s.get_uuid()) self.assertEqual('b', s.get_calc_state()) self.assertEqual(1, s.get_step_size()) self.assertEqual('c', s.get_create_time()) self.assertEqual('d', s.get_execute_time()) self.assertEqual('e', s.get_complete_time()) self.assertEqual('f', s.get_project_uuid()) self.assertEqual(2, s.get_parts_count())
def test_get_methods(self): propagation_params = {'a': 1} opm_params = {'b': 2} batch = Batch(propagation_params, opm_params) self.assertEqual(propagation_params, batch.get_propagation_params()) self.assertEqual(opm_params, batch.get_opm_params()) self.assertIsNone(batch.get_uuid()) self.assertIsNone(batch.get_calc_state()) self.assertIsNone(batch.get_state_summary()) self.assertIsNone(batch.get_results()) state_summary = StateSummary({'uuid': 'aaa', 'calc_state': 'RUNNING'}) batch.set_state_summary(state_summary) self.assertEqual(state_summary, batch.get_state_summary()) self.assertEqual('aaa', batch.get_uuid()) self.assertEqual('RUNNING', batch.get_calc_state()) results = {'c': 3} batch.set_results(results) self.assertEqual(results, batch.get_results())
def test_required_keys(self): with self.assertRaises(KeyError): StateSummary({'uuid': 'a'}) with self.assertRaises(KeyError): StateSummary({'calc_state': 'b'})
def get_summaries(self, project): summaries = {} for s in self._get_summaries(project): summaries[s['uuid']] = StateSummary(s) return summaries