github.com/hernad/nomad@v1.6.112/e2e/artifact/artifact_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package artifact 5 6 import ( 7 "testing" 8 9 "github.com/hernad/nomad/api" 10 "github.com/hernad/nomad/e2e/e2eutil" 11 "github.com/hernad/nomad/helper/uuid" 12 "github.com/shoenig/test/must" 13 ) 14 15 func TestArtifact(t *testing.T) { 16 nomad := e2eutil.NomadClient(t) 17 18 e2eutil.WaitForLeader(t, nomad) 19 e2eutil.WaitForNodesReady(t, nomad, 1) 20 21 t.Run("testLinux", testLinux) 22 t.Run("testWindows", testWindows) 23 t.Run("testLimits", testLimits) 24 } 25 26 // artifactCheckLogContents verifies expected logs for artifact downloader tests. 27 // 28 // each task is designed to download the artifact in some way then cat the go.mod 29 // file, so we just need to read the logs 30 // 31 // note: git requires the use of destination (hence no default form) 32 func artifactCheckLogContents(t *testing.T, nomad *api.Client, group, task string, allocations []map[string]string) { 33 var allocID string 34 for _, alloc := range allocations { 35 if alloc["Task Group"] == group { 36 allocID = alloc["ID"] 37 break 38 } 39 } 40 e2eutil.WaitForAllocStopped(t, nomad, allocID) 41 t.Run(task, func(t *testing.T) { 42 logs, err := e2eutil.AllocTaskLogs(allocID, task, e2eutil.LogsStdOut) 43 must.NoError(t, err) 44 must.StrContains(t, logs, "module github.com/hashicorp/go-set") 45 }) 46 } 47 48 func testWindows(t *testing.T) { 49 nomad := e2eutil.NomadClient(t) 50 jobID := "artifact-windows-" + uuid.Short() 51 jobIDs := []string{jobID} 52 t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs)) 53 54 // start job 55 e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/artifact_windows.nomad", jobID, "") 56 57 // get allocations 58 allocations, err := e2eutil.AllocsForJob(jobID, "") 59 must.NoError(t, err) 60 must.Len(t, 1, allocations) 61 62 // assert log contents for each task 63 check := func(group, task string) { 64 artifactCheckLogContents(t, nomad, group, task, allocations) 65 } 66 67 check("rawexec", "rawexec_file_default") 68 check("rawexec", "rawexec_file_custom") 69 check("rawexec", "rawexec_zip_default") 70 check("rawexec", "rawexec_zip_custom") 71 72 // todo(shoenig) needs git on windows 73 // https://github.com/hernad/nomad/issues/15505 74 // check("rawexec", "rawexec_git_custom") 75 } 76 77 func testLinux(t *testing.T) { 78 nomad := e2eutil.NomadClient(t) 79 jobID := "artifact-linux-" + uuid.Short() 80 jobIDs := []string{jobID} 81 t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs)) 82 83 // start job 84 e2eutil.RegisterAndWaitForAllocs(t, nomad, "./input/artifact_linux.nomad", jobID, "") 85 86 // get allocations 87 allocations, err := e2eutil.AllocsForJob(jobID, "") 88 must.NoError(t, err) 89 must.Len(t, 3, allocations) 90 91 // assert log contents for each task 92 check := func(group, task string) { 93 artifactCheckLogContents(t, nomad, group, task, allocations) 94 } 95 96 check("rawexec", "rawexec_file_default") 97 check("rawexec", "rawexec_file_custom") 98 check("rawexec", "rawexec_file_alloc_dots") 99 check("rawexec", "rawexec_file_alloc_env") 100 check("rawexec", "rawexec_zip_default") 101 check("rawexec", "rawexec_zip_custom") 102 check("rawexec", "rawexec_git_custom") 103 104 check("exec", "exec_file_default") 105 check("exec", "exec_file_custom") 106 check("exec", "exec_file_alloc") 107 check("exec", "exec_zip_default") 108 check("exec", "exec_zip_custom") 109 check("exec", "exec_git_custom") 110 111 check("docker", "docker_file_default") 112 check("docker", "docker_file_custom") 113 check("docker", "docker_file_alloc") 114 check("docker", "docker_zip_default") 115 check("docker", "docker_zip_custom") 116 check("docker", "docker_git_custom") 117 } 118 119 func testLimits(t *testing.T) { 120 // defaults are 100GB, 4096 files; we run into the files count here 121 122 jobID := "artifact-limits-" + uuid.Short() 123 jobIDs := []string{jobID} 124 t.Cleanup(e2eutil.CleanupJobsAndGC(t, &jobIDs)) 125 126 err := e2eutil.Register(jobID, "./input/artifact_limits.nomad") 127 must.NoError(t, err) 128 129 err = e2eutil.WaitForAllocStatusExpected(jobID, "", []string{"failed"}) 130 must.NoError(t, err) 131 132 m, err := e2eutil.AllocTaskEventsForJob(jobID, "") 133 must.NoError(t, err) 134 135 found := false 136 SCAN: 137 for _, events := range m { 138 for _, event := range events { 139 for label, description := range event { 140 if label == "Type" && description == "Failed Artifact Download" { 141 found = true 142 break SCAN 143 } 144 145 } 146 } 147 } 148 must.True(t, found) 149 }