github.com/replicatedhq/ship@v0.55.0/pkg/lifecycle/render/helm/render_test.go (about)

     1  package helm
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/golang/mock/gomock"
     9  	"github.com/replicatedhq/libyaml"
    10  	"github.com/replicatedhq/ship/pkg/api"
    11  	"github.com/replicatedhq/ship/pkg/lifecycle/render/root"
    12  	"github.com/replicatedhq/ship/pkg/test-mocks/helm"
    13  	"github.com/spf13/afero"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestRender(t *testing.T) {
    18  	tests := []struct {
    19  		name        string
    20  		fetchPath   string
    21  		fetchErr    error
    22  		templateErr error
    23  		expectErr   error
    24  	}{
    25  		{
    26  			name:        "fetch fails",
    27  			fetchPath:   "/etc/kfbr",
    28  			fetchErr:    errors.New("fetch failed"),
    29  			templateErr: nil,
    30  			expectErr:   errors.New("fetch chart: fetch failed"),
    31  		},
    32  		{
    33  			name:        "template fails",
    34  			fetchPath:   "/etc/kfbr",
    35  			fetchErr:    nil,
    36  			templateErr: errors.New("template failed"),
    37  			expectErr:   errors.New("execute templating: template failed"),
    38  		},
    39  		{
    40  			name:        "all good",
    41  			fetchPath:   "/etc/kfbr",
    42  			fetchErr:    nil,
    43  			templateErr: nil,
    44  			expectErr:   nil,
    45  		},
    46  	}
    47  	for _, test := range tests {
    48  		t.Run(test.name, func(t *testing.T) {
    49  			mc := gomock.NewController(t)
    50  			mockFetcher := helm.NewMockChartFetcher(mc)
    51  			mockTemplater := helm.NewMockTemplater(mc)
    52  			req := require.New(t)
    53  			renderer := &LocalRenderer{
    54  				Fetcher:   mockFetcher,
    55  				Templater: mockTemplater,
    56  			}
    57  
    58  			rootFs := root.Fs{
    59  				Afero:    afero.Afero{Fs: afero.NewMemMapFs()},
    60  				RootPath: "",
    61  			}
    62  			asset := api.HelmAsset{}
    63  			metadata := api.ReleaseMetadata{}
    64  			templateContext := map[string]interface{}{}
    65  			configGroups := []libyaml.ConfigGroup{}
    66  
    67  			ctx := context.Background()
    68  
    69  			mockFetcher.EXPECT().
    70  				FetchChart(ctx, asset, "", metadata, configGroups, templateContext).
    71  				Return(test.fetchPath, test.fetchErr)
    72  
    73  			if test.fetchErr == nil {
    74  				mockTemplater.EXPECT().
    75  					Template(test.fetchPath, rootFs, asset, metadata, configGroups, templateContext).
    76  					Return(test.templateErr)
    77  			}
    78  
    79  			err := renderer.Execute(
    80  				rootFs,
    81  				asset,
    82  				metadata,
    83  				templateContext,
    84  				configGroups,
    85  			)(ctx)
    86  
    87  			if test.expectErr == nil {
    88  				req.NoError(err)
    89  			} else {
    90  				req.Error(err, "expected error "+test.expectErr.Error())
    91  				req.Equal(test.expectErr.Error(), err.Error())
    92  			}
    93  
    94  		})
    95  	}
    96  }