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

[WIP] - Add e2e test suite #166

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ vendor/

# Ignore generated OpenAPI docs
openapi/client/docs/

# Ingore e2e binary and downloaded terraform
e2e/bin/*
e2e/nomad/
39 changes: 38 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ GIT_DIRTY=$$(test -n "`git status --porcelain`" && echo "+CHANGES" || true)
GIT_IMPORT="github.com/hashicorp/nomad-pack/internal/pkg/version"
GO_LDFLAGS="-s -w -X $(GIT_IMPORT).GitCommit=$(GIT_COMMIT)$(GIT_DIRTY)"

ifeq ($(CI),true)
$(info Running in a CI environment, verbose mode is disabled)
else
VERBOSE="true"
endif

.PHONY: dev
dev: GOPATH=$(shell go env GOPATH)
dev:
Expand All @@ -14,15 +20,17 @@ dev:
@cp ./bin/nomad-pack $(GOPATH)/bin/nomad-pack
@echo "==> Done"

.PHONY: test
test:
go test ./... -v -count=1

.PHONY: mod
mod:
go mod tidy

.PHONY: api
api:
go get github.com/hashicorp/nomad-openapi/v1
go get github.com/hashicorp/nomad-openapi

.PHONY: check
check: check-mod check-sdk
Expand All @@ -45,3 +53,32 @@ check-sdk: ## Checks the SDK is isolated
then echo " /sdk package depends the ^^ above internal packages. Remove such dependency"; \
exit 1; fi
@echo "==> Done"

.PHONY: e2e
e2e: GOPATH=$(shell go env GOPATH)
e2e: dev
@SECONDS=0
@echo $(GOPATH)
@echo "==> Building e2e test package"
@cd e2e && CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -o ./bin/nomad-pack-e2e
@cd e2e && cp ./bin/nomad-pack-e2e $(GOPATH)/bin/nomad-pack-e2e
@echo "==> Done"
@echo "==> Cloning Nomad"
@cd e2e && git clone https://github.com/hashicorp/nomad.git
@echo "==> Provisioning infrastructure with Terraform"
@cd e2e/nomad/e2e/terraform && terraform init && terraform apply -var="nomad_acls=true" -var="nomad_version=1.1.6" --auto-approve
@echo "==> Setting environment from Terraform output"
@cd e2e/nomad/e2e/terraform && $(terraform output --raw environment)
@echo "==> Running Nomad E2E test suites:"
@cd e2e && NOMAD_E2E=1 go test \
$(if $(ENABLE_RACE),-race) $(if $(VERBOSE),-v) \
-timeout=900s \
-count=1 \
./...
@duration=$SECONDS
@echo "==> E2E Tests complete in $(($duration / 60)) minutes and $(($duration % 60)) seconds."
@echo "==> De-provisioning infrastructure with Terraform"
@cd e2e && e2e/nomad/e2e/terraform && terraform destroy --auto-approve
@echo "==> Removing Nomad download"
@cd e2e && rm -rf nomad

86 changes: 30 additions & 56 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -35,7 +34,7 @@ func TestJobRun(t *testing.T) {
clearJob(t, &cache.PackConfig{Name: testPack})
}()

exitCode := runCmd().Run([]string{testPack})
exitCode := RunCmd().Run([]string{testPack})
require.Equal(t, 0, exitCode)
}

Expand All @@ -44,19 +43,19 @@ func TestJobRunConflictingDeployment(t *testing.T) {
testInit(t)

// Register the initial pack
exitCode := runCmd().Run([]string{testPack})
exitCode := RunCmd().Run([]string{testPack})
defer func() {
clearJob(t, &cache.PackConfig{Name: testPack})
}()

// deploymentName := runCmd.deploymentName
// deploymentName := RunCmd.deploymentName
require.Equal(t, 0, exitCode)

exitCode = runCmd().Run([]string{testPack, "--name=with-name"})
exitCode = RunCmd().Run([]string{testPack, "--name=with-name"})
require.Equal(t, 1, exitCode)

// Confirm that it's still possible to update the existing pack
exitCode = runCmd().Run([]string{testPack})
exitCode = RunCmd().Run([]string{testPack})
require.Equal(t, 0, exitCode)
}

Expand All @@ -72,7 +71,7 @@ func TestJobRunConflictingNonPackJob(t *testing.T) {
}()

// Now try to register the pack
exitCode := runCmd().Run([]string{testPack})
exitCode := RunCmd().Run([]string{testPack})
require.Equal(t, 1, exitCode)
}

Expand All @@ -87,14 +86,14 @@ func TestJobRunConflictingJobWithMeta(t *testing.T) {
nomadExec(t, "run", "../fixtures/simple-with-meta.nomad")

// Now try to register
exitCode := runCmd().Run([]string{testPack})
exitCode := RunCmd().Run([]string{testPack})
require.Equal(t, 1, exitCode)
}

func TestJobRunFails(t *testing.T) {
testInit(t)

exitCode := runCmd().Run([]string{"fake-job"})
exitCode := RunCmd().Run([]string{"fake-job"})
require.Equal(t, 1, exitCode)

reset()
Expand All @@ -103,7 +102,7 @@ func TestJobRunFails(t *testing.T) {
func TestJobPlan(t *testing.T) {
testInit(t)

exitCode := planCmd().Run([]string{testPack})
exitCode := PlanCmd().Run([]string{testPack})
// Should return 1 indicating an allocation will be placed
require.Equal(t, 1, exitCode)

Expand All @@ -119,10 +118,10 @@ func TestJobPlanConflictingDeployment(t *testing.T) {
}()

// Register the initial pack
exitCode := runCmd().Run([]string{testPack})
exitCode := RunCmd().Run([]string{testPack})
require.Equal(t, 0, exitCode)

exitCode = runCmd().Run([]string{testPack, testRefFlag})
exitCode = RunCmd().Run([]string{testPack, testRefFlag})
require.Equal(t, 1, exitCode)

reset()
Expand All @@ -139,7 +138,7 @@ func TestJobPlanConflictingNonPackJob(t *testing.T) {
nomadExec(t, "run", "../fixtures/simple.nomad")

// Now try to plan the pack
exitCode := planCmd().Run([]string{testPack})
exitCode := PlanCmd().Run([]string{testPack})
require.Equal(t, 255, exitCode)

reset()
Expand All @@ -148,10 +147,10 @@ func TestJobPlanConflictingNonPackJob(t *testing.T) {
func TestJobStop(t *testing.T) {
testInit(t)

exitCode := runCmd().Run([]string{testPack})
exitCode := RunCmd().Run([]string{testPack})
require.Equal(t, 0, exitCode)

exitCode = stopCmd().Run([]string{testPack, "--purge=true"})
exitCode = StopCmd().Run([]string{testPack, "--purge=true"})
require.Equal(t, 0, exitCode)

reset()
Expand Down Expand Up @@ -198,12 +197,12 @@ func TestJobStopConflicts(t *testing.T) {
} else {
deploymentName := fmt.Sprintf("--name=%s", c.deploymentName)
varJobName := fmt.Sprintf("--var=job_name=%s", c.jobName)
exitCode := runCmd().Run([]string{c.packName, deploymentName, varJobName})
exitCode := RunCmd().Run([]string{c.packName, deploymentName, varJobName})
require.Equal(t, 0, exitCode)
}

// Try to stop job
exitCode := stopCmd().Run([]string{c.packName})
exitCode := StopCmd().Run([]string{c.packName})
require.Equal(t, 1, exitCode)
})
}
Expand All @@ -216,10 +215,10 @@ func TestJobStopConflicts(t *testing.T) {
func TestJobDestroy(t *testing.T) {
testInit(t)

exitCode := runCmd().Run([]string{testPack})
exitCode := RunCmd().Run([]string{testPack})
require.Equal(t, 0, exitCode)

exitCode = destroyCmd().Run([]string{testPack})
exitCode = DestroyCmd().Run([]string{testPack})
require.Equal(t, 0, exitCode)

// Assert job no longer queryable
Expand All @@ -237,23 +236,23 @@ func TestJobDestroyWithOverrides(t *testing.T) {

jobNames := []string{"foo", "bar"}
for _, j := range jobNames {
exitCode := runCmd().Run([]string{testPack, `--var=job_name=` + j})
exitCode := RunCmd().Run([]string{testPack, `--var=job_name=` + j})
require.Equal(t, 0, exitCode)
}

// Stop nonexistent job
exitCode := destroyCmd().Run([]string{testPack, "--var=job_name=baz"})
exitCode := DestroyCmd().Run([]string{testPack, "--var=job_name=baz"})
require.Equal(t, 1, exitCode)

// Stop job with var override
exitCode = destroyCmd().Run([]string{testPack, "--var=job_name=foo"})
exitCode = DestroyCmd().Run([]string{testPack, "--var=job_name=foo"})
require.Equal(t, 0, exitCode)

// Assert job "bar" still exists
nomadExec(t, "status", "bar")

// Stop job with no overrides passed
exitCode = destroyCmd().Run([]string{testPack})
exitCode = DestroyCmd().Run([]string{testPack})
require.Equal(t, 0, exitCode)

// Assert job bar is gone
Expand All @@ -268,11 +267,11 @@ func TestFlagProvidedButNotDefined(t *testing.T) {
// There is no job flag. This tests that adding an unspecified flag does not
// create an invalid memory address error
// Posix case
exitCode := runCmd().Run([]string{"nginx", "--job=provided-but-not-defined"})
exitCode := RunCmd().Run([]string{"nginx", "--job=provided-but-not-defined"})
require.Equal(t, 1, exitCode)

// std go case
exitCode = runCmd().Run([]string{"-job=provided-but-not-defined", "nginx"})
exitCode = RunCmd().Run([]string{"-job=provided-but-not-defined", "nginx"})
require.Equal(t, 1, exitCode)

reset()
Expand All @@ -284,7 +283,7 @@ func TestStatus(t *testing.T) {
clearJob(t, &cache.PackConfig{Name: testPack})
}()

exitCode := runCmd().Run([]string{testPack})
exitCode := RunCmd().Run([]string{testPack})
require.Equal(t, 0, exitCode)

cases := []struct {
Expand All @@ -311,7 +310,7 @@ func TestStatus(t *testing.T) {

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
exitCode = statusCmd().Run(c.args)
exitCode = StatusCmd().Run(c.args)
require.Equal(t, 0, exitCode)
})
}
Expand All @@ -334,31 +333,6 @@ var nomadAddr string
var testPack = "simple_service"
var testRefFlag = "--ref=48eb7d5"

// reduce boilerplate copy pasta with a factory method.
func baseCmd() *baseCommand {
return &baseCommand{Ctx: context.Background()}
}

func planCmd() *PlanCommand {
return &PlanCommand{baseCommand: baseCmd()}
}

func runCmd() *RunCommand {
return &RunCommand{baseCommand: baseCmd()}
}

func destroyCmd() *DestroyCommand {
return &DestroyCommand{&StopCommand{baseCommand: baseCmd()}}
}

func statusCmd() *StatusCommand {
return &StatusCommand{baseCommand: baseCmd()}
}

func stopCmd() *StopCommand {
return &StopCommand{baseCommand: baseCmd()}
}

// Save the current machine's NOMAD_ADDR so that tests can reset developer.
// environment. Added to every test to allow one of ad hoc testing.
func testInit(t *testing.T) {
Expand Down Expand Up @@ -396,18 +370,18 @@ func clearJob(t *testing.T, cfg *cache.PackConfig) {
reset()
}

func nomadExec(t *testing.T, args ...string) {
func nomadExpectErr(t *testing.T, args ...string) {
nomadPath, err := exec.LookPath("nomad")
require.NoError(t, err)
nomadCmd := exec.Command(nomadPath, args...)
err = nomadCmd.Run()
require.NoError(t, err)
require.Error(t, err)
}

func nomadExpectErr(t *testing.T, args ...string) {
func nomadExec(t *testing.T, args ...string) {
nomadPath, err := exec.LookPath("nomad")
require.NoError(t, err)
nomadCmd := exec.Command(nomadPath, args...)
err = nomadCmd.Run()
require.Error(t, err)
require.NoError(t, err)
}
43 changes: 43 additions & 0 deletions cli/testutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cli

import (
"context"
"os/exec"
)

// reduce boilerplate copy pasta with a factory method.
func baseCmd() *baseCommand {
return &baseCommand{Ctx: context.Background()}
}

func PlanCmd() *PlanCommand {
return &PlanCommand{baseCommand: baseCmd()}
}

func RunCmd() *RunCommand {
return &RunCommand{baseCommand: baseCmd()}
}

func DestroyCmd() *DestroyCommand {
return &DestroyCommand{&StopCommand{baseCommand: baseCmd()}}
}

func StatusCmd() *StatusCommand {
return &StatusCommand{baseCommand: baseCmd()}
}

func StopCmd() *StopCommand {
return &StopCommand{baseCommand: baseCmd()}
}

func NomadExec(args ...string) error {
nomadPath, err := exec.LookPath("nomad")
if err != nil {
return err

}

nomadCmd := exec.Command(nomadPath, args...)
err = nomadCmd.Run()
return err
}
Loading