From 059c89dff0732e5e60426e1150e1a8807aff243e Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Tue, 26 Apr 2022 14:28:20 -0400 Subject: [PATCH] E2E: move volume mounts test to use golang's stdlib test runner (#12788) Part of ongoing work to remove the old E2E framework code. --- e2e/e2e_test.go | 5 +- e2e/volumes/doc.go | 4 ++ e2e/volumes/volumes.go | 134 ------------------------------------ e2e/volumes/volumes_test.go | 107 ++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 135 deletions(-) create mode 100644 e2e/volumes/doc.go delete mode 100644 e2e/volumes/volumes.go create mode 100644 e2e/volumes/volumes_test.go diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index b2ee48733..ecdacb4cc 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -15,7 +15,6 @@ import ( _ "github.com/hashicorp/nomad/e2e/consultemplate" _ "github.com/hashicorp/nomad/e2e/csi" _ "github.com/hashicorp/nomad/e2e/deployment" - _ "github.com/hashicorp/nomad/e2e/disconnectedclients" _ "github.com/hashicorp/nomad/e2e/eval_priority" _ "github.com/hashicorp/nomad/e2e/events" _ "github.com/hashicorp/nomad/e2e/isolation" @@ -40,6 +39,10 @@ import ( _ "github.com/hashicorp/nomad/e2e/spread" _ "github.com/hashicorp/nomad/e2e/taskevents" _ "github.com/hashicorp/nomad/e2e/vaultsecrets" + + // these are no longer on the old framework but by importing them + // we get a quick check that they compile on every commit + _ "github.com/hashicorp/nomad/e2e/disconnectedclients" _ "github.com/hashicorp/nomad/e2e/volumes" ) diff --git a/e2e/volumes/doc.go b/e2e/volumes/doc.go new file mode 100644 index 000000000..be5561ab3 --- /dev/null +++ b/e2e/volumes/doc.go @@ -0,0 +1,4 @@ +package volumes + +// 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 diff --git a/e2e/volumes/volumes.go b/e2e/volumes/volumes.go deleted file mode 100644 index 4846a0fad..000000000 --- a/e2e/volumes/volumes.go +++ /dev/null @@ -1,134 +0,0 @@ -package volumes - -import ( - "fmt" - "os" - "time" - - "github.com/hashicorp/nomad/api" - e2e "github.com/hashicorp/nomad/e2e/e2eutil" - "github.com/hashicorp/nomad/e2e/framework" - "github.com/hashicorp/nomad/helper/uuid" - "github.com/hashicorp/nomad/jobspec" - "github.com/hashicorp/nomad/testutil" -) - -const ns = "" - -type VolumesTest struct { - framework.TC - jobIDs []string -} - -func init() { - framework.AddSuites(&framework.TestSuite{ - Component: "Volumes", - CanRunLocal: true, - Cases: []framework.TestCase{ - new(VolumesTest), - }, - }) -} - -func (tc *VolumesTest) BeforeAll(f *framework.F) { - e2e.WaitForLeader(f.T(), tc.Nomad()) - e2e.WaitForNodesReady(f.T(), tc.Nomad(), 1) -} - -func (tc *VolumesTest) AfterEach(f *framework.F) { - if os.Getenv("NOMAD_TEST_SKIPCLEANUP") == "1" { - return - } - - for _, id := range tc.jobIDs { - err := e2e.StopJob(id, "-purge") - f.Assert().NoError(err) - } - tc.jobIDs = []string{} - - _, err := e2e.Command("nomad", "system", "gc") - f.Assert().NoError(err) -} - -// TestVolumeMounts exercises host volume and Docker volume functionality for -// the exec and docker task driver, particularly around mounting locations -// within the container and how this is exposed to the user. -func (tc *VolumesTest) TestVolumeMounts(f *framework.F) { - - jobID := "test-node-drain-" + uuid.Generate()[0:8] - f.NoError(e2e.Register(jobID, "volumes/input/volumes.nomad")) - tc.jobIDs = append(tc.jobIDs, jobID) - - expected := []string{"running"} - f.NoError(e2e.WaitForAllocStatusExpected(jobID, ns, expected), "job should be running") - - allocs, err := e2e.AllocsForJob(jobID, ns) - f.NoError(err, "could not get allocs for job") - allocID := allocs[0]["ID"] - nodeID := allocs[0]["Node ID"] - - cmdToExec := fmt.Sprintf("cat /tmp/foo/%s", allocID) - - out, err := e2e.AllocExec(allocID, "docker_task", cmdToExec, ns, nil) - f.NoError(err, "could not exec into task: docker_task") - f.Equal(allocID+"\n", out, "alloc data is missing from docker_task") - - out, err = e2e.AllocExec(allocID, "exec_task", cmdToExec, ns, nil) - f.NoError(err, "could not exec into task: exec_task") - f.Equal(out, allocID+"\n", "alloc data is missing from exec_task") - - err = e2e.StopJob(jobID) - f.NoError(err, "could not stop job") - - // modify the job so that we make sure it's placed back on the same host. - // we want to be able to verify that the data from the previous alloc is - // still there - job, err := jobspec.ParseFile("volumes/input/volumes.nomad") - f.NoError(err) - job.ID = &jobID - job.Constraints = []*api.Constraint{ - { - LTarget: "${node.unique.id}", - RTarget: nodeID, - Operand: "=", - }, - } - _, _, err = tc.Nomad().Jobs().Register(job, nil) - f.NoError(err, "could not register updated job") - - testutil.WaitForResultRetries(5000, func() (bool, error) { - time.Sleep(time.Millisecond * 100) - allocs, err = e2e.AllocsForJob(jobID, ns) - if err != nil { - return false, err - } - if len(allocs) < 2 { - return false, fmt.Errorf("no new allocation for %v: %v", jobID, allocs) - } - - return true, nil - }, func(e error) { - f.NoError(e, "failed to get new alloc") - - }) - - newAllocID := allocs[0]["ID"] - - newCmdToExec := fmt.Sprintf("cat /tmp/foo/%s", newAllocID) - - out, err = e2e.AllocExec(newAllocID, "docker_task", cmdToExec, ns, nil) - f.NoError(err, "could not exec into task: docker_task") - f.Equal(out, allocID+"\n", "previous alloc data is missing from docker_task") - - out, err = e2e.AllocExec(newAllocID, "docker_task", newCmdToExec, ns, nil) - f.NoError(err, "could not exec into task: docker_task") - f.Equal(out, newAllocID+"\n", "new alloc data is missing from docker_task") - - out, err = e2e.AllocExec(newAllocID, "exec_task", cmdToExec, ns, nil) - f.NoError(err, "could not exec into task: exec_task") - f.Equal(out, allocID+"\n", "previous alloc data is missing from exec_task") - - out, err = e2e.AllocExec(newAllocID, "exec_task", newCmdToExec, ns, nil) - f.NoError(err, "could not exec into task: exec_task") - f.Equal(out, newAllocID+"\n", "new alloc data is missing from exec_task") -} diff --git a/e2e/volumes/volumes_test.go b/e2e/volumes/volumes_test.go new file mode 100644 index 000000000..742f56825 --- /dev/null +++ b/e2e/volumes/volumes_test.go @@ -0,0 +1,107 @@ +package volumes + +import ( + "fmt" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/hashicorp/nomad/api" + "github.com/hashicorp/nomad/e2e/e2eutil" + "github.com/hashicorp/nomad/helper/uuid" + "github.com/hashicorp/nomad/jobspec" + "github.com/hashicorp/nomad/testutil" +) + +const ns = "" + +// TestVolumeMounts exercises host volume and Docker volume functionality for +// the exec and docker task driver, particularly around mounting locations +// within the container and how this is exposed to the user. +func TestVolumeMounts(t *testing.T) { + + nomad := e2eutil.NomadClient(t) + e2eutil.WaitForLeader(t, nomad) + e2eutil.WaitForNodesReady(t, nomad, 1) + + jobIDs := []string{} + t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs)) + + jobID := "test-node-drain-" + uuid.Short() + require.NoError(t, e2eutil.Register(jobID, "./input/volumes.nomad")) + jobIDs = append(jobIDs, jobID) + + expected := []string{"running"} + require.NoError(t, e2eutil.WaitForAllocStatusExpected(jobID, ns, expected), + "job should be running") + + allocs, err := e2eutil.AllocsForJob(jobID, ns) + require.NoError(t, err, "could not get allocs for job") + allocID := allocs[0]["ID"] + nodeID := allocs[0]["Node ID"] + + cmdToExec := fmt.Sprintf("cat /tmp/foo/%s", allocID) + + out, err := e2eutil.AllocExec(allocID, "docker_task", cmdToExec, ns, nil) + require.NoError(t, err, "could not exec into task: docker_task") + require.Equal(t, allocID+"\n", out, "alloc data is missing from docker_task") + + out, err = e2eutil.AllocExec(allocID, "exec_task", cmdToExec, ns, nil) + require.NoError(t, err, "could not exec into task: exec_task") + require.Equal(t, out, allocID+"\n", "alloc data is missing from exec_task") + + err = e2eutil.StopJob(jobID) + require.NoError(t, err, "could not stop job") + + // modify the job so that we make sure it's placed back on the same host. + // we want to be able to verify that the data from the previous alloc is + // still there + job, err := jobspec.ParseFile("./input/volumes.nomad") + require.NoError(t, err) + job.ID = &jobID + job.Constraints = []*api.Constraint{ + { + LTarget: "${node.unique.id}", + RTarget: nodeID, + Operand: "=", + }, + } + _, _, err = nomad.Jobs().Register(job, nil) + require.NoError(t, err, "could not register updated job") + + testutil.WaitForResultRetries(5000, func() (bool, error) { + time.Sleep(time.Millisecond * 100) + allocs, err = e2eutil.AllocsForJob(jobID, ns) + if err != nil { + return false, err + } + if len(allocs) < 2 { + return false, fmt.Errorf("no new allocation for %v: %v", jobID, allocs) + } + + return true, nil + }, func(e error) { + require.NoError(t, e, "failed to get new alloc") + }) + + newAllocID := allocs[0]["ID"] + + newCmdToExec := fmt.Sprintf("cat /tmp/foo/%s", newAllocID) + + out, err = e2eutil.AllocExec(newAllocID, "docker_task", cmdToExec, ns, nil) + require.NoError(t, err, "could not exec into task: docker_task") + require.Equal(t, out, allocID+"\n", "previous alloc data is missing from docker_task") + + out, err = e2eutil.AllocExec(newAllocID, "docker_task", newCmdToExec, ns, nil) + require.NoError(t, err, "could not exec into task: docker_task") + require.Equal(t, out, newAllocID+"\n", "new alloc data is missing from docker_task") + + out, err = e2eutil.AllocExec(newAllocID, "exec_task", cmdToExec, ns, nil) + require.NoError(t, err, "could not exec into task: exec_task") + require.Equal(t, out, allocID+"\n", "previous alloc data is missing from exec_task") + + out, err = e2eutil.AllocExec(newAllocID, "exec_task", newCmdToExec, ns, nil) + require.NoError(t, err, "could not exec into task: exec_task") + require.Equal(t, out, newAllocID+"\n", "new alloc data is missing from exec_task") +}