Skip to content

Commit 0ca3d5f

Browse files
RothAndrewjeff-mccoy
authored andcommitted
Add a 2nd E2E test (zarf-dev#110)
Signed-off-by: Jeff McCoy <code@jeffm.us>
1 parent 854b99a commit 0ca3d5f

File tree

4 files changed

+185
-6
lines changed

4 files changed

+185
-6
lines changed

.github/workflows/test-command.yml

+96
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,99 @@ jobs:
263263
GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
264264
GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }}
265265
GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }}
266+
267+
# Run the Game E2E test
268+
e2e-general-cli:
269+
runs-on: ubuntu-latest
270+
needs: [ parse, build ]
271+
if: needs.parse.outputs.run-e2e == 'true'
272+
container: cloudposse/test-harness:latest
273+
steps:
274+
# Update GitHub status for pending pipeline run
275+
- name: "Update GitHub Status for pending"
276+
uses: docker://cloudposse/github-status-updater
277+
with:
278+
args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}"
279+
env:
280+
GITHUB_TOKEN: ${{ secrets.PAT }}
281+
GITHUB_STATE: pending
282+
GITHUB_CONTEXT: "/test e2e - General CLI"
283+
GITHUB_DESCRIPTION: "started by @${{ github.event.client_payload.github.actor }}"
284+
GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
285+
GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }}
286+
GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }}
287+
288+
# Checkout the code from GitHub Pull Request
289+
- name: "Checkout the code"
290+
uses: actions/checkout@v2
291+
with:
292+
token: ${{ secrets.PAT }}
293+
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
294+
ref: ${{ github.event.client_payload.pull_request.head.ref }}
295+
296+
# Download the built artifacts
297+
- name: "Download the built artifacts"
298+
uses: actions/download-artifact@v2
299+
300+
- name: "Run E2E tests"
301+
shell: bash -x -e -o pipefail {0}
302+
env:
303+
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_DEFENSEUNICORNS_COMMERCIAL_SA_ZARF }}
304+
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_DEFENSEUNICORNS_COMMERCIAL_SA_ZARF }}
305+
AWS_DEFAULT_REGION: us-east-1
306+
run: |
307+
# cloudposse/test-harness has golang 1.15, we need 1.16. This is the easiest way I know to do it. This should definitely be revisited and cleaned up.
308+
git clone --branch v0.8.0 --depth 1 https://github.com/asdf-vm/asdf.git $HOME/.asdf
309+
source ~/.asdf/asdf.sh
310+
export PATH="$HOME/.asdf/bin:$PATH"
311+
asdf plugin-add golang https://github.com/kennyp/asdf-golang.git
312+
asdf install golang 1.16.7
313+
asdf global golang 1.16.7
314+
export GOPATH="$HOME/go"
315+
export PATH="$PATH:$GOPATH/bin"
316+
chmod +x build/zarf
317+
make test-cloud-e2e-general-cli
318+
319+
# Update GitHub status for failing pipeline run
320+
- name: "Update GitHub Status for failure"
321+
if: ${{ failure() }}
322+
uses: docker://cloudposse/github-status-updater
323+
with:
324+
args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}"
325+
env:
326+
GITHUB_TOKEN: ${{ secrets.PAT }}
327+
GITHUB_STATE: failure
328+
GITHUB_CONTEXT: "/test e2e - General CLI"
329+
GITHUB_DESCRIPTION: "run failed"
330+
GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
331+
GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }}
332+
GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }}
333+
334+
# Update GitHub status for successful pipeline run
335+
- name: "Update GitHub Status for success"
336+
uses: docker://cloudposse/github-status-updater
337+
with:
338+
args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}"
339+
env:
340+
GITHUB_TOKEN: ${{ secrets.PAT }}
341+
GITHUB_STATE: success
342+
GITHUB_CONTEXT: "/test e2e - General CLI"
343+
GITHUB_DESCRIPTION: "run passed"
344+
GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
345+
GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }}
346+
GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }}
347+
348+
# Update GitHub status for cancelled pipeline run
349+
- name: "Update GitHub Status for cancelled"
350+
if: ${{ cancelled() }}
351+
uses: docker://cloudposse/github-status-updater
352+
with:
353+
args: "-action update_state -ref ${{ github.event.client_payload.pull_request.head.sha }} -repo ${{ github.event.client_payload.github.payload.repository.name }}"
354+
env:
355+
GITHUB_TOKEN: ${{ secrets.PAT }}
356+
GITHUB_STATE: error
357+
GITHUB_CONTEXT: "/test e2e - General CLI"
358+
GITHUB_DESCRIPTION: "run cancelled"
359+
GITHUB_TARGET_URL: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
360+
GITHUB_REF: ${{ github.event.client_payload.pull_request.head.ref }}
361+
GITHUB_OWNER: ${{ github.event.client_payload.github.payload.repository.owner.login }}

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,9 @@ package-example-game: ## Create the Doom example
6666
test-cloud-e2e-example-game: ## Runs the Doom game as an E2E test in the cloud. Requires access to an AWS account. Costs money. Make sure you ran the `build-cli`, `init-package`, and `package-example-game` targets first
6767
cd test/e2e && go test ./... -run TestE2eExampleGame -v -timeout 1200s
6868

69+
.PHONY: test-cloud-e2e-general-cli
70+
test-cloud-e2e-general-cli: ## Runs tests of the CLI that don't need a cluster
71+
cd test/e2e && go test ./... -run TestGeneralCli -v -timeout 1200s
72+
6973
.PHONY: test-e2e
7074
test-e2e: package-example-game test-cloud-e2e-example-game ## DEPRECATED - to be replaced by individual e2e test targets

test/e2e/e2e_example_game_test.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func TestE2eExampleGame(t *testing.T) {
4444
keyPair := teststructure.LoadEc2KeyPair(t, tmpFolder)
4545

4646
// Finally run the actual test
47-
test(t, terraformOptions, keyPair,username)
47+
testGameExample(t, terraformOptions, keyPair,username)
4848
})
4949
}
5050

51-
func test(t *testing.T, terraformOptions *terraform.Options, keyPair *aws.Ec2Keypair, username string) {
51+
func testGameExample(t *testing.T, terraformOptions *terraform.Options, keyPair *aws.Ec2Keypair, username string) {
5252
// Run `terraform output` to get the value of an output variable
5353
publicInstanceIP := terraform.Output(t, terraformOptions, "public_instance_ip")
5454

@@ -65,18 +65,22 @@ func test(t *testing.T, terraformOptions *terraform.Options, keyPair *aws.Ec2Key
6565
require.NoError(t, err, output)
6666

6767
// run `zarf init`
68-
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("cd /home/%s/build && sudo ./zarf init --confirm --components management --host localhost", username))
68+
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build && ./zarf init --confirm --components management --host localhost'", username))
6969
require.NoError(t, err, output)
7070

7171
// Wait until the Docker registry is ready
72-
output, err = ssh.CheckSshCommandE(t, publicHost, "curl -sfSL --retry 15 --retry-connrefused --retry-delay 10 -o /dev/null -w \"%{http_code}\" \"https://localhost/v2/\"")
72+
output, err = ssh.CheckSshCommandE(t, publicHost, "timeout 300 bash -c 'while [[ \"$(curl -sfSL --retry 15 --retry-connrefused --retry-delay 5 -o /dev/null -w \"%{http_code}\" \"https://localhost/v2/\")\" != \"200\" ]]; do sleep 1; done' || false")
7373
require.NoError(t, err, output)
7474

7575
// Deploy the game
76-
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("cd /home/%s/build && sudo ./zarf package deploy zarf-package-appliance-demo-doom.tar.zst --confirm", username))
76+
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build && ./zarf package deploy zarf-package-appliance-demo-doom.tar.zst --confirm'", username))
7777
require.NoError(t, err, output)
7878

7979
// Wait for the game to be live. Right now we're just checking that `curl` returns 0. It can be enhanced by scraping the HTML that gets returned or something.
80-
output, err = ssh.CheckSshCommandE(t, publicHost, "timeout 60 bash -c 'while [[ \"$(curl -sfSL --retry 15 --retry-connrefused --retry-delay 5 -o /dev/null -w \"%{http_code}\" \"https://localhost\")\" != \"200\" ]]; do sleep 1; done' || false")
80+
output, err = ssh.CheckSshCommandE(t, publicHost, "timeout 300 bash -c 'while [[ \"$(curl -sfSL --retry 15 --retry-connrefused --retry-delay 5 -o /dev/null -w \"%{http_code}\" \"https://localhost\")\" != \"200\" ]]; do sleep 1; done' || false")
81+
require.NoError(t, err, output)
82+
83+
// Run `zarf destroy` to make sure that works correctly
84+
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build && ./zarf destroy --confirm'", username))
8185
require.NoError(t, err, output)
8286
}

test/e2e/e2e_general_cli_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package test
2+
3+
import (
4+
"fmt"
5+
"github.com/gruntwork-io/terratest/modules/aws"
6+
"github.com/gruntwork-io/terratest/modules/ssh"
7+
"github.com/gruntwork-io/terratest/modules/terraform"
8+
teststructure "github.com/gruntwork-io/terratest/modules/test-structure"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
"testing"
12+
)
13+
14+
func TestGeneralCli(t *testing.T) {
15+
t.Parallel()
16+
17+
// Our SSH username, will change based on which AMI we use
18+
username := "ubuntu"
19+
20+
// Copy the terraform folder to a temp directory so we can run multiple tests in parallel
21+
tmpFolder := teststructure.CopyTerraformFolderToTemp(t, "..", "tf/public-ec2-instance")
22+
23+
// At the end of the test, run `terraform destroy` to clean up any resources that were created
24+
defer teststructure.RunTestStage(t, "TEARDOWN", func() {
25+
teardown(t, tmpFolder)
26+
})
27+
28+
// Deploy the terraform infra
29+
teststructure.RunTestStage(t, "SETUP", func() {
30+
setup(t, tmpFolder)
31+
})
32+
33+
// Upload the Zarf artifacts
34+
teststructure.RunTestStage(t, "UPLOAD", func() {
35+
terraformOptions := teststructure.LoadTerraformOptions(t, tmpFolder)
36+
keyPair := teststructure.LoadEc2KeyPair(t, tmpFolder)
37+
38+
syncFileToRemoteServer(t, terraformOptions, keyPair, username, "../../build/zarf", fmt.Sprintf("/home/%s/build/zarf", username), "0700")
39+
})
40+
41+
teststructure.RunTestStage(t, "TEST", func() {
42+
terraformOptions := teststructure.LoadTerraformOptions(t, tmpFolder)
43+
keyPair := teststructure.LoadEc2KeyPair(t, tmpFolder)
44+
45+
// Finally run the actual test
46+
testGeneralCliStuff(t, terraformOptions, keyPair,username)
47+
})
48+
}
49+
50+
func testGeneralCliStuff(t *testing.T, terraformOptions *terraform.Options, keyPair *aws.Ec2Keypair, username string) {
51+
// Run `terraform output` to get the value of an output variable
52+
publicInstanceIP := terraform.Output(t, terraformOptions, "public_instance_ip")
53+
54+
// We're going to try to SSH to the instance IP, using the Key Pair we created earlier, and the user "ubuntu",
55+
// as we know the Instance is running an Ubuntu AMI that has such a user
56+
publicHost := ssh.Host{
57+
Hostname: publicInstanceIP,
58+
SshKeyPair: keyPair.KeyPair,
59+
SshUserName: username,
60+
}
61+
62+
// Test `zarf prepare sha256sum` for a local asset
63+
expectedShasum := "61b50898f982d015ed87093ba822de0fe011cec6dd67db39f99d8c56391a6109\n"
64+
output,err := ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("cd /home/%s/build && echo 'random test data 🦄' > shasum-test-file", username))
65+
require.NoError(t, err, output)
66+
output,err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("cd /home/%s/build && ./zarf prepare sha256sum shasum-test-file 2> /dev/null", username))
67+
require.NoError(t, err, output)
68+
assert.Equal(t, expectedShasum, output, "The expected SHASUM should equal the actual SHASUM")
69+
70+
// Test `zarf prepare sha256sum` for a remote asset
71+
expectedShasum = "c3cdea0573ba5a058ec090b5d2683bf398e8b1614c37ec81136ed03b78167617\n"
72+
output,err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("cd /home/%s/build && ./zarf prepare sha256sum https://zarf-public.s3-us-gov-west-1.amazonaws.com/pipelines/zarf-prepare-shasum-remote-test-file.txt 2> /dev/null", username))
73+
require.NoError(t, err, output)
74+
assert.Equal(t, expectedShasum, output, "The expected SHASUM should equal the actual SHASUM")
75+
}

0 commit comments

Comments
 (0)