github.com/hernad/nomad@v1.6.112/nomad/structs/config/artifact_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package config 5 6 import ( 7 "testing" 8 9 "github.com/hernad/nomad/ci" 10 "github.com/hernad/nomad/helper/pointer" 11 "github.com/shoenig/test/must" 12 ) 13 14 func TestArtifactConfig_Copy(t *testing.T) { 15 ci.Parallel(t) 16 17 a := DefaultArtifactConfig() 18 b := a.Copy() 19 must.Equal(t, a, b) 20 must.Equal(t, b, a) 21 22 b.HTTPReadTimeout = pointer.Of("5m") 23 b.HTTPMaxSize = pointer.Of("2MB") 24 b.GitTimeout = pointer.Of("3m") 25 b.HgTimeout = pointer.Of("2m") 26 b.DecompressionFileCountLimit = pointer.Of(7) 27 b.DecompressionSizeLimit = pointer.Of("2GB") 28 must.NotEqual(t, a, b) 29 } 30 31 func TestArtifactConfig_Merge(t *testing.T) { 32 ci.Parallel(t) 33 34 testCases := []struct { 35 name string 36 source *ArtifactConfig 37 other *ArtifactConfig 38 expected *ArtifactConfig 39 }{ 40 { 41 name: "merge all fields", 42 source: &ArtifactConfig{ 43 HTTPReadTimeout: pointer.Of("30m"), 44 HTTPMaxSize: pointer.Of("100GB"), 45 GCSTimeout: pointer.Of("30m"), 46 GitTimeout: pointer.Of("30m"), 47 HgTimeout: pointer.Of("30m"), 48 S3Timeout: pointer.Of("30m"), 49 DecompressionFileCountLimit: pointer.Of(4096), 50 DecompressionSizeLimit: pointer.Of("100GB"), 51 DisableFilesystemIsolation: pointer.Of(false), 52 SetEnvironmentVariables: pointer.Of(""), 53 }, 54 other: &ArtifactConfig{ 55 HTTPReadTimeout: pointer.Of("5m"), 56 HTTPMaxSize: pointer.Of("2GB"), 57 GCSTimeout: pointer.Of("1m"), 58 GitTimeout: pointer.Of("2m"), 59 HgTimeout: pointer.Of("3m"), 60 S3Timeout: pointer.Of("4m"), 61 DecompressionFileCountLimit: pointer.Of(100), 62 DecompressionSizeLimit: pointer.Of("8GB"), 63 DisableFilesystemIsolation: pointer.Of(true), 64 SetEnvironmentVariables: pointer.Of("FOO,BAR"), 65 }, 66 expected: &ArtifactConfig{ 67 HTTPReadTimeout: pointer.Of("5m"), 68 HTTPMaxSize: pointer.Of("2GB"), 69 GCSTimeout: pointer.Of("1m"), 70 GitTimeout: pointer.Of("2m"), 71 HgTimeout: pointer.Of("3m"), 72 S3Timeout: pointer.Of("4m"), 73 DecompressionFileCountLimit: pointer.Of(100), 74 DecompressionSizeLimit: pointer.Of("8GB"), 75 DisableFilesystemIsolation: pointer.Of(true), 76 SetEnvironmentVariables: pointer.Of("FOO,BAR"), 77 }, 78 }, 79 { 80 name: "null source", 81 source: nil, 82 other: &ArtifactConfig{ 83 HTTPReadTimeout: pointer.Of("5m"), 84 HTTPMaxSize: pointer.Of("2GB"), 85 GCSTimeout: pointer.Of("1m"), 86 GitTimeout: pointer.Of("2m"), 87 HgTimeout: pointer.Of("3m"), 88 S3Timeout: pointer.Of("4m"), 89 DecompressionFileCountLimit: pointer.Of(100), 90 DecompressionSizeLimit: pointer.Of("8GB"), 91 DisableFilesystemIsolation: pointer.Of(true), 92 SetEnvironmentVariables: pointer.Of("FOO,BAR"), 93 }, 94 expected: &ArtifactConfig{ 95 HTTPReadTimeout: pointer.Of("5m"), 96 HTTPMaxSize: pointer.Of("2GB"), 97 GCSTimeout: pointer.Of("1m"), 98 GitTimeout: pointer.Of("2m"), 99 HgTimeout: pointer.Of("3m"), 100 S3Timeout: pointer.Of("4m"), 101 DecompressionFileCountLimit: pointer.Of(100), 102 DecompressionSizeLimit: pointer.Of("8GB"), 103 DisableFilesystemIsolation: pointer.Of(true), 104 SetEnvironmentVariables: pointer.Of("FOO,BAR"), 105 }, 106 }, 107 { 108 name: "null other", 109 source: &ArtifactConfig{ 110 HTTPReadTimeout: pointer.Of("30m"), 111 HTTPMaxSize: pointer.Of("100GB"), 112 GCSTimeout: pointer.Of("30m"), 113 GitTimeout: pointer.Of("30m"), 114 HgTimeout: pointer.Of("30m"), 115 S3Timeout: pointer.Of("30m"), 116 DecompressionFileCountLimit: pointer.Of(4096), 117 DecompressionSizeLimit: pointer.Of("100GB"), 118 DisableFilesystemIsolation: pointer.Of(true), 119 SetEnvironmentVariables: pointer.Of("FOO,BAR"), 120 }, 121 other: nil, 122 expected: &ArtifactConfig{ 123 HTTPReadTimeout: pointer.Of("30m"), 124 HTTPMaxSize: pointer.Of("100GB"), 125 GCSTimeout: pointer.Of("30m"), 126 GitTimeout: pointer.Of("30m"), 127 HgTimeout: pointer.Of("30m"), 128 S3Timeout: pointer.Of("30m"), 129 DecompressionFileCountLimit: pointer.Of(4096), 130 DecompressionSizeLimit: pointer.Of("100GB"), 131 DisableFilesystemIsolation: pointer.Of(true), 132 SetEnvironmentVariables: pointer.Of("FOO,BAR"), 133 }, 134 }, 135 } 136 137 for _, tc := range testCases { 138 t.Run(tc.name, func(t *testing.T) { 139 got := tc.source.Merge(tc.other) 140 must.Equal(t, tc.expected, got) 141 }) 142 } 143 } 144 145 func TestArtifactConfig_Validate(t *testing.T) { 146 ci.Parallel(t) 147 148 testCases := []struct { 149 name string 150 config func(*ArtifactConfig) 151 expErr string 152 }{ 153 { 154 name: "default config is valid", 155 config: nil, 156 expErr: "", 157 }, 158 { 159 name: "missing http read timeout", 160 config: func(a *ArtifactConfig) { 161 a.HTTPReadTimeout = nil 162 }, 163 expErr: "http_read_timeout must be set", 164 }, 165 { 166 name: "http read timeout is invalid", 167 config: func(a *ArtifactConfig) { 168 a.HTTPReadTimeout = pointer.Of("invalid") 169 }, 170 expErr: "http_read_timeout not a valid duration", 171 }, 172 { 173 name: "http read timeout is empty", 174 config: func(a *ArtifactConfig) { 175 a.HTTPReadTimeout = pointer.Of("") 176 }, 177 expErr: "http_read_timeout not a valid duration", 178 }, 179 { 180 name: "http read timeout is zero", 181 config: func(a *ArtifactConfig) { 182 a.HTTPReadTimeout = pointer.Of("0") 183 }, 184 expErr: "", 185 }, 186 { 187 name: "http read timeout is negative", 188 config: func(a *ArtifactConfig) { 189 a.HTTPReadTimeout = pointer.Of("-10m") 190 }, 191 expErr: "http_read_timeout must be > 0", 192 }, 193 { 194 name: "http max size is missing", 195 config: func(a *ArtifactConfig) { 196 a.HTTPMaxSize = nil 197 }, 198 expErr: "http_max_size must be set", 199 }, 200 { 201 name: "http max size is invalid", 202 config: func(a *ArtifactConfig) { 203 a.HTTPMaxSize = pointer.Of("invalid") 204 }, 205 expErr: "http_max_size not a valid size", 206 }, 207 { 208 name: "http max size is empty", 209 config: func(a *ArtifactConfig) { 210 a.HTTPMaxSize = pointer.Of("") 211 }, 212 expErr: "http_max_size not a valid size", 213 }, 214 { 215 name: "http max size is zero", 216 config: func(a *ArtifactConfig) { 217 a.HTTPMaxSize = pointer.Of("0") 218 }, 219 expErr: "", 220 }, 221 { 222 name: "http max size is negative", 223 config: func(a *ArtifactConfig) { 224 a.HTTPMaxSize = pointer.Of("-l0MB") 225 }, 226 expErr: "http_max_size not a valid size", 227 }, 228 { 229 name: "gcs timeout is missing", 230 config: func(a *ArtifactConfig) { 231 a.GCSTimeout = nil 232 }, 233 expErr: "gcs_timeout must be set", 234 }, 235 { 236 name: "gcs timeout is invalid", 237 config: func(a *ArtifactConfig) { 238 a.GCSTimeout = pointer.Of("invalid") 239 }, 240 expErr: "gcs_timeout not a valid duration", 241 }, 242 { 243 name: "gcs timeout is empty", 244 config: func(a *ArtifactConfig) { 245 a.GCSTimeout = pointer.Of("") 246 }, 247 expErr: "gcs_timeout not a valid duration", 248 }, 249 { 250 name: "gcs timeout is zero", 251 config: func(a *ArtifactConfig) { 252 a.GCSTimeout = pointer.Of("0") 253 }, 254 expErr: "", 255 }, 256 { 257 name: "gcs timeout is negative", 258 config: func(a *ArtifactConfig) { 259 a.GCSTimeout = pointer.Of("-l0m") 260 }, 261 expErr: "gcs_timeout not a valid duration", 262 }, 263 { 264 name: "git timeout is missing", 265 config: func(a *ArtifactConfig) { 266 a.GitTimeout = nil 267 }, 268 expErr: "git_timeout must be set", 269 }, 270 { 271 name: "git timeout is invalid", 272 config: func(a *ArtifactConfig) { 273 a.GitTimeout = pointer.Of("invalid") 274 }, 275 expErr: "git_timeout not a valid duration", 276 }, 277 { 278 name: "git timeout is empty", 279 config: func(a *ArtifactConfig) { 280 a.GitTimeout = pointer.Of("") 281 }, 282 expErr: "git_timeout not a valid duration", 283 }, 284 { 285 name: "git timeout is zero", 286 config: func(a *ArtifactConfig) { 287 a.GitTimeout = pointer.Of("0") 288 }, 289 expErr: "", 290 }, 291 { 292 name: "git timeout is negative", 293 config: func(a *ArtifactConfig) { 294 a.GitTimeout = pointer.Of("-l0m") 295 }, 296 expErr: "git_timeout not a valid duration", 297 }, 298 { 299 name: "hg timeout is missing", 300 config: func(a *ArtifactConfig) { 301 a.HgTimeout = nil 302 }, 303 expErr: "hg_timeout must be set", 304 }, 305 { 306 name: "hg timeout is invalid", 307 config: func(a *ArtifactConfig) { 308 a.HgTimeout = pointer.Of("invalid") 309 }, 310 expErr: "hg_timeout not a valid duration", 311 }, 312 { 313 name: "hg timeout is empty", 314 config: func(a *ArtifactConfig) { 315 a.HgTimeout = pointer.Of("") 316 }, 317 expErr: "hg_timeout not a valid duration", 318 }, 319 { 320 name: "hg timeout is zero", 321 config: func(a *ArtifactConfig) { 322 a.HgTimeout = pointer.Of("0") 323 }, 324 expErr: "", 325 }, 326 { 327 name: "hg timeout is negative", 328 config: func(a *ArtifactConfig) { 329 a.HgTimeout = pointer.Of("-l0m") 330 }, 331 expErr: "hg_timeout not a valid duration", 332 }, 333 { 334 name: "s3 timeout is missing", 335 config: func(a *ArtifactConfig) { 336 a.S3Timeout = nil 337 }, 338 expErr: "s3_timeout must be set", 339 }, 340 { 341 name: "s3 timeout is invalid", 342 config: func(a *ArtifactConfig) { 343 a.S3Timeout = pointer.Of("invalid") 344 }, 345 expErr: "s3_timeout not a valid duration", 346 }, 347 { 348 name: "s3 timeout is empty", 349 config: func(a *ArtifactConfig) { 350 a.S3Timeout = pointer.Of("") 351 }, 352 expErr: "s3_timeout not a valid duration", 353 }, 354 { 355 name: "s3 timeout is zero", 356 config: func(a *ArtifactConfig) { 357 a.S3Timeout = pointer.Of("0") 358 }, 359 expErr: "", 360 }, 361 { 362 name: "s3 timeout is negative", 363 config: func(a *ArtifactConfig) { 364 a.S3Timeout = pointer.Of("-l0m") 365 }, 366 expErr: "s3_timeout not a valid duration", 367 }, 368 { 369 name: "decompression file count limit is nil", 370 config: func(a *ArtifactConfig) { 371 a.DecompressionFileCountLimit = nil 372 }, 373 expErr: "decompression_file_count_limit must not be nil", 374 }, 375 { 376 name: "decompression file count limit is negative", 377 config: func(a *ArtifactConfig) { 378 a.DecompressionFileCountLimit = pointer.Of(-1) 379 }, 380 expErr: "decompression_file_count_limit must be >= 0 but found -1", 381 }, 382 { 383 name: "decompression size limit is nil", 384 config: func(a *ArtifactConfig) { 385 a.DecompressionSizeLimit = nil 386 }, 387 expErr: "decompression_size_limit must not be nil", 388 }, 389 { 390 name: "decompression size limit is negative", 391 config: func(a *ArtifactConfig) { 392 a.DecompressionSizeLimit = pointer.Of("-1GB") 393 }, 394 expErr: "decompression_size_limit is not a valid size", 395 }, 396 { 397 name: "fs isolation not set", 398 config: func(a *ArtifactConfig) { 399 a.DisableFilesystemIsolation = nil 400 }, 401 expErr: "disable_filesystem_isolation must be set", 402 }, 403 { 404 name: "env not set", 405 config: func(a *ArtifactConfig) { 406 a.SetEnvironmentVariables = nil 407 }, 408 expErr: "set_environment_variables must be set", 409 }, 410 } 411 412 for _, tc := range testCases { 413 t.Run(tc.name, func(t *testing.T) { 414 a := DefaultArtifactConfig() 415 if tc.config != nil { 416 tc.config(a) 417 } 418 419 err := a.Validate() 420 if tc.expErr != "" { 421 must.Error(t, err) 422 must.StrContains(t, err.Error(), tc.expErr) 423 } else { 424 must.NoError(t, err) 425 } 426 }) 427 } 428 }