This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_app.py
167 lines (145 loc) · 4.78 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from click.testing import CliRunner
from mock import MagicMock, PropertyMock, patch
from check_status import cli
import pytest
MOCK_SERVICE_NAME = "myservice"
MOCK_GH_TOKEN = "aaaaa"
MOCK_REPO_NAME = "foocorp/bar"
MOCK_PR_NUMBER = 1
MOCK_COMMIT_SHA = "f000bee"
MOCK_PROJECT_ID = "foocorp"
runner = CliRunner()
def test_help():
response = runner.invoke(cli, ["--help"])
assert response.exit_code == 0
assert "Usage" in response.output
def test_set_no_project():
response = runner.invoke(cli, ["set"])
assert response.exit_code == 2
assert "Missing option '--project-id'" in response.output
def service_data(name, tags):
traffic = [
{
"revisionName": f"{name}-00001-aaa",
"percent": 100,
}
]
for t in tags:
tag = f"pr-{t}"
traffic.append(
{
"tag": tag,
"revisionName": f"{name}-00002-bbb",
"url": f"https://{tag}---{name}-yyyyyy-uc-a.run.app",
}
)
return {"metadata": {"name": name}, "status": {"traffic": traffic}}
@patch("check_status.discovery")
def test_set_wrongtag(discovery_mock):
service_mock = MagicMock()
service_mock.projects = MagicMock(return_value=service_mock)
service_mock.locations = MagicMock(return_value=service_mock)
service_mock.services = MagicMock(return_value=service_mock)
service_mock.get = MagicMock(return_value=service_mock)
service_mock.execute = MagicMock(
return_value=service_data(MOCK_SERVICE_NAME, [MOCK_PR_NUMBER])
)
discovery_mock.build = MagicMock(return_value=service_mock)
invalid_pr = MOCK_PR_NUMBER + 1 # intentionally wrong
response = runner.invoke(
cli,
[
"set",
"--project-id",
"foo",
"--region",
"us-central1",
"--service",
MOCK_SERVICE_NAME,
"--repo-name",
MOCK_REPO_NAME,
"--commit-sha",
MOCK_COMMIT_SHA,
"--pull-request",
invalid_pr,
"--dry-run",
],
)
print(response.output)
assert response.exit_code == 1
assert "Error finding revision" in response.output
assert f"pr-{invalid_pr}" in response.output
@patch("check_status.discovery")
@patch("check_status.secretmanager")
@patch("check_status.github")
def test_set_check_calls(github_mock, sm_mock, discovery_mock):
service_mock = MagicMock()
service_mock.projects = MagicMock(return_value=service_mock)
service_mock.locations = MagicMock(return_value=service_mock)
service_mock.services = MagicMock(return_value=service_mock)
service_mock.get = MagicMock(return_value=service_mock)
service_mock.execute = MagicMock(
return_value=service_data(MOCK_SERVICE_NAME, [MOCK_PR_NUMBER])
)
discovery_mock.build = MagicMock(return_value=service_mock)
sm_mock = MagicMock()
sm_mock.SecretManagerServiceClient = MagicMock(return_value=sm_mock)
sm_mock.access_secret_version = MagicMock(return_value=sm_mock)
sm_mock.payload = PropertyMock(return_value="TEST")
sm_mock.decode = PropertyMock(return_value=b"foobar")
gh_mock = MagicMock()
gh_mock.get_repo = MagicMock(return_value=gh_mock)
gh_mock.repo_name = MOCK_REPO_NAME
gh_mock.get_commit = MagicMock(return_value=gh_mock)
gh_mock.sha = MOCK_COMMIT_SHA
gh_mock.create_status = MagicMock(return_value=True)
github_mock.Github = MagicMock(return_value=gh_mock)
# Test Status Dry-Run
response = runner.invoke(
cli,
[
"set",
"--project-id",
MOCK_PROJECT_ID,
"--region",
"us-central1",
"--service",
MOCK_SERVICE_NAME,
"--repo-name",
MOCK_REPO_NAME,
"--commit-sha",
MOCK_COMMIT_SHA,
"--pull-request",
MOCK_PR_NUMBER,
"--dry-run",
],
)
assert response.exit_code == 0
assert "Dry-run" in response.output
assert MOCK_SERVICE_NAME in response.output
assert MOCK_REPO_NAME in response.output
assert MOCK_COMMIT_SHA in response.output
# Test Status real-run
response = runner.invoke(
cli,
[
"set",
"--project-id",
MOCK_PROJECT_ID,
"--region",
"us-central1",
"--service",
MOCK_SERVICE_NAME,
"--repo-name",
MOCK_REPO_NAME,
"--commit-sha",
MOCK_COMMIT_SHA,
"--pull-request",
MOCK_PR_NUMBER,
],
)
assert response.exit_code == 0
assert "Success" in response.output
assert MOCK_SERVICE_NAME in response.output
assert MOCK_REPO_NAME in response.output
assert MOCK_COMMIT_SHA in response.output