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

     1  package artifact
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/api"
     7  	"github.com/hashicorp/nomad/e2e/e2eutil"
     8  	"github.com/hashicorp/nomad/helper/uuid"
     9  	"github.com/shoenig/test/must"
    10  )
    11  
    12  func TestArtifact(t *testing.T) {
    13  	nomad := e2eutil.NomadClient(t)
    14  
    15  	e2eutil.WaitForLeader(t, nomad)
    16  	e2eutil.WaitForNodesReady(t, nomad, 1)
    17  
    18  	t.Run("testLinux", testLinux)
    19  	t.Run("testWindows", testWindows)
    20  }
    21  
    22  // artifactCheckLogContents verifies expected logs for artifact downloader tests.
    23  //
    24  // each task is designed to download the artifact in some way then cat the go.mod
    25  // file, so we just need to read the logs
    26  //
    27  // note: git requires the use of destination (hence no default form)
    28  func artifactCheckLogContents(t *testing.T, nomad *api.Client, group, task string, allocations []map[string]string) {
    29  	var allocID string
    30  	for _, alloc := range allocations {
    31  		if alloc["Task Group"] == group {
    32  			allocID = alloc["ID"]
    33  			break
    34  		}
    35  	}
    36  	e2eutil.WaitForAllocStopped(t, nomad, allocID)
    37  	t.Run(task, func(t *testing.T) {
    38  		logs, err := e2eutil.AllocTaskLogs(allocID, task, e2eutil.LogsStdOut)
    39  		must.NoError(t, err)
    40  		must.StrContains(t, logs, "module github.com/hashicorp/go-set")
    41  	})
    42  }
    43  
    44  func testWindows(t *testing.T) {
    45  	nomad := e2eutil.NomadClient(t)
    46  
    47  	jobID := "artifact-linux-" + uuid.Short()
    48  	jobIDs := []string{jobID}
    49  	t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))
    50  
    51  	// start job
    52  	e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/artifact_windows.nomad", jobID, "")
    53  
    54  	// get allocations
    55  	allocations, err := e2eutil.AllocsForJob(jobID, "")
    56  	must.NoError(t, err)
    57  	must.Len(t, 1, allocations)
    58  
    59  	// assert log contents for each task
    60  	check := func(group, task string) {
    61  		artifactCheckLogContents(t, nomad, group, task, allocations)
    62  	}
    63  
    64  	check("rawexec", "rawexec_file_default")
    65  	check("rawexec", "rawexec_file_custom")
    66  	check("rawexec", "rawexec_zip_default")
    67  	check("rawexec", "rawexec_zip_custom")
    68  	check("rawexec", "rawexec_git_custom")
    69  }
    70  
    71  func testLinux(t *testing.T) {
    72  	nomad := e2eutil.NomadClient(t)
    73  
    74  	jobID := "artifact-linux-" + uuid.Short()
    75  	jobIDs := []string{jobID}
    76  	t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs))
    77  
    78  	// start job
    79  	e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/artifact_linux.nomad", jobID, "")
    80  
    81  	// get allocations
    82  	allocations, err := e2eutil.AllocsForJob(jobID, "")
    83  	must.NoError(t, err)
    84  	must.Len(t, 3, allocations)
    85  
    86  	// assert log contents for each task
    87  	check := func(group, task string) {
    88  		artifactCheckLogContents(t, nomad, group, task, allocations)
    89  	}
    90  
    91  	check("rawexec", "rawexec_file_default")
    92  	check("rawexec", "rawexec_file_custom")
    93  	check("rawexec", "rawexec_zip_default")
    94  	check("rawexec", "rawexec_zip_custom")
    95  	check("rawexec", "rawexec_git_custom")
    96  
    97  	check("exec", "exec_file_default")
    98  	check("exec", "exec_file_custom")
    99  	check("exec", "exec_zip_default")
   100  	check("exec", "exec_zip_custom")
   101  	check("exec", "exec_git_custom")
   102  
   103  	check("docker", "docker_file_default")
   104  	check("docker", "docker_file_custom")
   105  	check("docker", "docker_zip_default")
   106  	check("docker", "docker_zip_custom")
   107  	check("docker", "docker_git_custom")
   108  }