github.com/replicatedhq/ship@v0.55.0/pkg/lifecycle/render/helm/fetch_test.go (about) 1 package helm 2 3 import ( 4 "context" 5 "strings" 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/github" 13 "github.com/replicatedhq/ship/pkg/testing/logger" 14 "github.com/replicatedhq/ship/pkg/testing/matchers" 15 "github.com/spf13/afero" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestFetch(t *testing.T) { 20 tests := []struct { 21 name string 22 asset api.HelmAsset 23 renderRoot string 24 expect string 25 mockExpect func(t *testing.T, gh *github.MockRenderer) 26 expectError string 27 }{ 28 { 29 name: "nil local fails", 30 asset: api.HelmAsset{ 31 Local: nil, 32 GitHub: nil, 33 }, 34 expect: "", 35 expectError: "only 'local', 'github' and 'helm_fetch' chart rendering is supported", 36 }, 37 { 38 name: "local returns location", 39 asset: api.HelmAsset{ 40 Local: &api.LocalHelmOpts{ 41 ChartRoot: "charts/nginx", 42 }, 43 }, 44 expect: "charts/nginx", 45 expectError: "", 46 }, 47 { 48 name: "github fetches from github", 49 asset: api.HelmAsset{ 50 GitHub: &api.GitHubAsset{ 51 Ref: "", 52 Repo: "", 53 Path: "", 54 Source: "", 55 }, 56 }, 57 mockExpect: func(t *testing.T, gh *github.MockRenderer) { 58 gh.EXPECT().Execute( 59 &matchers.Is{ 60 Describe: "is rootFs with empty root path", 61 Test: func(rootFs interface{}) bool { 62 fs, ok := rootFs.(root.Fs) 63 if !ok { 64 return false 65 } 66 return fs.RootPath == "." 67 68 }, 69 }, 70 &matchers.Is{ 71 Describe: "is github asset and has dest overridden", 72 Test: func(asset interface{}) bool { 73 githubAsset, ok := asset.(api.GitHubAsset) 74 if !ok { 75 return false 76 } 77 return strings.HasPrefix(githubAsset.Dest, ".ship/tmp/helmchart") 78 79 }, 80 }, 81 []libyaml.ConfigGroup{}, 82 "", 83 api.ReleaseMetadata{}, 84 map[string]interface{}{}, 85 ).Return(func(ctx context.Context) error { return nil }) 86 }, 87 expect: "/helmchart", 88 expectError: "", 89 }, 90 { 91 name: "github fetches from github with '' root, event though rootFs has installer/", 92 renderRoot: "installer/", 93 asset: api.HelmAsset{ 94 GitHub: &api.GitHubAsset{ 95 Ref: "", 96 Repo: "", 97 Path: "", 98 Source: "", 99 }, 100 }, 101 mockExpect: func(t *testing.T, gh *github.MockRenderer) { 102 gh.EXPECT().Execute( 103 &matchers.Is{ 104 Describe: "is rootFs with empty root path", 105 Test: func(rootFs interface{}) bool { 106 fs, ok := rootFs.(root.Fs) 107 if !ok { 108 return false 109 } 110 return fs.RootPath == "." 111 112 }, 113 }, 114 &matchers.Is{ 115 Describe: "is github asset and has dest overridden", 116 Test: func(asset interface{}) bool { 117 githubAsset, ok := asset.(api.GitHubAsset) 118 if !ok { 119 return false 120 } 121 return strings.HasPrefix(githubAsset.Dest, ".ship/tmp/helmchart") 122 }, 123 }, 124 []libyaml.ConfigGroup{}, 125 "installer/", 126 api.ReleaseMetadata{}, 127 map[string]interface{}{}, 128 ).Return(func(ctx context.Context) error { return nil }) 129 }, 130 expect: "/helmchart", 131 expectError: "", 132 }, 133 } 134 135 for _, test := range tests { 136 t.Run(test.name, func(t *testing.T) { 137 mc := gomock.NewController(t) 138 req := require.New(t) 139 gh := github.NewMockRenderer(mc) 140 mockfs := afero.Afero{Fs: afero.NewMemMapFs()} 141 fetcher := &ClientFetcher{ 142 Logger: &logger.TestLogger{T: t}, 143 GitHub: gh, 144 FS: mockfs, 145 } 146 147 if test.mockExpect != nil { 148 test.mockExpect(t, gh) 149 } 150 151 dest, err := fetcher.FetchChart( 152 context.Background(), 153 test.asset, 154 test.renderRoot, 155 api.ReleaseMetadata{}, 156 []libyaml.ConfigGroup{}, 157 map[string]interface{}{}, 158 ) 159 160 if test.expectError == "" { 161 req.NoError(err) 162 } else { 163 req.Error(err, "expected error "+test.expectError) 164 req.Equal(test.expectError, err.Error()) 165 } 166 167 req.True( 168 strings.Contains(dest, test.expect), 169 "expected %s to have prefix %s", 170 dest, 171 test.expect, 172 ) 173 }) 174 } 175 }