github.com/felipejfc/helm@v2.1.2+incompatible/pkg/helm/helm_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 helm // import "k8s.io/helm/pkg/helm"
    18  
    19  import (
    20  	"errors"
    21  	"path/filepath"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/golang/protobuf/proto"
    26  	"golang.org/x/net/context"
    27  
    28  	"k8s.io/helm/pkg/chartutil"
    29  	cpb "k8s.io/helm/pkg/proto/hapi/chart"
    30  	rls "k8s.io/helm/pkg/proto/hapi/release"
    31  	tpb "k8s.io/helm/pkg/proto/hapi/services"
    32  )
    33  
    34  // path to example charts relative to pkg/helm.
    35  const chartsDir = "../../docs/examples/"
    36  
    37  // sentinel error to indicate to the helm client to not send the request to tiller.
    38  var errSkip = errors.New("test: skip")
    39  
    40  // Verify ReleaseListOption's are applied to a ListReleasesRequest correctly.
    41  func TestListReleases_VerifyOptions(t *testing.T) {
    42  	// Options testdata
    43  	var limit = 2
    44  	var offset = "offset"
    45  	var filter = "filter"
    46  	var sortBy = int32(2)
    47  	var sortOrd = int32(1)
    48  	var codes = []rls.Status_Code{
    49  		rls.Status_FAILED,
    50  		rls.Status_DELETED,
    51  		rls.Status_DEPLOYED,
    52  		rls.Status_SUPERSEDED,
    53  	}
    54  
    55  	// Expected ListReleasesRequest message
    56  	exp := &tpb.ListReleasesRequest{
    57  		Limit:       int64(limit),
    58  		Offset:      offset,
    59  		Filter:      filter,
    60  		SortBy:      tpb.ListSort_SortBy(sortBy),
    61  		SortOrder:   tpb.ListSort_SortOrder(sortOrd),
    62  		StatusCodes: codes,
    63  	}
    64  
    65  	// Options used in ListReleases
    66  	ops := []ReleaseListOption{
    67  		ReleaseListSort(sortBy),
    68  		ReleaseListOrder(sortOrd),
    69  		ReleaseListLimit(limit),
    70  		ReleaseListOffset(offset),
    71  		ReleaseListFilter(filter),
    72  		ReleaseListStatuses(codes),
    73  	}
    74  
    75  	// BeforeCall option to intercept helm client ListReleasesRequest
    76  	b4c := BeforeCall(func(_ context.Context, msg proto.Message) error {
    77  		switch act := msg.(type) {
    78  		case *tpb.ListReleasesRequest:
    79  			t.Logf("ListReleasesRequest: %#+v\n", act)
    80  			assert(t, exp, act)
    81  		default:
    82  			t.Fatalf("expected message of type ListReleasesRequest, got %T\n", act)
    83  		}
    84  		return errSkip
    85  	})
    86  
    87  	NewClient(b4c).ListReleases(ops...)
    88  }
    89  
    90  // Verify InstallOption's are applied to an InstallReleaseRequest correctly.
    91  func TestInstallRelease_VerifyOptions(t *testing.T) {
    92  	// Options testdata
    93  	var disableHooks = true
    94  	var releaseName = "test"
    95  	var namespace = "default"
    96  	var reuseName = true
    97  	var dryRun = true
    98  	var chartName = "alpine"
    99  	var overrides = []byte("key1=value1,key2=value2")
   100  
   101  	// Expected InstallReleaseRequest message
   102  	exp := &tpb.InstallReleaseRequest{
   103  		Chart:        loadChart(t, chartName),
   104  		Values:       &cpb.Config{Raw: string(overrides)},
   105  		DryRun:       dryRun,
   106  		Name:         releaseName,
   107  		DisableHooks: disableHooks,
   108  		Namespace:    namespace,
   109  		ReuseName:    reuseName,
   110  	}
   111  
   112  	// Options used in InstallRelease
   113  	ops := []InstallOption{
   114  		ValueOverrides(overrides),
   115  		InstallDryRun(dryRun),
   116  		ReleaseName(releaseName),
   117  		InstallReuseName(reuseName),
   118  		InstallDisableHooks(disableHooks),
   119  	}
   120  
   121  	// BeforeCall option to intercept helm client InstallReleaseRequest
   122  	b4c := BeforeCall(func(_ context.Context, msg proto.Message) error {
   123  		switch act := msg.(type) {
   124  		case *tpb.InstallReleaseRequest:
   125  			t.Logf("InstallReleaseRequest: %#+v\n", act)
   126  			assert(t, exp, act)
   127  		default:
   128  			t.Fatalf("expected message of type InstallReleaseRequest, got %T\n", act)
   129  		}
   130  		return errSkip
   131  	})
   132  
   133  	NewClient(b4c).InstallRelease(chartName, namespace, ops...)
   134  }
   135  
   136  // Verify DeleteOptions's are applied to an UninstallReleaseRequest correctly.
   137  func TestDeleteRelease_VerifyOptions(t *testing.T) {
   138  	// Options testdata
   139  	var releaseName = "test"
   140  	var disableHooks = true
   141  	var purgeFlag = true
   142  
   143  	// Expected DeleteReleaseRequest message
   144  	exp := &tpb.UninstallReleaseRequest{
   145  		Name:         releaseName,
   146  		Purge:        purgeFlag,
   147  		DisableHooks: disableHooks,
   148  	}
   149  
   150  	// Options used in DeleteRelease
   151  	ops := []DeleteOption{
   152  		DeletePurge(purgeFlag),
   153  		DeleteDisableHooks(disableHooks),
   154  	}
   155  
   156  	// BeforeCall option to intercept helm client DeleteReleaseRequest
   157  	b4c := BeforeCall(func(_ context.Context, msg proto.Message) error {
   158  		switch act := msg.(type) {
   159  		case *tpb.UninstallReleaseRequest:
   160  			t.Logf("UninstallReleaseRequest: %#+v\n", act)
   161  			assert(t, exp, act)
   162  		default:
   163  			t.Fatalf("expected message of type UninstallReleaseRequest, got %T\n", act)
   164  		}
   165  		return errSkip
   166  	})
   167  
   168  	NewClient(b4c).DeleteRelease(releaseName, ops...)
   169  }
   170  
   171  // Verify UpdateOption's are applied to an UpdateReleaseRequest correctly.
   172  func TestUpdateRelease_VerifyOptions(t *testing.T) {
   173  	// Options testdata
   174  	var chartName = "alpine"
   175  	var releaseName = "test"
   176  	var disableHooks = true
   177  	var overrides = []byte("key1=value1,key2=value2")
   178  	var dryRun = false
   179  
   180  	// Expected UpdateReleaseRequest message
   181  	exp := &tpb.UpdateReleaseRequest{
   182  		Name:         releaseName,
   183  		Chart:        loadChart(t, chartName),
   184  		Values:       &cpb.Config{Raw: string(overrides)},
   185  		DryRun:       dryRun,
   186  		DisableHooks: disableHooks,
   187  	}
   188  
   189  	// Options used in UpdateRelease
   190  	ops := []UpdateOption{
   191  		UpgradeDryRun(dryRun),
   192  		UpdateValueOverrides(overrides),
   193  		UpgradeDisableHooks(disableHooks),
   194  	}
   195  
   196  	// BeforeCall option to intercept helm client UpdateReleaseRequest
   197  	b4c := BeforeCall(func(_ context.Context, msg proto.Message) error {
   198  		switch act := msg.(type) {
   199  		case *tpb.UpdateReleaseRequest:
   200  			t.Logf("UpdateReleaseRequest: %#+v\n", act)
   201  			assert(t, exp, act)
   202  		default:
   203  			t.Fatalf("expected message of type UpdateReleaseRequest, got %T\n", act)
   204  		}
   205  		return errSkip
   206  	})
   207  
   208  	NewClient(b4c).UpdateRelease(releaseName, chartName, ops...)
   209  }
   210  
   211  // Verify RollbackOption's are applied to a RollbackReleaseRequest correctly.
   212  func TestRollbackRelease_VerifyOptions(t *testing.T) {
   213  	// Options testdata
   214  	var disableHooks = true
   215  	var releaseName = "test"
   216  	var revision = int32(2)
   217  	var dryRun = true
   218  
   219  	// Expected RollbackReleaseRequest message
   220  	exp := &tpb.RollbackReleaseRequest{
   221  		Name:         releaseName,
   222  		DryRun:       dryRun,
   223  		Version:      revision,
   224  		DisableHooks: disableHooks,
   225  	}
   226  
   227  	// Options used in RollbackRelease
   228  	ops := []RollbackOption{
   229  		RollbackDryRun(dryRun),
   230  		RollbackVersion(revision),
   231  		RollbackDisableHooks(disableHooks),
   232  	}
   233  
   234  	// BeforeCall option to intercept helm client RollbackReleaseRequest
   235  	b4c := BeforeCall(func(_ context.Context, msg proto.Message) error {
   236  		switch act := msg.(type) {
   237  		case *tpb.RollbackReleaseRequest:
   238  			t.Logf("RollbackReleaseRequest: %#+v\n", act)
   239  			assert(t, exp, act)
   240  		default:
   241  			t.Fatalf("expected message of type RollbackReleaseRequest, got %T\n", act)
   242  		}
   243  		return errSkip
   244  	})
   245  
   246  	NewClient(b4c).RollbackRelease(releaseName, ops...)
   247  }
   248  
   249  // Verify StatusOption's are applied to a GetReleaseStatusRequest correctly.
   250  func TestReleaseStatus_VerifyOptions(t *testing.T) {
   251  	// Options testdata
   252  	var releaseName = "test"
   253  	var revision = int32(2)
   254  
   255  	// Expected GetReleaseStatusRequest message
   256  	exp := &tpb.GetReleaseStatusRequest{
   257  		Name:    releaseName,
   258  		Version: revision,
   259  	}
   260  
   261  	// BeforeCall option to intercept helm client GetReleaseStatusRequest
   262  	b4c := BeforeCall(func(_ context.Context, msg proto.Message) error {
   263  		switch act := msg.(type) {
   264  		case *tpb.GetReleaseStatusRequest:
   265  			t.Logf("GetReleaseStatusRequest: %#+v\n", act)
   266  			assert(t, exp, act)
   267  		default:
   268  			t.Fatalf("expected message of type GetReleaseStatusRequest, got %T\n", act)
   269  		}
   270  		return errSkip
   271  	})
   272  
   273  	NewClient(b4c).ReleaseStatus(releaseName, StatusReleaseVersion(revision))
   274  }
   275  
   276  // Verify ContentOption's are applied to a GetReleaseContentRequest correctly.
   277  func TestReleaseContent_VerifyOptions(t *testing.T) {
   278  	// Options testdata
   279  	var releaseName = "test"
   280  	var revision = int32(2)
   281  
   282  	// Expected GetReleaseContentRequest message
   283  	exp := &tpb.GetReleaseContentRequest{
   284  		Name:    releaseName,
   285  		Version: revision,
   286  	}
   287  
   288  	// BeforeCall option to intercept helm client GetReleaseContentRequest
   289  	b4c := BeforeCall(func(_ context.Context, msg proto.Message) error {
   290  		switch act := msg.(type) {
   291  		case *tpb.GetReleaseContentRequest:
   292  			t.Logf("GetReleaseContentRequest: %#+v\n", act)
   293  			assert(t, exp, act)
   294  		default:
   295  			t.Fatalf("expected message of type GetReleaseContentRequest, got %T\n", act)
   296  		}
   297  		return errSkip
   298  	})
   299  
   300  	NewClient(b4c).ReleaseContent(releaseName, ContentReleaseVersion(revision))
   301  }
   302  
   303  func assert(t *testing.T, expect, actual interface{}) {
   304  	if !reflect.DeepEqual(expect, actual) {
   305  		t.Fatalf("expected %#+v, actual %#+v\n", expect, actual)
   306  	}
   307  }
   308  
   309  func loadChart(t *testing.T, name string) *cpb.Chart {
   310  	c, err := chartutil.Load(filepath.Join(chartsDir, name))
   311  	if err != nil {
   312  		t.Fatalf("failed to load test chart (%q): %s\n", name, err)
   313  	}
   314  	return c
   315  }