github.com/reynencourt/helm@v3.0.0-beta.3+incompatible/cmd/helm/helm_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 17 package main 18 19 import ( 20 "bytes" 21 "io/ioutil" 22 "os" 23 "strings" 24 "testing" 25 "time" 26 27 shellwords "github.com/mattn/go-shellwords" 28 "github.com/spf13/cobra" 29 30 "helm.sh/helm/internal/test" 31 "helm.sh/helm/pkg/action" 32 "helm.sh/helm/pkg/chartutil" 33 kubefake "helm.sh/helm/pkg/kube/fake" 34 "helm.sh/helm/pkg/release" 35 "helm.sh/helm/pkg/storage" 36 "helm.sh/helm/pkg/storage/driver" 37 ) 38 39 func testTimestamper() time.Time { return time.Unix(242085845, 0).UTC() } 40 41 func init() { 42 action.Timestamper = testTimestamper 43 } 44 45 func runTestCmd(t *testing.T, tests []cmdTestCase) { 46 t.Helper() 47 for _, tt := range tests { 48 t.Run(tt.name, func(t *testing.T) { 49 defer resetEnv()() 50 51 storage := storageFixture() 52 for _, rel := range tt.rels { 53 if err := storage.Create(rel); err != nil { 54 t.Fatal(err) 55 } 56 } 57 t.Log("running cmd: ", tt.cmd) 58 _, out, err := executeActionCommandC(storage, tt.cmd) 59 if (err != nil) != tt.wantError { 60 t.Errorf("expected error, got '%v'", err) 61 } 62 if tt.golden != "" { 63 test.AssertGoldenString(t, out, tt.golden) 64 } 65 }) 66 } 67 } 68 69 func runTestActionCmd(t *testing.T, tests []cmdTestCase) { 70 t.Helper() 71 for _, tt := range tests { 72 t.Run(tt.name, func(t *testing.T) { 73 defer resetEnv()() 74 75 store := storageFixture() 76 for _, rel := range tt.rels { 77 store.Create(rel) 78 } 79 _, out, err := executeActionCommandC(store, tt.cmd) 80 if (err != nil) != tt.wantError { 81 t.Errorf("expected error, got '%v'", err) 82 } 83 if tt.golden != "" { 84 test.AssertGoldenString(t, out, tt.golden) 85 } 86 }) 87 } 88 } 89 90 func storageFixture() *storage.Storage { 91 return storage.Init(driver.NewMemory()) 92 } 93 94 func executeActionCommandC(store *storage.Storage, cmd string) (*cobra.Command, string, error) { 95 args, err := shellwords.Parse(cmd) 96 if err != nil { 97 return nil, "", err 98 } 99 buf := new(bytes.Buffer) 100 101 actionConfig := &action.Configuration{ 102 Releases: store, 103 KubeClient: &kubefake.PrintingKubeClient{Out: ioutil.Discard}, 104 Capabilities: chartutil.DefaultCapabilities, 105 Log: func(format string, v ...interface{}) {}, 106 } 107 108 root := newRootCmd(actionConfig, buf, args) 109 root.SetOutput(buf) 110 root.SetArgs(args) 111 112 c, err := root.ExecuteC() 113 114 return c, buf.String(), err 115 } 116 117 // cmdTestCase describes a test case that works with releases. 118 type cmdTestCase struct { 119 name string 120 cmd string 121 golden string 122 wantError bool 123 // Rels are the available releases at the start of the test. 124 rels []*release.Release 125 } 126 127 func executeActionCommand(cmd string) (*cobra.Command, string, error) { 128 return executeActionCommandC(storageFixture(), cmd) 129 } 130 131 func resetEnv() func() { 132 origSettings, origEnv := settings, os.Environ() 133 return func() { 134 os.Clearenv() 135 settings = origSettings 136 for _, pair := range origEnv { 137 kv := strings.SplitN(pair, "=", 2) 138 os.Setenv(kv[0], kv[1]) 139 } 140 } 141 } 142 143 func testChdir(t *testing.T, dir string) func() { 144 t.Helper() 145 old, err := os.Getwd() 146 if err != nil { 147 t.Fatal(err) 148 } 149 if err := os.Chdir(dir); err != nil { 150 t.Fatal(err) 151 } 152 return func() { os.Chdir(old) } 153 }