github.com/openshift/installer@v1.4.17/pkg/asset/imagebased/configimage/imagedigestsources_test.go (about) 1 package configimage 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 "os" 8 "testing" 9 10 "github.com/golang/mock/gomock" 11 "github.com/stretchr/testify/assert" 12 13 "github.com/openshift/installer/pkg/asset" 14 "github.com/openshift/installer/pkg/asset/mock" 15 "github.com/openshift/installer/pkg/types" 16 ) 17 18 func TestImageDigestSources_Generate(t *testing.T) { 19 installConfigWithImageDigestSources := defaultInstallConfig() 20 installConfigWithImageDigestSources.Config.ImageDigestSources = imageDigestSources() 21 22 cases := []struct { 23 name string 24 dependencies []asset.Asset 25 expectedError string 26 expectedConfig []types.ImageDigestSource 27 }{ 28 { 29 name: "missing install config should not generate image-digest-sources.json", 30 dependencies: []asset.Asset{ 31 &InstallConfig{}, 32 }, 33 }, 34 { 35 name: "missing ImageDigestSources in install config should not generate image-digest-sources.json", 36 dependencies: []asset.Asset{ 37 defaultInstallConfig(), 38 }, 39 }, 40 { 41 name: "valid configuration", 42 dependencies: []asset.Asset{ 43 installConfigWithImageDigestSources, 44 }, 45 expectedConfig: imageDigestSources(), 46 }, 47 } 48 for _, tc := range cases { 49 t.Run(tc.name, func(t *testing.T) { 50 parents := asset.Parents{} 51 parents.Add(tc.dependencies...) 52 53 asset := &ImageDigestSources{} 54 err := asset.Generate(context.TODO(), parents) 55 56 switch { 57 case tc.expectedError != "": 58 assert.Equal(t, tc.expectedError, err.Error()) 59 case tc.expectedConfig == nil: 60 assert.NoError(t, err) 61 assert.Empty(t, asset.Files()) 62 default: 63 assert.NoError(t, err) 64 assert.Equal(t, tc.expectedConfig, asset.Config) 65 assert.NotEmpty(t, asset.Files()) 66 67 configFile := asset.Files()[0] 68 assert.Equal(t, "cluster-configuration/manifests/image-digest-sources.json", configFile.Filename) 69 70 var actualConfig []types.ImageDigestSource 71 err = json.Unmarshal(configFile.Data, &actualConfig) 72 assert.NoError(t, err) 73 assert.Equal(t, tc.expectedConfig, actualConfig) 74 } 75 }) 76 } 77 } 78 79 func TestImageDigestSources_LoadedFromDisk(t *testing.T) { 80 cases := []struct { 81 name string 82 data string 83 fetchError error 84 expectedFound bool 85 expectedError string 86 expectedConfig []types.ImageDigestSource 87 }{ 88 { 89 name: "valid-image-digest-sources-file", 90 data: `[{"source":"test.registry.io","mirrors":["another.registry"]}]`, 91 expectedFound: true, 92 expectedConfig: imageDigestSources(), 93 }, 94 { 95 name: "not-json", 96 data: `This is not a JSON file`, 97 expectedError: "failed to unmarshal cluster-configuration/manifests/image-digest-sources.json: invalid JSON syntax", 98 }, 99 { 100 name: "empty", 101 data: "", 102 expectedError: "failed to unmarshal cluster-configuration/manifests/image-digest-sources.json: invalid JSON syntax", 103 }, 104 { 105 name: "file-not-found", 106 fetchError: &os.PathError{Err: os.ErrNotExist}, 107 }, 108 { 109 name: "error-fetching-file", 110 fetchError: errors.New("fetch failed"), 111 expectedError: "failed to load cluster-configuration/manifests/image-digest-sources.json file: fetch failed", 112 }, 113 { 114 name: "unknown-field", 115 data: `[{"wrongField":"wrongValue"}]`, 116 expectedError: "failed to unmarshal cluster-configuration/manifests/image-digest-sources.json: unknown field \"[0].wrongField\"", 117 }, 118 } 119 for _, tc := range cases { 120 t.Run(tc.name, func(t *testing.T) { 121 mockCtrl := gomock.NewController(t) 122 defer mockCtrl.Finish() 123 124 fileFetcher := mock.NewMockFileFetcher(mockCtrl) 125 fileFetcher.EXPECT().FetchByName(imageDigestSourcesFilename). 126 Return( 127 &asset.File{ 128 Filename: imageDigestSourcesFilename, 129 Data: []byte(tc.data)}, 130 tc.fetchError, 131 ) 132 133 asset := &ImageDigestSources{} 134 found, err := asset.Load(fileFetcher) 135 assert.Equal(t, tc.expectedFound, found, "unexpected found value returned from Load") 136 137 if tc.expectedError != "" { 138 assert.Equal(t, tc.expectedError, err.Error()) 139 } else { 140 assert.NoError(t, err) 141 } 142 143 if tc.expectedFound { 144 assert.Equal(t, tc.expectedConfig, asset.Config, "unexpected Config in ImageDigestSources") 145 } 146 }) 147 } 148 } 149 150 func imageDigestSources() []types.ImageDigestSource { 151 return []types.ImageDigestSource{ 152 { 153 Source: "test.registry.io", 154 Mirrors: []string{"another.registry"}, 155 }, 156 } 157 }