github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/action/action_test.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 package action 17 18 import ( 19 "flag" 20 "io" 21 "io/ioutil" 22 "testing" 23 "time" 24 25 "github.com/pkg/errors" 26 27 "helm.sh/helm/pkg/chart" 28 "helm.sh/helm/pkg/chartutil" 29 "helm.sh/helm/pkg/kube" 30 "helm.sh/helm/pkg/release" 31 "helm.sh/helm/pkg/storage" 32 "helm.sh/helm/pkg/storage/driver" 33 ) 34 35 var verbose = flag.Bool("test.log", false, "enable test logging") 36 37 func actionConfigFixture(t *testing.T) *Configuration { 38 t.Helper() 39 40 return &Configuration{ 41 Releases: storage.Init(driver.NewMemory()), 42 KubeClient: &kube.PrintingKubeClient{Out: ioutil.Discard}, 43 Capabilities: chartutil.DefaultCapabilities, 44 Log: func(format string, v ...interface{}) { 45 t.Helper() 46 if *verbose { 47 t.Logf(format, v...) 48 } 49 }, 50 } 51 } 52 53 var manifestWithHook = `kind: ConfigMap 54 metadata: 55 name: test-cm 56 annotations: 57 "helm.sh/hook": post-install,pre-delete 58 data: 59 name: value` 60 61 var manifestWithTestHook = `kind: Pod 62 metadata: 63 name: finding-nemo, 64 annotations: 65 "helm.sh/hook": test-success 66 spec: 67 containers: 68 - name: nemo-test 69 image: fake-image 70 cmd: fake-command 71 ` 72 73 type chartOptions struct { 74 *chart.Chart 75 } 76 77 type chartOption func(*chartOptions) 78 79 func buildChart(opts ...chartOption) *chart.Chart { 80 c := &chartOptions{ 81 Chart: &chart.Chart{ 82 // TODO: This should be more complete. 83 Metadata: &chart.Metadata{ 84 Name: "hello", 85 }, 86 // This adds a basic template and hooks. 87 Templates: []*chart.File{ 88 {Name: "templates/hello", Data: []byte("hello: world")}, 89 {Name: "templates/hooks", Data: []byte(manifestWithHook)}, 90 }, 91 }, 92 } 93 94 for _, opt := range opts { 95 opt(c) 96 } 97 98 return c.Chart 99 } 100 101 func withNotes(notes string) chartOption { 102 return func(opts *chartOptions) { 103 opts.Templates = append(opts.Templates, &chart.File{ 104 Name: "templates/NOTES.txt", 105 Data: []byte(notes), 106 }) 107 } 108 } 109 110 func withDependency(dependencyOpts ...chartOption) chartOption { 111 return func(opts *chartOptions) { 112 opts.AddDependency(buildChart(dependencyOpts...)) 113 } 114 } 115 116 func withSampleTemplates() chartOption { 117 return func(opts *chartOptions) { 118 sampleTemplates := []*chart.File{ 119 // This adds basic templates and partials. 120 {Name: "templates/goodbye", Data: []byte("goodbye: world")}, 121 {Name: "templates/empty", Data: []byte("")}, 122 {Name: "templates/with-partials", Data: []byte(`hello: {{ template "_planet" . }}`)}, 123 {Name: "templates/partials/_planet", Data: []byte(`{{define "_planet"}}Earth{{end}}`)}, 124 } 125 opts.Templates = append(opts.Templates, sampleTemplates...) 126 } 127 } 128 129 func withKube(version string) chartOption { 130 return func(opts *chartOptions) { 131 opts.Metadata.KubeVersion = version 132 } 133 } 134 135 // releaseStub creates a release stub, complete with the chartStub as its chart. 136 func releaseStub() *release.Release { 137 return namedReleaseStub("angry-panda", release.StatusDeployed) 138 } 139 140 func namedReleaseStub(name string, status release.Status) *release.Release { 141 now := time.Now() 142 return &release.Release{ 143 Name: name, 144 Info: &release.Info{ 145 FirstDeployed: now, 146 LastDeployed: now, 147 Status: status, 148 Description: "Named Release Stub", 149 }, 150 Chart: buildChart(withSampleTemplates()), 151 Config: map[string]interface{}{"name": "value"}, 152 Version: 1, 153 Hooks: []*release.Hook{ 154 { 155 Name: "test-cm", 156 Kind: "ConfigMap", 157 Path: "test-cm", 158 Manifest: manifestWithHook, 159 Events: []release.HookEvent{ 160 release.HookPostInstall, 161 release.HookPreDelete, 162 }, 163 }, 164 { 165 Name: "finding-nemo", 166 Kind: "Pod", 167 Path: "finding-nemo", 168 Manifest: manifestWithTestHook, 169 Events: []release.HookEvent{ 170 release.HookReleaseTestSuccess, 171 }, 172 }, 173 }, 174 } 175 } 176 177 func newHookFailingKubeClient() *hookFailingKubeClient { 178 return &hookFailingKubeClient{ 179 PrintingKubeClient: kube.PrintingKubeClient{Out: ioutil.Discard}, 180 } 181 } 182 183 type hookFailingKubeClient struct { 184 kube.PrintingKubeClient 185 } 186 187 func (h *hookFailingKubeClient) WatchUntilReady(r io.Reader, timeout time.Duration) error { 188 return errors.New("Failed watch") 189 }