diff --git a/katka/releases.py b/katka/releases.py index 1fd1ae8..b3f800a 100644 --- a/katka/releases.py +++ b/katka/releases.py @@ -80,13 +80,13 @@ def _gather_steps_pre_conditions(pipeline): continue _add_output(pipeline_output=pipeline_output, step_output=step.output) if constants.TAG_PRODUCTION_CHANGE_STARTED in step.tags.split(" "): - prod_start_date = step.modified + prod_start_date = step.started if prod_start_date is not None: success_status_between_start_end.append(step.status == constants.STEP_STATUS_SUCCESS) if constants.TAG_PRODUCTION_CHANGE_ENDED in step.tags.split(" "): - prod_end_date = step.modified + prod_end_date = step.ended break return StepsPreConditions(pipeline_output.get("release.version"), diff --git a/tests/conftest.py b/tests/conftest.py index 3d01c98..0abf35c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -349,7 +349,7 @@ def deactivated_scm_pipeline_run(scm_pipeline_run): def scm_step_run(scm_pipeline_run): scm_step_run = models.SCMStepRun(slug='release', name='Release Katka', stage='Production', scm_pipeline_run=scm_pipeline_run, sequence_id='1.1-1', - started='2018-11-11 08:25:30', ended='2018-11-11 09:01:40') + started='2018-11-11 08:25:30+0000', ended='2018-11-11 09:01:40+0000') with username_on_model(models.SCMStepRun, 'initial'): scm_step_run.save() @@ -450,13 +450,20 @@ def not_my_metadata(not_my_application): def step_data(scm_pipeline_run): version = '{"release.version": "1.0.0"}' steps = [ - {"name": "step0", "stage": "prepare", "seq": "1.1-1", "status": "success", "tags": "", "output": version}, - {"name": "step1", "stage": "prepare", "seq": "1.2-1", "status": "success", "tags": "", "output": ""}, - {"name": "step2", "stage": "deploy", "seq": "2.1-1", "status": "success", "tags": "", "output": ""}, - {"name": "step3", "stage": "deploy", "seq": "2.2-1", "status": "success", "tags": "", "output": ""}, - {"name": "step4", "stage": "deploy", "seq": "2.3-1", "status": "success", "tags": "", "output": ""}, - {"name": "step5", "stage": "deploy", "seq": "2.4-1", "status": "success", "tags": "", "output": ""}, - {"name": "step6", "stage": "deploy", "seq": "2.5-1", "status": "success", "tags": "", "output": ""}, + {"name": "step0", "stage": "prepare", "seq": "1.1-1", "status": "success", "tags": "", "output": version, + "started": "2018-11-11 08:25:30+0000", "ended": "2018-11-11 08:25:41+0000"}, + {"name": "step1", "stage": "prepare", "seq": "1.2-1", "status": "success", "tags": "", "output": "", + "started": "2018-11-11 08:35:30+0000", "ended": "2018-11-11 08:35:41+0000"}, + {"name": "step2", "stage": "deploy", "seq": "2.1-1", "status": "success", "tags": "", "output": "", + "started": "2018-11-11 08:45:30+0000", "ended": "2018-11-11 08:45:41+0000"}, + {"name": "step3", "stage": "deploy", "seq": "2.2-1", "status": "success", "tags": "", "output": "", + "started": "2018-11-11 08:55:30+0000", "ended": "2018-11-11 08:55:41+0000"}, + {"name": "step4", "stage": "deploy", "seq": "2.3-1", "status": "success", "tags": "", "output": "", + "started": "2018-11-11 09:05:30+0000", "ended": "2018-11-11 09:05:41+0000"}, + {"name": "step5", "stage": "deploy", "seq": "2.4-1", "status": "success", "tags": "", "output": "", + "started": "2018-11-11 09:15:30+0000", "ended": "2018-11-11 09:15:41+0000"}, + {"name": "step6", "stage": "deploy", "seq": "2.5-1", "status": "success", "tags": "", "output": "", + "started": "2018-11-11 09:25:30+0000", "ended": "2018-11-11 09:25:41+0000"}, ] return steps @@ -466,7 +473,8 @@ def _create_steps_from_dict(scm_pipeline_run, step_data): for step in step_data: scm_step_run = models.SCMStepRun(slug=step["name"], name=step["name"], stage=step["stage"], scm_pipeline_run=scm_pipeline_run, sequence_id=step["seq"], - status=step["status"], tags=step["tags"], output=step["output"]) + status=step["status"], tags=step["tags"], output=step["output"], + started=step["started"], ended=step["ended"]) with username_on_model(models.SCMStepRun, 'initial'): scm_step_run.save() diff --git a/tests/integration/test_scmsteprun_view.py b/tests/integration/test_scmsteprun_view.py index 61dd094..124b959 100644 --- a/tests/integration/test_scmsteprun_view.py +++ b/tests/integration/test_scmsteprun_view.py @@ -70,8 +70,8 @@ def test_list(self, client, logged_in_user, scm_pipeline_run, scm_step_run): assert parsed[0]['stage'] == 'Production' assert parsed[0]['status'] == 'not started' assert parsed[0]['output'] == '' - assert parsed[0]['started'] == '2018-11-11T08:25:30' - assert parsed[0]['ended'] == '2018-11-11T09:01:40' + assert parsed[0]['started'] == '2018-11-11T08:25:30Z' + assert parsed[0]['ended'] == '2018-11-11T09:01:40Z' assert UUID(parsed[0]['scm_pipeline_run']) == scm_pipeline_run.public_identifier UUID(parsed[0]['public_identifier']) # should not raise @@ -117,8 +117,8 @@ def test_get(self, client, logged_in_user, scm_pipeline_run, scm_step_run): assert parsed['output'] == '' assert parsed['sequence_id'] == '1.1-1' assert parsed['tags'] == '' - assert parsed['started'] == '2018-11-11T08:25:30' - assert parsed['ended'] == '2018-11-11T09:01:40' + assert parsed['started'] == '2018-11-11T08:25:30Z' + assert parsed['ended'] == '2018-11-11T09:01:40Z' assert UUID(parsed['scm_pipeline_run']) == scm_pipeline_run.public_identifier UUID(parsed['public_identifier']) # should not raise @@ -141,8 +141,8 @@ def test_update(self, client, logged_in_user, scm_pipeline_run, scm_step_run): 'output': 'Command completed', 'sequence_id': '01.02-03', 'tags': 'tag1 tag2', - 'started': '2019-01-25 01:02:03', - 'ended': '2019-02-13 02:03:04', + 'started': '2019-01-25 01:02:03+0100', + 'ended': '2019-02-13 02:03:04Z', 'scm_pipeline_run': scm_pipeline_run.public_identifier} response = client.put(url, data, content_type='application/json') assert response.status_code == 200 @@ -150,20 +150,20 @@ def test_update(self, client, logged_in_user, scm_pipeline_run, scm_step_run): assert p.name == 'Release product' assert p.sequence_id == '01.02-03' assert p.tags == 'tag1 tag2' - assert p.started == parse_datetime('2019-01-25T01:02:03') - assert p.ended == parse_datetime('2019-02-13T02:03:04') + assert p.started == parse_datetime('2019-01-25T01:02:03+0100') + assert p.ended == parse_datetime('2019-02-13T02:03:04Z') def test_partial_update(self, client, logged_in_user, scm_step_run): url = f'/scm-step-runs/{scm_step_run.public_identifier}/' data = {'output': 'Step executed.', - 'started': '2019-01-25 01:02:03', - 'ended': '2019-02-13 02:03:04'} + 'started': '2019-01-25 01:02:03+0200', + 'ended': '2019-02-13 02:03:04Z'} response = client.patch(url, data, content_type='application/json') assert response.status_code == 200 p = models.SCMStepRun.objects.get(pk=scm_step_run.public_identifier) assert p.output == 'Step executed.' - assert p.started == parse_datetime('2019-01-25T01:02:03') - assert p.ended == parse_datetime('2019-02-13T02:03:04') + assert p.started == parse_datetime('2019-01-25T02:02:03+0300') + assert p.ended == parse_datetime('2019-02-13T02:03:04Z') def test_create(self, client, logged_in_user, scm_pipeline_run, scm_step_run): initial_count = models.SCMStepRun.objects.count() diff --git a/tests/settings.py b/tests/settings.py index 84fa7e2..b2402ad 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -52,6 +52,9 @@ }, ) +TIME_ZONE = 'UTC' +USE_TZ = True + REQUESTS_CA_BUNDLE = '/etc/ssl/certs/ca-certificates.crt' PIPELINE_CHANGE_NOTIFICATION_SESSION = session.Session() diff --git a/tests/unit/test_close_release.py b/tests/unit/test_close_release.py index 0a5c62e..213b85f 100644 --- a/tests/unit/test_close_release.py +++ b/tests/unit/test_close_release.py @@ -1,3 +1,5 @@ +from django.utils.dateparse import parse_datetime + import pytest from katka import constants from katka.models import SCMRelease @@ -22,6 +24,20 @@ def _assert_release_has_status(status): release = SCMRelease.objects.first() assert release.status == status + @staticmethod + def _assert_release_has_start_and_end_date(started=None, ended=None): + assert SCMRelease.objects.count() == 1 + release = SCMRelease.objects.first() + if started: + assert release.started == parse_datetime(started) + else: + assert release.started is None + + if ended: + assert release.ended == parse_datetime(ended) + else: + assert release.ended is None + def test_do_nothing_if_pipeline_run_status_not_finished(self, scm_pipeline_run): self._assert_release_has_status(constants.RELEASE_STATUS_IN_PROGRESS) @@ -65,6 +81,7 @@ def test_all_steps_are_success_with_start_and_end_tag(self, scm_pipeline_run, assert result_status == constants.PIPELINE_STATUS_SUCCESS self._assert_release_success_with_name("1.0.0") + self._assert_release_has_start_and_end_date("2018-11-11 08:45:30+00:00", "2018-11-11 09:15:41+00:00") def test_one_failed_step_between_start_end_tags(self, scm_pipeline_run, scm_step_run_one_failed_step_list_with_start_end_tags): @@ -75,6 +92,7 @@ def test_one_failed_step_between_start_end_tags(self, scm_pipeline_run, assert result_status == constants.RELEASE_STATUS_FAILED self._assert_release_has_status(constants.RELEASE_STATUS_FAILED) + self._assert_release_has_start_and_end_date("2018-11-11 08:45:30+00:00", "2018-11-11 09:15:41+00:00") def test_one_failed_step_before_start_tag(self, scm_pipeline_run, scm_step_run_one_failed_step_before_start_tag): @@ -85,6 +103,7 @@ def test_one_failed_step_before_start_tag(self, scm_pipeline_run, assert result_status is None self._assert_release_has_status(constants.RELEASE_STATUS_IN_PROGRESS) + self._assert_release_has_start_and_end_date(None, None) def test_one_failed_step_after_end_tag(self, scm_pipeline_run, scm_step_run_one_failed_step_after_end_tag): @@ -94,6 +113,7 @@ def test_one_failed_step_after_end_tag(self, scm_pipeline_run, result_status = close_release_if_pipeline_finished(scm_pipeline_run) assert result_status == constants.RELEASE_STATUS_SUCCESS self._assert_release_success_with_name("1.0.0") + self._assert_release_has_start_and_end_date("2018-11-11 08:45:30+00:00", "2018-11-11 09:15:41+00:00") def test_fails_if_version_not_present_in_context(self, scm_pipeline_run, scm_step_run_without_version_output): @@ -104,6 +124,7 @@ def test_fails_if_version_not_present_in_context(self, scm_pipeline_run, assert result_status is None self._assert_release_has_status(constants.RELEASE_STATUS_IN_PROGRESS) + self._assert_release_has_start_and_end_date(None, None) def test_output_with_broken_json_does_not_break_release(self, scm_pipeline_run, scm_step_run_with_broken_output): @@ -114,3 +135,4 @@ def test_output_with_broken_json_does_not_break_release(self, scm_pipeline_run, assert result_status == constants.PIPELINE_STATUS_SUCCESS self._assert_release_success_with_name("1.0.0") + self._assert_release_has_start_and_end_date("2018-11-11 08:45:30+00:00", "2018-11-11 09:15:41+00:00")