mirror of https://github.com/helm/helm
* This is a simple mvp which processes a test definition with the hook annotation for test when you run `helm test [release]` * helm client cmd, proto def, tiller logicpull/1777/head
parent
8d75b0c0c0
commit
d46d63a8f7
@ -0,0 +1,35 @@
|
||||
|
||||
// Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package hapi.release;
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
|
||||
option go_package = "release";
|
||||
|
||||
message TestResult {
|
||||
enum Status {
|
||||
UNKNOWN = 0;
|
||||
SUCCESS = 1;
|
||||
FAILURE = 2;
|
||||
}
|
||||
|
||||
string name = 1;
|
||||
Status status = 2;
|
||||
string info = 3;
|
||||
google.protobuf.Timestamp last_run = 4;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
// Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
syntax = "proto3";
|
||||
|
||||
package hapi.release;
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "hapi/release/test_result.proto";
|
||||
|
||||
option go_package = "release";
|
||||
|
||||
// TestSuite comprises of the last run of the pre-defined test suite of a release version
|
||||
message TestSuite {
|
||||
// LastRun indicates the date/time this test was last run.
|
||||
google.protobuf.Timestamp last_run = 1;
|
||||
|
||||
// Results are the results of each segment of the test
|
||||
repeated hapi.release.TestResult results = 2;
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/gosuri/uitable"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/helm/pkg/helm"
|
||||
//"k8s.io/helm/pkg/proto/hapi/release"
|
||||
)
|
||||
|
||||
const releaseTestDesc = `
|
||||
Th test command runs the tests for a release.
|
||||
|
||||
The argument this command takes is the name of a deployed release.
|
||||
The tests to be run are defined in the chart that was installed.
|
||||
`
|
||||
|
||||
type releaseTestCmd struct {
|
||||
name string
|
||||
out io.Writer
|
||||
client helm.Interface
|
||||
timeout int64
|
||||
}
|
||||
|
||||
func newReleaseTestCmd(c helm.Interface, out io.Writer) *cobra.Command {
|
||||
rlsTest := &releaseTestCmd{
|
||||
out: out,
|
||||
client: c,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "test [RELEASE]",
|
||||
Short: "test a release",
|
||||
Long: releaseTestDesc,
|
||||
PersistentPreRunE: setupConnection,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := checkArgsLength(len(args), "release name"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rlsTest.name = args[0]
|
||||
rlsTest.client = ensureHelmClient(rlsTest.client)
|
||||
return rlsTest.run()
|
||||
},
|
||||
}
|
||||
|
||||
f := cmd.Flags()
|
||||
f.Int64Var(&rlsTest.timeout, "timeout", 300, "time in seconds to wait for any individual kubernetes operation (like Jobs for hooks)")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (t *releaseTestCmd) run() error {
|
||||
res, err := t.client.ReleaseTest(t.name, helm.ReleaseTestTimeout(t.timeout))
|
||||
if err != nil {
|
||||
return prettyError(err)
|
||||
}
|
||||
|
||||
table := uitable.New()
|
||||
table.MaxColWidth = 50
|
||||
table.AddRow("NAME", "Result", "Info")
|
||||
//TODO: change Result to Suite
|
||||
for _, r := range res.Result.Results {
|
||||
table.AddRow(r.Name, r.Status, r.Info)
|
||||
}
|
||||
|
||||
fmt.Fprintln(t.out, table.String()) //TODO: or no tests found
|
||||
return nil
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: "{{.Release.Name}}-service-test"
|
||||
annotations:
|
||||
"helm.sh/hook": test
|
||||
spec:
|
||||
containers:
|
||||
- name: curl
|
||||
image: radial/busyboxplus:curl
|
||||
command: ['curl']
|
||||
args: [ '{{ template "fullname" .}}:{{default 80 .Values.httpPort}}' ]
|
||||
restartPolicy: Never
|
||||
---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: "{{.Release.Name}}-service-failing-test"
|
||||
annotations:
|
||||
"helm.sh/hook": test
|
||||
spec:
|
||||
containers:
|
||||
- name: curl
|
||||
image: radial/busyboxplus:curl
|
||||
command: ['curl']
|
||||
args: [ '{{ template "fullname" .}}-service:{{default 80 .Values.httpPort}}' ]
|
||||
restartPolicy: Never
|
@ -0,0 +1,85 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: hapi/release/test_result.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
package release
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
import google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
type TestResult_Status int32
|
||||
|
||||
const (
|
||||
TestResult_UNKNOWN TestResult_Status = 0
|
||||
TestResult_SUCCESS TestResult_Status = 1
|
||||
TestResult_FAILURE TestResult_Status = 2
|
||||
)
|
||||
|
||||
var TestResult_Status_name = map[int32]string{
|
||||
0: "UNKNOWN",
|
||||
1: "SUCCESS",
|
||||
2: "FAILURE",
|
||||
}
|
||||
var TestResult_Status_value = map[string]int32{
|
||||
"UNKNOWN": 0,
|
||||
"SUCCESS": 1,
|
||||
"FAILURE": 2,
|
||||
}
|
||||
|
||||
func (x TestResult_Status) String() string {
|
||||
return proto.EnumName(TestResult_Status_name, int32(x))
|
||||
}
|
||||
func (TestResult_Status) EnumDescriptor() ([]byte, []int) { return fileDescriptor4, []int{0, 0} }
|
||||
|
||||
type TestResult struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||
Status TestResult_Status `protobuf:"varint,2,opt,name=status,enum=hapi.release.TestResult_Status" json:"status,omitempty"`
|
||||
Info string `protobuf:"bytes,3,opt,name=info" json:"info,omitempty"`
|
||||
LastRun *google_protobuf.Timestamp `protobuf:"bytes,4,opt,name=last_run,json=lastRun" json:"last_run,omitempty"`
|
||||
}
|
||||
|
||||
func (m *TestResult) Reset() { *m = TestResult{} }
|
||||
func (m *TestResult) String() string { return proto.CompactTextString(m) }
|
||||
func (*TestResult) ProtoMessage() {}
|
||||
func (*TestResult) Descriptor() ([]byte, []int) { return fileDescriptor4, []int{0} }
|
||||
|
||||
func (m *TestResult) GetLastRun() *google_protobuf.Timestamp {
|
||||
if m != nil {
|
||||
return m.LastRun
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*TestResult)(nil), "hapi.release.TestResult")
|
||||
proto.RegisterEnum("hapi.release.TestResult_Status", TestResult_Status_name, TestResult_Status_value)
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("hapi/release/test_result.proto", fileDescriptor4) }
|
||||
|
||||
var fileDescriptor4 = []byte{
|
||||
// 244 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x4c, 0x8e, 0x41, 0x4b, 0xc3, 0x30,
|
||||
0x18, 0x86, 0xcd, 0x1c, 0xad, 0xcb, 0x44, 0x4a, 0x4e, 0x65, 0x07, 0x57, 0x76, 0xea, 0x29, 0x81,
|
||||
0x89, 0x78, 0xd6, 0x31, 0x41, 0x94, 0x0a, 0xe9, 0x8a, 0xe0, 0x45, 0x32, 0xf8, 0x36, 0x0b, 0x6d,
|
||||
0x53, 0x9a, 0x2f, 0x3f, 0xd5, 0xff, 0x23, 0x49, 0x5a, 0xf4, 0xf6, 0xbd, 0xbc, 0x6f, 0x9e, 0x3c,
|
||||
0xf4, 0xf6, 0x5b, 0xf5, 0xb5, 0x18, 0xa0, 0x01, 0x65, 0x40, 0x20, 0x18, 0xfc, 0x1a, 0xc0, 0xd8,
|
||||
0x06, 0x79, 0x3f, 0x68, 0xd4, 0xec, 0xda, 0xf5, 0x7c, 0xec, 0x57, 0xeb, 0xb3, 0xd6, 0xe7, 0x06,
|
||||
0x84, 0xef, 0x8e, 0xf6, 0x24, 0xb0, 0x6e, 0xc1, 0xa0, 0x6a, 0xfb, 0x30, 0xdf, 0xfc, 0x10, 0x4a,
|
||||
0x0f, 0x60, 0x50, 0x7a, 0x06, 0x63, 0x74, 0xde, 0xa9, 0x16, 0x52, 0x92, 0x91, 0x7c, 0x21, 0xfd,
|
||||
0xcd, 0x1e, 0x68, 0x64, 0x50, 0xa1, 0x35, 0xe9, 0x2c, 0x23, 0xf9, 0xcd, 0x76, 0xcd, 0xff, 0x7f,
|
||||
0xc1, 0xff, 0x5e, 0xf3, 0xd2, 0xcf, 0xe4, 0x38, 0x77, 0xb0, 0xba, 0x3b, 0xe9, 0xf4, 0x32, 0xc0,
|
||||
0xdc, 0xcd, 0xee, 0xe9, 0x55, 0xa3, 0x9c, 0xb3, 0xed, 0xd2, 0x79, 0x46, 0xf2, 0xe5, 0x76, 0xc5,
|
||||
0x83, 0x23, 0x9f, 0x1c, 0xf9, 0x61, 0x72, 0x94, 0xb1, 0xdb, 0x4a, 0xdb, 0x6d, 0x04, 0x8d, 0x02,
|
||||
0x9c, 0x2d, 0x69, 0x5c, 0x15, 0xaf, 0xc5, 0xfb, 0x47, 0x91, 0x5c, 0xb8, 0x50, 0x56, 0xbb, 0xdd,
|
||||
0xbe, 0x2c, 0x13, 0xe2, 0xc2, 0xf3, 0xe3, 0xcb, 0x5b, 0x25, 0xf7, 0xc9, 0xec, 0x69, 0xf1, 0x19,
|
||||
0x8f, 0x82, 0xc7, 0xc8, 0x83, 0xef, 0x7e, 0x03, 0x00, 0x00, 0xff, 0xff, 0x4c, 0x44, 0x22, 0xbb,
|
||||
0x3a, 0x01, 0x00, 0x00,
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: hapi/release/test_suite.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
package release
|
||||
|
||||
import proto "github.com/golang/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
import google_protobuf "github.com/golang/protobuf/ptypes/timestamp"
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// TestSuite comprises of the last run of the pre-defined test suite of a release version
|
||||
type TestSuite struct {
|
||||
// LastRun indicates the date/time this test was last run.
|
||||
LastRun *google_protobuf.Timestamp `protobuf:"bytes,1,opt,name=last_run,json=lastRun" json:"last_run,omitempty"`
|
||||
// Results are the results of each segment of the test
|
||||
Results []*TestResult `protobuf:"bytes,2,rep,name=results" json:"results,omitempty"`
|
||||
}
|
||||
|
||||
func (m *TestSuite) Reset() { *m = TestSuite{} }
|
||||
func (m *TestSuite) String() string { return proto.CompactTextString(m) }
|
||||
func (*TestSuite) ProtoMessage() {}
|
||||
func (*TestSuite) Descriptor() ([]byte, []int) { return fileDescriptor5, []int{0} }
|
||||
|
||||
func (m *TestSuite) GetLastRun() *google_protobuf.Timestamp {
|
||||
if m != nil {
|
||||
return m.LastRun
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestSuite) GetResults() []*TestResult {
|
||||
if m != nil {
|
||||
return m.Results
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*TestSuite)(nil), "hapi.release.TestSuite")
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("hapi/release/test_suite.proto", fileDescriptor5) }
|
||||
|
||||
var fileDescriptor5 = []byte{
|
||||
// 183 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x64, 0x8e, 0xc1, 0x8a, 0x83, 0x30,
|
||||
0x14, 0x45, 0x71, 0x06, 0xc6, 0x31, 0xce, 0xca, 0x95, 0x08, 0xd3, 0x4a, 0x57, 0xae, 0x5e, 0xc0,
|
||||
0xd2, 0x1f, 0xe8, 0x27, 0xa4, 0xae, 0xba, 0x29, 0x11, 0x5e, 0xad, 0x10, 0x8d, 0xf8, 0x5e, 0xfa,
|
||||
0xfd, 0x25, 0x46, 0xa1, 0xd0, 0xf5, 0x39, 0xdc, 0x73, 0xc5, 0xff, 0x43, 0x4f, 0xbd, 0x9c, 0xd1,
|
||||
0xa0, 0x26, 0x94, 0x8c, 0xc4, 0x37, 0x72, 0x3d, 0x23, 0x4c, 0xb3, 0x65, 0x9b, 0xfd, 0x79, 0x0c,
|
||||
0x2b, 0x2e, 0xf6, 0x9d, 0xb5, 0x9d, 0x41, 0xb9, 0xb0, 0xd6, 0xdd, 0x25, 0xf7, 0x03, 0x12, 0xeb,
|
||||
0x61, 0x0a, 0x7a, 0xb1, 0xfb, 0x5c, 0x9b, 0x91, 0x9c, 0xe1, 0xc0, 0x0f, 0x4f, 0x91, 0x34, 0x48,
|
||||
0x7c, 0xf1, 0x85, 0xec, 0x24, 0x7e, 0x8d, 0xf6, 0x86, 0x1b, 0xf3, 0xa8, 0x8c, 0xaa, 0xb4, 0x2e,
|
||||
0x20, 0x04, 0x60, 0x0b, 0x40, 0xb3, 0x05, 0x54, 0xec, 0x5d, 0xe5, 0xc6, 0xac, 0x16, 0x71, 0xd8,
|
||||
0xa4, 0xfc, 0xab, 0xfc, 0xae, 0xd2, 0x3a, 0x87, 0xf7, 0x93, 0xe0, 0x03, 0x6a, 0x11, 0xd4, 0x26,
|
||||
0x9e, 0x93, 0x6b, 0xbc, 0xe2, 0xf6, 0x67, 0xd9, 0x3e, 0xbe, 0x02, 0x00, 0x00, 0xff, 0xff, 0x05,
|
||||
0x00, 0xf5, 0xbb, 0xf9, 0x00, 0x00, 0x00,
|
||||
}
|
@ -0,0 +1,147 @@
|
||||
/*
|
||||
Copyright 2016 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package tiller
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
"k8s.io/helm/pkg/tiller/environment"
|
||||
"k8s.io/helm/pkg/timeconv"
|
||||
)
|
||||
|
||||
// change name to runReleaseTestSuite
|
||||
func runReleaseTestSuite(hooks []*release.Hook, kube environment.KubeClient, name, namespace string, timeout int64) (*release.TestSuite, error) {
|
||||
|
||||
suite := &release.TestSuite{}
|
||||
suite.LastRun = timeconv.Now()
|
||||
results := []*release.TestResult{}
|
||||
|
||||
tests, err := prepareTests(hooks, name)
|
||||
if err != nil {
|
||||
return suite, err
|
||||
}
|
||||
|
||||
for _, h := range tests {
|
||||
var sh simpleHead
|
||||
err := yaml.Unmarshal([]byte(h), &sh)
|
||||
if err != nil {
|
||||
//handle err better
|
||||
return nil, err
|
||||
}
|
||||
ts := &release.TestResult{Name: sh.Metadata.Name}
|
||||
|
||||
// should this be lower? should we even be saving time to hook?
|
||||
// TODO: should be start time really
|
||||
ts.LastRun = timeconv.Now()
|
||||
|
||||
resourceCreated := true
|
||||
b := bytes.NewBufferString(h)
|
||||
if err := kube.Create(namespace, b); err != nil {
|
||||
log.Printf("Could not create %s(%s): %v", ts.Name, sh.Kind, err)
|
||||
ts.Info = err.Error()
|
||||
//TODO: status option should be constant not random int
|
||||
ts.Status = 2
|
||||
resourceCreated = false
|
||||
}
|
||||
|
||||
status := api.PodUnknown
|
||||
resourceCleanExit := true
|
||||
if resourceCreated {
|
||||
b.Reset()
|
||||
b.WriteString(h)
|
||||
status, err = kube.WaitAndGetCompletedPodStatus(namespace, b, time.Duration(timeout)*time.Second)
|
||||
if err != nil {
|
||||
log.Printf("Error getting status for %s(%s): %s", ts.Name, sh.Kind, err)
|
||||
ts.Info = err.Error()
|
||||
ts.Status = 0
|
||||
resourceCleanExit = false
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: maybe better suited as a switch statement and include
|
||||
// PodUnknown, PodFailed, PodRunning, and PodPending scenarios
|
||||
if resourceCreated && resourceCleanExit && status == api.PodSucceeded {
|
||||
ts.Status = 1
|
||||
} else if resourceCreated && resourceCleanExit && status == api.PodFailed {
|
||||
ts.Status = 2
|
||||
}
|
||||
|
||||
results = append(results, ts)
|
||||
log.Printf("Test %s(%s) complete", ts.Name, sh.Kind)
|
||||
|
||||
//TODO: recordTests() - add test results to configmap with standardized name
|
||||
}
|
||||
|
||||
suite.Results = results
|
||||
log.Printf("Finished running test suite for %s", name)
|
||||
|
||||
return suite, nil
|
||||
}
|
||||
|
||||
func filterTests(hooks []*release.Hook, releaseName string) ([]*release.Hook, error) {
|
||||
testHooks := []*release.Hook{}
|
||||
notFoundErr := fmt.Errorf("no tests found for release %s", releaseName)
|
||||
if len(hooks) == 0 {
|
||||
return nil, notFoundErr
|
||||
}
|
||||
|
||||
code, ok := events[releaseTest]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown hook %q", releaseTest)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, h := range hooks {
|
||||
for _, e := range h.Events {
|
||||
if e == code {
|
||||
found = true
|
||||
testHooks = append(testHooks, h)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: probably don't need to check found
|
||||
if found == false && len(testHooks) == 0 {
|
||||
return nil, notFoundErr
|
||||
}
|
||||
|
||||
return testHooks, nil
|
||||
}
|
||||
|
||||
func prepareTests(hooks []*release.Hook, releaseName string) ([]string, error) {
|
||||
testHooks, err := filterTests(hooks, releaseName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tests := []string{}
|
||||
for _, h := range testHooks {
|
||||
individualTests := splitManifests(h.Manifest)
|
||||
for _, t := range individualTests {
|
||||
tests = append(tests, t)
|
||||
}
|
||||
}
|
||||
return tests, nil
|
||||
}
|
Loading…
Reference in new issue