Skip to content

Commit f84e4bd

Browse files
committed
Fix ability to run integration tests in Werft
1 parent cc238b3 commit f84e4bd

File tree

9 files changed

+180
-208
lines changed

9 files changed

+180
-208
lines changed

.github/pull_request_template.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ Optional annotations to add to the werft job.
3636
* with-preview - whether to create a preview environment for this PR
3737
-->
3838
- [ ] /werft with-preview
39+
- [ ] /werft with-integration-tests=all
40+
Valid options are `all`, `workspace`, `webapp`, `ide`

.werft/build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { buildAndPublish } from "./jobs/build/build-and-publish";
88
import { validateChanges } from "./jobs/build/validate-changes";
99
import { prepare } from "./jobs/build/prepare";
1010
import { deployToPreviewEnvironment } from "./jobs/build/deploy-to-preview-environment";
11-
import { triggerIntegrationTests } from "./jobs/build/trigger-integration-tests";
11+
import { runIntegrationTests } from "./jobs/build/trigger-integration-tests";
1212
import { triggerSelfHostedPreview, triggerUpgradeTests } from "./jobs/build/self-hosted-upgrade-tests";
1313
import { jobConfig } from "./jobs/build/job-config";
1414
import { typecheckWerftJobs } from "./jobs/build/typecheck-werft-jobs";
@@ -82,5 +82,5 @@ async function run(context: any) {
8282
throw e;
8383
}
8484

85-
await triggerIntegrationTests(werft, config, context.Owner);
85+
await runIntegrationTests(werft, config, context.Owner);
8686
}

.werft/build.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,18 @@ pod:
221221
value: "/mnt/secrets/github-token-gitpod-bot/token"
222222
- name: WERFT_CREDENTIAL_HELPER
223223
value: "/workspace/dev/preview/werft-credential-helper.sh"
224+
# When running the build with 'with-integration-tests' these are used
225+
# to specify what Gitpod user to use in those tests.
226+
- name: INTEGRATION_TEST_USERNAME
227+
valueFrom:
228+
secretKeyRef:
229+
name: integration-test-user
230+
key: username
231+
- name: INTEGRATION_TEST_USER_TOKEN
232+
valueFrom:
233+
secretKeyRef:
234+
name: integration-test-user
235+
key: token
224236
command:
225237
- bash
226238
- -c

.werft/jobs/build/job-config.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { exec } from "../../util/shell";
22
import { Werft } from "../../util/werft";
33
import { previewNameFromBranchName } from "../../util/preview";
44

5+
type WithIntegrationTests = "skip" | "all" | "workspace" | "ide" | "webapp";
6+
57
export interface JobConfig {
68
analytics: string;
79
buildConfig: any;
@@ -23,7 +25,7 @@ export interface JobConfig {
2325
storage: string;
2426
version: string;
2527
withContrib: boolean;
26-
withIntegrationTests: boolean;
28+
withIntegrationTests: WithIntegrationTests;
2729
withUpgradeTests: boolean;
2830
withSelfHostedPreview: boolean;
2931
withObservability: boolean;
@@ -78,7 +80,7 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
7880
const withContrib = "with-contrib" in buildConfig || mainBuild;
7981
const withPreview = "with-preview" in buildConfig && !mainBuild;
8082
const storage = buildConfig["storage"] || "";
81-
const withIntegrationTests = "with-integration-tests" in buildConfig && !mainBuild;
83+
const withIntegrationTests = parseWithIntegrationTests(werft, sliceId, buildConfig["with-integration-tests"]);
8284
const withUpgradeTests = "with-upgrade-tests" in buildConfig && !mainBuild;
8385
const fromVersion = withUpgradeTests ? buildConfig["from-version"] : "";
8486
const replicatedChannel = buildConfig["channel"];
@@ -151,7 +153,7 @@ export function jobConfig(werft: Werft, context: any): JobConfig {
151153
};
152154

153155
werft.logOutput(sliceId, JSON.stringify(jobConfig, null, 2));
154-
werft.log(sliceId, "Expand to see the parsed configuration")
156+
werft.log(sliceId, "Expand to see the parsed configuration");
155157
const globalAttributes = Object.fromEntries(
156158
Object.entries(jobConfig).map((kv) => {
157159
const [key, value] = kv;
@@ -179,3 +181,23 @@ function parseVersion(context: any) {
179181
}
180182
return version;
181183
}
184+
185+
export function parseWithIntegrationTests(werft: Werft, sliceID: string, value?: string): WithIntegrationTests {
186+
switch (value) {
187+
case null:
188+
case undefined:
189+
return "skip";
190+
case "skip":
191+
case "all":
192+
case "webapp":
193+
case "ide":
194+
case "webapp":
195+
return value;
196+
case "":
197+
werft.log(sliceID, "with-integration-tests was set but no value was provided - falling back to 'all'");
198+
return "all";
199+
default:
200+
werft.log(sliceID, `Unknown value for with-integration-tests: '${value}' - falling back to 'all'`);
201+
return "all";
202+
}
203+
}
Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,36 @@
11
import { exec } from "../../util/shell";
22
import { Werft } from "../../util/werft";
33
import { JobConfig } from "./job-config";
4+
import { PREVIEW_K3S_KUBECONFIG_PATH } from "./const";
45

56
const phases = {
6-
TRIGGER_INTEGRATION_TESTS: "trigger integration tests",
7+
RUN_INTEGRATION_TESTS: "Run integration tests",
78
};
89

910
/**
1011
* Trigger integration tests
1112
*/
12-
export async function triggerIntegrationTests(werft: Werft, config: JobConfig, username: string) {
13-
werft.phase(phases.TRIGGER_INTEGRATION_TESTS, "Trigger integration tests");
13+
export async function runIntegrationTests(werft: Werft, config: JobConfig, username: string) {
14+
werft.phase(phases.RUN_INTEGRATION_TESTS, "Run integration tests");
1415

15-
if (!config.withIntegrationTests) {
16+
if (config.withIntegrationTests == "skip") {
1617
// If we're skipping integration tests we wont trigger the job, which in turn won't create the
1718
// ci/werft/run-integration-tests Github Check. As ci/werft/run-integration-tests is a required
1819
// check this means you can't merge your PR without override checks.
19-
werft.log(phases.TRIGGER_INTEGRATION_TESTS, "Skipped integration tests");
20-
werft.done(phases.TRIGGER_INTEGRATION_TESTS);
20+
werft.log(phases.RUN_INTEGRATION_TESTS, "Skipped integration tests");
21+
werft.done(phases.RUN_INTEGRATION_TESTS);
2122
return;
2223
}
2324

2425
try {
25-
const imageVersion = exec(
26-
`docker run --rm eu.gcr.io/gitpod-core-dev/build/versions:${config.version} cat /versions.yaml | yq r - 'components.integrationTest.version'`,
27-
{ silent: true },
28-
).stdout.trim();
29-
30-
exec(`git config --global user.name "${username}"`);
31-
const annotations = [
32-
`version="${imageVersion}"`,
33-
`namespace="${config.previewEnvironment.namespace}"`,
34-
`username="${username}"`,
35-
`updateGitHubStatus="gitpod-io/gitpod"`,
36-
]
37-
.map((annotation) => `-a ${annotation}`)
38-
.join(" ");
39-
exec(`werft run --remote-job-path .werft/run-integration-tests.yaml ${annotations} github`, {
40-
slice: phases.TRIGGER_INTEGRATION_TESTS,
41-
}).trim();
42-
43-
werft.done(phases.TRIGGER_INTEGRATION_TESTS);
26+
exec(
27+
`KUBECONFIG="${PREVIEW_K3S_KUBECONFIG_PATH}" GOOGLE_APPLICATION_CREDENTIALS=/home/gitpod/.config/gcloud/legacy_credentials/[email protected]/adc.json /workspace/test/run.sh ${config.withIntegrationTests}`,
28+
);
29+
werft.done(phases.RUN_INTEGRATION_TESTS);
4430
} catch (err) {
4531
if (!config.mainBuild) {
46-
werft.fail(phases.TRIGGER_INTEGRATION_TESTS, err);
32+
werft.fail(phases.RUN_INTEGRATION_TESTS, err);
4733
}
48-
exec("exit 0");
34+
throw err;
4935
}
5036
}

.werft/run-integration-tests.yaml

Lines changed: 0 additions & 87 deletions
This file was deleted.

test/README.md

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,52 +21,64 @@ Such tests are for example:
2121

2222
### Automatically at Gitpod
2323

24-
There is a [werft job](../.werft/run-integration-tests.yaml) that runs the integration tests against `core-dev` preview environments.
24+
You can opt-in to run the integrations tests as part of the build job. that runs the integration tests against preview environments.
2525

2626
> For tests that require an existing user the framework tries to automatically select one from the DB.
2727
> - On preview envs make sure to create one before running tests against it!
2828
> - If it's important to use a certain user (with fixed settings, for example) pass the additional `username` parameter.
2929
3030
Example command:
31-
```
32-
werft run github -j .werft/run-integration-tests.yaml -a namespace=staging-gpl-2658-int-tests -a version=gpl-2658-int-tests.57 -f
31+
32+
```sh
33+
werft job run github -a with-preview=true -a with-integration-tests=webapp -f
3334
```
3435

3536
### Manually
3637

3738
You may want to run tests to assert whether a Gitpod installation is successfully integrated.
3839

39-
#### Using a pod
40-
41-
Best for when you want to validate an environment.
42-
43-
1. Update image name in `integration.yaml` for job `integration-job` to latest built by werft.
44-
2. Optionally add your username in that job argument or any other additional params.
45-
2. Apply yaml file that will add all necessary permissions and create a job that will run tests.
46-
* [`kubectl apply -f ./integration.yaml`](./integration.yaml)
47-
3. Check logs to inspect test results like so `kubectl logs -f jobs/integration-job`.
48-
4. Tear down the integration user and job when testing is done.
49-
* [`kubectl delete -f ./integration.yaml`](./integration.yaml)
50-
5140
#### Go test
5241

5342
This is best for when you're actively developing Gitpod.
43+
5444
Test will work if images that they use are already cached by Gitpod instance. If not, they might fail if it takes too long to pull an image.
45+
5546
There are 4 different types of tests:
47+
5648
1. Enterprise specific, that require valid license to be installed. Run those with `-enterprise=true`
5749
2. Tests that require correct user (user should have github OAuth integration setup with gitpod). Run those with `-username=<gitpod_username>`. Make sure to load https://github.com/gitpod-io/gitpod-test-repo and https://github.com/gitpod-io/gitpod workspaces inside your gitpod that you are testing to preload those images onto your node. Wait for it to finish pulling those image, this will ensure that test will not fail due to timeout while waiting to pull an image for the first time.
5850
3. To test gitlab integration, add `-gitlab=true`
5951
4. All other tests.
6052

61-
To run the tests:
62-
1. Clone this repo (`git clone [email protected]:gitpod-io/gitpod.git`), and `cd` to `./gitpod/test`
63-
2. Run the tests like so
64-
```console
65-
go test -v ./... \
66-
-kubeconfig=<path_to_kube_config_file> \
67-
-namespace=<namespace_where_gitpod_is_installed> \
68-
-username=<gitpod_user_with_oauth_setup> \
69-
-enterprise=<true|false> \
70-
-gitlab=<true|false>
71-
```
72-
3. If you want to run specific test, add `-run <test>` before `-kubeconfig` parameter.
53+
If you want to run an entire test suite, the easiest is to use `./test/run.sh`:
54+
55+
```sh
56+
# This will run all test suites
57+
./test/run.sh
58+
59+
# This will run only the webapp test suite
60+
./test/run.sh webapp
61+
```
62+
63+
If you're iterating on a single test, the easiest is to use `go test` directly. If your integration tests depends on having having a user token available, then you'll have to set USER_TOKEN manually (see run.sh on how to fetch the credentials that are used during our build)
64+
65+
```sh
66+
cd test
67+
go test -v ./... \
68+
-run <test> \
69+
-kubeconfig=/home/gitpod/.kube/config \
70+
-namespace=default \
71+
-username=<gitpod_user_with_oauth_setup> \
72+
-enterprise=<true|false> \
73+
-gitlab=<true|false>
74+
```
75+
76+
A concrete example would be
77+
78+
```sh
79+
cd test
80+
go test -v ./... \
81+
-run TestAdminBlockUser \
82+
-kubeconfig=/home/gitpod/.kube/config \
83+
-namespace=default
84+
```

0 commit comments

Comments
 (0)