-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
E2E: move example test to use golangs stdlib test runner (#12383)
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
Showing
8 changed files
with
121 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
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()) | ||
out, err := e2eutil.Command("nomad", "node", "status") | ||
require.NoError(t, err, "failed to run `nomad node status`") | ||
|
||
rows, err := e2eutil.ParseColumns(out) | ||
require.NoError(t, err, "failed to parse `nomad node status`") | ||
for _, row := range rows { | ||
require.Equal(t, "ready", row["Status"]) | ||
} | ||
} | ||
|
||
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.