github.com/Beeketing/helm@v2.12.1+incompatible/pkg/releasetesting/environment.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 "bytes" 21 "fmt" 22 "log" 23 "time" 24 25 "k8s.io/api/core/v1" 26 27 "k8s.io/helm/pkg/proto/hapi/release" 28 "k8s.io/helm/pkg/proto/hapi/services" 29 "k8s.io/helm/pkg/tiller/environment" 30 ) 31 32 // Environment encapsulates information about where test suite executes and returns results 33 type Environment struct { 34 Namespace string 35 KubeClient environment.KubeClient 36 Stream services.ReleaseService_RunReleaseTestServer 37 Timeout int64 38 } 39 40 func (env *Environment) createTestPod(test *test) error { 41 b := bytes.NewBufferString(test.manifest) 42 if err := env.KubeClient.Create(env.Namespace, b, env.Timeout, false); err != nil { 43 log.Printf(err.Error()) 44 test.result.Info = err.Error() 45 test.result.Status = release.TestRun_FAILURE 46 return err 47 } 48 49 return nil 50 } 51 52 func (env *Environment) getTestPodStatus(test *test) (v1.PodPhase, error) { 53 b := bytes.NewBufferString(test.manifest) 54 status, err := env.KubeClient.WaitAndGetCompletedPodPhase(env.Namespace, b, time.Duration(env.Timeout)*time.Second) 55 if err != nil { 56 log.Printf("Error getting status for pod %s: %s", test.result.Name, err) 57 test.result.Info = err.Error() 58 test.result.Status = release.TestRun_UNKNOWN 59 return status, err 60 } 61 62 return status, err 63 } 64 65 func (env *Environment) streamResult(r *release.TestRun) error { 66 switch r.Status { 67 case release.TestRun_SUCCESS: 68 if err := env.streamSuccess(r.Name); err != nil { 69 return err 70 } 71 case release.TestRun_FAILURE: 72 if err := env.streamFailed(r.Name); err != nil { 73 return err 74 } 75 76 default: 77 if err := env.streamUnknown(r.Name, r.Info); err != nil { 78 return err 79 } 80 } 81 return nil 82 } 83 84 func (env *Environment) streamRunning(name string) error { 85 msg := "RUNNING: " + name 86 return env.streamMessage(msg, release.TestRun_RUNNING) 87 } 88 89 func (env *Environment) streamError(info string) error { 90 msg := "ERROR: " + info 91 return env.streamMessage(msg, release.TestRun_FAILURE) 92 } 93 94 func (env *Environment) streamFailed(name string) error { 95 msg := fmt.Sprintf("FAILED: %s, run `kubectl logs %s --namespace %s` for more info", name, name, env.Namespace) 96 return env.streamMessage(msg, release.TestRun_FAILURE) 97 } 98 99 func (env *Environment) streamSuccess(name string) error { 100 msg := fmt.Sprintf("PASSED: %s", name) 101 return env.streamMessage(msg, release.TestRun_SUCCESS) 102 } 103 104 func (env *Environment) streamUnknown(name, info string) error { 105 msg := fmt.Sprintf("UNKNOWN: %s: %s", name, info) 106 return env.streamMessage(msg, release.TestRun_UNKNOWN) 107 } 108 109 func (env *Environment) streamMessage(msg string, status release.TestRun_Status) error { 110 resp := &services.TestReleaseResponse{Msg: msg, Status: status} 111 return env.Stream.Send(resp) 112 } 113 114 // DeleteTestPods deletes resources given in testManifests 115 func (env *Environment) DeleteTestPods(testManifests []string) { 116 for _, testManifest := range testManifests { 117 err := env.KubeClient.Delete(env.Namespace, bytes.NewBufferString(testManifest)) 118 if err != nil { 119 env.streamError(err.Error()) 120 } 121 } 122 }