github.com/openshift/installer@v1.4.17/pkg/asset/agent/manifests/extramanifests_test.go (about)

     1  package manifests
     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  		fetchError error
    21  
    22  		expectedFound bool
    23  		expectedFiles []string
    24  		expectedError string
    25  	}{
    26  		{
    27  			name:  "no-extras",
    28  			files: []string{},
    29  
    30  			expectedFound: false,
    31  			expectedFiles: []string{},
    32  		},
    33  		{
    34  			name:  "just-yaml",
    35  			files: []string{"/openshift/test-configmap.yaml"},
    36  
    37  			expectedFound: true,
    38  			expectedFiles: []string{"/openshift/test-configmap.yaml"},
    39  		},
    40  		{
    41  			name:  "just-yml",
    42  			files: []string{"/openshift/another-test-configmap.yml"},
    43  
    44  			expectedFound: true,
    45  			expectedFiles: []string{"/openshift/another-test-configmap.yml"},
    46  		},
    47  		{
    48  			name: "mixed",
    49  			files: []string{
    50  				"/openshift/test-configmap.yaml",
    51  				"/openshift/another-test-configmap.yml",
    52  			},
    53  
    54  			expectedFound: true,
    55  			expectedFiles: []string{
    56  				"/openshift/test-configmap.yaml",
    57  				"/openshift/another-test-configmap.yml",
    58  			},
    59  		},
    60  		{
    61  			name:       "error",
    62  			fetchError: os.ErrNotExist,
    63  
    64  			expectedError: "failed to load *.yaml files: file does not exist",
    65  		},
    66  	}
    67  
    68  	for _, tc := range cases {
    69  		t.Run(tc.name, func(t *testing.T) {
    70  
    71  			yamlFiles := []*asset.File{}
    72  			ymlFiles := []*asset.File{}
    73  			for _, f := range tc.files {
    74  				assetFile := &asset.File{
    75  					Filename: f,
    76  					Data:     []byte(f),
    77  				}
    78  
    79  				switch filepath.Ext(f) {
    80  				case ".yaml":
    81  					yamlFiles = append(yamlFiles, assetFile)
    82  				case ".yml":
    83  					ymlFiles = append(ymlFiles, assetFile)
    84  				default:
    85  					t.Error("extension not valid")
    86  				}
    87  			}
    88  
    89  			mockCtrl := gomock.NewController(t)
    90  			defer mockCtrl.Finish()
    91  
    92  			fileFetcher := mock.NewMockFileFetcher(mockCtrl)
    93  			fileFetcher.EXPECT().FetchByPattern("openshift/*.yaml").Return(
    94  				yamlFiles,
    95  				tc.fetchError,
    96  			)
    97  			if tc.fetchError == nil {
    98  				fileFetcher.EXPECT().FetchByPattern("openshift/*.yml").Return(
    99  					ymlFiles,
   100  					nil,
   101  				)
   102  			}
   103  
   104  			extraManifestsAsset := &ExtraManifests{}
   105  			found, err := extraManifestsAsset.Load(fileFetcher)
   106  
   107  			assert.Equal(t, tc.expectedFound, found)
   108  			if tc.expectedError != "" {
   109  				assert.Equal(t, tc.expectedError, err.Error())
   110  			} else {
   111  				assert.NoError(t, err)
   112  				assert.Equal(t, len(tc.expectedFiles), len(extraManifestsAsset.FileList))
   113  				for _, f := range tc.expectedFiles {
   114  					found := false
   115  					for _, a := range extraManifestsAsset.FileList {
   116  						if a.Filename == f {
   117  							found = true
   118  							break
   119  						}
   120  					}
   121  					assert.True(t, found, fmt.Sprintf("Expected file %s not found", f))
   122  				}
   123  			}
   124  		})
   125  	}
   126  }