Skip to content
This repository has been archived by the owner on May 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #45 from JeroennC/master
Browse files Browse the repository at this point in the history
Timezone aware tests, set release start and end from steps start and end
  • Loading branch information
JeroennC authored Nov 5, 2019
2 parents 90e1290 + f1e6bda commit 28fb04f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
4 changes: 2 additions & 2 deletions katka/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
26 changes: 17 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down
24 changes: 12 additions & 12 deletions tests/integration/test_scmsteprun_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -141,29 +141,29 @@ 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
p = models.SCMStepRun.objects.get(pk=scm_step_run.public_identifier)
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()
Expand Down
3 changes: 3 additions & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_close_release.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from django.utils.dateparse import parse_datetime

import pytest
from katka import constants
from katka.models import SCMRelease
Expand All @@ -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)

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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")

0 comments on commit 28fb04f

Please sign in to comment.