Skip to content

Commit

Permalink
E2E: move example test to use golangs stdlib test runner
Browse files Browse the repository at this point in the history
Our E2E "framework" has a bunch of features around test discovery and
standing up infra that were never completed or fully used, and we
ended up building out a large test suite that ignored all that in lieu
of Terraform-provided infrastructure for the last couple years.

This changeset is a proposal (and demonstration) for gradually
migrating our E2E tests off the framework code so that developers can
write fairly ordinary golang stdlib testing tests.
  • Loading branch information
tgross committed Mar 25, 2022
1 parent 38e92e5 commit acd68f8
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 41 deletions.
1 change: 0 additions & 1 deletion e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
_ "github.com/hashicorp/nomad/e2e/deployment"
_ "github.com/hashicorp/nomad/e2e/eval_priority"
_ "github.com/hashicorp/nomad/e2e/events"
_ "github.com/hashicorp/nomad/e2e/example"
_ "github.com/hashicorp/nomad/e2e/isolation"
_ "github.com/hashicorp/nomad/e2e/lifecycle"
_ "github.com/hashicorp/nomad/e2e/metrics"
Expand Down
35 changes: 35 additions & 0 deletions e2e/e2eutil/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package e2eutil

import (
"testing"

capi "github.com/hashicorp/consul/api"
napi "github.com/hashicorp/nomad/api"
vapi "github.com/hashicorp/vault/api"

"github.com/stretchr/testify/require"
)

// NomadClient creates a default Nomad client based on the env vars
// from the test environment. Fails the test if it can't be created
func NomadClient(t *testing.T) *napi.Client {
client, err := napi.NewClient(napi.DefaultConfig())
require.NoError(t, err, "could not create Nomad client")
return client
}

// ConsulClient creates a default Consul client based on the env vars
// from the test environment. Fails the test if it can't be created
func ConsulClient(t *testing.T) *capi.Client {
client, err := capi.NewClient(capi.DefaultConfig())
require.NoError(t, err, "could not create Consul client")
return client
}

// VaultClient creates a default Vault client based on the env vars
// from the test environment. Fails the test if it can't be created
func VaultClient(t *testing.T) *vapi.Client {
client, err := vapi.NewClient(vapi.DefaultConfig())
require.NoError(t, err, "could not create Vault client")
return client
}
17 changes: 17 additions & 0 deletions e2e/e2eutil/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"os/exec"
"regexp"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

// Register registers a jobspec from a file but with a unique ID.
Expand Down Expand Up @@ -231,3 +234,17 @@ func StopJob(jobID string, args ...string) error {
}
return err
}

// CleanupJobsAndGC stops and purges the list of jobIDs and runs a
// system gc. Returns a func so that the return value can be used
// in t.Cleanup
func CleanupJobsAndGC(t *testing.T, jobIDs *[]string) func() {
return func() {
for _, jobID := range *jobIDs {
err := StopJob(jobID, "-purge")
assert.NoError(t, err)
}
_, err := Command("nomad", "system", "gc")
assert.NoError(t, err)
}
}
4 changes: 4 additions & 0 deletions e2e/example/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package example

// This package contains only tests, so this is a placeholder file to
// make sure builds don't fail with "no non-test Go files in" errors
40 changes: 0 additions & 40 deletions e2e/example/example.go

This file was deleted.

50 changes: 50 additions & 0 deletions e2e/example/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package example

import (
"os"
"testing"

"github.com/stretchr/testify/require"

"github.com/hashicorp/nomad/e2e/e2eutil"
"github.com/hashicorp/nomad/helper/uuid"
)

func TestExample(t *testing.T) {
nomad := e2eutil.NomadClient(t)

e2eutil.WaitForLeader(t, nomad)
e2eutil.WaitForNodesReady(t, nomad, 2)

t.Run("TestExample_Simple", testExample_Simple)
t.Run("TestExample_WithCleanup", testExample_WithCleanup)
}

func testExample_Simple(t *testing.T) {
t.Logf("Logging %s", t.Name())
nomad := e2eutil.NomadClient(t)
jobs, _, err := nomad.Jobs().List(nil)
require.NoError(t, err)
require.Empty(t, jobs)
}

func testExample_WithCleanup(t *testing.T) {

t.Logf("Logging %s", t.Name())
nomad := e2eutil.NomadClient(t)

_, err := e2eutil.Command("nomad", "job", "init", "-short", "./input/example.nomad")
require.NoError(t, err, "failed to run `nomad job init -short`")
t.Cleanup(func() { os.Remove("input/example.nomad") })

jobIDs := []string{}
t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))

jobID := "example-" + uuid.Short()
jobIDs = append(jobIDs, jobID)
e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/example.nomad", jobID, "")

jobs, _, err := nomad.Jobs().List(nil)
require.NoError(t, err)
require.NotEmpty(t, jobs)
}
Empty file added e2e/example/input/.gitkeep
Empty file.

0 comments on commit acd68f8

Please sign in to comment.