github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/reset_test.go (about)

     1  /*
     2  Copyright 2016 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 main
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  
    26  	"k8s.io/kubernetes/pkg/api"
    27  	"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
    28  
    29  	"k8s.io/helm/cmd/helm/helmpath"
    30  	"k8s.io/helm/pkg/proto/hapi/release"
    31  )
    32  
    33  func TestResetCmd(t *testing.T) {
    34  	home, err := ioutil.TempDir("", "helm_home")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	defer os.Remove(home)
    39  
    40  	var buf bytes.Buffer
    41  	c := &fakeReleaseClient{}
    42  	fc := fake.NewSimpleClientset()
    43  	cmd := &resetCmd{
    44  		out:        &buf,
    45  		home:       helmpath.Home(home),
    46  		client:     c,
    47  		kubeClient: fc,
    48  		namespace:  api.NamespaceDefault,
    49  	}
    50  	if err := cmd.run(); err != nil {
    51  		t.Errorf("unexpected error: %v", err)
    52  	}
    53  	actions := fc.Actions()
    54  	if len(actions) != 2 {
    55  		t.Errorf("Expected 2 actions, got %d", len(actions))
    56  	}
    57  	if !actions[0].Matches("get", "services") {
    58  		t.Errorf("unexpected action: %v, expected get service", actions[1])
    59  	}
    60  	if !actions[1].Matches("get", "deployments") {
    61  		t.Errorf("unexpected action: %v, expected get deployment", actions[0])
    62  	}
    63  	expected := "Tiller (the helm server side component) has been uninstalled from your Kubernetes Cluster."
    64  	if !strings.Contains(buf.String(), expected) {
    65  		t.Errorf("expected %q, got %q", expected, buf.String())
    66  	}
    67  	if _, err := os.Stat(home); err != nil {
    68  		t.Errorf("Helm home directory %s does not exists", home)
    69  	}
    70  }
    71  
    72  func TestResetCmd_removeHelmHome(t *testing.T) {
    73  	home, err := ioutil.TempDir("", "helm_home")
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	defer os.Remove(home)
    78  
    79  	var buf bytes.Buffer
    80  	c := &fakeReleaseClient{}
    81  	fc := fake.NewSimpleClientset()
    82  	cmd := &resetCmd{
    83  		removeHelmHome: true,
    84  		out:            &buf,
    85  		home:           helmpath.Home(home),
    86  		client:         c,
    87  		kubeClient:     fc,
    88  		namespace:      api.NamespaceDefault,
    89  	}
    90  	if err := cmd.run(); err != nil {
    91  		t.Errorf("unexpected error: %v", err)
    92  	}
    93  	actions := fc.Actions()
    94  	if len(actions) != 2 {
    95  		t.Errorf("Expected 2 actions, got %d", len(actions))
    96  	}
    97  	if !actions[0].Matches("get", "services") {
    98  		t.Errorf("unexpected action: %v, expected get service", actions[1])
    99  	}
   100  	if !actions[1].Matches("get", "deployments") {
   101  		t.Errorf("unexpected action: %v, expected get deployment", actions[0])
   102  	}
   103  	expected := "Tiller (the helm server side component) has been uninstalled from your Kubernetes Cluster."
   104  	if !strings.Contains(buf.String(), expected) {
   105  		t.Errorf("expected %q, got %q", expected, buf.String())
   106  	}
   107  	if _, err := os.Stat(home); err == nil {
   108  		t.Errorf("Helm home directory %s already exists", home)
   109  	}
   110  }
   111  
   112  func TestReset_deployedReleases(t *testing.T) {
   113  	home, err := ioutil.TempDir("", "helm_home")
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	defer os.Remove(home)
   118  
   119  	var buf bytes.Buffer
   120  	resp := []*release.Release{
   121  		releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}),
   122  	}
   123  	c := &fakeReleaseClient{
   124  		rels: resp,
   125  	}
   126  	fc := fake.NewSimpleClientset()
   127  	cmd := &resetCmd{
   128  		out:        &buf,
   129  		home:       helmpath.Home(home),
   130  		client:     c,
   131  		kubeClient: fc,
   132  		namespace:  api.NamespaceDefault,
   133  	}
   134  	err = cmd.run()
   135  	expected := "There are still 1 deployed releases (Tip: use --force)"
   136  	if !strings.Contains(err.Error(), expected) {
   137  		t.Errorf("unexpected error: %v", err)
   138  	}
   139  	if _, err := os.Stat(home); err != nil {
   140  		t.Errorf("Helm home directory %s does not exists", home)
   141  	}
   142  }
   143  
   144  func TestReset_forceFlag(t *testing.T) {
   145  	home, err := ioutil.TempDir("", "helm_home")
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	defer os.Remove(home)
   150  
   151  	var buf bytes.Buffer
   152  	resp := []*release.Release{
   153  		releaseMock(&releaseOptions{name: "atlas-guide", statusCode: release.Status_DEPLOYED}),
   154  	}
   155  	c := &fakeReleaseClient{
   156  		rels: resp,
   157  	}
   158  	fc := fake.NewSimpleClientset()
   159  	cmd := &resetCmd{
   160  		force:      true,
   161  		out:        &buf,
   162  		home:       helmpath.Home(home),
   163  		client:     c,
   164  		kubeClient: fc,
   165  		namespace:  api.NamespaceDefault,
   166  	}
   167  	if err := cmd.run(); err != nil {
   168  		t.Errorf("unexpected error: %v", err)
   169  	}
   170  	actions := fc.Actions()
   171  	if len(actions) != 2 {
   172  		t.Errorf("Expected 2 actions, got %d", len(actions))
   173  	}
   174  	if !actions[0].Matches("get", "services") {
   175  		t.Errorf("unexpected action: %v, expected get service", actions[1])
   176  	}
   177  	if !actions[1].Matches("get", "deployments") {
   178  		t.Errorf("unexpected action: %v, expected get deployment", actions[0])
   179  	}
   180  	expected := "Tiller (the helm server side component) has been uninstalled from your Kubernetes Cluster."
   181  	if !strings.Contains(buf.String(), expected) {
   182  		t.Errorf("expected %q, got %q", expected, buf.String())
   183  	}
   184  	if _, err := os.Stat(home); err != nil {
   185  		t.Errorf("Helm home directory %s does not exists", home)
   186  	}
   187  }