def test_metrics_build(self): func_name = 'test' exec_date = datetime.utcnow() namespace = Namespace(dag_id='foo', task_id='bar', subcommand='test', execution_date=exec_date) metrics = cli._build_metrics(func_name, namespace) expected = { 'user': os.environ.get('USER'), 'sub_command': 'test', 'dag_id': 'foo', 'task_id': 'bar', 'execution_date': exec_date, } for k, v in expected.items(): self.assertEqual(v, metrics.get(k)) self.assertTrue(metrics.get('start_datetime') <= datetime.utcnow()) self.assertTrue(metrics.get('full_command')) log_dao = metrics.get('log') self.assertTrue(log_dao) self.assertEqual(log_dao.dag_id, metrics.get('dag_id')) self.assertEqual(log_dao.task_id, metrics.get('task_id')) self.assertEqual(log_dao.execution_date, metrics.get('execution_date')) self.assertEqual(log_dao.owner, metrics.get('user'))
def test_cli_create_user_supplied_password_is_masked(self, given_command, expected_masked_command): args = given_command.split() expected_command = expected_masked_command.split() exec_date = datetime.utcnow() namespace = Namespace(dag_id='foo', task_id='bar', subcommand='test', execution_date=exec_date) with mock.patch.object(sys, "argv", args): metrics = cli._build_metrics(args[1], namespace) assert metrics.get('start_datetime') <= datetime.utcnow() log = metrics.get('log') command = json.loads(log.extra).get('full_command') # type: str # Replace single quotes to double quotes to avoid json decode error command = json.loads(command.replace("'", '"')) assert command == expected_command
def test_metrics_build(self): func_name = 'test' exec_date = datetime.utcnow() ns = Namespace(dag_id='foo', task_id='bar', subcommand='test', execution_date=exec_date) metrics = cli._build_metrics(func_name, ns) expected = {'user': os.environ.get('USER'), 'sub_command': 'test', 'dag_id': 'foo', 'task_id': 'bar', 'execution_date': exec_date} for k, v in expected.items(): self.assertEquals(v, metrics.get(k)) self.assertTrue(metrics.get('start_datetime') <= datetime.utcnow()) self.assertTrue(metrics.get('full_command')) log_dao = metrics.get('log') self.assertTrue(log_dao) self.assertEquals(log_dao.dag_id, metrics.get('dag_id')) self.assertEquals(log_dao.task_id, metrics.get('task_id')) self.assertEquals(log_dao.execution_date, metrics.get('execution_date')) self.assertEquals(log_dao.owner, metrics.get('user'))