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

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