github.com/sgoings/helm@v2.0.0-alpha.2.0.20170406211108-734e92851ac3+incompatible/cmd/helm/init_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  	"github.com/ghodss/yaml"
    27  
    28  	"k8s.io/kubernetes/pkg/api"
    29  	"k8s.io/kubernetes/pkg/api/errors"
    30  	"k8s.io/kubernetes/pkg/apis/extensions"
    31  	"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
    32  	testcore "k8s.io/kubernetes/pkg/client/testing/core"
    33  	"k8s.io/kubernetes/pkg/runtime"
    34  
    35  	"k8s.io/helm/pkg/helm/helmpath"
    36  )
    37  
    38  func TestInitCmd(t *testing.T) {
    39  	home, err := ioutil.TempDir("", "helm_home")
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	defer os.Remove(home)
    44  
    45  	var buf bytes.Buffer
    46  	fc := fake.NewSimpleClientset()
    47  	cmd := &initCmd{
    48  		out:        &buf,
    49  		home:       helmpath.Home(home),
    50  		kubeClient: fc,
    51  		namespace:  api.NamespaceDefault,
    52  	}
    53  	if err := cmd.run(); err != nil {
    54  		t.Errorf("expected error: %v", err)
    55  	}
    56  	actions := fc.Actions()
    57  	if len(actions) != 2 {
    58  		t.Errorf("Expected 2 actions, got %d", len(actions))
    59  	}
    60  	if !actions[0].Matches("create", "deployments") {
    61  		t.Errorf("unexpected action: %v, expected create deployment", actions[0])
    62  	}
    63  	if !actions[1].Matches("create", "services") {
    64  		t.Errorf("unexpected action: %v, expected create service", actions[1])
    65  	}
    66  	expected := "Tiller (the helm server side component) has been installed into your Kubernetes Cluster."
    67  	if !strings.Contains(buf.String(), expected) {
    68  		t.Errorf("expected %q, got %q", expected, buf.String())
    69  	}
    70  }
    71  
    72  func TestInitCmd_exists(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  	fc := fake.NewSimpleClientset(&extensions.Deployment{
    81  		ObjectMeta: api.ObjectMeta{
    82  			Namespace: api.NamespaceDefault,
    83  			Name:      "tiller-deploy",
    84  		},
    85  	})
    86  	fc.PrependReactor("*", "*", func(action testcore.Action) (bool, runtime.Object, error) {
    87  		return true, nil, errors.NewAlreadyExists(api.Resource("deployments"), "1")
    88  	})
    89  	cmd := &initCmd{
    90  		out:        &buf,
    91  		home:       helmpath.Home(home),
    92  		kubeClient: fc,
    93  		namespace:  api.NamespaceDefault,
    94  	}
    95  	if err := cmd.run(); err != nil {
    96  		t.Errorf("expected error: %v", err)
    97  	}
    98  	expected := "Warning: Tiller is already installed in the cluster.\n" +
    99  		"(Use --client-only to suppress this message, or --upgrade to upgrade Tiller to the current version.)"
   100  	if !strings.Contains(buf.String(), expected) {
   101  		t.Errorf("expected %q, got %q", expected, buf.String())
   102  	}
   103  }
   104  
   105  func TestInitCmd_clientOnly(t *testing.T) {
   106  	home, err := ioutil.TempDir("", "helm_home")
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	defer os.Remove(home)
   111  
   112  	var buf bytes.Buffer
   113  	fc := fake.NewSimpleClientset()
   114  	cmd := &initCmd{
   115  		out:        &buf,
   116  		home:       helmpath.Home(home),
   117  		kubeClient: fc,
   118  		clientOnly: true,
   119  		namespace:  api.NamespaceDefault,
   120  	}
   121  	if err := cmd.run(); err != nil {
   122  		t.Errorf("unexpected error: %v", err)
   123  	}
   124  	if len(fc.Actions()) != 0 {
   125  		t.Error("expected client call")
   126  	}
   127  	expected := "Not installing tiller due to 'client-only' flag having been set"
   128  	if !strings.Contains(buf.String(), expected) {
   129  		t.Errorf("expected %q, got %q", expected, buf.String())
   130  	}
   131  }
   132  
   133  func TestInitCmd_dryRun(t *testing.T) {
   134  	// This is purely defensive in this case.
   135  	home, err := ioutil.TempDir("", "helm_home")
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	dbg := flagDebug
   140  	flagDebug = true
   141  	defer func() {
   142  		os.Remove(home)
   143  		flagDebug = dbg
   144  	}()
   145  
   146  	var buf bytes.Buffer
   147  	fc := fake.NewSimpleClientset()
   148  	cmd := &initCmd{
   149  		out:        &buf,
   150  		home:       helmpath.Home(home),
   151  		kubeClient: fc,
   152  		clientOnly: true,
   153  		dryRun:     true,
   154  		namespace:  api.NamespaceDefault,
   155  	}
   156  	if err := cmd.run(); err != nil {
   157  		t.Fatal(err)
   158  	}
   159  	if got := len(fc.Actions()); got != 0 {
   160  		t.Errorf("expected no server calls, got %d", got)
   161  	}
   162  
   163  	docs := bytes.Split(buf.Bytes(), []byte("\n---"))
   164  	if got, want := len(docs), 2; got != want {
   165  		t.Fatalf("Expected document count of %d, got %d", want, got)
   166  	}
   167  	for _, doc := range docs {
   168  		var y map[string]interface{}
   169  		if err := yaml.Unmarshal(doc, &y); err != nil {
   170  			t.Errorf("Expected parseable YAML, got %q\n\t%s", doc, err)
   171  		}
   172  	}
   173  }
   174  
   175  func TestEnsureHome(t *testing.T) {
   176  	home, err := ioutil.TempDir("", "helm_home")
   177  	if err != nil {
   178  		t.Fatal(err)
   179  	}
   180  	defer os.Remove(home)
   181  
   182  	b := bytes.NewBuffer(nil)
   183  	hh := helmpath.Home(home)
   184  	helmHome = home
   185  	if err := ensureDirectories(hh, b); err != nil {
   186  		t.Error(err)
   187  	}
   188  	if err := ensureDefaultRepos(hh, b, false); err != nil {
   189  		t.Error(err)
   190  	}
   191  	if err := ensureDefaultRepos(hh, b, true); err != nil {
   192  		t.Error(err)
   193  	}
   194  	if err := ensureRepoFileFormat(hh.RepositoryFile(), b); err != nil {
   195  		t.Error(err)
   196  	}
   197  
   198  	expectedDirs := []string{hh.String(), hh.Repository(), hh.Cache(), hh.LocalRepository()}
   199  	for _, dir := range expectedDirs {
   200  		if fi, err := os.Stat(dir); err != nil {
   201  			t.Errorf("%s", err)
   202  		} else if !fi.IsDir() {
   203  			t.Errorf("%s is not a directory", fi)
   204  		}
   205  	}
   206  
   207  	if fi, err := os.Stat(hh.RepositoryFile()); err != nil {
   208  		t.Error(err)
   209  	} else if fi.IsDir() {
   210  		t.Errorf("%s should not be a directory", fi)
   211  	}
   212  
   213  	if fi, err := os.Stat(hh.LocalRepository(localRepoIndexFilePath)); err != nil {
   214  		t.Errorf("%s", err)
   215  	} else if fi.IsDir() {
   216  		t.Errorf("%s should not be a directory", fi)
   217  	}
   218  }