github.com/openshift/installer@v1.4.17/pkg/asset/imagebased/configimage/extramanifests_test.go (about) 1 package configimage 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/golang/mock/gomock" 10 "github.com/stretchr/testify/assert" 11 12 "github.com/openshift/installer/pkg/asset" 13 "github.com/openshift/installer/pkg/asset/mock" 14 ) 15 16 func TestExtraManifests_Load(t *testing.T) { 17 cases := []struct { 18 name string 19 files []string 20 yamlFetchError error 21 ymlFetchError error 22 23 expectedFound bool 24 expectedFiles []string 25 expectedError string 26 }{ 27 { 28 name: "no-extra-manifests", 29 files: []string{}, 30 31 expectedFound: false, 32 expectedFiles: []string{}, 33 }, 34 { 35 name: "only-yaml", 36 files: []string{"/extra-manifests/test.yaml"}, 37 38 expectedFound: true, 39 expectedFiles: []string{"/extra-manifests/test.yaml"}, 40 }, 41 { 42 name: "only-yml", 43 files: []string{"/extra-manifests/another-test.yml"}, 44 45 expectedFound: true, 46 expectedFiles: []string{"/extra-manifests/another-test.yml"}, 47 }, 48 { 49 name: "both", 50 files: []string{ 51 "/extra-manifests/test.yaml", 52 "/extra-manifests/another-test.yml", 53 }, 54 55 expectedFound: true, 56 expectedFiles: []string{ 57 "/extra-manifests/test.yaml", 58 "/extra-manifests/another-test.yml", 59 }, 60 }, 61 { 62 name: "error", 63 yamlFetchError: os.ErrNotExist, 64 65 expectedError: "failed to load *.yaml files: file does not exist", 66 }, 67 { 68 name: "error", 69 ymlFetchError: os.ErrNotExist, 70 71 expectedError: "failed to load *.yml files: file does not exist", 72 }, 73 } 74 75 for _, tc := range cases { 76 t.Run(tc.name, func(t *testing.T) { 77 yamlFiles := []*asset.File{} 78 ymlFiles := []*asset.File{} 79 for _, f := range tc.files { 80 assetFile := &asset.File{ 81 Filename: f, 82 Data: []byte(f), 83 } 84 85 switch filepath.Ext(f) { 86 case ".yaml": 87 yamlFiles = append(yamlFiles, assetFile) 88 case ".yml": 89 ymlFiles = append(ymlFiles, assetFile) 90 default: 91 t.Error("invalid extension") 92 } 93 } 94 95 mockCtrl := gomock.NewController(t) 96 defer mockCtrl.Finish() 97 98 fileFetcher := mock.NewMockFileFetcher(mockCtrl) 99 if tc.yamlFetchError != nil { 100 fileFetcher.EXPECT().FetchByPattern("extra-manifests/*.yaml").Return( 101 []*asset.File{}, 102 tc.yamlFetchError, 103 ) 104 } else { 105 fileFetcher.EXPECT().FetchByPattern("extra-manifests/*.yaml").Return(yamlFiles, nil) 106 107 if tc.ymlFetchError != nil { 108 fileFetcher.EXPECT().FetchByPattern("extra-manifests/*.yml").Return( 109 []*asset.File{}, 110 tc.ymlFetchError, 111 ) 112 } else { 113 fileFetcher.EXPECT().FetchByPattern("extra-manifests/*.yml").Return(ymlFiles, nil) 114 } 115 } 116 117 extraManifestsAsset := &ExtraManifests{} 118 found, err := extraManifestsAsset.Load(fileFetcher) 119 120 assert.Equal(t, tc.expectedFound, found) 121 122 if tc.expectedError != "" { 123 assert.Equal(t, tc.expectedError, err.Error()) 124 } else { 125 assert.NoError(t, err) 126 assert.Equal(t, len(tc.expectedFiles), len(extraManifestsAsset.FileList)) 127 for _, f := range tc.expectedFiles { 128 found := false 129 for _, a := range extraManifestsAsset.FileList { 130 if a.Filename == f { 131 found = true 132 break 133 } 134 } 135 assert.True(t, found, fmt.Sprintf("Expected file %s not found", f)) 136 } 137 } 138 }) 139 } 140 }