2022-05-03 22:38:32 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2022-12-08 18:29:23 +00:00
|
|
|
"github.com/shoenig/test/must"
|
2022-05-03 22:38:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestArtifactConfig_Copy(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
a := DefaultArtifactConfig()
|
|
|
|
b := a.Copy()
|
2022-12-08 18:29:23 +00:00
|
|
|
must.Equal(t, a, b)
|
|
|
|
must.Equal(t, b, a)
|
2022-05-03 22:38:32 +00:00
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
b.HTTPReadTimeout = pointer.Of("5m")
|
|
|
|
b.HTTPMaxSize = pointer.Of("2MB")
|
|
|
|
b.GitTimeout = pointer.Of("3m")
|
|
|
|
b.HgTimeout = pointer.Of("2m")
|
2022-12-08 18:29:23 +00:00
|
|
|
must.NotEqual(t, a, b)
|
2022-05-03 22:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestArtifactConfig_Merge(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
source *ArtifactConfig
|
|
|
|
other *ArtifactConfig
|
|
|
|
expected *ArtifactConfig
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "merge all fields",
|
|
|
|
source: &ArtifactConfig{
|
2022-12-08 18:29:23 +00:00
|
|
|
HTTPReadTimeout: pointer.Of("30m"),
|
|
|
|
HTTPMaxSize: pointer.Of("100GB"),
|
|
|
|
GCSTimeout: pointer.Of("30m"),
|
|
|
|
GitTimeout: pointer.Of("30m"),
|
|
|
|
HgTimeout: pointer.Of("30m"),
|
|
|
|
S3Timeout: pointer.Of("30m"),
|
|
|
|
DisableFilesystemIsolation: pointer.Of(false),
|
2022-12-09 21:46:07 +00:00
|
|
|
SetEnvironmentVariables: pointer.Of(""),
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
other: &ArtifactConfig{
|
2022-12-08 18:29:23 +00:00
|
|
|
HTTPReadTimeout: pointer.Of("5m"),
|
|
|
|
HTTPMaxSize: pointer.Of("2GB"),
|
|
|
|
GCSTimeout: pointer.Of("1m"),
|
|
|
|
GitTimeout: pointer.Of("2m"),
|
|
|
|
HgTimeout: pointer.Of("3m"),
|
|
|
|
S3Timeout: pointer.Of("4m"),
|
|
|
|
DisableFilesystemIsolation: pointer.Of(true),
|
2022-12-09 21:46:07 +00:00
|
|
|
SetEnvironmentVariables: pointer.Of("FOO,BAR"),
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
expected: &ArtifactConfig{
|
2022-12-08 18:29:23 +00:00
|
|
|
HTTPReadTimeout: pointer.Of("5m"),
|
|
|
|
HTTPMaxSize: pointer.Of("2GB"),
|
|
|
|
GCSTimeout: pointer.Of("1m"),
|
|
|
|
GitTimeout: pointer.Of("2m"),
|
|
|
|
HgTimeout: pointer.Of("3m"),
|
|
|
|
S3Timeout: pointer.Of("4m"),
|
|
|
|
DisableFilesystemIsolation: pointer.Of(true),
|
2022-12-09 21:46:07 +00:00
|
|
|
SetEnvironmentVariables: pointer.Of("FOO,BAR"),
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "null source",
|
|
|
|
source: nil,
|
|
|
|
other: &ArtifactConfig{
|
2022-12-08 18:29:23 +00:00
|
|
|
HTTPReadTimeout: pointer.Of("5m"),
|
|
|
|
HTTPMaxSize: pointer.Of("2GB"),
|
|
|
|
GCSTimeout: pointer.Of("1m"),
|
|
|
|
GitTimeout: pointer.Of("2m"),
|
|
|
|
HgTimeout: pointer.Of("3m"),
|
|
|
|
S3Timeout: pointer.Of("4m"),
|
|
|
|
DisableFilesystemIsolation: pointer.Of(true),
|
2022-12-09 21:46:07 +00:00
|
|
|
SetEnvironmentVariables: pointer.Of("FOO,BAR"),
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
expected: &ArtifactConfig{
|
2022-12-08 18:29:23 +00:00
|
|
|
HTTPReadTimeout: pointer.Of("5m"),
|
|
|
|
HTTPMaxSize: pointer.Of("2GB"),
|
|
|
|
GCSTimeout: pointer.Of("1m"),
|
|
|
|
GitTimeout: pointer.Of("2m"),
|
|
|
|
HgTimeout: pointer.Of("3m"),
|
|
|
|
S3Timeout: pointer.Of("4m"),
|
|
|
|
DisableFilesystemIsolation: pointer.Of(true),
|
2022-12-09 21:46:07 +00:00
|
|
|
SetEnvironmentVariables: pointer.Of("FOO,BAR"),
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "null other",
|
|
|
|
source: &ArtifactConfig{
|
2022-12-08 18:29:23 +00:00
|
|
|
HTTPReadTimeout: pointer.Of("30m"),
|
|
|
|
HTTPMaxSize: pointer.Of("100GB"),
|
|
|
|
GCSTimeout: pointer.Of("30m"),
|
|
|
|
GitTimeout: pointer.Of("30m"),
|
|
|
|
HgTimeout: pointer.Of("30m"),
|
|
|
|
S3Timeout: pointer.Of("30m"),
|
|
|
|
DisableFilesystemIsolation: pointer.Of(true),
|
2022-12-09 21:46:07 +00:00
|
|
|
SetEnvironmentVariables: pointer.Of("FOO,BAR"),
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
other: nil,
|
|
|
|
expected: &ArtifactConfig{
|
2022-12-08 18:29:23 +00:00
|
|
|
HTTPReadTimeout: pointer.Of("30m"),
|
|
|
|
HTTPMaxSize: pointer.Of("100GB"),
|
|
|
|
GCSTimeout: pointer.Of("30m"),
|
|
|
|
GitTimeout: pointer.Of("30m"),
|
|
|
|
HgTimeout: pointer.Of("30m"),
|
|
|
|
S3Timeout: pointer.Of("30m"),
|
|
|
|
DisableFilesystemIsolation: pointer.Of(true),
|
2022-12-09 21:46:07 +00:00
|
|
|
SetEnvironmentVariables: pointer.Of("FOO,BAR"),
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
got := tc.source.Merge(tc.other)
|
2022-12-08 18:29:23 +00:00
|
|
|
must.Equal(t, tc.expected, got)
|
2022-05-03 22:38:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArtifactConfig_Validate(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-12-08 18:29:23 +00:00
|
|
|
name string
|
|
|
|
config func(*ArtifactConfig)
|
|
|
|
expErr string
|
2022-05-03 22:38:32 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-12-08 18:29:23 +00:00
|
|
|
name: "default config is valid",
|
|
|
|
config: nil,
|
|
|
|
expErr: "",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing http read timeout",
|
|
|
|
config: func(a *ArtifactConfig) {
|
|
|
|
a.HTTPReadTimeout = nil
|
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "http_read_timeout must be set",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http read timeout is invalid",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HTTPReadTimeout = pointer.Of("invalid")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "http_read_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http read timeout is empty",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HTTPReadTimeout = pointer.Of("")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "http_read_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http read timeout is zero",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HTTPReadTimeout = pointer.Of("0")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http read timeout is negative",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HTTPReadTimeout = pointer.Of("-10m")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "http_read_timeout must be > 0",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http max size is missing",
|
|
|
|
config: func(a *ArtifactConfig) {
|
|
|
|
a.HTTPMaxSize = nil
|
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "http_max_size must be set",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http max size is invalid",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HTTPMaxSize = pointer.Of("invalid")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "http_max_size not a valid size",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http max size is empty",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HTTPMaxSize = pointer.Of("")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "http_max_size not a valid size",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http max size is zero",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HTTPMaxSize = pointer.Of("0")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "http max size is negative",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HTTPMaxSize = pointer.Of("-l0MB")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "http_max_size not a valid size",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gcs timeout is missing",
|
|
|
|
config: func(a *ArtifactConfig) {
|
|
|
|
a.GCSTimeout = nil
|
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "gcs_timeout must be set",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gcs timeout is invalid",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.GCSTimeout = pointer.Of("invalid")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "gcs_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gcs timeout is empty",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.GCSTimeout = pointer.Of("")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "gcs_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gcs timeout is zero",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.GCSTimeout = pointer.Of("0")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gcs timeout is negative",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.GCSTimeout = pointer.Of("-l0m")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "gcs_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "git timeout is missing",
|
|
|
|
config: func(a *ArtifactConfig) {
|
|
|
|
a.GitTimeout = nil
|
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "git_timeout must be set",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "git timeout is invalid",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.GitTimeout = pointer.Of("invalid")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "git_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "git timeout is empty",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.GitTimeout = pointer.Of("")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "git_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "git timeout is zero",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.GitTimeout = pointer.Of("0")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "git timeout is negative",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.GitTimeout = pointer.Of("-l0m")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "git_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hg timeout is missing",
|
|
|
|
config: func(a *ArtifactConfig) {
|
|
|
|
a.HgTimeout = nil
|
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "hg_timeout must be set",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hg timeout is invalid",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HgTimeout = pointer.Of("invalid")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "hg_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hg timeout is empty",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HgTimeout = pointer.Of("")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "hg_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hg timeout is zero",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HgTimeout = pointer.Of("0")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "hg timeout is negative",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.HgTimeout = pointer.Of("-l0m")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "hg_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "s3 timeout is missing",
|
|
|
|
config: func(a *ArtifactConfig) {
|
|
|
|
a.S3Timeout = nil
|
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "s3_timeout must be set",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "s3 timeout is invalid",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.S3Timeout = pointer.Of("invalid")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "s3_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "s3 timeout is empty",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.S3Timeout = pointer.Of("")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "s3_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "s3 timeout is zero",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.S3Timeout = pointer.Of("0")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "s3 timeout is negative",
|
|
|
|
config: func(a *ArtifactConfig) {
|
2022-08-17 16:26:34 +00:00
|
|
|
a.S3Timeout = pointer.Of("-l0m")
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-08 18:29:23 +00:00
|
|
|
expErr: "s3_timeout not a valid duration",
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
2022-12-09 21:46:07 +00:00
|
|
|
{
|
|
|
|
name: "fs isolation not set",
|
|
|
|
config: func(a *ArtifactConfig) {
|
|
|
|
a.DisableFilesystemIsolation = nil
|
|
|
|
},
|
|
|
|
expErr: "disable_filesystem_isolation must be set",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "env not set",
|
|
|
|
config: func(a *ArtifactConfig) {
|
|
|
|
a.SetEnvironmentVariables = nil
|
|
|
|
},
|
|
|
|
expErr: "set_environment_variables must be set",
|
|
|
|
},
|
2022-05-03 22:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
a := DefaultArtifactConfig()
|
|
|
|
if tc.config != nil {
|
|
|
|
tc.config(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := a.Validate()
|
2022-12-08 18:29:23 +00:00
|
|
|
if tc.expErr != "" {
|
|
|
|
must.Error(t, err)
|
|
|
|
must.StrContains(t, err.Error(), tc.expErr)
|
2022-05-03 22:38:32 +00:00
|
|
|
} else {
|
2022-12-08 18:29:23 +00:00
|
|
|
must.NoError(t, err)
|
2022-05-03 22:38:32 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|