def test_pick_newest_on_duplicate_open_releases(self, application, my_scm_release, my_scm_pipeline_run, caplog): """This should not happen, but if it happens, handle it gracefully""" session = mock.MagicMock() overrides = {"PIPELINE_RUNNER_SESSION": session} with override_settings(**overrides), username_on_model( SCMRelease, "signal_tester"): open_release_2 = SCMRelease.objects.create() open_release_2.scm_pipeline_runs.add(my_scm_pipeline_run) open_release_2.save() assert my_scm_release.created_at < open_release_2.created_at with override_settings(**overrides), username_on_model( SCMPipelineRun, "signal_tester"): pipeline_run = SCMPipelineRun.objects.create( application=application) pipeline_run.status = constants.PIPELINE_STATUS_IN_PROGRESS pipeline_run.save() assert SCMRelease.objects.count() == 2 assert not my_scm_release.scm_pipeline_runs.filter( pk=pipeline_run.pk).exists() assert open_release_2.scm_pipeline_runs.filter( pk=pipeline_run.pk).exists() katka_records = [ record.message for record in caplog.records if record.name == "katka" ] assert len(katka_records) == 1 assert "Multiple open releases found" in katka_records[0] assert "picking newest" in katka_records[0]
def test_send_on_create_pipeline(self, application, caplog): session = mock.MagicMock() overrides = { "PIPELINE_RUNNER_SESSION": session, "PIPELINE_CHANGE_NOTIFICATION_URL": "http://override-url/", } with override_settings(**overrides), username_on_model( SCMPipelineRun, "signal_tester"): pipeline_run = SCMPipelineRun.objects.create( steps_total=0, application=application) # should not be called because the pipeline is still initializing assert session.post.call_args_list == [ mock.call("http://override-url/", json={ "public_identifier": str(pipeline_run.public_identifier) }), ] with override_settings(**overrides), username_on_model( SCMPipelineRun, "signal_tester"): pipeline_run.steps_total = 1 pipeline_run.save() assert len(session.post.call_args_list) == 1 with override_settings(**overrides), username_on_model( SCMPipelineRun, "signal_tester"): pipeline_run.status = constants.PIPELINE_STATUS_IN_PROGRESS pipeline_run.save() assert len(session.post.call_args_list) == 2 assert "Failed to notify pipeline runner" not in caplog.messages
def test_send_on_create_pipeline_exception(self, application, caplog): session = mock.MagicMock() mock_response = mock.MagicMock() mock_response.raise_for_status.side_effect = HTTPError("Error", 404) session.post.return_value = mock_response overrides = { "PIPELINE_RUNNER_SESSION": session, "PIPELINE_CHANGE_NOTIFICATION_URL": "http://override-url/", } with override_settings(**overrides), username_on_model( SCMPipelineRun, "signal_tester"): pipeline_run = SCMPipelineRun.objects.create( steps_total=0, application=application) # should not be called because the pipeline is still initializing assert session.post.call_args_list == [ mock.call("http://override-url/", json={ "public_identifier": str(pipeline_run.public_identifier) }), ] with override_settings(**overrides), username_on_model( SCMPipelineRun, "signal_tester"): pipeline_run.steps_total = 1 pipeline_run.save() assert len(session.post.call_args_list) == 1 assert "Failed to notify pipeline runner" in caplog.messages
def test_pipeline_gets_added_to_new_release(self, my_scm_pipeline_run, my_scm_release, application): """When a release is closed, a new release should be created when a new pipeline is created""" before = SCMRelease.objects.count() session = mock.MagicMock() overrides = {"PIPELINE_RUNNER_SESSION": session} with override_settings(**overrides), username_on_model( SCMRelease, "signal_tester"): my_scm_release.status = "closed" my_scm_release.save() with override_settings(**overrides), username_on_model( SCMPipelineRun, "signal_tester"): pipeline_run = SCMPipelineRun.objects.create( application=application) pipeline_run.status = constants.PIPELINE_STATUS_IN_PROGRESS pipeline_run.save() assert SCMRelease.objects.count() == before + 1 release = SCMRelease.objects.filter( scm_pipeline_runs__pk__exact=pipeline_run.pk).first() assert release.pk != my_scm_release.pk assert len(my_scm_release.scm_pipeline_runs.all()) == 1 assert my_scm_release.scm_pipeline_runs.filter( pk__exact=my_scm_pipeline_run.pk).count() == 1 assert len(release.scm_pipeline_runs.all()) == 1 assert release.scm_pipeline_runs.filter( pk__exact=pipeline_run.pk).count() == 1
def test_notify_post(self, scm_step_run, my_scm_pipeline_run): session = mock.MagicMock() overrides = { "PIPELINE_RUNNER_SESSION": session, "PIPELINE_RUNNER_BASE_URL": "http://override-url/", "PIPELINE_CHANGE_NOTIFICATION_EP": "change/", } with override_settings(**overrides): with username_on_model(SCMPipelineRun, "signal_tester"): my_scm_pipeline_run.status = constants.PIPELINE_STATUS_IN_PROGRESS my_scm_pipeline_run.save() with username_on_model(SCMStepRun, "signal_tester"): scm_step_run.status = constants.STEP_STATUS_IN_PROGRESS scm_step_run.save() scm_step_run.status = constants.STEP_STATUS_FAILED scm_step_run.save() # should be called twice: # - once for the status update of the pipeline (from initializing to "in progress"), and # - once because the number of completed steps changed # but not a third time, because: # - the step status changed from non-final ("not started") to non-final ("in progress") so the # number of completed steps in the pipeline did not change assert session.post.call_args_list == [ mock.call( "http://override-url/change/", json={"public_identifier": str(my_scm_pipeline_run.public_identifier)}, ), mock.call( "http://override-url/change/", json={"public_identifier": str(my_scm_pipeline_run.public_identifier)}, ), ]
def test_pick_newest_on_duplicate_open_releases(self, application, scm_release, scm_pipeline_run, caplog): """This should not happen, but if it happens, handle it gracefully""" session = mock.MagicMock() overrides = {'PIPELINE_CHANGE_NOTIFICATION_SESSION': session} with override_settings(**overrides), username_on_model( SCMRelease, 'signal_tester'): open_release_2 = SCMRelease.objects.create() open_release_2.scm_pipeline_runs.add(scm_pipeline_run) open_release_2.save() assert scm_release.created_at < open_release_2.created_at with override_settings(**overrides), username_on_model( SCMPipelineRun, 'signal_tester'): pipeline_run = SCMPipelineRun.objects.create( application=application) assert SCMRelease.objects.count() == 2 assert not scm_release.scm_pipeline_runs.filter( pk=pipeline_run.pk).exists() assert open_release_2.scm_pipeline_runs.filter( pk=pipeline_run.pk).exists() katka_records = [ record.message for record in caplog.records if record.name == 'katka' ] assert len(katka_records) == 1 assert 'Multiple open releases found' in katka_records[0] assert 'picking newest' in katka_records[0]
def test_new_pipeline_gets_added_to_new_release(self, scm_pipeline_run, scm_release, application): """When a release is closed, a new release should be created when a new pipeline is created""" assert SCMRelease.objects.count() == 1 session = mock.MagicMock() overrides = {'PIPELINE_CHANGE_NOTIFICATION_SESSION': session} with override_settings(**overrides), username_on_model( SCMRelease, 'signal_tester'): scm_release.status = 'closed' scm_release.save() with override_settings(**overrides), username_on_model( SCMPipelineRun, 'signal_tester'): pipeline_run = SCMPipelineRun.objects.create( application=application) assert SCMRelease.objects.count() == 2 release = SCMRelease.objects.filter( scm_pipeline_runs__pk__exact=pipeline_run.pk).first() assert release.pk != scm_release.pk assert len(scm_release.scm_pipeline_runs.all()) == 1 assert scm_release.scm_pipeline_runs.filter( pk__exact=scm_pipeline_run.pk).count() == 1 assert len(release.scm_pipeline_runs.all()) == 1 assert release.scm_pipeline_runs.filter( pk__exact=pipeline_run.pk).count() == 1
def scm_pipeline_run_with_no_open_release(another_project, another_scm_repository, scm_step_run_success_list_with_start_end_tags): application = models.Application(project=another_project, scm_repository=another_scm_repository, name='Application 2', slug='APP2') with username_on_model(models.Application, 'initial'): application.save() pipeline_yaml = '''stages: - release do-release: stage: release ''' scm_pipeline_run = models.SCMPipelineRun(application=application, pipeline_yaml=pipeline_yaml, steps_total=5, status="success", commit_hash='4015B57A143AEC5156FD1444A017A32137A3FD0F') with username_on_model(models.SCMPipelineRun, 'initial'): scm_pipeline_run.save() with username_on_model(models.SCMStepRun, 'initial'): for step in scm_step_run_success_list_with_start_end_tags: step.scm_pipeline_run = scm_pipeline_run step.save() return scm_pipeline_run
def test_only_on_create(self): model = OnlyOnCreate() with username_on_model(OnlyOnCreate, 'test_user'): model.save() assert model.field == 'test_user' with username_on_model(OnlyOnCreate, 'user2'): model.save() assert model.field == 'test_user'
def test_always_update(self): model = AlwaysUpdate() with username_on_model(AlwaysUpdate, "test_user"): model.save() assert model.field == "test_user" with username_on_model(AlwaysUpdate, "user2"): model.save() assert model.field == "user2"
def test_only_on_create(self): model = OnlyOnCreate() with username_on_model(OnlyOnCreate, "test_user"): model.save() assert model.field == "test_user" with username_on_model(OnlyOnCreate, "user2"): model.save() assert model.field == "test_user"
def test_always_update(self): model = AlwaysUpdate() with username_on_model(AlwaysUpdate, 'test_user'): model.save() assert model.field == 'test_user' with username_on_model(AlwaysUpdate, 'user2'): model.save() assert model.field == 'user2'
def my_team(group): a_team = models.Team(name='A-Team', slug='ATM', group=group) with username_on_model(models.Team, 'initial'): a_team.save() return a_team
def not_my_team(not_my_group): z_team = models.Team(name='Z-Team', slug='ZTM', group=not_my_group) with username_on_model(models.Team, 'initial'): z_team.save() return z_team
def deactivated_team(my_group): team = models.Team(name="Deactivated-Team", slug="DTM", group=my_group) team.deleted = True with username_on_model(models.Team, "deactivator"): team.save() return team
def scm_repository(scm_service, credential): scm_repository = models.SCMRepository(scm_service=scm_service, credential=credential, organisation='acme', repository_name='sample') with username_on_model(models.SCMRepository, 'initial'): scm_repository.save() return scm_repository
def deactivated_secret(credential): secret = models.CredentialSecret(key='username', value='full_access_value', credential=credential) secret.deleted = True with username_on_model(models.CredentialSecret, 'initial'): secret.save() return secret
def deactivated_credential(team): credential = models.Credential(name='System user deactivated', team=team) credential.deleted = True with username_on_model(models.Credential, 'initial'): credential.save() return credential
def my_team(group, not_my_team): team = models.Team(name="A-Team", slug="ATM", group=group) with username_on_model(models.Team, "initial"): team.save() return team
def deactivated_scm_release(deactivated_scm_pipeline_run): scm_release = models.SCMRelease() scm_release.deleted = True with username_on_model(models.SCMRelease, "initial"): scm_release.save() return scm_release
def an_scm_service(): scm_service = models.SCMService(scm_service_type="bitbucket", server_url="www.example.com") with username_on_model(models.SCMService, "initial"): scm_service.save() return scm_service
def my_other_teams_credential(my_other_team): credential = models.Credential(name="System user my other team", team=my_other_team) with username_on_model(models.Credential, "initial"): credential.save() return credential
def another_scm_repository(another_scm_service, my_other_teams_credential): scm_repository = models.SCMRepository(scm_service=another_scm_service, credential=my_other_teams_credential, organisation='acme', repository_name='another') with username_on_model(models.SCMRepository, 'initial'): scm_repository.save() return scm_repository
def not_my_team(not_my_group): team = models.Team(name="Z-Team", slug="ZTM", group=not_my_group) with username_on_model(models.Team, "initial"): team.save() return team
def my_other_team(my_other_group): team = models.Team(name="B-Team", slug="BTM", group=my_other_group) with username_on_model(models.Team, "initial"): team.save() return team
def application(project, scm_repository): app = Application(name="application1", project=project, scm_repository=scm_repository) with username_on_model(Application, "audit_user"): app.save() return app
def my_other_team(my_other_group): a_team = models.Team(name='B-Team', slug='BTM', group=my_other_group) with username_on_model(models.Team, 'initial'): a_team.save() return a_team
def another_scm_step_run(another_scm_pipeline_run): scm_step_run = models.SCMStepRun(slug='another-release', name='Another Release Katka', stage='Production', scm_pipeline_run=another_scm_pipeline_run) with username_on_model(models.SCMStepRun, 'initial'): scm_step_run.save() return scm_step_run
def not_my_application(another_scm_repository, not_my_project): application = models.Application(project=not_my_project, scm_repository=another_scm_repository, name='Application 98', slug='APP98') with username_on_model(models.Application, 'initial'): application.save() return application
def another_scm_release(another_scm_pipeline_run): scm_release = models.SCMRelease.objects.filter(scm_pipeline_runs__pk__exact=another_scm_pipeline_run.pk).first() with username_on_model(models.SCMRelease, 'initial'): scm_release.name = 'Version 15.0' scm_release.save() return scm_release