Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve jest test patterns #73

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { spawnSync } from "child_process";

export function zarfExec(args: string[], captureOutput = false) {
const argString = args.join(" ")
return spawnSync(
`uds zarf ${argString}`, {
// Run command in a shell
shell: true,
// Print the command output directly to the shell if we don'e want to capture it (useful for debugging)
stdio: captureOutput ? undefined : 'inherit'
});
}

export async function retry(funcCb: Function, retries = 3, timeout = 3000) {
let result = false
for (let i = 0; i < retries; i++) {
await new Promise(r => setTimeout(r, timeout))
result = await funcCb()
if (result) {
break
}
}
return result
}
47 changes: 24 additions & 23 deletions test/journey/pipeline-run.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
import { expect, test} from '@jest/globals';
import { K8s, kind } from "kubernetes-fluent-client";
import { spawnSync } from "child_process";
import { zarfExec, retry } from "../common";

test('test kicking off a pipeline run', async () => {
// Get the root password for GitLab
const rootPasswordSecret = await K8s(kind.Secret).InNamespace("gitlab").Get("gitlab-gitlab-initial-root-password")
const rootPassword = atob(rootPasswordSecret.data!.password)

// Create a test repository in GitLab using Zarf
spawnSync(`uds zarf package create package --confirm`, {
shell: true, // Run command in a shell
stdio: 'inherit' // Print the command output directly to the shell (useful for debugging)
});
spawnSync(
`uds zarf package mirror-resources zarf-package-gitlab-runner-test-amd64-0.0.1.tar.zst \
--git-url https://gitlab.uds.dev/ --git-push-username root --git-push-password ${rootPassword} --confirm`, {
shell: true, // Run command in a shell
stdio: 'inherit' // Print the command output directly to the shell (useful for debugging)
});
zarfExec(["package", "create", "package", "--confirm"]);
zarfExec([
"package",
"mirror-resources",
"zarf-package-gitlab-runner-test-amd64-0.0.1.tar.zst",
"--git-url", "https://gitlab.uds.dev/",
"--git-push-username", "root",
"--git-push-password", rootPassword,
"--confirm"
]);

// Get the toolbox pod and add a token to the root GitLab user
const tokenName = `if-you-see-me-in-production-something-is-horribly-wrong-${new Date()}`
const toolboxPods = await K8s(kind.Pod).InNamespace("gitlab").WithLabel("app", "toolbox").Get()
const toolboxPod = toolboxPods.items.at(0)
spawnSync(
`uds zarf tools kubectl --namespace gitlab exec -i ${toolboxPod?.metadata?.name} -- \
gitlab-rails runner "token = User.find_by_username('root').personal_access_tokens.create(scopes: ['api', 'admin_mode'], name: 'Root Test Token', expires_at: 1.days.from_now); token.set_token('${tokenName}'); token.save!"`, {
shell: true, // Run command in a shell
stdio: 'inherit' // Print the command output directly to the shell (useful for debugging)
});
zarfExec(["tools",
"kubectl",
"--namespace", "gitlab",
"exec",
"-i",
toolboxPod?.metadata?.name!,
"--",
`gitlab-rails runner "token = User.find_by_username('root').personal_access_tokens.create(scopes: ['api', 'admin_mode'], name: 'Root Test Token', expires_at: 1.days.from_now); token.set_token('${tokenName}'); token.save!"`
]);

const headers: HeadersInit = [["PRIVATE-TOKEN", tokenName]]

Expand All @@ -46,9 +49,7 @@ test('test kicking off a pipeline run', async () => {
expect(runnerResp.status).toBe(200)

// Check that the pipeline actually ran successfully
let foundTheKitteh = false
for (let i = 0; i < 7; i++) {
await new Promise(r => setTimeout(r, 7000))
let foundTheKitteh = await retry(async () => {
const jobIDResp = await (await fetch(`https://gitlab.uds.dev/api/v4/projects/1/jobs`, { headers })).json()

// Print the job response (useful for debugging)
Expand All @@ -62,11 +63,11 @@ test('test kicking off a pipeline run', async () => {
console.log(jobLog)

if (jobLog.indexOf("Hello Kitteh") > -1) {
foundTheKitteh = true
break
return true
}
}
}
return false
}, 7, 7000);
expect(foundTheKitteh).toBe(true)

}, 90000);
18 changes: 7 additions & 11 deletions test/journey/registration.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { expect, test} from '@jest/globals';
import { K8s, kind } from "kubernetes-fluent-client";
import { spawnSync } from "child_process";
import { zarfExec, retry } from "../common";

test('test registration of the runner succeeded', async () => {
let foundRegistrationSuccess = false
for (let i = 0; i < 3; i++) {
await new Promise(r => setTimeout(r, 3000))
let foundRegistrationSuccess = await retry(async () => {
const runnerPods = await K8s(kind.Pod).InNamespace("gitlab-runner").WithLabel("app", "gitlab-runner").Get()
if (runnerPods.items.at(0)?.status?.phase === "Running") {
const podName = runnerPods.items.at(0)?.metadata?.name
const runnerLogs = spawnSync(
`uds zarf tools kubectl logs -n gitlab-runner ${podName} -c gitlab-runner`, {
shell: true, // Run command in a shell
});
const podName = runnerPods.items.at(0)?.metadata?.name!
const runnerLogs = zarfExec(["tools", "kubectl", "logs", "-n", "gitlab-runner", podName, "-c", "gitlab-runner"], true);
if (runnerLogs.stdout.indexOf("Registering runner... succeeded") > -1) {
foundRegistrationSuccess = true
return true
}
}
}
return false
})
expect(foundRegistrationSuccess).toBe(true)
});
Loading