github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/releasetesting/test_suite_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 releasetesting 18 19 import ( 20 "io" 21 "testing" 22 "time" 23 24 v1 "k8s.io/api/core/v1" 25 26 "helm.sh/helm/pkg/kube" 27 "helm.sh/helm/pkg/release" 28 ) 29 30 const manifestWithTestSuccessHook = ` 31 apiVersion: v1 32 kind: Pod 33 metadata: 34 name: finding-nemo, 35 annotations: 36 "helm.sh/hook": test-success 37 spec: 38 containers: 39 - name: nemo-test 40 image: fake-image 41 cmd: fake-command 42 ` 43 44 const manifestWithTestFailureHook = ` 45 apiVersion: v1 46 kind: Pod 47 metadata: 48 name: gold-rush, 49 annotations: 50 "helm.sh/hook": test-failure 51 spec: 52 containers: 53 - name: gold-finding-test 54 image: fake-gold-finding-image 55 cmd: fake-gold-finding-command 56 ` 57 const manifestWithInstallHooks = `apiVersion: v1 58 kind: ConfigMap 59 metadata: 60 name: test-cm 61 annotations: 62 "helm.sh/hook": post-install,pre-delete 63 data: 64 name: value 65 ` 66 67 func TestRun(t *testing.T) { 68 testManifests := []string{manifestWithTestSuccessHook, manifestWithTestFailureHook} 69 ts := testSuiteFixture(testManifests) 70 env := testEnvFixture() 71 72 go func() { 73 defer close(env.Messages) 74 if err := ts.Run(env); err != nil { 75 t.Error(err) 76 } 77 }() 78 79 for i := 0; i <= 4; i++ { 80 <-env.Messages 81 } 82 if _, ok := <-env.Messages; ok { 83 t.Errorf("Expected 4 messages streamed") 84 } 85 86 if ts.StartedAt.IsZero() { 87 t.Errorf("Expected StartedAt to not be nil. Got: %v", ts.StartedAt) 88 } 89 if ts.CompletedAt.IsZero() { 90 t.Errorf("Expected CompletedAt to not be nil. Got: %v", ts.CompletedAt) 91 } 92 if len(ts.Results) != 2 { 93 t.Errorf("Expected 2 test result. Got %v", len(ts.Results)) 94 } 95 96 result := ts.Results[0] 97 if result.StartedAt.IsZero() { 98 t.Errorf("Expected test StartedAt to not be nil. Got: %v", result.StartedAt) 99 } 100 if result.CompletedAt.IsZero() { 101 t.Errorf("Expected test CompletedAt to not be nil. Got: %v", result.CompletedAt) 102 } 103 if result.Name != "finding-nemo" { 104 t.Errorf("Expected test name to be finding-nemo. Got: %v", result.Name) 105 } 106 if result.Status != release.TestRunSuccess { 107 t.Errorf("Expected test result to be successful, got: %v", result.Status) 108 } 109 result2 := ts.Results[1] 110 if result2.StartedAt.IsZero() { 111 t.Errorf("Expected test StartedAt to not be nil. Got: %v", result2.StartedAt) 112 } 113 if result2.CompletedAt.IsZero() { 114 t.Errorf("Expected test CompletedAt to not be nil. Got: %v", result2.CompletedAt) 115 } 116 if result2.Name != "gold-rush" { 117 t.Errorf("Expected test name to be gold-rush, Got: %v", result2.Name) 118 } 119 if result2.Status != release.TestRunFailure { 120 t.Errorf("Expected test result to be successful, got: %v", result2.Status) 121 } 122 } 123 124 func TestRunEmptyTestSuite(t *testing.T) { 125 ts := testSuiteFixture([]string{}) 126 env := testEnvFixture() 127 128 go func() { 129 defer close(env.Messages) 130 if err := ts.Run(env); err != nil { 131 t.Error(err) 132 } 133 }() 134 135 msg := <-env.Messages 136 if msg.Msg != "No Tests Found" { 137 t.Errorf("Expected message 'No Tests Found', Got: %v", msg.Msg) 138 } 139 140 for range env.Messages { 141 } 142 143 if ts.StartedAt.IsZero() { 144 t.Errorf("Expected StartedAt to not be nil. Got: %v", ts.StartedAt) 145 } 146 if ts.CompletedAt.IsZero() { 147 t.Errorf("Expected CompletedAt to not be nil. Got: %v", ts.CompletedAt) 148 } 149 if len(ts.Results) != 0 { 150 t.Errorf("Expected 0 test result. Got %v", len(ts.Results)) 151 } 152 } 153 154 func TestRunSuccessWithTestFailureHook(t *testing.T) { 155 ts := testSuiteFixture([]string{manifestWithTestFailureHook}) 156 env := testEnvFixture() 157 env.KubeClient = &mockKubeClient{podFail: true} 158 159 go func() { 160 defer close(env.Messages) 161 if err := ts.Run(env); err != nil { 162 t.Error(err) 163 } 164 }() 165 166 for i := 0; i <= 4; i++ { 167 <-env.Messages 168 } 169 if _, ok := <-env.Messages; ok { 170 t.Errorf("Expected 4 messages streamed") 171 } 172 173 if ts.StartedAt.IsZero() { 174 t.Errorf("Expected StartedAt to not be nil. Got: %v", ts.StartedAt) 175 } 176 if ts.CompletedAt.IsZero() { 177 t.Errorf("Expected CompletedAt to not be nil. Got: %v", ts.CompletedAt) 178 } 179 if len(ts.Results) != 1 { 180 t.Errorf("Expected 1 test result. Got %v", len(ts.Results)) 181 } 182 183 result := ts.Results[0] 184 if result.StartedAt.IsZero() { 185 t.Errorf("Expected test StartedAt to not be nil. Got: %v", result.StartedAt) 186 } 187 if result.CompletedAt.IsZero() { 188 t.Errorf("Expected test CompletedAt to not be nil. Got: %v", result.CompletedAt) 189 } 190 if result.Name != "gold-rush" { 191 t.Errorf("Expected test name to be gold-rush, Got: %v", result.Name) 192 } 193 if result.Status != release.TestRunSuccess { 194 t.Errorf("Expected test result to be successful, got: %v", result.Status) 195 } 196 } 197 198 func TestExtractTestManifestsFromHooks(t *testing.T) { 199 testManifests := extractTestManifestsFromHooks(hooksStub) 200 201 if len(testManifests) != 1 { 202 t.Errorf("Expected 1 test manifest, Got: %v", len(testManifests)) 203 } 204 } 205 206 var hooksStub = []*release.Hook{ 207 { 208 Manifest: manifestWithTestSuccessHook, 209 Events: []release.HookEvent{ 210 release.HookReleaseTestSuccess, 211 }, 212 }, 213 { 214 Manifest: manifestWithInstallHooks, 215 Events: []release.HookEvent{ 216 release.HookPostInstall, 217 }, 218 }, 219 } 220 221 func testFixture() *test { 222 return &test{ 223 manifest: manifestWithTestSuccessHook, 224 result: &release.TestRun{}, 225 } 226 } 227 228 func testSuiteFixture(testManifests []string) *TestSuite { 229 testResults := []*release.TestRun{} 230 ts := &TestSuite{ 231 TestManifests: testManifests, 232 Results: testResults, 233 } 234 return ts 235 } 236 237 func testEnvFixture() *Environment { 238 return &Environment{ 239 Namespace: "default", 240 KubeClient: &mockKubeClient{}, 241 Timeout: 1, 242 Messages: make(chan *release.TestReleaseResponse, 1), 243 } 244 } 245 246 type mockKubeClient struct { 247 kube.Interface 248 podFail bool 249 err error 250 } 251 252 func (c *mockKubeClient) WaitAndGetCompletedPodPhase(_ string, _ time.Duration) (v1.PodPhase, error) { 253 if c.podFail { 254 return v1.PodFailed, nil 255 } 256 return v1.PodSucceeded, nil 257 } 258 func (c *mockKubeClient) Create(_ io.Reader) error { return c.err } 259 func (c *mockKubeClient) Delete(_ io.Reader) error { return nil }