mirror of https://github.com/helm/helm
Merge branch 'master' of https://github.com/kubernetes/helm into move-to-pkg
commit
a32f71c735
@ -0,0 +1,36 @@
|
||||
|
||||
// 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 TestRun {
|
||||
enum Status {
|
||||
UNKNOWN = 0;
|
||||
SUCCESS = 1;
|
||||
FAILURE = 2;
|
||||
}
|
||||
|
||||
string name = 1;
|
||||
Status status = 2;
|
||||
string info = 3;
|
||||
google.protobuf.Timestamp started_at = 4;
|
||||
google.protobuf.Timestamp completed_at = 5;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
// 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_run.proto";
|
||||
|
||||
option go_package = "release";
|
||||
|
||||
// TestSuite comprises of the last run of the pre-defined test suite of a release version
|
||||
message TestSuite {
|
||||
// StartedAt indicates the date/time this test suite was kicked off
|
||||
google.protobuf.Timestamp started_at = 1;
|
||||
|
||||
// CompletedAt indicates the date/time this test suite was completed
|
||||
google.protobuf.Timestamp completed_at = 2;
|
||||
|
||||
// Results are the results of each segment of the test
|
||||
repeated hapi.release.TestRun results = 3;
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
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 installer // import "k8s.io/helm/cmd/helm/installer"
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
kerrors "k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion"
|
||||
|
||||
"k8s.io/helm/pkg/kube"
|
||||
)
|
||||
|
||||
// Uninstall uses kubernetes client to uninstall tiller
|
||||
func Uninstall(kubeClient internalclientset.Interface, kubeCmd *kube.Client, namespace string, verbose bool) error {
|
||||
if _, err := kubeClient.Core().Services(namespace).Get("tiller-deploy"); err != nil {
|
||||
if !kerrors.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
} else if err := deleteService(kubeClient.Core(), namespace); err != nil {
|
||||
return err
|
||||
}
|
||||
if obj, err := kubeClient.Extensions().Deployments(namespace).Get("tiller-deploy"); err != nil {
|
||||
if !kerrors.IsNotFound(err) {
|
||||
return err
|
||||
}
|
||||
} else if err := deleteDeployment(kubeCmd, namespace, obj); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteService deletes the Tiller Service resource
|
||||
func deleteService(client internalversion.ServicesGetter, namespace string) error {
|
||||
return client.Services(namespace).Delete("tiller-deploy", &api.DeleteOptions{})
|
||||
}
|
||||
|
||||
// deleteDeployment deletes the Tiller Deployment resource
|
||||
// We need to use the kubeCmd reaper instead of the kube API because GC for deployment dependents
|
||||
// is not yet supported at the k8s server level (<= 1.5)
|
||||
func deleteDeployment(kubeCmd *kube.Client, namespace string, obj *extensions.Deployment) error {
|
||||
obj.Kind = "Deployment"
|
||||
obj.APIVersion = "extensions/v1beta1"
|
||||
buf, err := yaml.Marshal(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reader := strings.NewReader(string(buf))
|
||||
infos, err := kubeCmd.Build(namespace, reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, info := range infos {
|
||||
reaper, err := kubeCmd.Reaper(info.Mapping)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = reaper.Stop(info.Namespace, info.Name, 0, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,160 @@
|
||||
/*
|
||||
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 installer // import "k8s.io/helm/cmd/helm/installer"
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
apierrors "k8s.io/kubernetes/pkg/api/errors"
|
||||
"k8s.io/kubernetes/pkg/api/meta"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
||||
testcore "k8s.io/kubernetes/pkg/client/testing/core"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
||||
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
|
||||
"k8s.io/helm/pkg/kube"
|
||||
)
|
||||
|
||||
type fakeReaper struct {
|
||||
namespace string
|
||||
name string
|
||||
}
|
||||
|
||||
func (r *fakeReaper) Stop(namespace, name string, timeout time.Duration, gracePeriod *api.DeleteOptions) error {
|
||||
r.namespace = namespace
|
||||
r.name = name
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeReaperFactory struct {
|
||||
cmdutil.Factory
|
||||
reaper kubectl.Reaper
|
||||
}
|
||||
|
||||
func (f *fakeReaperFactory) Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error) {
|
||||
return f.reaper, nil
|
||||
}
|
||||
|
||||
func TestUninstall(t *testing.T) {
|
||||
existingService := service(api.NamespaceDefault)
|
||||
existingDeployment := deployment(api.NamespaceDefault, "image", false)
|
||||
|
||||
fc := &fake.Clientset{}
|
||||
fc.AddReactor("get", "services", func(action testcore.Action) (bool, runtime.Object, error) {
|
||||
return true, existingService, nil
|
||||
})
|
||||
fc.AddReactor("delete", "services", func(action testcore.Action) (bool, runtime.Object, error) {
|
||||
return true, nil, nil
|
||||
})
|
||||
fc.AddReactor("get", "deployments", func(action testcore.Action) (bool, runtime.Object, error) {
|
||||
return true, existingDeployment, nil
|
||||
})
|
||||
|
||||
f, _, _, _ := cmdtesting.NewAPIFactory()
|
||||
r := &fakeReaper{}
|
||||
rf := &fakeReaperFactory{Factory: f, reaper: r}
|
||||
kc := &kube.Client{Factory: rf}
|
||||
|
||||
if err := Uninstall(fc, kc, api.NamespaceDefault, false); err != nil {
|
||||
t.Errorf("unexpected error: %#+v", err)
|
||||
}
|
||||
|
||||
if actions := fc.Actions(); len(actions) != 3 {
|
||||
t.Errorf("unexpected actions: %v, expected 3 actions got %d", actions, len(actions))
|
||||
}
|
||||
|
||||
if r.namespace != api.NamespaceDefault {
|
||||
t.Errorf("unexpected reaper namespace: %s", r.name)
|
||||
}
|
||||
|
||||
if r.name != "tiller-deploy" {
|
||||
t.Errorf("unexpected reaper name: %s", r.name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUninstall_serviceNotFound(t *testing.T) {
|
||||
existingDeployment := deployment(api.NamespaceDefault, "imageToReplace", false)
|
||||
|
||||
fc := &fake.Clientset{}
|
||||
fc.AddReactor("get", "services", func(action testcore.Action) (bool, runtime.Object, error) {
|
||||
return true, nil, apierrors.NewNotFound(api.Resource("services"), "1")
|
||||
})
|
||||
fc.AddReactor("get", "deployments", func(action testcore.Action) (bool, runtime.Object, error) {
|
||||
return true, existingDeployment, nil
|
||||
})
|
||||
|
||||
f, _, _, _ := cmdtesting.NewAPIFactory()
|
||||
r := &fakeReaper{}
|
||||
rf := &fakeReaperFactory{Factory: f, reaper: r}
|
||||
kc := &kube.Client{Factory: rf}
|
||||
|
||||
if err := Uninstall(fc, kc, api.NamespaceDefault, false); err != nil {
|
||||
t.Errorf("unexpected error: %#+v", err)
|
||||
}
|
||||
|
||||
if actions := fc.Actions(); len(actions) != 2 {
|
||||
t.Errorf("unexpected actions: %v, expected 2 actions got %d", actions, len(actions))
|
||||
}
|
||||
|
||||
if r.namespace != api.NamespaceDefault {
|
||||
t.Errorf("unexpected reaper namespace: %s", r.name)
|
||||
}
|
||||
|
||||
if r.name != "tiller-deploy" {
|
||||
t.Errorf("unexpected reaper name: %s", r.name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUninstall_deploymentNotFound(t *testing.T) {
|
||||
existingService := service(api.NamespaceDefault)
|
||||
|
||||
fc := &fake.Clientset{}
|
||||
fc.AddReactor("get", "services", func(action testcore.Action) (bool, runtime.Object, error) {
|
||||
return true, existingService, nil
|
||||
})
|
||||
fc.AddReactor("delete", "services", func(action testcore.Action) (bool, runtime.Object, error) {
|
||||
return true, nil, nil
|
||||
})
|
||||
fc.AddReactor("get", "deployments", func(action testcore.Action) (bool, runtime.Object, error) {
|
||||
return true, nil, apierrors.NewNotFound(api.Resource("deployments"), "1")
|
||||
})
|
||||
|
||||
f, _, _, _ := cmdtesting.NewAPIFactory()
|
||||
r := &fakeReaper{}
|
||||
rf := &fakeReaperFactory{Factory: f, reaper: r}
|
||||
kc := &kube.Client{Factory: rf}
|
||||
|
||||
if err := Uninstall(fc, kc, api.NamespaceDefault, false); err != nil {
|
||||
t.Errorf("unexpected error: %#+v", err)
|
||||
}
|
||||
|
||||
if actions := fc.Actions(); len(actions) != 3 {
|
||||
t.Errorf("unexpected actions: %v, expected 3 actions got %d", actions, len(actions))
|
||||
}
|
||||
|
||||
if r.namespace != "" {
|
||||
t.Errorf("unexpected reaper namespace: %s", r.name)
|
||||
}
|
||||
|
||||
if r.name != "" {
|
||||
t.Errorf("unexpected reaper name: %s", r.name)
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
/*
|
||||
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/spf13/cobra"
|
||||
|
||||
"k8s.io/helm/pkg/helm"
|
||||
)
|
||||
|
||||
const releaseTestDesc = `
|
||||
The 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() (err error) {
|
||||
c, errc := t.client.RunReleaseTest(t.name, helm.ReleaseTestTimeout(t.timeout))
|
||||
|
||||
for {
|
||||
select {
|
||||
case err := <-errc:
|
||||
return prettyError(err)
|
||||
case res, ok := <-c:
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
fmt.Fprintf(t.out, res.Msg+"\n")
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,131 @@
|
||||
/*
|
||||
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 (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
|
||||
"k8s.io/helm/cmd/helm/helmpath"
|
||||
"k8s.io/helm/cmd/helm/installer"
|
||||
"k8s.io/helm/pkg/helm"
|
||||
"k8s.io/helm/pkg/kube"
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
)
|
||||
|
||||
const resetDesc = `
|
||||
This command uninstalls Tiller (the helm server side component) from your
|
||||
Kubernetes Cluster and optionally deletes local configuration in
|
||||
$HELM_HOME (default ~/.helm/)
|
||||
`
|
||||
|
||||
type resetCmd struct {
|
||||
force bool
|
||||
removeHelmHome bool
|
||||
namespace string
|
||||
out io.Writer
|
||||
home helmpath.Home
|
||||
client helm.Interface
|
||||
kubeClient internalclientset.Interface
|
||||
kubeCmd *kube.Client
|
||||
}
|
||||
|
||||
func newResetCmd(client helm.Interface, out io.Writer) *cobra.Command {
|
||||
d := &resetCmd{
|
||||
out: out,
|
||||
client: client,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "reset",
|
||||
Short: "uninstalls Tiller from a cluster",
|
||||
Long: resetDesc,
|
||||
PersistentPreRunE: setupConnection,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 0 {
|
||||
return errors.New("This command does not accept arguments")
|
||||
}
|
||||
|
||||
d.namespace = tillerNamespace
|
||||
d.home = helmpath.Home(homePath())
|
||||
d.client = ensureHelmClient(d.client)
|
||||
|
||||
return d.run()
|
||||
},
|
||||
}
|
||||
|
||||
f := cmd.Flags()
|
||||
f.BoolVarP(&d.force, "force", "f", false, "forces Tiller uninstall even if there are releases installed")
|
||||
f.BoolVar(&d.removeHelmHome, "remove-helm-home", false, "if set deletes $HELM_HOME")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
// runReset uninstalls tiller from Kubernetes Cluster and deletes local config
|
||||
func (d *resetCmd) run() error {
|
||||
if d.kubeClient == nil {
|
||||
_, c, err := getKubeClient(kubeContext)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not get kubernetes client: %s", err)
|
||||
}
|
||||
d.kubeClient = c
|
||||
}
|
||||
if d.kubeCmd == nil {
|
||||
d.kubeCmd = getKubeCmd(kubeContext)
|
||||
}
|
||||
|
||||
res, err := d.client.ListReleases(
|
||||
helm.ReleaseListStatuses([]release.Status_Code{release.Status_DEPLOYED}),
|
||||
)
|
||||
if err != nil {
|
||||
return prettyError(err)
|
||||
}
|
||||
|
||||
if len(res.Releases) > 0 && !d.force {
|
||||
return fmt.Errorf("There are still %d deployed releases (Tip: use --force).", len(res.Releases))
|
||||
}
|
||||
|
||||
if err := installer.Uninstall(d.kubeClient, d.kubeCmd, d.namespace, flagDebug); err != nil {
|
||||
return fmt.Errorf("error unstalling Tiller: %s", err)
|
||||
}
|
||||
|
||||
if d.removeHelmHome {
|
||||
if err := deleteDirectories(d.home, d.out); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprintln(d.out, "Tiller (the helm server side component) has been uninstalled from your Kubernetes Cluster.")
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteDirectories deletes $HELM_HOME
|
||||
func deleteDirectories(home helmpath.Home, out io.Writer) error {
|
||||
if _, err := os.Stat(home.String()); err == nil {
|
||||
fmt.Fprintf(out, "Deleting %s \n", home.String())
|
||||
if err := os.RemoveAll(home.String()); err != nil {
|
||||
return fmt.Errorf("Could not remove %s: %s", home.String(), err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -0,0 +1,187 @@
|
||||
/*
|
||||
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 (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
||||
|
||||
"k8s.io/helm/cmd/helm/helmpath"
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
)
|
||||
|
||||
func TestResetCmd(t *testing.T) {
|
||||
home, err := ioutil.TempDir("", "helm_home")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(home)
|
||||
|
||||
var buf bytes.Buffer
|
||||
c := &fakeReleaseClient{}
|
||||
fc := fake.NewSimpleClientset()
|
||||
cmd := &resetCmd{
|
||||
out: &buf,
|
||||
home: helmpath.Home(home),
|
||||
client: c,
|
||||
kubeClient: fc,
|
||||
namespace: api.NamespaceDefault,
|
||||
}
|
||||
if err := cmd.run(); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
actions := fc.Actions()
|
||||
if len(actions) != 2 {
|
||||
t.Errorf("Expected 2 actions, got %d", len(actions))
|
||||
}
|
||||
if !actions[0].Matches("get", "services") {
|
||||
t.Errorf("unexpected action: %v, expected get service", actions[1])
|
||||
}
|
||||
if !actions[1].Matches("get", "deployments") {
|
||||
t.Errorf("unexpected action: %v, expected get deployment", actions[0])
|
||||
}
|
||||
expected := "Tiller (the helm server side component) has been uninstalled from your Kubernetes Cluster."
|
||||
if !strings.Contains(buf.String(), expected) {
|
||||
t.Errorf("expected %q, got %q", expected, buf.String())
|
||||
}
|
||||
if _, err := os.Stat(home); err != nil {
|
||||
t.Errorf("Helm home directory %s does not exists", home)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetCmd_removeHelmHome(t *testing.T) {
|
||||
home, err := ioutil.TempDir("", "helm_home")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(home)
|
||||
|
||||
var buf bytes.Buffer
|
||||
c := &fakeReleaseClient{}
|
||||
fc := fake.NewSimpleClientset()
|
||||
cmd := &resetCmd{
|
||||
removeHelmHome: true,
|
||||
out: &buf,
|
||||
home: helmpath.Home(home),
|
||||
client: c,
|
||||
kubeClient: fc,
|
||||
namespace: api.NamespaceDefault,
|
||||
}
|
||||
if err := cmd.run(); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
actions := fc.Actions()
|
||||
if len(actions) != 2 {
|
||||
t.Errorf("Expected 2 actions, got %d", len(actions))
|
||||
}
|
||||
if !actions[0].Matches("get", "services") {
|
||||
t.Errorf("unexpected action: %v, expected get service", actions[1])
|
||||
}
|
||||
if !actions[1].Matches("get", "deployments") {
|
||||
t.Errorf("unexpected action: %v, expected get deployment", actions[0])
|
||||
}
|
||||
expected := "Tiller (the helm server side component) has been uninstalled from your Kubernetes Cluster."
|
||||
if !strings.Contains(buf.String(), expected) {
|
||||
t.Errorf("expected %q, got %q", expected, buf.String())
|
||||
}
|
||||
if _, err := os.Stat(home); err == nil {
|
||||
t.Errorf("Helm home directory %s already exists", home)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReset_deployedReleases(t *testing.T) {
|
||||
home, err := ioutil.TempDir("", "helm_home")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(home)
|
||||
|
||||
var buf bytes.Buffer
|
||||
resp := []*release.Release{
|
||||
releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}),
|
||||
}
|
||||
c := &fakeReleaseClient{
|
||||
rels: resp,
|
||||
}
|
||||
fc := fake.NewSimpleClientset()
|
||||
cmd := &resetCmd{
|
||||
out: &buf,
|
||||
home: helmpath.Home(home),
|
||||
client: c,
|
||||
kubeClient: fc,
|
||||
namespace: api.NamespaceDefault,
|
||||
}
|
||||
err = cmd.run()
|
||||
expected := "There are still 1 deployed releases (Tip: use --force)"
|
||||
if !strings.Contains(err.Error(), expected) {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(home); err != nil {
|
||||
t.Errorf("Helm home directory %s does not exists", home)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReset_forceFlag(t *testing.T) {
|
||||
home, err := ioutil.TempDir("", "helm_home")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(home)
|
||||
|
||||
var buf bytes.Buffer
|
||||
resp := []*release.Release{
|
||||
releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}),
|
||||
}
|
||||
c := &fakeReleaseClient{
|
||||
rels: resp,
|
||||
}
|
||||
fc := fake.NewSimpleClientset()
|
||||
cmd := &resetCmd{
|
||||
force: true,
|
||||
out: &buf,
|
||||
home: helmpath.Home(home),
|
||||
client: c,
|
||||
kubeClient: fc,
|
||||
namespace: api.NamespaceDefault,
|
||||
}
|
||||
if err := cmd.run(); err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
actions := fc.Actions()
|
||||
if len(actions) != 2 {
|
||||
t.Errorf("Expected 2 actions, got %d", len(actions))
|
||||
}
|
||||
if !actions[0].Matches("get", "services") {
|
||||
t.Errorf("unexpected action: %v, expected get service", actions[1])
|
||||
}
|
||||
if !actions[1].Matches("get", "deployments") {
|
||||
t.Errorf("unexpected action: %v, expected get deployment", actions[0])
|
||||
}
|
||||
expected := "Tiller (the helm server side component) has been uninstalled from your Kubernetes Cluster."
|
||||
if !strings.Contains(buf.String(), expected) {
|
||||
t.Errorf("expected %q, got %q", expected, buf.String())
|
||||
}
|
||||
if _, err := os.Stat(home); err != nil {
|
||||
t.Errorf("Helm home directory %s does not exists", home)
|
||||
}
|
||||
}
|
@ -0,0 +1,149 @@
|
||||
/*
|
||||
Copyright 2017 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 (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/ptypes/timestamp"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
"k8s.io/helm/pkg/timeconv"
|
||||
)
|
||||
|
||||
var (
|
||||
date = timestamp.Timestamp{Seconds: 242085845, Nanos: 0}
|
||||
dateString = timeconv.String(&date)
|
||||
)
|
||||
|
||||
// statusCase describes a test case dealing with the status of a release
|
||||
type statusCase struct {
|
||||
name string
|
||||
args []string
|
||||
flags []string
|
||||
expected string
|
||||
err bool
|
||||
rel *release.Release
|
||||
}
|
||||
|
||||
func TestStatusCmd(t *testing.T) {
|
||||
tests := []statusCase{
|
||||
{
|
||||
name: "get status of a deployed release",
|
||||
args: []string{"flummoxed-chickadee"},
|
||||
expected: outputWithStatus("DEPLOYED\n\n"),
|
||||
rel: releaseMockWithStatus(&release.Status{
|
||||
Code: release.Status_DEPLOYED,
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "get status of a deployed release with notes",
|
||||
args: []string{"flummoxed-chickadee"},
|
||||
expected: outputWithStatus("DEPLOYED\n\nNOTES:\nrelease notes\n"),
|
||||
rel: releaseMockWithStatus(&release.Status{
|
||||
Code: release.Status_DEPLOYED,
|
||||
Notes: "release notes",
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "get status of a deployed release with resources",
|
||||
args: []string{"flummoxed-chickadee"},
|
||||
expected: outputWithStatus("DEPLOYED\n\nRESOURCES:\nresource A\nresource B\n\n"),
|
||||
rel: releaseMockWithStatus(&release.Status{
|
||||
Code: release.Status_DEPLOYED,
|
||||
Resources: "resource A\nresource B\n",
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "get status of a deployed release with test suite",
|
||||
args: []string{"flummoxed-chickadee"},
|
||||
expected: outputWithStatus(
|
||||
fmt.Sprintf("DEPLOYED\n\nTEST SUITE:\nLast Started: %s\nLast Completed: %s\n\n", dateString, dateString) +
|
||||
fmt.Sprint("TEST \tSTATUS \tINFO \tSTARTED \tCOMPLETED \n") +
|
||||
fmt.Sprintf("test run 1\tSUCCESS \textra info\t%s\t%s\n", dateString, dateString) +
|
||||
fmt.Sprintf("test run 2\tFAILURE \t \t%s\t%s\n", dateString, dateString)),
|
||||
rel: releaseMockWithStatus(&release.Status{
|
||||
Code: release.Status_DEPLOYED,
|
||||
LastTestSuiteRun: &release.TestSuite{
|
||||
StartedAt: &date,
|
||||
CompletedAt: &date,
|
||||
Results: []*release.TestRun{
|
||||
{
|
||||
Name: "test run 1",
|
||||
Status: release.TestRun_SUCCESS,
|
||||
Info: "extra info",
|
||||
StartedAt: &date,
|
||||
CompletedAt: &date,
|
||||
},
|
||||
{
|
||||
Name: "test run 2",
|
||||
Status: release.TestRun_FAILURE,
|
||||
StartedAt: &date,
|
||||
CompletedAt: &date,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
scmd := func(c *fakeReleaseClient, out io.Writer) *cobra.Command {
|
||||
return newStatusCmd(c, out)
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
for _, tt := range tests {
|
||||
c := &fakeReleaseClient{
|
||||
rels: []*release.Release{tt.rel},
|
||||
}
|
||||
cmd := scmd(c, &buf)
|
||||
cmd.ParseFlags(tt.flags)
|
||||
err := cmd.RunE(cmd, tt.args)
|
||||
if (err != nil) != tt.err {
|
||||
t.Errorf("%q. expected error, got '%v'", tt.name, err)
|
||||
}
|
||||
|
||||
expected := strings.Replace(tt.expected, " ", "", -1)
|
||||
got := strings.Replace(buf.String(), " ", "", -1)
|
||||
if expected != got {
|
||||
t.Errorf("%q. expected\n%q\ngot\n%q", tt.name, expected, got)
|
||||
}
|
||||
buf.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
func outputWithStatus(status string) string {
|
||||
return fmt.Sprintf("LAST DEPLOYED: %s\nNAMESPACE: \nSTATUS: %s",
|
||||
dateString,
|
||||
status)
|
||||
}
|
||||
|
||||
func releaseMockWithStatus(status *release.Status) *release.Release {
|
||||
return &release.Release{
|
||||
Name: "flummoxed-chickadee",
|
||||
Info: &release.Info{
|
||||
FirstDeployed: &date,
|
||||
LastDeployed: &date,
|
||||
Status: status,
|
||||
},
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: "{{.Release.Name}}-service-test"
|
||||
annotations:
|
||||
"helm.sh/hook": test-success
|
||||
spec:
|
||||
containers:
|
||||
- name: curl
|
||||
image: radial/busyboxplus:curl
|
||||
command: ['curl']
|
||||
args: [ '{{ template "fullname" .}}:{{default 80 .Values.httpPort}}' ]
|
||||
restartPolicy: Never
|
@ -0,0 +1,94 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: hapi/release/test_run.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 TestRun_Status int32
|
||||
|
||||
const (
|
||||
TestRun_UNKNOWN TestRun_Status = 0
|
||||
TestRun_SUCCESS TestRun_Status = 1
|
||||
TestRun_FAILURE TestRun_Status = 2
|
||||
)
|
||||
|
||||
var TestRun_Status_name = map[int32]string{
|
||||
0: "UNKNOWN",
|
||||
1: "SUCCESS",
|
||||
2: "FAILURE",
|
||||
}
|
||||
var TestRun_Status_value = map[string]int32{
|
||||
"UNKNOWN": 0,
|
||||
"SUCCESS": 1,
|
||||
"FAILURE": 2,
|
||||
}
|
||||
|
||||
func (x TestRun_Status) String() string {
|
||||
return proto.EnumName(TestRun_Status_name, int32(x))
|
||||
}
|
||||
func (TestRun_Status) EnumDescriptor() ([]byte, []int) { return fileDescriptor4, []int{0, 0} }
|
||||
|
||||
type TestRun struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||
Status TestRun_Status `protobuf:"varint,2,opt,name=status,enum=hapi.release.TestRun_Status" json:"status,omitempty"`
|
||||
Info string `protobuf:"bytes,3,opt,name=info" json:"info,omitempty"`
|
||||
StartedAt *google_protobuf.Timestamp `protobuf:"bytes,4,opt,name=started_at,json=startedAt" json:"started_at,omitempty"`
|
||||
CompletedAt *google_protobuf.Timestamp `protobuf:"bytes,5,opt,name=completed_at,json=completedAt" json:"completed_at,omitempty"`
|
||||
}
|
||||
|
||||
func (m *TestRun) Reset() { *m = TestRun{} }
|
||||
func (m *TestRun) String() string { return proto.CompactTextString(m) }
|
||||
func (*TestRun) ProtoMessage() {}
|
||||
func (*TestRun) Descriptor() ([]byte, []int) { return fileDescriptor4, []int{0} }
|
||||
|
||||
func (m *TestRun) GetStartedAt() *google_protobuf.Timestamp {
|
||||
if m != nil {
|
||||
return m.StartedAt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestRun) GetCompletedAt() *google_protobuf.Timestamp {
|
||||
if m != nil {
|
||||
return m.CompletedAt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*TestRun)(nil), "hapi.release.TestRun")
|
||||
proto.RegisterEnum("hapi.release.TestRun_Status", TestRun_Status_name, TestRun_Status_value)
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("hapi/release/test_run.proto", fileDescriptor4) }
|
||||
|
||||
var fileDescriptor4 = []byte{
|
||||
// 265 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x8f, 0x41, 0x4b, 0xfb, 0x40,
|
||||
0x14, 0xc4, 0xff, 0xc9, 0xbf, 0x26, 0x64, 0x53, 0x24, 0xec, 0x29, 0x54, 0xc1, 0xd0, 0x53, 0x4e,
|
||||
0xbb, 0x50, 0xbd, 0x78, 0xf0, 0x10, 0x4b, 0x05, 0x51, 0x22, 0x6c, 0x1a, 0x04, 0x2f, 0x65, 0xab,
|
||||
0xaf, 0x35, 0x90, 0x64, 0x43, 0xf6, 0xe5, 0x8b, 0xf8, 0x89, 0x65, 0x93, 0xad, 0x78, 0xf3, 0xf6,
|
||||
0x86, 0xf9, 0xcd, 0x30, 0x8f, 0x5c, 0x7c, 0xca, 0xae, 0xe2, 0x3d, 0xd4, 0x20, 0x35, 0x70, 0x04,
|
||||
0x8d, 0xbb, 0x7e, 0x68, 0x59, 0xd7, 0x2b, 0x54, 0x74, 0x6e, 0x4c, 0x66, 0xcd, 0xc5, 0xd5, 0x51,
|
||||
0xa9, 0x63, 0x0d, 0x7c, 0xf4, 0xf6, 0xc3, 0x81, 0x63, 0xd5, 0x80, 0x46, 0xd9, 0x74, 0x13, 0xbe,
|
||||
0xfc, 0x72, 0x89, 0xbf, 0x05, 0x8d, 0x62, 0x68, 0x29, 0x25, 0xb3, 0x56, 0x36, 0x10, 0x3b, 0x89,
|
||||
0x93, 0x06, 0x62, 0xbc, 0xe9, 0x0d, 0xf1, 0x34, 0x4a, 0x1c, 0x74, 0xec, 0x26, 0x4e, 0x7a, 0xbe,
|
||||
0xba, 0x64, 0xbf, 0xfb, 0x99, 0x8d, 0xb2, 0x62, 0x64, 0x84, 0x65, 0x4d, 0x53, 0xd5, 0x1e, 0x54,
|
||||
0xfc, 0x7f, 0x6a, 0x32, 0x37, 0xbd, 0x25, 0x44, 0xa3, 0xec, 0x11, 0x3e, 0x76, 0x12, 0xe3, 0x59,
|
||||
0xe2, 0xa4, 0xe1, 0x6a, 0xc1, 0xa6, 0x7d, 0xec, 0xb4, 0x8f, 0x6d, 0x4f, 0xfb, 0x44, 0x60, 0xe9,
|
||||
0x0c, 0xe9, 0x1d, 0x99, 0xbf, 0xab, 0xa6, 0xab, 0xc1, 0x86, 0xcf, 0xfe, 0x0c, 0x87, 0x3f, 0x7c,
|
||||
0x86, 0x4b, 0x4e, 0xbc, 0x69, 0x1f, 0x0d, 0x89, 0x5f, 0xe6, 0x4f, 0xf9, 0xcb, 0x6b, 0x1e, 0xfd,
|
||||
0x33, 0xa2, 0x28, 0xd7, 0xeb, 0x4d, 0x51, 0x44, 0x8e, 0x11, 0x0f, 0xd9, 0xe3, 0x73, 0x29, 0x36,
|
||||
0x91, 0x7b, 0x1f, 0xbc, 0xf9, 0xf6, 0xc1, 0xbd, 0x37, 0x96, 0x5f, 0x7f, 0x07, 0x00, 0x00, 0xff,
|
||||
0xff, 0x8d, 0xb9, 0xce, 0x57, 0x74, 0x01, 0x00, 0x00,
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
// 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 {
|
||||
// StartedAt indicates the date/time this test suite was kicked off
|
||||
StartedAt *google_protobuf.Timestamp `protobuf:"bytes,1,opt,name=started_at,json=startedAt" json:"started_at,omitempty"`
|
||||
// CompletedAt indicates the date/time this test suite was completed
|
||||
CompletedAt *google_protobuf.Timestamp `protobuf:"bytes,2,opt,name=completed_at,json=completedAt" json:"completed_at,omitempty"`
|
||||
// Results are the results of each segment of the test
|
||||
Results []*TestRun `protobuf:"bytes,3,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) GetStartedAt() *google_protobuf.Timestamp {
|
||||
if m != nil {
|
||||
return m.StartedAt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestSuite) GetCompletedAt() *google_protobuf.Timestamp {
|
||||
if m != nil {
|
||||
return m.CompletedAt
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestSuite) GetResults() []*TestRun {
|
||||
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{
|
||||
// 207 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x84, 0x8f, 0xc1, 0x4a, 0x86, 0x40,
|
||||
0x14, 0x85, 0x31, 0x21, 0x71, 0x74, 0x35, 0x10, 0x88, 0x11, 0x49, 0x2b, 0x57, 0x33, 0x60, 0xab,
|
||||
0x16, 0x2d, 0xec, 0x11, 0xcc, 0x55, 0x1b, 0x19, 0xeb, 0x66, 0xc2, 0xe8, 0x0c, 0x73, 0xef, 0xbc,
|
||||
0x5a, 0xcf, 0x17, 0xea, 0x18, 0x41, 0x8b, 0x7f, 0xfd, 0x7d, 0xe7, 0x9c, 0x7b, 0xd9, 0xdd, 0x97,
|
||||
0xb2, 0xb3, 0x74, 0xa0, 0x41, 0x21, 0x48, 0x02, 0xa4, 0x01, 0xfd, 0x4c, 0x20, 0xac, 0x33, 0x64,
|
||||
0x78, 0xbe, 0x61, 0x11, 0x70, 0x79, 0x3f, 0x19, 0x33, 0x69, 0x90, 0x3b, 0x1b, 0xfd, 0xa7, 0xa4,
|
||||
0x79, 0x01, 0x24, 0xb5, 0xd8, 0x43, 0x2f, 0x6f, 0xff, 0xb7, 0x39, 0xbf, 0x1e, 0xf0, 0xe1, 0x3b,
|
||||
0x62, 0x69, 0x0f, 0x48, 0xaf, 0x5b, 0x3f, 0x7f, 0x62, 0x0c, 0x49, 0x39, 0x82, 0x8f, 0x41, 0x51,
|
||||
0x11, 0x55, 0x51, 0x9d, 0x35, 0xa5, 0x38, 0x06, 0xc4, 0x39, 0x20, 0xfa, 0x73, 0xa0, 0x4b, 0x83,
|
||||
0xdd, 0x12, 0x7f, 0x66, 0xf9, 0xbb, 0x59, 0xac, 0x86, 0x10, 0xbe, 0xba, 0x18, 0xce, 0x7e, 0xfd,
|
||||
0x96, 0xb8, 0x64, 0x89, 0x03, 0xf4, 0x9a, 0xb0, 0x88, 0xab, 0xb8, 0xce, 0x9a, 0x1b, 0xf1, 0xf7,
|
||||
0x4b, 0xb1, 0xdd, 0xd8, 0xf9, 0xb5, 0x3b, 0xad, 0x97, 0xf4, 0x2d, 0x09, 0x6c, 0xbc, 0xde, 0xcb,
|
||||
0x1f, 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0x8c, 0x59, 0x65, 0x4f, 0x37, 0x01, 0x00, 0x00,
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
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 releasetesting
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/services"
|
||||
"k8s.io/helm/pkg/tiller/environment"
|
||||
)
|
||||
|
||||
// Environment encapsulates information about where test suite executes and returns results
|
||||
type Environment struct {
|
||||
Namespace string
|
||||
KubeClient environment.KubeClient
|
||||
Stream services.ReleaseService_RunReleaseTestServer
|
||||
Timeout int64
|
||||
}
|
||||
|
||||
func streamRunning(name string, stream services.ReleaseService_RunReleaseTestServer) error {
|
||||
msg := "RUNNING: " + name
|
||||
err := streamMessage(msg, stream)
|
||||
return err
|
||||
}
|
||||
|
||||
func streamError(info string, stream services.ReleaseService_RunReleaseTestServer) error {
|
||||
msg := "ERROR: " + info
|
||||
err := streamMessage(msg, stream)
|
||||
return err
|
||||
}
|
||||
|
||||
func streamFailed(name, namespace string, stream services.ReleaseService_RunReleaseTestServer) error {
|
||||
msg := fmt.Sprintf("FAILED: %s, run `kubectl logs %s --namespace %s` for more info", name, name, namespace)
|
||||
err := streamMessage(msg, stream)
|
||||
return err
|
||||
}
|
||||
|
||||
func streamSuccess(name string, stream services.ReleaseService_RunReleaseTestServer) error {
|
||||
msg := fmt.Sprintf("PASSED: %s", name)
|
||||
err := streamMessage(msg, stream)
|
||||
return err
|
||||
}
|
||||
|
||||
func streamUnknown(name, info string, stream services.ReleaseService_RunReleaseTestServer) error {
|
||||
msg := fmt.Sprintf("UNKNOWN: %s: %s", name, info)
|
||||
err := streamMessage(msg, stream)
|
||||
return err
|
||||
}
|
||||
|
||||
func streamMessage(msg string, stream services.ReleaseService_RunReleaseTestServer) error {
|
||||
resp := &services.TestReleaseResponse{Msg: msg}
|
||||
err := stream.Send(resp)
|
||||
return err
|
||||
}
|
@ -0,0 +1,204 @@
|
||||
/*
|
||||
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 releasetesting
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/golang/protobuf/ptypes/timestamp"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
util "k8s.io/helm/pkg/releaseutil"
|
||||
"k8s.io/helm/pkg/timeconv"
|
||||
)
|
||||
|
||||
// TestSuite what tests are run, results, and metadata
|
||||
type TestSuite struct {
|
||||
StartedAt *timestamp.Timestamp
|
||||
CompletedAt *timestamp.Timestamp
|
||||
TestManifests []string
|
||||
Results []*release.TestRun
|
||||
}
|
||||
|
||||
type test struct {
|
||||
manifest string
|
||||
result *release.TestRun
|
||||
}
|
||||
|
||||
// NewTestSuite takes a release object and returns a TestSuite object with test definitions
|
||||
// extracted from the release
|
||||
func NewTestSuite(rel *release.Release) (*TestSuite, error) {
|
||||
testManifests, err := extractTestManifestsFromHooks(rel.Hooks, rel.Name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
results := []*release.TestRun{}
|
||||
|
||||
return &TestSuite{
|
||||
TestManifests: testManifests,
|
||||
Results: results,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Run executes tests in a test suite and stores a result within the context of a given environment
|
||||
func (t *TestSuite) Run(env *Environment) error {
|
||||
t.StartedAt = timeconv.Now()
|
||||
|
||||
for _, testManifest := range t.TestManifests {
|
||||
test, err := newTest(testManifest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
test.result.StartedAt = timeconv.Now()
|
||||
if err := streamRunning(test.result.Name, env.Stream); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resourceCreated := true
|
||||
if err := t.createTestPod(test, env); err != nil {
|
||||
resourceCreated = false
|
||||
if streamErr := streamError(test.result.Info, env.Stream); streamErr != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
resourceCleanExit := true
|
||||
status := api.PodUnknown
|
||||
if resourceCreated {
|
||||
status, err = t.getTestPodStatus(test, env)
|
||||
if err != nil {
|
||||
resourceCleanExit = false
|
||||
if streamErr := streamUnknown(test.result.Name, test.result.Info, env.Stream); streamErr != nil {
|
||||
return streamErr
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if resourceCreated && resourceCleanExit && status == api.PodSucceeded {
|
||||
test.result.Status = release.TestRun_SUCCESS
|
||||
if streamErr := streamSuccess(test.result.Name, env.Stream); streamErr != nil {
|
||||
return streamErr
|
||||
}
|
||||
} else if resourceCreated && resourceCleanExit && status == api.PodFailed {
|
||||
test.result.Status = release.TestRun_FAILURE
|
||||
if streamErr := streamFailed(test.result.Name, env.Namespace, env.Stream); streamErr != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
test.result.CompletedAt = timeconv.Now()
|
||||
t.Results = append(t.Results, test.result)
|
||||
}
|
||||
|
||||
t.CompletedAt = timeconv.Now()
|
||||
return nil
|
||||
}
|
||||
|
||||
// NOTE: may want to move this function to pkg/tiller in the future
|
||||
func filterHooksForTestHooks(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
|
||||
}
|
||||
|
||||
for _, h := range hooks {
|
||||
for _, e := range h.Events {
|
||||
if e == release.Hook_RELEASE_TEST_SUCCESS {
|
||||
testHooks = append(testHooks, h)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(testHooks) == 0 {
|
||||
return nil, notFoundErr
|
||||
}
|
||||
|
||||
return testHooks, nil
|
||||
}
|
||||
|
||||
// NOTE: may want to move this function to pkg/tiller in the future
|
||||
func extractTestManifestsFromHooks(hooks []*release.Hook, releaseName string) ([]string, error) {
|
||||
testHooks, err := filterHooksForTestHooks(hooks, releaseName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tests := []string{}
|
||||
for _, h := range testHooks {
|
||||
individualTests := util.SplitManifests(h.Manifest)
|
||||
for _, t := range individualTests {
|
||||
tests = append(tests, t)
|
||||
}
|
||||
}
|
||||
return tests, nil
|
||||
}
|
||||
|
||||
func newTest(testManifest string) (*test, error) {
|
||||
var sh util.SimpleHead
|
||||
err := yaml.Unmarshal([]byte(testManifest), &sh)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if sh.Kind != "Pod" {
|
||||
return nil, fmt.Errorf("%s is not a pod", sh.Metadata.Name)
|
||||
}
|
||||
|
||||
name := strings.TrimSuffix(sh.Metadata.Name, ",")
|
||||
return &test{
|
||||
manifest: testManifest,
|
||||
result: &release.TestRun{
|
||||
Name: name,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *TestSuite) createTestPod(test *test, env *Environment) error {
|
||||
b := bytes.NewBufferString(test.manifest)
|
||||
if err := env.KubeClient.Create(env.Namespace, b, env.Timeout, false); err != nil {
|
||||
log.Printf(err.Error())
|
||||
test.result.Info = err.Error()
|
||||
test.result.Status = release.TestRun_FAILURE
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TestSuite) getTestPodStatus(test *test, env *Environment) (api.PodPhase, error) {
|
||||
b := bytes.NewBufferString(test.manifest)
|
||||
status, err := env.KubeClient.WaitAndGetCompletedPodPhase(env.Namespace, b, time.Duration(env.Timeout)*time.Second)
|
||||
if err != nil {
|
||||
log.Printf("Error getting status for pod %s: %s", test.result.Name, err)
|
||||
test.result.Info = err.Error()
|
||||
test.result.Status = release.TestRun_UNKNOWN
|
||||
return status, err
|
||||
}
|
||||
|
||||
return status, err
|
||||
}
|
@ -0,0 +1,247 @@
|
||||
/*
|
||||
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 releasetesting
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/ptypes/timestamp"
|
||||
"golang.org/x/net/context"
|
||||
grpc "google.golang.org/grpc"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
|
||||
"k8s.io/helm/pkg/helm"
|
||||
"k8s.io/helm/pkg/proto/hapi/chart"
|
||||
"k8s.io/helm/pkg/proto/hapi/release"
|
||||
"k8s.io/helm/pkg/proto/hapi/services"
|
||||
"k8s.io/helm/pkg/storage"
|
||||
"k8s.io/helm/pkg/storage/driver"
|
||||
tillerEnv "k8s.io/helm/pkg/tiller/environment"
|
||||
)
|
||||
|
||||
func TestNewTestSuite(t *testing.T) {
|
||||
rel := releaseStub()
|
||||
|
||||
_, err := NewTestSuite(rel)
|
||||
if err != nil {
|
||||
t.Errorf("%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRun(t *testing.T) {
|
||||
|
||||
ts := testSuiteFixture()
|
||||
if err := ts.Run(testEnvFixture()); err != nil {
|
||||
t.Errorf("%s", err)
|
||||
}
|
||||
|
||||
if ts.StartedAt == nil {
|
||||
t.Errorf("Expected StartedAt to not be nil. Got: %v", ts.StartedAt)
|
||||
}
|
||||
|
||||
if ts.CompletedAt == nil {
|
||||
t.Errorf("Expected CompletedAt to not be nil. Got: %v", ts.CompletedAt)
|
||||
}
|
||||
|
||||
if len(ts.Results) != 1 {
|
||||
t.Errorf("Expected 1 test result. Got %v", len(ts.Results))
|
||||
}
|
||||
|
||||
result := ts.Results[0]
|
||||
if result.StartedAt == nil {
|
||||
t.Errorf("Expected test StartedAt to not be nil. Got: %v", result.StartedAt)
|
||||
}
|
||||
|
||||
if result.CompletedAt == nil {
|
||||
t.Errorf("Expected test CompletedAt to not be nil. Got: %v", result.CompletedAt)
|
||||
}
|
||||
|
||||
if result.Name != "finding-nemo" {
|
||||
t.Errorf("Expected test name to be finding-nemo. Got: %v", result.Name)
|
||||
}
|
||||
|
||||
if result.Status != release.TestRun_SUCCESS {
|
||||
t.Errorf("Expected test result to be successful, got: %v", result.Status)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGetTestPodStatus(t *testing.T) {
|
||||
ts := testSuiteFixture()
|
||||
|
||||
status, err := ts.getTestPodStatus(testFixture(), testEnvFixture())
|
||||
if err != nil {
|
||||
t.Errorf("Expected getTestPodStatus not to return err, Got: %s", err)
|
||||
}
|
||||
|
||||
if status != api.PodSucceeded {
|
||||
t.Errorf("Expected pod status to be succeeded, Got: %s ", status)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestExtractTestManifestsFromHooks(t *testing.T) {
|
||||
rel := releaseStub()
|
||||
testManifests, err := extractTestManifestsFromHooks(rel.Hooks, rel.Name)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no error, Got: %s", err)
|
||||
}
|
||||
|
||||
if len(testManifests) != 1 {
|
||||
t.Errorf("Expected 1 test manifest, Got: %v", len(testManifests))
|
||||
}
|
||||
}
|
||||
|
||||
func chartStub() *chart.Chart {
|
||||
return &chart.Chart{
|
||||
Metadata: &chart.Metadata{
|
||||
Name: "nemo",
|
||||
},
|
||||
Templates: []*chart.Template{
|
||||
{Name: "templates/hello", Data: []byte("hello: world")},
|
||||
{Name: "templates/hooks", Data: []byte(manifestWithTestHook)},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var manifestWithTestHook = `
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: finding-nemo,
|
||||
annotations:
|
||||
"helm.sh/hook": test-success
|
||||
spec:
|
||||
containers:
|
||||
- name: nemo-test
|
||||
image: fake-image
|
||||
cmd: fake-command
|
||||
`
|
||||
var manifestWithInstallHooks = `apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: test-cm
|
||||
annotations:
|
||||
"helm.sh/hook": post-install,pre-delete
|
||||
data:
|
||||
name: value
|
||||
`
|
||||
|
||||
func releaseStub() *release.Release {
|
||||
date := timestamp.Timestamp{Seconds: 242085845, Nanos: 0}
|
||||
return &release.Release{
|
||||
Name: "lost-fish",
|
||||
Info: &release.Info{
|
||||
FirstDeployed: &date,
|
||||
LastDeployed: &date,
|
||||
Status: &release.Status{Code: release.Status_DEPLOYED},
|
||||
Description: "a release stub",
|
||||
},
|
||||
Chart: chartStub(),
|
||||
Config: &chart.Config{Raw: `name: value`},
|
||||
Version: 1,
|
||||
Hooks: []*release.Hook{
|
||||
{
|
||||
Name: "finding-nemo",
|
||||
Kind: "Pod",
|
||||
Path: "finding-nemo",
|
||||
Manifest: manifestWithTestHook,
|
||||
Events: []release.Hook_Event{
|
||||
release.Hook_RELEASE_TEST_SUCCESS,
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "test-cm",
|
||||
Kind: "ConfigMap",
|
||||
Path: "test-cm",
|
||||
Manifest: manifestWithInstallHooks,
|
||||
Events: []release.Hook_Event{
|
||||
release.Hook_POST_INSTALL,
|
||||
release.Hook_PRE_DELETE,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func testFixture() *test {
|
||||
return &test{
|
||||
manifest: manifestWithTestHook,
|
||||
result: &release.TestRun{},
|
||||
}
|
||||
}
|
||||
|
||||
func testSuiteFixture() *TestSuite {
|
||||
testManifests := []string{manifestWithTestHook}
|
||||
testResults := []*release.TestRun{}
|
||||
ts := &TestSuite{
|
||||
TestManifests: testManifests,
|
||||
Results: testResults,
|
||||
}
|
||||
|
||||
return ts
|
||||
}
|
||||
|
||||
func testEnvFixture() *Environment {
|
||||
tillerEnv := mockTillerEnvironment()
|
||||
|
||||
return &Environment{
|
||||
Namespace: "default",
|
||||
KubeClient: tillerEnv.KubeClient,
|
||||
Timeout: 5,
|
||||
Stream: mockStream{},
|
||||
}
|
||||
}
|
||||
|
||||
func mockTillerEnvironment() *tillerEnv.Environment {
|
||||
e := tillerEnv.New()
|
||||
e.Releases = storage.Init(driver.NewMemory())
|
||||
e.KubeClient = newPodSucceededKubeClient()
|
||||
return e
|
||||
}
|
||||
|
||||
type mockStream struct {
|
||||
stream grpc.ServerStream
|
||||
}
|
||||
|
||||
func (rs mockStream) Send(m *services.TestReleaseResponse) error {
|
||||
return nil
|
||||
}
|
||||
func (rs mockStream) SetHeader(m metadata.MD) error { return nil }
|
||||
func (rs mockStream) SendHeader(m metadata.MD) error { return nil }
|
||||
func (rs mockStream) SetTrailer(m metadata.MD) {}
|
||||
func (rs mockStream) SendMsg(v interface{}) error { return nil }
|
||||
func (rs mockStream) RecvMsg(v interface{}) error { return nil }
|
||||
func (rs mockStream) Context() context.Context { return helm.NewContext() }
|
||||
|
||||
func newPodSucceededKubeClient() *podSucceededKubeClient {
|
||||
return &podSucceededKubeClient{
|
||||
PrintingKubeClient: tillerEnv.PrintingKubeClient{Out: os.Stdout},
|
||||
}
|
||||
}
|
||||
|
||||
type podSucceededKubeClient struct {
|
||||
tillerEnv.PrintingKubeClient
|
||||
}
|
||||
|
||||
func (p *podSucceededKubeClient) WaitAndGetCompletedPodPhase(ns string, r io.Reader, timeout time.Duration) (api.PodPhase, error) {
|
||||
return api.PodSucceeded, nil
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
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 releaseutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SimpleHead defines what the structure of the head of a manifest file
|
||||
type SimpleHead struct {
|
||||
Version string `json:"apiVersion"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Metadata *struct {
|
||||
Name string `json:"name"`
|
||||
Annotations map[string]string `json:"annotations"`
|
||||
} `json:"metadata,omitempty"`
|
||||
}
|
||||
|
||||
// SplitManifests takes a string of manifest and returns a map contains individual manifests
|
||||
func SplitManifests(bigfile string) map[string]string {
|
||||
// This is not the best way of doing things, but it's how k8s itself does it.
|
||||
// Basically, we're quickly splitting a stream of YAML documents into an
|
||||
// array of YAML docs. In the current implementation, the file name is just
|
||||
// a place holder, and doesn't have any further meaning.
|
||||
sep := "\n---\n"
|
||||
tpl := "manifest-%d"
|
||||
res := map[string]string{}
|
||||
tmp := strings.Split(bigfile, sep)
|
||||
for i, d := range tmp {
|
||||
res[fmt.Sprintf(tpl, i)] = d
|
||||
}
|
||||
return res
|
||||
}
|
Loading…
Reference in new issue