github.com/openshift/installer@v1.4.17/pkg/asset/imagebased/image/postdeployment_test.go (about)

     1  package image
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/openshift/installer/pkg/asset"
    12  	"github.com/openshift/installer/pkg/asset/mock"
    13  )
    14  
    15  func TestPostDeployment_Load(t *testing.T) {
    16  	cases := []struct {
    17  		name       string
    18  		fetchError error
    19  
    20  		expectedFound bool
    21  		expectedFile  string
    22  		expectedError string
    23  	}{
    24  		{
    25  			name:       "no-post-deployment-script",
    26  			fetchError: os.ErrNotExist,
    27  
    28  			expectedFound: false,
    29  		},
    30  		{
    31  			name: "post-deployment-script",
    32  
    33  			expectedFound: true,
    34  			expectedFile:  "post.sh",
    35  		},
    36  		{
    37  			name:       "error",
    38  			fetchError: os.ErrPermission,
    39  
    40  			expectedError: "failed to load the post.sh file: permission denied",
    41  		},
    42  	}
    43  
    44  	for _, tc := range cases {
    45  		t.Run(tc.name, func(t *testing.T) {
    46  			assetFile := &asset.File{
    47  				Filename: postDeploymentFilename,
    48  				Data:     []byte("test-bash-script"),
    49  			}
    50  
    51  			mockCtrl := gomock.NewController(t)
    52  			defer mockCtrl.Finish()
    53  
    54  			fileFetcher := mock.NewMockFileFetcher(mockCtrl)
    55  
    56  			if tc.fetchError != nil {
    57  				fileFetcher.EXPECT().FetchByName("post.sh").Return(nil, tc.fetchError)
    58  			} else {
    59  				fileFetcher.EXPECT().FetchByName("post.sh").Return(assetFile, nil)
    60  			}
    61  
    62  			postDeploymentAsset := &PostDeployment{}
    63  			found, err := postDeploymentAsset.Load(fileFetcher)
    64  
    65  			assert.Equal(t, tc.expectedFound, found)
    66  
    67  			if tc.expectedError != "" {
    68  				assert.Equal(t, tc.expectedError, err.Error())
    69  			} else {
    70  				assert.NoError(t, err)
    71  
    72  				if tc.expectedFile != "" {
    73  					assert.True(t,
    74  						tc.expectedFile == postDeploymentAsset.File.Filename,
    75  						fmt.Sprintf("Expected file %s not found", tc.expectedFile))
    76  				}
    77  			}
    78  		})
    79  	}
    80  }