Example #1
0
def test_send_message(monkeypatch, mocker, repo, message_title, url,
                      result_message):
    old_version = repo.head.commit.hexsha
    repo.index.commit('1')

    fake_deployment = Deployment(repo=repo,
                                 new_version='HEAD',
                                 old_version=old_version)

    monkeypatch.setattr(uut, 'deployment', fake_deployment)
    fake_post = mocker.patch.object(requests.Session, 'post')

    slack_hook = uut.Hook()
    slack_hook.channel_id = 'asd'
    slack_hook.send_message({
        'attachments': [{
            'fields': AttachmentFields([message_title])
        }],
    })
    base_data = {'token': '', 'channel': 'asd'}
    fake_post.assert_called_with(url,
                                 data={
                                     **base_data, 'attachments':
                                     result_message,
                                     'link_names': '1'
                                 })
Example #2
0
def test_send_reply(monkeypatch, mocker, repo, message_id, text):
    old_version = repo.head.commit.hexsha
    for commit in ["4"]:
        repo.index.commit(commit)

    fake_post = mocker.patch.object(requests.Session, "post")
    fake_deployment = Deployment(repo=repo, new_version="HEAD", old_version=old_version)

    monkeypatch.setattr(uut, "deployment", fake_deployment)

    slack_hook = uut.Hook()
    slack_hook.channel_ids = ["asd"]
    slack_hook.send_reply("asd", message_id, text)

    fake_post.assert_called_with(
        "https://slack.com/api/chat.postMessage",
        data={
            "token": "xoxp-123-456",
            "channel": "asd",
            "thread_ts": message_id,
            "text": text,
            "reply_broadcast": "false",
            "link_names": "1",
        },
    )
Example #3
0
def test_set_status(monkeypatch, repo, environment_before, environment_after,
                    expected):
    old_version = repo.head.commit.hexsha
    for commit in ['1']:
        repo.index.commit(commit)

    fake_deployment = Deployment(repo=repo,
                                 new_version='HEAD',
                                 old_version=old_version)
    monkeypatch.setattr(uut, 'deployment', fake_deployment)
    message = {
        'attachments': [
            {
                'fields': {
                    'Environment': environment_before,
                },
            },
        ],
    }

    slack_hook = uut.Hook()
    slack_hook.channel_id = 'asd'
    slack_hook.set_status(message, environment_after)

    assert message['attachments'][0]['fields']['Environment'] == expected
Example #4
0
def test_send_reply(monkeypatch, mocker, repo, message_id, text):
    old_version = repo.head.commit.hexsha
    for commit in ['4']:
        repo.index.commit(commit)

    fake_post = mocker.patch.object(requests.Session, 'post')
    fake_deployment = Deployment(repo=repo,
                                 new_version='HEAD',
                                 old_version=old_version)

    monkeypatch.setattr(uut, 'deployment', fake_deployment)

    slack_hook = uut.Hook()
    slack_hook.channel_id = 'asd'
    slack_hook.send_reply(message_id, text)

    fake_post.assert_called_with('https://slack.com/api/chat.postMessage',
                                 data={
                                     'token': '',
                                     'channel': 'asd',
                                     'thread_ts': message_id,
                                     'text': text,
                                     'reply_broadcast': 'false',
                                     'link_names': '1',
                                 })
Example #5
0
def test_get_changelog_redeploy(monkeypatch, repo):
    fake_deployment = Deployment(repo=repo, new_version="HEAD", old_version="HEAD")
    monkeypatch.setattr(uut, "deployment", fake_deployment)
    slack_hook = uut.Hook()
    slack_hook.users_by_email = {}
    assert fake_deployment.is_redeploy, "deployment should be redeploy"
    assert slack_hook.get_changelog() == "Re-deploy without changes."
Example #6
0
def test_create_event(monkeypatch, mocker, repo, commits, event, text):
    old_version = repo.head.commit.hexsha
    for commit in commits:
        repo.index.commit(commit)

    tags = [
        'releaser:[email protected]', 'project:foo/bar', 'environment:a-b/c-d'
    ]

    fake_create = mocker.patch.object(datadog.api.Event, 'create')
    fake_deployment = Deployment(repo=repo,
                                 new_version='HEAD',
                                 old_version=old_version)

    monkeypatch.setattr(uut, 'deployment', fake_deployment)

    dd_hook = uut.Hook()
    if event == 'success':
        dd_hook.after_upgrade_success()
    elif event == 'error':
        dd_hook.after_upgrade_failure()

    fake_create.assert_called_with(
        title='foo/bar deployment',
        text=text,
        tags=tags,
        alert_type=event,
    )
Example #7
0
def test_get_existing_messages(monkeypatch, mocker, repo, slack_response, result):
    old_version = repo.head.commit.hexsha
    for commit in ["1"]:
        repo.index.commit(commit, author=Actor("test_author", "*****@*****.**"))

    fake_deployment = Deployment(repo=repo, new_version="HEAD", old_version=old_version)
    monkeypatch.setattr(uut, "deployment", fake_deployment)
    slack_hook = uut.Hook()
    slack_hook.channel_ids = ["123", "asd"]
    fake_get = mocker.patch.object(uut.session, "get")
    fake_response = mocker.Mock()
    fake_response.json = lambda: {"messages": []}
    fake_get.return_value = fake_response

    with pytest.raises(KeyError):
        slack_hook.get_existing_messages()

    deployment_id = f"<{uut.deployment.id}.com| >"

    fake_response.json = lambda: {
        "messages": [
            {"text": deployment_id, "attachments": [{"fields": []}]},
            {"text": "colemak", "attachments": [{"fields": []}]},
        ]
    }
    messages = slack_hook.get_existing_messages()
    assert len(messages) == 2
    assert messages["123"]["text"] == deployment_id
    assert messages["asd"]["attachments"][0]["fields"] == uut.AttachmentFields([])
Example #8
0
def test_create_event(monkeypatch, mocker, repo, commits, event, text):
    old_version = repo.head.commit.hexsha
    for commit in commits:
        repo.index.commit(commit)

    tags = [
        "releaser:[email protected]", "project:foo/bar", "environment:a-b/c-d"
    ]

    fake_create = mocker.patch.object(datadog.api.Event, "create")
    fake_deployment = Deployment(repo=repo,
                                 new_version="HEAD",
                                 old_version=old_version)

    monkeypatch.setattr(uut, "deployment", fake_deployment)

    dd_hook = uut.Hook()
    if event == "success":
        dd_hook.after_upgrade_success()
    elif event == "error":
        dd_hook.after_upgrade_failure()

    fake_create.assert_called_with(title="foo/bar deployment",
                                   text=text,
                                   tags=tags,
                                   alert_type=event)
Example #9
0
def test_get_changelog_force_push(monkeypatch, repo, commits, expected):
    old_version = "0000000000000000000000000000000000000000"
    for commit in commits:
        repo.index.commit(commit, author=Actor("test_author", "*****@*****.**"))

    fake_deployment = Deployment(repo=repo, new_version="HEAD", old_version=old_version)
    monkeypatch.setattr(uut, "deployment", fake_deployment)

    slack_hook = uut.Hook()
    slack_hook.users_by_email = {}

    changelog = slack_hook.get_changelog()
    assert re.fullmatch(expected, changelog)
Example #10
0
def test_get_changelog(monkeypatch, repo, commits, result):
    old_version = repo.head.commit.hexsha
    for commit in commits:
        repo.index.commit(commit, author=Actor('test_author', '*****@*****.**'))

    fake_deployment = Deployment(repo=repo, new_version='HEAD', old_version=old_version)
    monkeypatch.setattr(uut, 'deployment', fake_deployment)

    slack_hook = uut.Hook()
    slack_hook.users_by_email = {}

    changelog = slack_hook.get_changelog()
    match = re.fullmatch(result, changelog)
    assert match is not None
Example #11
0
def test_set_status(monkeypatch, repo, environment_before, environment_after, expected):
    old_version = repo.head.commit.hexsha
    for commit in ["1"]:
        repo.index.commit(commit)

    fake_deployment = Deployment(repo=repo, new_version="HEAD", old_version=old_version)
    monkeypatch.setattr(uut, "deployment", fake_deployment)
    message = {"attachments": [{"fields": {"Environment": environment_before}}]}

    slack_hook = uut.Hook()
    slack_hook.channel_ids = ["asd"]
    slack_hook.set_status(message, environment_after)

    assert message["attachments"][0]["fields"]["Environment"] == expected
Example #12
0
def test_get_changelog_rollback(monkeypatch, repo):
    ts = datetime(2017, 1, 1, 0, 0, 0).isoformat()
    old_version = repo.head.commit.hexsha
    for commit in ['1']:
        repo.index.commit(commit, author=Actor('test_author', '*****@*****.**'), commit_date=ts)

    fake_deployment = Deployment(repo=repo, new_version='HEAD', old_version=old_version)
    monkeypatch.setattr(uut, 'deployment', fake_deployment)
    slack_hook = uut.Hook()
    slack_hook.users_by_email = {}

    changelog = slack_hook.get_changelog()

    assert fake_deployment.is_rollback, "deployment should be rollback"
    assert changelog.startswith(':warning: Rolling back the following changes:\n'), "rollback didn't display correct changelog"
Example #13
0
def test_generate_new_message(monkeypatch, repo, commits, expected):
    monkeypatch.setenv('CI_ENVIRONMENT_URL', 'example.com')

    old_version = repo.head.commit.hexsha
    for commit in commits:
        repo.index.commit(commit, author=Actor('test_author', '*****@*****.**'))

    fake_deployment = Deployment(repo=repo, new_version='HEAD', old_version=old_version,
                                 stack=rancher.Stack('0st0', 'foo'))
    monkeypatch.setattr(uut, 'deployment', fake_deployment)

    slack_hook = uut.Hook()
    slack_hook.get_changelog = lambda: ''
    slack_hook.users_by_email = {'*****@*****.**': "@picky"}

    msg = slack_hook.generate_new_message()
    expected['text'] = slack_hook.deployment_text
    assert msg == expected
Example #14
0
def test_send_message(monkeypatch, mocker, repo, message_title, url, result_message):
    old_version = repo.head.commit.hexsha
    repo.index.commit("1")

    fake_deployment = Deployment(repo=repo, new_version="HEAD", old_version=old_version)

    monkeypatch.setattr(uut, "deployment", fake_deployment)
    fake_post = mocker.patch.object(requests.Session, "post")

    slack_hook = uut.Hook()
    slack_hook.channel_ids = ["asd"]
    slack_hook.send_message(
        "asd", {"attachments": [{"fields": AttachmentFields([message_title])}]}
    )
    base_data = {"token": "xoxp-123-456", "channel": "asd"}
    fake_post.assert_called_with(
        url, data={**base_data, "attachments": result_message, "link_names": "1"}
    )
Example #15
0
def test_get_existing_message(monkeypatch, mocker, repo, slack_response,
                              result):
    old_version = repo.head.commit.hexsha
    for commit in ['1']:
        repo.index.commit(commit, author=Actor('test_author', '*****@*****.**'))

    fake_deployment = Deployment(repo=repo,
                                 new_version='HEAD',
                                 old_version=old_version)
    monkeypatch.setattr(uut, 'deployment', fake_deployment)
    slack_hook = uut.Hook()
    slack_hook.channel_id = 'asd'
    fake_get = mocker.patch.object(uut.session, 'get')
    fake_response = mocker.Mock()
    fake_response.json = lambda: {'messages': []}
    fake_get.return_value = fake_response

    assert slack_hook.get_existing_message() is None

    deployment_id = f'<{uut.deployment.id}.com| >'

    fake_response.json = lambda: {
        'messages': [
            {
                'text': deployment_id,
                'attachments': [{
                    'fields': []
                }]
            },
            {
                'text': 'colemak',
                'attachments': [{
                    'fields': []
                }]
            },
        ]
    }
    assert slack_hook.get_existing_message()['text'] == deployment_id
    assert slack_hook.get_existing_message(
    )['attachments'][0]['fields'] == uut.AttachmentFields([])
Example #16
0
def test_generate_new_message(monkeypatch, repo, commits, expected):
    monkeypatch.setenv("CI_ENVIRONMENT_URL", "example.com")

    old_version = repo.head.commit.hexsha
    for commit in commits:
        repo.index.commit(commit, author=Actor("test_author", "*****@*****.**"))

    fake_deployment = Deployment(
        repo=repo,
        new_version="HEAD",
        old_version=old_version,
        stack=rancher.Stack("0st0", "foo"),
    )
    monkeypatch.setattr(uut, "deployment", fake_deployment)

    slack_hook = uut.Hook()
    slack_hook.get_changelog = lambda: ""
    slack_hook.users_by_email = {"*****@*****.**": "@picky"}

    msg = slack_hook.generate_new_message()
    expected["text"] = slack_hook.deployment_text
    assert msg == expected
Example #17
0
def mock_deployment(repo, monkeypatch):
    fake_deployment = Deployment(
        repo=repo, new_version="HEAD", old_version=repo.head.commit.hexsha
    )
    monkeypatch.setattr(uut, "deployment", fake_deployment)