github.com/cloudposse/helm@v2.2.3+incompatible/pkg/releasetesting/environment_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors All rights reserved. 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 "bytes" 21 "errors" 22 "io" 23 "os" 24 "testing" 25 26 "k8s.io/helm/pkg/proto/hapi/release" 27 tillerEnv "k8s.io/helm/pkg/tiller/environment" 28 ) 29 30 func TestCreateTestPodSuccess(t *testing.T) { 31 env := testEnvFixture() 32 test := testFixture() 33 34 err := env.createTestPod(test) 35 if err != nil { 36 t.Errorf("Expected no error, got an error: %s", err) 37 } 38 } 39 40 func TestCreateTestPodFailure(t *testing.T) { 41 env := testEnvFixture() 42 env.KubeClient = newCreateFailingKubeClient() 43 test := testFixture() 44 45 err := env.createTestPod(test) 46 if err == nil { 47 t.Errorf("Expected error, got no error") 48 } 49 50 if test.result.Info == "" { 51 t.Errorf("Expected error to be saved in test result info but found empty string") 52 } 53 54 if test.result.Status != release.TestRun_FAILURE { 55 t.Errorf("Expected test result status to be failure but got: %v", test.result.Status) 56 } 57 } 58 59 func TestDeleteTestPods(t *testing.T) { 60 mockTestSuite := testSuiteFixture([]string{manifestWithTestSuccessHook}) 61 mockTestEnv := newMockTestingEnvironment() 62 mockTestEnv.KubeClient = newGetFailingKubeClient() 63 64 mockTestEnv.DeleteTestPods(mockTestSuite.TestManifests) 65 66 stream := mockTestEnv.Stream.(*mockStream) 67 if len(stream.messages) != 0 { 68 t.Errorf("Expected 0 errors, got at least one: %v", stream.messages) 69 } 70 71 for _, testManifest := range mockTestSuite.TestManifests { 72 if _, err := mockTestEnv.KubeClient.Get(mockTestEnv.Namespace, bytes.NewBufferString(testManifest)); err == nil { 73 t.Error("Expected error, got nil") 74 } 75 } 76 } 77 78 func TestDeleteTestPodsFailingDelete(t *testing.T) { 79 mockTestSuite := testSuiteFixture([]string{manifestWithTestSuccessHook}) 80 mockTestEnv := newMockTestingEnvironment() 81 mockTestEnv.KubeClient = newDeleteFailingKubeClient() 82 83 mockTestEnv.DeleteTestPods(mockTestSuite.TestManifests) 84 85 stream := mockTestEnv.Stream.(*mockStream) 86 if len(stream.messages) != 1 { 87 t.Errorf("Expected 1 error, got: %v", len(stream.messages)) 88 } 89 } 90 91 type MockTestingEnvironment struct { 92 *Environment 93 } 94 95 func newMockTestingEnvironment() *MockTestingEnvironment { 96 tEnv := mockTillerEnvironment() 97 98 return &MockTestingEnvironment{ 99 Environment: &Environment{ 100 Namespace: "default", 101 KubeClient: tEnv.KubeClient, 102 Timeout: 5, 103 Stream: &mockStream{}, 104 }, 105 } 106 } 107 108 func (mte MockTestingEnvironment) streamRunning(name string) error { return nil } 109 func (mte MockTestingEnvironment) streamError(info string) error { return nil } 110 func (mte MockTestingEnvironment) streamFailed(name string) error { return nil } 111 func (mte MockTestingEnvironment) streamSuccess(name string) error { return nil } 112 func (mte MockTestingEnvironment) streamUnknown(name, info string) error { return nil } 113 func (mte MockTestingEnvironment) streamMessage(msg string) error { return nil } 114 115 type getFailingKubeClient struct { 116 tillerEnv.PrintingKubeClient 117 } 118 119 func newGetFailingKubeClient() *getFailingKubeClient { 120 return &getFailingKubeClient{ 121 PrintingKubeClient: tillerEnv.PrintingKubeClient{Out: os.Stdout}, 122 } 123 } 124 125 func (p *getFailingKubeClient) Get(ns string, r io.Reader) (string, error) { 126 return "", errors.New("In the end, they did not find Nemo.") 127 } 128 129 type deleteFailingKubeClient struct { 130 tillerEnv.PrintingKubeClient 131 } 132 133 func newDeleteFailingKubeClient() *deleteFailingKubeClient { 134 return &deleteFailingKubeClient{ 135 PrintingKubeClient: tillerEnv.PrintingKubeClient{Out: os.Stdout}, 136 } 137 } 138 139 func (p *deleteFailingKubeClient) Delete(ns string, r io.Reader) error { 140 return errors.New("delete failed") 141 } 142 143 type createFailingKubeClient struct { 144 tillerEnv.PrintingKubeClient 145 } 146 147 func newCreateFailingKubeClient() *createFailingKubeClient { 148 return &createFailingKubeClient{ 149 PrintingKubeClient: tillerEnv.PrintingKubeClient{Out: os.Stdout}, 150 } 151 } 152 153 func (p *createFailingKubeClient) Create(ns string, r io.Reader, t int64, shouldWait bool) error { 154 return errors.New("We ran out of budget and couldn't create finding-nemo") 155 }