github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/installer/uninstall_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 installer // import "k8s.io/helm/cmd/helm/installer"
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"k8s.io/kubernetes/pkg/api"
    24  	apierrors "k8s.io/kubernetes/pkg/api/errors"
    25  	"k8s.io/kubernetes/pkg/api/meta"
    26  	"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
    27  	testcore "k8s.io/kubernetes/pkg/client/testing/core"
    28  	"k8s.io/kubernetes/pkg/kubectl"
    29  	cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
    30  	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
    31  	"k8s.io/kubernetes/pkg/runtime"
    32  
    33  	"k8s.io/helm/pkg/kube"
    34  )
    35  
    36  type fakeReaper struct {
    37  	namespace string
    38  	name      string
    39  }
    40  
    41  func (r *fakeReaper) Stop(namespace, name string, timeout time.Duration, gracePeriod *api.DeleteOptions) error {
    42  	r.namespace = namespace
    43  	r.name = name
    44  	return nil
    45  }
    46  
    47  type fakeReaperFactory struct {
    48  	cmdutil.Factory
    49  	reaper kubectl.Reaper
    50  }
    51  
    52  func (f *fakeReaperFactory) Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error) {
    53  	return f.reaper, nil
    54  }
    55  
    56  func TestUninstall(t *testing.T) {
    57  	existingService := service(api.NamespaceDefault)
    58  	existingDeployment := deployment(api.NamespaceDefault, "image", false)
    59  
    60  	fc := &fake.Clientset{}
    61  	fc.AddReactor("get", "services", func(action testcore.Action) (bool, runtime.Object, error) {
    62  		return true, existingService, nil
    63  	})
    64  	fc.AddReactor("delete", "services", func(action testcore.Action) (bool, runtime.Object, error) {
    65  		return true, nil, nil
    66  	})
    67  	fc.AddReactor("get", "deployments", func(action testcore.Action) (bool, runtime.Object, error) {
    68  		return true, existingDeployment, nil
    69  	})
    70  
    71  	f, _, _, _ := cmdtesting.NewAPIFactory()
    72  	r := &fakeReaper{}
    73  	rf := &fakeReaperFactory{Factory: f, reaper: r}
    74  	kc := &kube.Client{Factory: rf}
    75  
    76  	if err := Uninstall(fc, kc, api.NamespaceDefault, false); err != nil {
    77  		t.Errorf("unexpected error: %#+v", err)
    78  	}
    79  
    80  	if actions := fc.Actions(); len(actions) != 3 {
    81  		t.Errorf("unexpected actions: %v, expected 3 actions got %d", actions, len(actions))
    82  	}
    83  
    84  	if r.namespace != api.NamespaceDefault {
    85  		t.Errorf("unexpected reaper namespace: %s", r.name)
    86  	}
    87  
    88  	if r.name != "tiller-deploy" {
    89  		t.Errorf("unexpected reaper name: %s", r.name)
    90  	}
    91  }
    92  
    93  func TestUninstall_serviceNotFound(t *testing.T) {
    94  	existingDeployment := deployment(api.NamespaceDefault, "imageToReplace", false)
    95  
    96  	fc := &fake.Clientset{}
    97  	fc.AddReactor("get", "services", func(action testcore.Action) (bool, runtime.Object, error) {
    98  		return true, nil, apierrors.NewNotFound(api.Resource("services"), "1")
    99  	})
   100  	fc.AddReactor("get", "deployments", func(action testcore.Action) (bool, runtime.Object, error) {
   101  		return true, existingDeployment, nil
   102  	})
   103  
   104  	f, _, _, _ := cmdtesting.NewAPIFactory()
   105  	r := &fakeReaper{}
   106  	rf := &fakeReaperFactory{Factory: f, reaper: r}
   107  	kc := &kube.Client{Factory: rf}
   108  
   109  	if err := Uninstall(fc, kc, api.NamespaceDefault, false); err != nil {
   110  		t.Errorf("unexpected error: %#+v", err)
   111  	}
   112  
   113  	if actions := fc.Actions(); len(actions) != 2 {
   114  		t.Errorf("unexpected actions: %v, expected 2 actions got %d", actions, len(actions))
   115  	}
   116  
   117  	if r.namespace != api.NamespaceDefault {
   118  		t.Errorf("unexpected reaper namespace: %s", r.name)
   119  	}
   120  
   121  	if r.name != "tiller-deploy" {
   122  		t.Errorf("unexpected reaper name: %s", r.name)
   123  	}
   124  }
   125  
   126  func TestUninstall_deploymentNotFound(t *testing.T) {
   127  	existingService := service(api.NamespaceDefault)
   128  
   129  	fc := &fake.Clientset{}
   130  	fc.AddReactor("get", "services", func(action testcore.Action) (bool, runtime.Object, error) {
   131  		return true, existingService, nil
   132  	})
   133  	fc.AddReactor("delete", "services", func(action testcore.Action) (bool, runtime.Object, error) {
   134  		return true, nil, nil
   135  	})
   136  	fc.AddReactor("get", "deployments", func(action testcore.Action) (bool, runtime.Object, error) {
   137  		return true, nil, apierrors.NewNotFound(api.Resource("deployments"), "1")
   138  	})
   139  
   140  	f, _, _, _ := cmdtesting.NewAPIFactory()
   141  	r := &fakeReaper{}
   142  	rf := &fakeReaperFactory{Factory: f, reaper: r}
   143  	kc := &kube.Client{Factory: rf}
   144  
   145  	if err := Uninstall(fc, kc, api.NamespaceDefault, false); err != nil {
   146  		t.Errorf("unexpected error: %#+v", err)
   147  	}
   148  
   149  	if actions := fc.Actions(); len(actions) != 3 {
   150  		t.Errorf("unexpected actions: %v, expected 3 actions got %d", actions, len(actions))
   151  	}
   152  
   153  	if r.namespace != "" {
   154  		t.Errorf("unexpected reaper namespace: %s", r.name)
   155  	}
   156  
   157  	if r.name != "" {
   158  		t.Errorf("unexpected reaper name: %s", r.name)
   159  	}
   160  }