-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathe2e.test.ts
More file actions
256 lines (225 loc) · 8.3 KB
/
e2e.test.ts
File metadata and controls
256 lines (225 loc) · 8.3 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test } from 'node:test';
import assert from 'node:assert';
import { getExecOutput } from '@actions/exec';
import { run_v1 } from 'googleapis';
import { skipIfMissingEnv } from '@google-github-actions/actions-utils';
test(
'e2e tests',
{
concurrency: true,
skip: skipIfMissingEnv('PROJECT_ID'),
},
async (suite) => {
let service: run_v1.Schema$Service;
let job: run_v1.Schema$Job;
let worker_pool: run_v1.Schema$WorkerPool;
let metadata: run_v1.Schema$ObjectMeta;
let spec: run_v1.Schema$TaskSpec | run_v1.Schema$RevisionSpec;
suite.before(async () => {
if (process.env.JOB) {
const output = await getExecOutput('gcloud', [
'run',
'jobs',
'describe',
process.env.JOB!,
'--project',
process.env.PROJECT_ID!,
'--format',
'json',
'--region',
'us-central1',
]);
job = JSON.parse(output.stdout) as run_v1.Schema$Job;
if (!job) {
throw new Error('failed to find job definition');
}
metadata = job.spec!.template!.metadata!;
spec = job.spec!.template!.spec!.template!.spec!;
} else if (process.env.SERVICE) {
const output = await getExecOutput('gcloud', [
'run',
'services',
'describe',
process.env.SERVICE!,
'--project',
process.env.PROJECT_ID!,
'--format',
'json',
'--region',
'us-central1',
]);
service = JSON.parse(output.stdout) as run_v1.Schema$Service;
if (!service) {
throw new Error('failed to find service definition');
}
metadata = service.spec!.template!.metadata!;
spec = service.spec!.template!.spec!;
} else if (process.env.WORKER_POOL) {
const output = await getExecOutput('gcloud', [
'beta',
'run',
'worker-pools',
'describe',
process.env.WORKER_POOL!,
'--project',
process.env.PROJECT_ID!,
'--format',
'json',
'--region',
'us-central1',
]);
worker_pool = JSON.parse(output.stdout) as run_v1.Schema$Service;
if (!worker_pool) {
throw new Error('failed to find worker_pool definition');
}
metadata = worker_pool.spec!.template!.metadata!;
spec = worker_pool.spec!.template!.spec!;
} else {
throw new Error(`missing service, job or worker_pool`);
}
});
await suite.test('has the correct envvars', { skip: skipIfMissingEnv('ENV') }, async () => {
const expected = parseEnvVars(process.env.ENV!);
const actual = spec.containers?.at(0)?.env?.filter((e) => e?.value);
const subset = expected.map((e) => {
return actual?.find((a) => a.name == e.name);
});
assert.deepStrictEqual(subset, expected);
});
await suite.test(
'has the correct secret vars',
{ skip: skipIfMissingEnv('SECRET_ENV') },
async () => {
const expected = parseEnvVars(process.env.SECRET_ENV!);
const actual = spec.containers
?.at(0)
?.env?.filter((entry) => entry && entry.valueFrom)
.map((entry) => {
const ref = entry.valueFrom?.secretKeyRef;
return { name: entry.name, value: `${ref?.name}:${ref?.key}` };
});
const subset = expected.map((e) => {
return actual?.find((a) => a.name == e.name);
});
assert.deepStrictEqual(subset, expected);
},
);
await suite.test(
'has the correct secret volumes',
{ skip: skipIfMissingEnv('SECRET_VOLUMES') },
async () => {
const expected = parseEnvVars(process.env.SECRET_VOLUMES!);
const actual = spec.containers?.at(0)?.volumeMounts?.map((volumeMount) => {
const secretVolume = spec.volumes?.find(
(volume) => volumeMount.name === volume.name,
)?.secret;
const secretName = secretVolume?.secretName;
const secretData = secretVolume?.items?.at(0);
const secretPath = `${volumeMount.mountPath}/${secretData?.path}`;
const secretRef = `${secretName}:${secretData?.key}`;
return { name: secretPath, value: secretRef };
});
const subset = expected.map((e) => {
return actual?.find((a) => a.name == e.name);
});
assert.deepStrictEqual(subset, expected);
},
);
await suite.test('has the correct params', { skip: skipIfMissingEnv('PARAMS') }, async () => {
const expected = JSON.parse(process.env.PARAMS!);
if (expected.containerConncurrency) {
assert.deepStrictEqual(
(spec as run_v1.Schema$RevisionSpec).containerConcurrency,
expected.containerConncurrency,
);
}
if (expected.timeoutSeconds) {
assert.deepStrictEqual(spec.timeoutSeconds, expected.timeoutSeconds);
}
const limits = spec.containers?.at(0)?.resources?.limits;
if (expected.cpu) {
assert.deepStrictEqual(limits?.cpu, expected.cpu.toString());
}
if (expected.memory) {
assert.deepStrictEqual(limits?.memory, expected.memory);
}
});
await suite.test(
'has the correct annotations',
{ skip: skipIfMissingEnv('ANNOTATIONS') },
async () => {
const expected = JSON.parse(process.env.ANNOTATIONS!) as Record<string, string>;
const actual = metadata.annotations;
const subset = Object.assign(
{},
...Object.keys(expected).map((k) => ({ [k]: actual?.[k] })),
);
assert.deepStrictEqual(subset, expected);
},
);
await suite.test('has the correct labels', { skip: skipIfMissingEnv('LABELS') }, async () => {
const expected = JSON.parse(process.env.LABELS!) as Record<string, string>;
const actual = metadata.labels;
const subset = Object.assign({}, ...Object.keys(expected).map((k) => ({ [k]: actual?.[k] })));
assert.deepStrictEqual(subset, expected);
});
await suite.test('has the revision name', { skip: skipIfMissingEnv('REVISION') }, async () => {
const expected = process.env.REVISION! as string;
const actual = service.metadata?.name;
assert.deepStrictEqual(actual, expected);
});
await suite.test(
'has the worker_pool name',
{ skip: skipIfMissingEnv('WORKER_POOL') },
async () => {
const expected = process.env.WORKER_POOL! as string;
const actual = worker_pool.metadata?.name;
assert.deepStrictEqual(actual, expected);
},
);
await suite.test('has the job name', { skip: skipIfMissingEnv('JOB') }, async () => {
const expected = process.env.JOB! as string;
const actual = job.metadata?.name;
assert.deepStrictEqual(actual, expected);
});
await suite.test('has the correct tag', { skip: skipIfMissingEnv('TAG') }, async () => {
const expected = process.env.TAG!;
const actual = service?.spec?.traffic?.map((revision) => revision.tag);
assert.deepStrictEqual(actual, expected);
});
await suite.test(
'has the correct traffic',
{ skip: skipIfMissingEnv('TRAFFIC', 'TAG') },
async () => {
const expected = process.env.TRAFFIC!;
const tagged = service?.spec?.traffic?.find((revision) => {
return revision.tag == process.env.TAG!;
});
const percent = tagged?.percent;
assert.deepStrictEqual(percent, expected);
},
);
},
);
const parseEnvVars = (envVarInput: string): run_v1.Schema$EnvVar[] => {
const m = JSON.parse(envVarInput) as Record<string, string>;
const envVars = Object.entries(m).map(([key, value]) => {
return { name: key, value: value };
});
return envVars;
};