github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/config/config_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package config
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"reflect"
    25  	"testing"
    26  	"time"
    27  
    28  	"k8s.io/api/core/v1"
    29  	"k8s.io/test-infra/prow/kube"
    30  )
    31  
    32  func TestDecorationDefaulting(t *testing.T) {
    33  	defaults := &kube.DecorationConfig{
    34  		Timeout:     1 * time.Minute,
    35  		GracePeriod: 10 * time.Second,
    36  		UtilityImages: &kube.UtilityImages{
    37  			CloneRefs:  "clonerefs",
    38  			InitUpload: "iniupload",
    39  			Entrypoint: "entrypoint",
    40  			Sidecar:    "sidecar",
    41  		},
    42  		GCSConfiguration: &kube.GCSConfiguration{
    43  			Bucket:       "bucket",
    44  			PathPrefix:   "prefix",
    45  			PathStrategy: kube.PathStrategyLegacy,
    46  			DefaultOrg:   "org",
    47  			DefaultRepo:  "repo",
    48  		},
    49  		GCSCredentialsSecret: "secretName",
    50  		SSHKeySecrets:        []string{"first", "second"},
    51  	}
    52  
    53  	var testCases = []struct {
    54  		name     string
    55  		provided *kube.DecorationConfig
    56  		expected *kube.DecorationConfig
    57  	}{
    58  		{
    59  			name:     "nothing provided",
    60  			provided: &kube.DecorationConfig{},
    61  			expected: &kube.DecorationConfig{
    62  				Timeout:     1 * time.Minute,
    63  				GracePeriod: 10 * time.Second,
    64  				UtilityImages: &kube.UtilityImages{
    65  					CloneRefs:  "clonerefs",
    66  					InitUpload: "iniupload",
    67  					Entrypoint: "entrypoint",
    68  					Sidecar:    "sidecar",
    69  				},
    70  				GCSConfiguration: &kube.GCSConfiguration{
    71  					Bucket:       "bucket",
    72  					PathPrefix:   "prefix",
    73  					PathStrategy: kube.PathStrategyLegacy,
    74  					DefaultOrg:   "org",
    75  					DefaultRepo:  "repo",
    76  				},
    77  				GCSCredentialsSecret: "secretName",
    78  				SSHKeySecrets:        []string{"first", "second"},
    79  			},
    80  		},
    81  		{
    82  			name: "timeout provided",
    83  			provided: &kube.DecorationConfig{
    84  				Timeout: 10 * time.Minute,
    85  			},
    86  			expected: &kube.DecorationConfig{
    87  				Timeout:     10 * time.Minute,
    88  				GracePeriod: 10 * time.Second,
    89  				UtilityImages: &kube.UtilityImages{
    90  					CloneRefs:  "clonerefs",
    91  					InitUpload: "iniupload",
    92  					Entrypoint: "entrypoint",
    93  					Sidecar:    "sidecar",
    94  				},
    95  				GCSConfiguration: &kube.GCSConfiguration{
    96  					Bucket:       "bucket",
    97  					PathPrefix:   "prefix",
    98  					PathStrategy: kube.PathStrategyLegacy,
    99  					DefaultOrg:   "org",
   100  					DefaultRepo:  "repo",
   101  				},
   102  				GCSCredentialsSecret: "secretName",
   103  				SSHKeySecrets:        []string{"first", "second"},
   104  			},
   105  		},
   106  		{
   107  			name: "grace period provided",
   108  			provided: &kube.DecorationConfig{
   109  				GracePeriod: 10 * time.Hour,
   110  			},
   111  			expected: &kube.DecorationConfig{
   112  				Timeout:     1 * time.Minute,
   113  				GracePeriod: 10 * time.Hour,
   114  				UtilityImages: &kube.UtilityImages{
   115  					CloneRefs:  "clonerefs",
   116  					InitUpload: "iniupload",
   117  					Entrypoint: "entrypoint",
   118  					Sidecar:    "sidecar",
   119  				},
   120  				GCSConfiguration: &kube.GCSConfiguration{
   121  					Bucket:       "bucket",
   122  					PathPrefix:   "prefix",
   123  					PathStrategy: kube.PathStrategyLegacy,
   124  					DefaultOrg:   "org",
   125  					DefaultRepo:  "repo",
   126  				},
   127  				GCSCredentialsSecret: "secretName",
   128  				SSHKeySecrets:        []string{"first", "second"},
   129  			},
   130  		},
   131  		{
   132  			name: "utility images provided",
   133  			provided: &kube.DecorationConfig{
   134  				UtilityImages: &kube.UtilityImages{
   135  					CloneRefs:  "clonerefs-special",
   136  					InitUpload: "iniupload-special",
   137  					Entrypoint: "entrypoint-special",
   138  					Sidecar:    "sidecar-special",
   139  				},
   140  			},
   141  			expected: &kube.DecorationConfig{
   142  				Timeout:     1 * time.Minute,
   143  				GracePeriod: 10 * time.Second,
   144  				UtilityImages: &kube.UtilityImages{
   145  					CloneRefs:  "clonerefs-special",
   146  					InitUpload: "iniupload-special",
   147  					Entrypoint: "entrypoint-special",
   148  					Sidecar:    "sidecar-special",
   149  				},
   150  				GCSConfiguration: &kube.GCSConfiguration{
   151  					Bucket:       "bucket",
   152  					PathPrefix:   "prefix",
   153  					PathStrategy: kube.PathStrategyLegacy,
   154  					DefaultOrg:   "org",
   155  					DefaultRepo:  "repo",
   156  				},
   157  				GCSCredentialsSecret: "secretName",
   158  				SSHKeySecrets:        []string{"first", "second"},
   159  			},
   160  		},
   161  		{
   162  			name: "gcs configuration provided",
   163  			provided: &kube.DecorationConfig{
   164  				GCSConfiguration: &kube.GCSConfiguration{
   165  					Bucket:       "bucket-1",
   166  					PathPrefix:   "prefix-2",
   167  					PathStrategy: kube.PathStrategyExplicit,
   168  				},
   169  			},
   170  			expected: &kube.DecorationConfig{
   171  				Timeout:     1 * time.Minute,
   172  				GracePeriod: 10 * time.Second,
   173  				UtilityImages: &kube.UtilityImages{
   174  					CloneRefs:  "clonerefs",
   175  					InitUpload: "iniupload",
   176  					Entrypoint: "entrypoint",
   177  					Sidecar:    "sidecar",
   178  				},
   179  				GCSConfiguration: &kube.GCSConfiguration{
   180  					Bucket:       "bucket-1",
   181  					PathPrefix:   "prefix-2",
   182  					PathStrategy: kube.PathStrategyExplicit,
   183  				},
   184  				GCSCredentialsSecret: "secretName",
   185  				SSHKeySecrets:        []string{"first", "second"},
   186  			},
   187  		},
   188  		{
   189  			name: "secret name provided",
   190  			provided: &kube.DecorationConfig{
   191  				GCSCredentialsSecret: "somethingSecret",
   192  			},
   193  			expected: &kube.DecorationConfig{
   194  				Timeout:     1 * time.Minute,
   195  				GracePeriod: 10 * time.Second,
   196  				UtilityImages: &kube.UtilityImages{
   197  					CloneRefs:  "clonerefs",
   198  					InitUpload: "iniupload",
   199  					Entrypoint: "entrypoint",
   200  					Sidecar:    "sidecar",
   201  				},
   202  				GCSConfiguration: &kube.GCSConfiguration{
   203  					Bucket:       "bucket",
   204  					PathPrefix:   "prefix",
   205  					PathStrategy: kube.PathStrategyLegacy,
   206  					DefaultOrg:   "org",
   207  					DefaultRepo:  "repo",
   208  				},
   209  				GCSCredentialsSecret: "somethingSecret",
   210  				SSHKeySecrets:        []string{"first", "second"},
   211  			},
   212  		},
   213  		{
   214  			name: "ssh secrets provided",
   215  			provided: &kube.DecorationConfig{
   216  				SSHKeySecrets: []string{"my", "special"},
   217  			},
   218  			expected: &kube.DecorationConfig{
   219  				Timeout:     1 * time.Minute,
   220  				GracePeriod: 10 * time.Second,
   221  				UtilityImages: &kube.UtilityImages{
   222  					CloneRefs:  "clonerefs",
   223  					InitUpload: "iniupload",
   224  					Entrypoint: "entrypoint",
   225  					Sidecar:    "sidecar",
   226  				},
   227  				GCSConfiguration: &kube.GCSConfiguration{
   228  					Bucket:       "bucket",
   229  					PathPrefix:   "prefix",
   230  					PathStrategy: kube.PathStrategyLegacy,
   231  					DefaultOrg:   "org",
   232  					DefaultRepo:  "repo",
   233  				},
   234  				GCSCredentialsSecret: "secretName",
   235  				SSHKeySecrets:        []string{"my", "special"},
   236  			},
   237  		},
   238  	}
   239  
   240  	for _, testCase := range testCases {
   241  		if actual, expected := setDecorationDefaults(testCase.provided, defaults), testCase.expected; !reflect.DeepEqual(actual, expected) {
   242  			t.Errorf("%s: expected defaulted config %v but got %v", testCase.name, expected, actual)
   243  		}
   244  	}
   245  }
   246  
   247  // integration test for fake config loading
   248  func TestValidConfigLoading(t *testing.T) {
   249  	var testCases = []struct {
   250  		name               string
   251  		prowConfig         string
   252  		jobConfigs         []string
   253  		expectError        bool
   254  		expectPodNameSpace string
   255  		expectEnv          map[string][]v1.EnvVar
   256  	}{
   257  		{
   258  			name:       "one config",
   259  			prowConfig: ``,
   260  		},
   261  		{
   262  			name:       "invalid periodic",
   263  			prowConfig: ``,
   264  			jobConfigs: []string{
   265  				`
   266  periodics:
   267  - interval: 10m
   268    agent: kubernetes
   269    name: foo`,
   270  			},
   271  			expectError: true,
   272  		},
   273  		{
   274  			name:       "one periodic",
   275  			prowConfig: ``,
   276  			jobConfigs: []string{
   277  				`
   278  periodics:
   279  - interval: 10m
   280    agent: kubernetes
   281    name: foo
   282    spec:
   283      containers:
   284      - image: alpine`,
   285  			},
   286  		},
   287  		{
   288  			name:       "two periodics",
   289  			prowConfig: ``,
   290  			jobConfigs: []string{
   291  				`
   292  periodics:
   293  - interval: 10m
   294    agent: kubernetes
   295    name: foo
   296    spec:
   297      containers:
   298      - image: alpine`,
   299  				`
   300  periodics:
   301  - interval: 10m
   302    agent: kubernetes
   303    name: bar
   304    spec:
   305      containers:
   306      - image: alpine`,
   307  			},
   308  		},
   309  		{
   310  			name:       "duplicated periodics",
   311  			prowConfig: ``,
   312  			jobConfigs: []string{
   313  				`
   314  periodics:
   315  - interval: 10m
   316    agent: kubernetes
   317    name: foo
   318    spec:
   319      containers:
   320      - image: alpine`,
   321  				`
   322  periodics:
   323  - interval: 10m
   324    agent: kubernetes
   325    name: foo
   326    spec:
   327      containers:
   328      - image: alpine`,
   329  			},
   330  			expectError: true,
   331  		},
   332  		{
   333  			name:       "one presubmit, no context",
   334  			prowConfig: ``,
   335  			jobConfigs: []string{
   336  				`
   337  presubmits:
   338    foo/bar:
   339    - agent: kubernetes
   340      name: presubmit-bar
   341      spec:
   342        containers:
   343        - image: alpine`,
   344  			},
   345  			expectError: true,
   346  		},
   347  		{
   348  			name:       "one presubmit, ok",
   349  			prowConfig: ``,
   350  			jobConfigs: []string{
   351  				`
   352  presubmits:
   353    foo/bar:
   354    - agent: kubernetes
   355      name: presubmit-bar
   356      context: bar
   357      spec:
   358        containers:
   359        - image: alpine`,
   360  			},
   361  		},
   362  		{
   363  			name:       "two presubmits",
   364  			prowConfig: ``,
   365  			jobConfigs: []string{
   366  				`
   367  presubmits:
   368    foo/bar:
   369    - agent: kubernetes
   370      name: presubmit-bar
   371      context: bar
   372      spec:
   373        containers:
   374        - image: alpine`,
   375  				`
   376  presubmits:
   377    foo/baz:
   378    - agent: kubernetes
   379      name: presubmit-baz
   380      context: baz
   381      spec:
   382        containers:
   383        - image: alpine`,
   384  			},
   385  		},
   386  		{
   387  			name:       "dup presubmits, one file",
   388  			prowConfig: ``,
   389  			jobConfigs: []string{
   390  				`
   391  presubmits:
   392    foo/bar:
   393    - agent: kubernetes
   394      name: presubmit-bar
   395      context: bar
   396      spec:
   397        containers:
   398        - image: alpine
   399    - agent: kubernetes
   400      name: presubmit-bar
   401      context: bar
   402      spec:
   403        containers:
   404        - image: alpine`,
   405  			},
   406  			expectError: true,
   407  		},
   408  		{
   409  			name:       "dup presubmits, two files",
   410  			prowConfig: ``,
   411  			jobConfigs: []string{
   412  				`
   413  presubmits:
   414    foo/bar:
   415    - agent: kubernetes
   416      name: presubmit-bar
   417      context: bar
   418      spec:
   419        containers:
   420        - image: alpine`,
   421  				`
   422  presubmits:
   423    foo/bar:
   424    - agent: kubernetes
   425      context: bar
   426      name: presubmit-bar
   427      spec:
   428        containers:
   429        - image: alpine`,
   430  			},
   431  			expectError: true,
   432  		},
   433  		{
   434  			name:       "dup presubmits not the same branch, two files",
   435  			prowConfig: ``,
   436  			jobConfigs: []string{
   437  				`
   438  presubmits:
   439    foo/bar:
   440    - agent: kubernetes
   441      name: presubmit-bar
   442      context: bar
   443      branches:
   444      - master
   445      spec:
   446        containers:
   447        - image: alpine`,
   448  				`
   449  presubmits:
   450    foo/bar:
   451    - agent: kubernetes
   452      context: bar
   453      branches:
   454      - other
   455      name: presubmit-bar
   456      spec:
   457        containers:
   458        - image: alpine`,
   459  			},
   460  			expectError: false,
   461  		},
   462  		{
   463  			name: "dup presubmits main file",
   464  			prowConfig: `
   465  presubmits:
   466    foo/bar:
   467    - agent: kubernetes
   468      name: presubmit-bar
   469      context: bar
   470      spec:
   471        containers:
   472        - image: alpine
   473    - agent: kubernetes
   474      context: bar
   475      name: presubmit-bar
   476      spec:
   477        containers:
   478        - image: alpine`,
   479  			expectError: true,
   480  		},
   481  		{
   482  			name: "dup presubmits main file not on the same branch",
   483  			prowConfig: `
   484  presubmits:
   485    foo/bar:
   486    - agent: kubernetes
   487      name: presubmit-bar
   488      context: bar
   489      branches:
   490      - other
   491      spec:
   492        containers:
   493        - image: alpine
   494    - agent: kubernetes
   495      context: bar
   496      branches:
   497      - master
   498      name: presubmit-bar
   499      spec:
   500        containers:
   501        - image: alpine`,
   502  			expectError: false,
   503  		},
   504  
   505  		{
   506  			name:       "one postsubmit, ok",
   507  			prowConfig: ``,
   508  			jobConfigs: []string{
   509  				`
   510  postsubmits:
   511    foo/bar:
   512    - agent: kubernetes
   513      name: postsubmit-bar
   514      spec:
   515        containers:
   516        - image: alpine`,
   517  			},
   518  		},
   519  		{
   520  			name:       "two postsubmits",
   521  			prowConfig: ``,
   522  			jobConfigs: []string{
   523  				`
   524  postsubmits:
   525    foo/bar:
   526    - agent: kubernetes
   527      name: postsubmit-bar
   528      context: bar
   529      spec:
   530        containers:
   531        - image: alpine`,
   532  				`
   533  postsubmits:
   534    foo/baz:
   535    - agent: kubernetes
   536      name: postsubmit-baz
   537      context: baz
   538      spec:
   539        containers:
   540        - image: alpine`,
   541  			},
   542  		},
   543  		{
   544  			name:       "dup postsubmits, one file",
   545  			prowConfig: ``,
   546  			jobConfigs: []string{
   547  				`
   548  postsubmits:
   549    foo/bar:
   550    - agent: kubernetes
   551      name: postsubmit-bar
   552      context: bar
   553      spec:
   554        containers:
   555        - image: alpine
   556    - agent: kubernetes
   557      name: postsubmit-bar
   558      context: bar
   559      spec:
   560        containers:
   561        - image: alpine`,
   562  			},
   563  			expectError: true,
   564  		},
   565  		{
   566  			name:       "dup postsubmits, two files",
   567  			prowConfig: ``,
   568  			jobConfigs: []string{
   569  				`
   570  postsubmits:
   571    foo/bar:
   572    - agent: kubernetes
   573      name: postsubmit-bar
   574      context: bar
   575      spec:
   576        containers:
   577        - image: alpine`,
   578  				`
   579  postsubmits:
   580    foo/bar:
   581    - agent: kubernetes
   582      context: bar
   583      name: postsubmit-bar
   584      spec:
   585        containers:
   586        - image: alpine`,
   587  			},
   588  			expectError: true,
   589  		},
   590  		{
   591  			name: "overwrite PodNamespace",
   592  			prowConfig: `
   593  pod_namespace: test`,
   594  			jobConfigs: []string{
   595  				`
   596  pod_namespace: debug`,
   597  			},
   598  			expectPodNameSpace: "test",
   599  		},
   600  		{
   601  			name: "test valid presets in main config",
   602  			prowConfig: `
   603  presets:
   604  - labels:
   605      preset-baz: "true"
   606    env:
   607    - name: baz
   608      value: fejtaverse`,
   609  			jobConfigs: []string{
   610  				`periodics:
   611  - interval: 10m
   612    agent: kubernetes
   613    name: foo
   614    labels:
   615      preset-baz: "true"
   616    spec:
   617      containers:
   618      - image: alpine`,
   619  				`
   620  periodics:
   621  - interval: 10m
   622    agent: kubernetes
   623    name: bar
   624    labels:
   625      preset-baz: "true"
   626    spec:
   627      containers:
   628      - image: alpine`,
   629  			},
   630  			expectEnv: map[string][]v1.EnvVar{
   631  				"foo": {
   632  					{
   633  						Name:  "baz",
   634  						Value: "fejtaverse",
   635  					},
   636  				},
   637  				"bar": {
   638  					{
   639  						Name:  "baz",
   640  						Value: "fejtaverse",
   641  					},
   642  				},
   643  			},
   644  		},
   645  		{
   646  			name:       "test valid presets in job configs",
   647  			prowConfig: ``,
   648  			jobConfigs: []string{
   649  				`
   650  presets:
   651  - labels:
   652      preset-baz: "true"
   653    env:
   654    - name: baz
   655      value: fejtaverse
   656  periodics:
   657  - interval: 10m
   658    agent: kubernetes
   659    name: foo
   660    labels:
   661      preset-baz: "true"
   662    spec:
   663      containers:
   664      - image: alpine`,
   665  				`
   666  periodics:
   667  - interval: 10m
   668    agent: kubernetes
   669    name: bar
   670    labels:
   671      preset-baz: "true"
   672    spec:
   673      containers:
   674      - image: alpine`,
   675  			},
   676  			expectEnv: map[string][]v1.EnvVar{
   677  				"foo": {
   678  					{
   679  						Name:  "baz",
   680  						Value: "fejtaverse",
   681  					},
   682  				},
   683  				"bar": {
   684  					{
   685  						Name:  "baz",
   686  						Value: "fejtaverse",
   687  					},
   688  				},
   689  			},
   690  		},
   691  		{
   692  			name: "test valid presets in both main & job configs",
   693  			prowConfig: `
   694  presets:
   695  - labels:
   696      preset-baz: "true"
   697    env:
   698    - name: baz
   699      value: fejtaverse`,
   700  			jobConfigs: []string{
   701  				`
   702  presets:
   703  - labels:
   704      preset-k8s: "true"
   705    env:
   706    - name: k8s
   707      value: kubernetes
   708  periodics:
   709  - interval: 10m
   710    agent: kubernetes
   711    name: foo
   712    labels:
   713      preset-baz: "true"
   714      preset-k8s: "true"
   715    spec:
   716      containers:
   717      - image: alpine`,
   718  				`
   719  periodics:
   720  - interval: 10m
   721    agent: kubernetes
   722    name: bar
   723    labels:
   724      preset-baz: "true"
   725    spec:
   726      containers:
   727      - image: alpine`,
   728  			},
   729  			expectEnv: map[string][]v1.EnvVar{
   730  				"foo": {
   731  					{
   732  						Name:  "baz",
   733  						Value: "fejtaverse",
   734  					},
   735  					{
   736  						Name:  "k8s",
   737  						Value: "kubernetes",
   738  					},
   739  				},
   740  				"bar": {
   741  					{
   742  						Name:  "baz",
   743  						Value: "fejtaverse",
   744  					},
   745  				},
   746  			},
   747  		},
   748  	}
   749  
   750  	for _, tc := range testCases {
   751  		// save the config
   752  		prowConfigDir, err := ioutil.TempDir("", "prowConfig")
   753  		if err != nil {
   754  			t.Fatalf("fail to make tempdir: %v", err)
   755  		}
   756  		defer os.RemoveAll(prowConfigDir)
   757  
   758  		prowConfig := filepath.Join(prowConfigDir, "config.yaml")
   759  		if err := ioutil.WriteFile(prowConfig, []byte(tc.prowConfig), 0666); err != nil {
   760  			t.Fatalf("fail to write prow config: %v", err)
   761  		}
   762  
   763  		jobConfig := ""
   764  		if len(tc.jobConfigs) > 0 {
   765  			jobConfigDir, err := ioutil.TempDir("", "jobConfig")
   766  			if err != nil {
   767  				t.Fatalf("fail to make tempdir: %v", err)
   768  			}
   769  			defer os.RemoveAll(jobConfigDir)
   770  
   771  			// cover both job config as a file & a dir
   772  			if len(tc.jobConfigs) == 1 {
   773  				// a single file
   774  				jobConfig = filepath.Join(jobConfigDir, "config.yaml")
   775  				if err := ioutil.WriteFile(jobConfig, []byte(tc.jobConfigs[0]), 0666); err != nil {
   776  					t.Fatalf("fail to write job config: %v", err)
   777  				}
   778  			} else {
   779  				// a dir
   780  				jobConfig = jobConfigDir
   781  				for idx, config := range tc.jobConfigs {
   782  					subConfig := filepath.Join(jobConfigDir, fmt.Sprintf("config_%d.yaml", idx))
   783  					if err := ioutil.WriteFile(subConfig, []byte(config), 0666); err != nil {
   784  						t.Fatalf("fail to write job config: %v", err)
   785  					}
   786  				}
   787  			}
   788  		}
   789  
   790  		cfg, err := Load(prowConfig, jobConfig)
   791  		if tc.expectError && err == nil {
   792  			t.Errorf("tc %s: Expect error, but got nil", tc.name)
   793  		} else if !tc.expectError && err != nil {
   794  			t.Errorf("tc %s: Expect no error, but got error %v", tc.name, err)
   795  		}
   796  
   797  		if err == nil {
   798  			if tc.expectPodNameSpace == "" {
   799  				tc.expectPodNameSpace = "default"
   800  			}
   801  
   802  			if cfg.PodNamespace != tc.expectPodNameSpace {
   803  				t.Errorf("tc %s: Expect PodNamespace %s, but got %v", tc.name, tc.expectPodNameSpace, cfg.PodNamespace)
   804  			}
   805  
   806  			if len(tc.expectEnv) > 0 {
   807  				for _, j := range cfg.AllPresubmits(nil) {
   808  					if envs, ok := tc.expectEnv[j.Name]; ok {
   809  						if !reflect.DeepEqual(envs, j.Spec.Containers[0].Env) {
   810  							t.Errorf("tc %s: expect env %v for job %s, got %+v", tc.name, envs, j.Name, j.Spec.Containers[0].Env)
   811  						}
   812  					}
   813  				}
   814  
   815  				for _, j := range cfg.AllPostsubmits(nil) {
   816  					if envs, ok := tc.expectEnv[j.Name]; ok {
   817  						if !reflect.DeepEqual(envs, j.Spec.Containers[0].Env) {
   818  							t.Errorf("tc %s: expect env %v for job %s, got %+v", tc.name, envs, j.Name, j.Spec.Containers[0].Env)
   819  						}
   820  					}
   821  				}
   822  
   823  				for _, j := range cfg.AllPeriodics() {
   824  					if envs, ok := tc.expectEnv[j.Name]; ok {
   825  						if !reflect.DeepEqual(envs, j.Spec.Containers[0].Env) {
   826  							t.Errorf("tc %s: expect env %v for job %s, got %+v", tc.name, envs, j.Name, j.Spec.Containers[0].Env)
   827  						}
   828  					}
   829  				}
   830  			}
   831  		}
   832  	}
   833  }
   834  
   835  func TestBrancher_Intersects(t *testing.T) {
   836  	testCases := []struct {
   837  		name   string
   838  		a, b   Brancher
   839  		result bool
   840  	}{
   841  		{
   842  			name: "TwodifferentBranches",
   843  			a: Brancher{
   844  				Branches: []string{"a"},
   845  			},
   846  			b: Brancher{
   847  				Branches: []string{"b"},
   848  			},
   849  		},
   850  		{
   851  			name: "Opposite",
   852  			a: Brancher{
   853  				SkipBranches: []string{"b"},
   854  			},
   855  			b: Brancher{
   856  				Branches: []string{"b"},
   857  			},
   858  		},
   859  		{
   860  			name:   "BothRunOnAllBranches",
   861  			a:      Brancher{},
   862  			b:      Brancher{},
   863  			result: true,
   864  		},
   865  		{
   866  			name: "RunsOnAllBranchesAndSpecified",
   867  			a:    Brancher{},
   868  			b: Brancher{
   869  				Branches: []string{"b"},
   870  			},
   871  			result: true,
   872  		},
   873  		{
   874  			name: "SkipBranchesAndSet",
   875  			a: Brancher{
   876  				SkipBranches: []string{"a", "b", "c"},
   877  			},
   878  			b: Brancher{
   879  				Branches: []string{"a"},
   880  			},
   881  		},
   882  		{
   883  			name: "SkipBranchesAndSet",
   884  			a: Brancher{
   885  				Branches: []string{"c"},
   886  			},
   887  			b: Brancher{
   888  				Branches: []string{"a"},
   889  			},
   890  		},
   891  		{
   892  			name: "BothSkipBranches",
   893  			a: Brancher{
   894  				SkipBranches: []string{"a", "b", "c"},
   895  			},
   896  			b: Brancher{
   897  				SkipBranches: []string{"d", "e", "f"},
   898  			},
   899  			result: true,
   900  		},
   901  		{
   902  			name: "BothSkipCommonBranches",
   903  			a: Brancher{
   904  				SkipBranches: []string{"a", "b", "c"},
   905  			},
   906  			b: Brancher{
   907  				SkipBranches: []string{"b", "e", "f"},
   908  			},
   909  			result: true,
   910  		},
   911  	}
   912  
   913  	for _, tc := range testCases {
   914  		t.Run(tc.name, func(st *testing.T) {
   915  			r1 := tc.a.Intersects(tc.b)
   916  			r2 := tc.b.Intersects(tc.a)
   917  			for _, result := range []bool{r1, r2} {
   918  				if result != tc.result {
   919  					st.Errorf("Expected %v got %v", tc.result, result)
   920  				}
   921  			}
   922  		})
   923  	}
   924  }
   925  
   926  // Integration test for fake secrets loading in a secret agent.
   927  // Checking also if the agent changes the secret's values as expected.
   928  func TestSecretAgentLoading(t *testing.T) {
   929  	tempTokenValue := "121f3cb3e7f70feeb35f9204f5a988d7292c7ba1"
   930  	changedTokenValue := "121f3cb3e7f70feeb35f9204f5a988d7292c7ba0"
   931  
   932  	// Creating a temporary directory.
   933  	secretDir, err := ioutil.TempDir("", "secretDir")
   934  	if err != nil {
   935  		t.Fatalf("fail to create a temporary directory: %v", err)
   936  	}
   937  	defer os.RemoveAll(secretDir)
   938  
   939  	// Create the first temporary secret.
   940  	firstTempSecret := filepath.Join(secretDir, "firstTempSecret")
   941  	if err := ioutil.WriteFile(firstTempSecret, []byte(tempTokenValue), 0666); err != nil {
   942  		t.Fatalf("fail to write secret: %v", err)
   943  	}
   944  
   945  	// Create the second temporary secret.
   946  	secondTempSecret := filepath.Join(secretDir, "secondTempSecret")
   947  	if err := ioutil.WriteFile(secondTempSecret, []byte(tempTokenValue), 0666); err != nil {
   948  		t.Fatalf("fail to write secret: %v", err)
   949  	}
   950  
   951  	tempSecrets := []string{firstTempSecret, secondTempSecret}
   952  	// Starting the agent and add the two temporary secrets.
   953  	secretAgent := &SecretAgent{}
   954  	if err := secretAgent.Start(tempSecrets); err != nil {
   955  		t.Fatalf("Error starting secrets agent. %v", err)
   956  	}
   957  
   958  	// Check if the values are as expected.
   959  	for _, tempSecret := range tempSecrets {
   960  		tempSecretValue := secretAgent.GetSecret(tempSecret)
   961  		if string(tempSecretValue) != tempTokenValue {
   962  			t.Fatalf("In secret %s it was expected %s but found %s",
   963  				tempSecret, tempTokenValue, tempSecretValue)
   964  		}
   965  	}
   966  
   967  	// Change the values of the files.
   968  	if err := ioutil.WriteFile(firstTempSecret, []byte(changedTokenValue), 0666); err != nil {
   969  		t.Fatalf("fail to write secret: %v", err)
   970  	}
   971  	if err := ioutil.WriteFile(secondTempSecret, []byte(changedTokenValue), 0666); err != nil {
   972  		t.Fatalf("fail to write secret: %v", err)
   973  	}
   974  
   975  	retries := 10
   976  	var errors []string
   977  
   978  	// Check if the values changed as expected.
   979  	for _, tempSecret := range tempSecrets {
   980  		// Reset counter
   981  		counter := 0
   982  		for counter <= retries {
   983  			tempSecretValue := secretAgent.GetSecret(tempSecret)
   984  			if string(tempSecretValue) != changedTokenValue {
   985  				if counter == retries {
   986  					errors = append(errors, fmt.Sprintf("In secret %s it was expected %s but found %s\n",
   987  						tempSecret, changedTokenValue, tempSecretValue))
   988  				} else {
   989  					// Secret agent needs some time to update the values. So wait and retry.
   990  					time.Sleep(100 * time.Millisecond)
   991  				}
   992  			} else {
   993  				break
   994  			}
   995  			counter++
   996  		}
   997  	}
   998  
   999  	if len(errors) > 0 {
  1000  		t.Fatal(errors)
  1001  	}
  1002  
  1003  }