github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/e2e/volumes/volumes.go (about)

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