github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/e2e/volumes/volumes_test.go (about)

     1  package volumes
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/hashicorp/nomad/api"
    11  	"github.com/hashicorp/nomad/e2e/e2eutil"
    12  	"github.com/hashicorp/nomad/helper/uuid"
    13  	"github.com/hashicorp/nomad/jobspec"
    14  	"github.com/hashicorp/nomad/testutil"
    15  )
    16  
    17  const ns = ""
    18  
    19  // TestVolumeMounts exercises host volume and Docker volume functionality for
    20  // the exec and docker task driver, particularly around mounting locations
    21  // within the container and how this is exposed to the user.
    22  func TestVolumeMounts(t *testing.T) {
    23  
    24  	nomad := e2eutil.NomadClient(t)
    25  	e2eutil.WaitForLeader(t, nomad)
    26  	e2eutil.WaitForNodesReady(t, nomad, 1)
    27  
    28  	jobIDs := []string{}
    29  	t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))
    30  
    31  	jobID := "test-node-drain-" + uuid.Short()
    32  	require.NoError(t, e2eutil.Register(jobID, "./input/volumes.nomad"))
    33  	jobIDs = append(jobIDs, jobID)
    34  
    35  	expected := []string{"running"}
    36  	require.NoError(t, e2eutil.WaitForAllocStatusExpected(jobID, ns, expected),
    37  		"job should be running")
    38  
    39  	allocs, err := e2eutil.AllocsForJob(jobID, ns)
    40  	require.NoError(t, err, "could not get allocs for job")
    41  	allocID := allocs[0]["ID"]
    42  	nodeID := allocs[0]["Node ID"]
    43  
    44  	cmdToExec := fmt.Sprintf("cat /tmp/foo/%s", allocID)
    45  
    46  	out, err := e2eutil.AllocExec(allocID, "docker_task", cmdToExec, ns, nil)
    47  	require.NoError(t, err, "could not exec into task: docker_task")
    48  	require.Equal(t, allocID+"\n", out, "alloc data is missing from docker_task")
    49  
    50  	out, err = e2eutil.AllocExec(allocID, "exec_task", cmdToExec, ns, nil)
    51  	require.NoError(t, err, "could not exec into task: exec_task")
    52  	require.Equal(t, out, allocID+"\n", "alloc data is missing from exec_task")
    53  
    54  	err = e2eutil.StopJob(jobID)
    55  	require.NoError(t, err, "could not stop job")
    56  
    57  	// modify the job so that we make sure it's placed back on the same host.
    58  	// we want to be able to verify that the data from the previous alloc is
    59  	// still there
    60  	job, err := jobspec.ParseFile("./input/volumes.nomad")
    61  	require.NoError(t, err)
    62  	job.ID = &jobID
    63  	job.Constraints = []*api.Constraint{
    64  		{
    65  			LTarget: "${node.unique.id}",
    66  			RTarget: nodeID,
    67  			Operand: "=",
    68  		},
    69  	}
    70  	_, _, err = nomad.Jobs().Register(job, nil)
    71  	require.NoError(t, err, "could not register updated job")
    72  
    73  	testutil.WaitForResultRetries(5000, func() (bool, error) {
    74  		time.Sleep(time.Millisecond * 100)
    75  		allocs, err = e2eutil.AllocsForJob(jobID, ns)
    76  		if err != nil {
    77  			return false, err
    78  		}
    79  		if len(allocs) < 2 {
    80  			return false, fmt.Errorf("no new allocation for %v: %v", jobID, allocs)
    81  		}
    82  
    83  		return true, nil
    84  	}, func(e error) {
    85  		require.NoError(t, e, "failed to get new alloc")
    86  	})
    87  
    88  	newAllocID := allocs[0]["ID"]
    89  
    90  	newCmdToExec := fmt.Sprintf("cat /tmp/foo/%s", newAllocID)
    91  
    92  	out, err = e2eutil.AllocExec(newAllocID, "docker_task", cmdToExec, ns, nil)
    93  	require.NoError(t, err, "could not exec into task: docker_task")
    94  	require.Equal(t, out, allocID+"\n", "previous alloc data is missing from docker_task")
    95  
    96  	out, err = e2eutil.AllocExec(newAllocID, "docker_task", newCmdToExec, ns, nil)
    97  	require.NoError(t, err, "could not exec into task: docker_task")
    98  	require.Equal(t, out, newAllocID+"\n", "new alloc data is missing from docker_task")
    99  
   100  	out, err = e2eutil.AllocExec(newAllocID, "exec_task", cmdToExec, ns, nil)
   101  	require.NoError(t, err, "could not exec into task: exec_task")
   102  	require.Equal(t, out, allocID+"\n", "previous alloc data is missing from exec_task")
   103  
   104  	out, err = e2eutil.AllocExec(newAllocID, "exec_task", newCmdToExec, ns, nil)
   105  	require.NoError(t, err, "could not exec into task: exec_task")
   106  	require.Equal(t, out, newAllocID+"\n", "new alloc data is missing from exec_task")
   107  }