github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/allocrunner/taskrunner/getter/params_test.go (about) 1 package getter 2 3 import ( 4 "context" 5 "io" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/hashicorp/go-getter" 11 "github.com/shoenig/test/must" 12 ) 13 14 const paramsAsJSON = ` 15 { 16 "http_read_timeout": 1000000000, 17 "http_max_bytes": 2000, 18 "gcs_timeout": 2000000000, 19 "git_timeout": 3000000000, 20 "hg_timeout": 4000000000, 21 "s3_timeout": 5000000000, 22 "disable_filesystem_isolation": true, 23 "set_environment_variables": "", 24 "artifact_mode": 2, 25 "artifact_source": "https://example.com/file.txt", 26 "artifact_destination": "local/out.txt", 27 "artifact_headers": { 28 "X-Nomad-Artifact": ["hi"] 29 }, 30 "task_dir": "/path/to/task" 31 }` 32 33 var paramsAsStruct = ¶meters{ 34 HTTPReadTimeout: 1 * time.Second, 35 HTTPMaxBytes: 2000, 36 GCSTimeout: 2 * time.Second, 37 GitTimeout: 3 * time.Second, 38 HgTimeout: 4 * time.Second, 39 S3Timeout: 5 * time.Second, 40 DisableFilesystemIsolation: true, 41 42 Mode: getter.ClientModeFile, 43 Source: "https://example.com/file.txt", 44 Destination: "local/out.txt", 45 TaskDir: "/path/to/task", 46 Headers: map[string][]string{ 47 "X-Nomad-Artifact": {"hi"}, 48 }, 49 } 50 51 func TestParameters_reader(t *testing.T) { 52 p := paramsAsStruct 53 reader := p.reader() 54 b, err := io.ReadAll(reader) 55 must.NoError(t, err) 56 must.EqJSON(t, paramsAsJSON, string(b)) 57 } 58 59 func TestParameters_read(t *testing.T) { 60 reader := strings.NewReader(paramsAsJSON) 61 p := new(parameters) 62 err := p.read(reader) 63 must.NoError(t, err) 64 must.Equal(t, paramsAsStruct, p) 65 } 66 67 func TestParameters_deadline(t *testing.T) { 68 t.Run("typical", func(t *testing.T) { 69 dur := paramsAsStruct.deadline() 70 must.Eq(t, 31*time.Minute, dur) 71 }) 72 73 t.Run("long", func(t *testing.T) { 74 params := ¶meters{ 75 HTTPReadTimeout: 1 * time.Hour, 76 GCSTimeout: 2 * time.Hour, 77 GitTimeout: 3 * time.Hour, 78 HgTimeout: 4 * time.Hour, 79 S3Timeout: 5 * time.Hour, 80 } 81 dur := params.deadline() 82 must.Eq(t, 5*time.Hour+1*time.Minute, dur) 83 }) 84 } 85 86 func TestParameters_client(t *testing.T) { 87 ctx := context.Background() 88 c := paramsAsStruct.client(ctx) 89 must.NotNil(t, c) 90 91 // security options 92 must.False(t, c.Insecure) 93 must.True(t, c.DisableSymlinks) 94 must.Eq(t, umask, c.Umask) 95 96 // artifact options 97 must.Eq(t, "https://example.com/file.txt", c.Src) 98 must.Eq(t, "local/out.txt", c.Dst) 99 } 100 101 func TestParameters_Equal_headers(t *testing.T) { 102 p1 := ¶meters{ 103 Headers: map[string][]string{ 104 "East": []string{"New York", "Florida"}, 105 "West": []string{"California"}, 106 }, 107 } 108 109 p2 := ¶meters{ 110 Headers: map[string][]string{ 111 "East": []string{"New York", "Florida"}, 112 "West": []string{"California"}, 113 }, 114 } 115 116 // equal 117 must.Equal(t, p1, p2) 118 119 // not equal 120 p2.Headers["East"] = []string{"New York"} 121 must.NotEqual(t, p1, p2) 122 }