Skip to content

Commit

Permalink
update e2e gitops for refactored test boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-mccoy committed Feb 8, 2022
1 parent 377c6ef commit a503d0e
Showing 1 changed file with 45 additions and 83 deletions.
128 changes: 45 additions & 83 deletions test/e2e/e2e_gitops_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,64 @@ package test

import (
"fmt"
"github.com/gruntwork-io/terratest/modules/aws"
"github.com/gruntwork-io/terratest/modules/ssh"
"github.com/gruntwork-io/terratest/modules/terraform"
"testing"

teststructure "github.com/gruntwork-io/terratest/modules/test-structure"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
)

func TestGitopsExample(t *testing.T) {
t.Parallel()

// Our SSH username, will change based on which AMI we use
username := "ubuntu"

// Copy the terraform folder to a temp directory so we can run multiple tests in parallel
tmpFolder := teststructure.CopyTerraformFolderToTemp(t, "..", "tf/public-ec2-instance")
e2e := NewE2ETest(t)

// At the end of the test, run `terraform destroy` to clean up any resources that were created
defer teststructure.RunTestStage(t, "TEARDOWN", func() {
teardown(t, tmpFolder)
})

// Deploy the terraform infra
teststructure.RunTestStage(t, "SETUP", func() {
setup(t, tmpFolder)
})
defer teststructure.RunTestStage(e2e.testing, "TEARDOWN", e2e.teardown)

// Upload the Zarf artifacts
teststructure.RunTestStage(t, "UPLOAD", func() {
terraformOptions := teststructure.LoadTerraformOptions(t, tmpFolder)
keyPair := teststructure.LoadEc2KeyPair(t, tmpFolder)

syncFileToRemoteServer(t, terraformOptions, keyPair, username, "../../build/zarf", fmt.Sprintf("/home/%s/build/zarf", username), "0700")
syncFileToRemoteServer(t, terraformOptions, keyPair, username, "../../build/zarf-init.tar.zst", fmt.Sprintf("/home/%s/build/zarf-init.tar.zst", username), "0600")
syncFileToRemoteServer(t, terraformOptions, keyPair, username, "../../build/zarf-package-gitops-service-data.tar.zst", fmt.Sprintf("/home/%s/build/zarf-package-gitops-service-data.tar.zst", username), "0600")
teststructure.RunTestStage(e2e.testing, "UPLOAD", func() {
e2e.syncFileToRemoteServer("../../build/zarf", fmt.Sprintf("/home/%s/build/zarf", e2e.username), "0700")
e2e.syncFileToRemoteServer("../../build/zarf-init.tar.zst", fmt.Sprintf("/home/%s/build/zarf-init.tar.zst", e2e.username), "0600")
e2e.syncFileToRemoteServer("../../build/zarf-package-gitops-service-data.tar.zst", fmt.Sprintf("/home/%s/build/zarf-package-gitops-service-data.tar.zst", e2e.username), "0600")
})

teststructure.RunTestStage(t, "TEST", func() {
terraformOptions := teststructure.LoadTerraformOptions(t, tmpFolder)
keyPair := teststructure.LoadEc2KeyPair(t, tmpFolder)

// Finally run the actual test
testGitopsExample(t, terraformOptions, keyPair, username)
// run `zarf init`
output, err := e2e.runSSHCommand("sudo bash -c 'cd /home/%s/build && ./zarf init --confirm --components management,logging,gitops-service --host 127.0.0.1'", e2e.username)
require.NoError(t, err, output)

// Make sure Gitea comes up cleanly
output, err = e2e.runSSHCommand(`bash -c '[[ $(curl -sfSL -o /dev/null -w '%%{http_code}' 'http://127.0.0.1:45003/explore/repos') == 200 ]]'`)
require.NoError(e2e.testing, err, output)

// Deploy the gitops example
output, err = e2e.runSSHCommand("sudo bash -c 'cd /home/%s/build && ./zarf package deploy zarf-package-gitops-service-data.tar.zst --confirm'", e2e.username)
require.NoError(t, err, output)

// Check for full git repo mirror(foo.git) from https://github.com/stefanprodan/podinfo.git
output, err = e2e.runSSHCommand("sudo bash -c 'cd /home/%s/build && git clone https://zarf-git-user:$(./zarf tools get-admin-password)@127.0.0.1/zarf-git-user/mirror__github.com__stefanprodan__podinfo.git'", e2e.username)
require.NoError(t, err, output)

// Check for tagged git repo mirror (foo.git@1.2.3) from https://github.com/defenseunicorns/zarf.git@v0.12.0
output, err = e2e.runSSHCommand("sudo bash -c 'cd /home/%s/build && git clone https://zarf-git-user:$(./zarf tools get-admin-password)@127.0.0.1/zarf-git-user/mirror__github.com__defenseunicorns__zarf.git'", e2e.username)
require.NoError(t, err, output)

// Check for correct tag
expectedTag := "v0.12.0\n"
output, err = e2e.runSSHCommand("sudo bash -c 'cd /home/%s/build/mirror__github.com__defenseunicorns__zarf && git tag'", e2e.username)
require.NoError(t, err, output)
assert.Equal(t, expectedTag, output, "Expected tag should match output")

// Check for correct commits
expectedCommits := "4fb0f14\ncd45237\n9ac3338"
output, err = e2e.runSSHCommand("sudo bash -c 'cd /home/%s/build/mirror__github.com__defenseunicorns__zarf && git log -3 --oneline --pretty=format:\"%%h\"'", e2e.username)
require.NoError(t, err, output)
assert.Equal(t, expectedCommits, output, "Expected commits should match output")

// Check for correct branches
expectedBranch := "* master\n"
output, err = e2e.runSSHCommand("sudo bash -c 'cd /home/%s/build/mirror__github.com__stefanprodan__podinfo && git branch --list'", e2e.username)
require.NoError(t, err, output)
assert.Equal(t, expectedBranch, output, "Expected Branch should match output")
})
}

func testGitopsExample(t *testing.T, terraformOptions *terraform.Options, keyPair *aws.Ec2Keypair, username string) {
// Run `terraform output` to get the value of an output variable
publicInstanceIP := terraform.Output(t, terraformOptions, "public_instance_ip")

// We're going to try to SSH to the instance IP, using the Key Pair we created earlier, and the user "ubuntu",
// as we know the Instance is running an Ubuntu AMI that has such a user
publicHost := ssh.Host{
Hostname: publicInstanceIP,
SshKeyPair: keyPair.KeyPair,
SshUserName: username,
}

// run `zarf init`
output, err := ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build && ./zarf init --confirm --components management,logging,gitops-service --host 127.0.0.1'", username))
require.NoError(t, err, output)

// Make sure Gitea comes up cleanly
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://127.0.0.1/api/v1/user\")\" != \"401\" ]]; do sleep 1; done' || false")
require.NoError(t, err, output)

// Deploy the gitops example
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build && ./zarf package deploy zarf-package-gitops-service-data.tar.zst --confirm'", username))
require.NoError(t, err, output)

// Check for full git repo mirror(foo.git) from https://github.com/stefanprodan/podinfo.git
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build && git clone https://zarf-git-user:$(./zarf tools get-admin-password)@127.0.0.1/zarf-git-user/mirror__github.com__stefanprodan__podinfo.git'", username))
require.NoError(t, err, output)

// Check for tagged git repo mirror (foo.git@1.2.3) from https://github.com/defenseunicorns/zarf.git@v0.12.0
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build && git clone https://zarf-git-user:$(./zarf tools get-admin-password)@127.0.0.1/zarf-git-user/mirror__github.com__defenseunicorns__zarf.git'", username))
require.NoError(t, err, output)

// Check for correct tag
expectedTag := "v0.12.0\n"
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build/mirror__github.com__defenseunicorns__zarf && git tag'", username))
require.NoError(t, err, output)
assert.Equal(t, expectedTag, output, "Expected tag should match output")

// Check for correct commits
expectedCommits := "4fb0f14\ncd45237\n9ac3338"
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build/mirror__github.com__defenseunicorns__zarf && git log -3 --oneline --pretty=format:\"%%h\"'", username))
require.NoError(t, err, output)
assert.Equal(t, expectedCommits, output, "Expected commits should match output")

// Check for correct branches
expectedBranch := "* master\n"
output, err = ssh.CheckSshCommandE(t, publicHost, fmt.Sprintf("sudo bash -c 'cd /home/%s/build/mirror__github.com__stefanprodan__podinfo && git branch --list'", username))
require.NoError(t, err, output)
assert.Equal(t, expectedBranch, output, "Expected Branch should match output")


}

0 comments on commit a503d0e

Please sign in to comment.