def test_environment(self): operation_id = 999 color = 9 color_descriptor = mock.Mock() color_descriptor.color = color parent_log_dir = '/happy/log/dir' operation_data = OperationData(net_key=self.net.key, operation_id=operation_id, color=color) environment = { 'foo': 'bar', 'baz': 'buz', } expected_environment = { 'FLOW_WORKFLOW_OPERATION_DATA': operation_data.dumps(), 'FLOW_PARENT_WORKFLOW_LOG_DIR': parent_log_dir, } expected_environment.update(environment) self.net.constant.return_value = environment with mock.patch('flow_workflow.perl_action.actions.factory') as factory: operation = mock.Mock() operation.log_dir = parent_log_dir factory.load_operation.return_value = operation result_env = self.action.environment(self.net, color_descriptor) factory.load_operation.assert_called_once_with(self.net, self.args['operation_id']) self.net.constant.assert_called_once_with('environment', {}) self.assertEqual(expected_environment, result_env)
def environment(self, net, color_descriptor): env = net.constant('environment', {}) operation = self._get_operation(net) operation_data = OperationData(net_key=net.key, operation_id=self.args['operation_id'], color=color_descriptor.color) env['FLOW_WORKFLOW_OPERATION_DATA'] = operation_data.dumps() env['FLOW_PARENT_WORKFLOW_LOG_DIR'] = operation.log_dir return env
class OperationDataTests(TestCase): def setUp(self): self.net_key = 'some_net_key' self.operation_id = 999 self.color = 888 self.operation_data = OperationData(net_key=self.net_key, operation_id=self.operation_id, color=self.color) self.string = '{"color": 888, "net_key": "some_net_key", "operation_id": 999}' def test_to_dict(self): expected_dict = { 'net_key':self.net_key, 'operation_id':self.operation_id, 'color':self.color, } self.assertItemsEqual(expected_dict, self.operation_data.to_dict) def test_from_dict(self): some_dict = { 'net_key':'some_net_key', 'operation_id':999, 'color':888, } operation_data = OperationData.from_dict(some_dict) self.assertEqual(self.operation_data, operation_data) def test_dumps(self): self.assertEqual(self.string, self.operation_data.dumps()) def test_loads(self): operation_data = OperationData.loads(self.string) self.assertEqual(self.operation_data, operation_data)
def setUp(self): self.net_key = 'some_net_key' self.operation_id = 999 self.color = 888 self.operation_data = OperationData(net_key=self.net_key, operation_id=self.operation_id, color=self.color) self.string = '{"color": 888, "net_key": "some_net_key", "operation_id": 999}'
def test_from_dict(self): some_dict = { 'net_key':'some_net_key', 'operation_id':999, 'color':888, } operation_data = OperationData.from_dict(some_dict) self.assertEqual(self.operation_data, operation_data)
def _get_message_dict(self, message): message_dict = message.to_dict() message_dict['status'] = Status(message_dict['status']) OPERATION_DATA_FIELDS = ['operation_data', 'parent_operation_data', 'peer_operation_data'] for field in OPERATION_DATA_FIELDS: if field in message_dict: message_dict[field] = OperationData.from_dict( message_dict[field]) return message_dict
def test_loads(self): operation_data = OperationData.loads(self.string) self.assertEqual(self.operation_data, operation_data)
def operation_data(self): string = os.environ.get('FLOW_WORKFLOW_OPERATION_DATA') if string: return OperationData.loads(string) else: return False