github.com/zoumo/helm@v2.5.0+incompatible/pkg/tiller/release_history_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 tiller
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/helm/pkg/helm"
    24  	rpb "k8s.io/helm/pkg/proto/hapi/release"
    25  	tpb "k8s.io/helm/pkg/proto/hapi/services"
    26  )
    27  
    28  func TestGetHistory_WithRevisions(t *testing.T) {
    29  	mk := func(name string, vers int32, code rpb.Status_Code) *rpb.Release {
    30  		return &rpb.Release{
    31  			Name:    name,
    32  			Version: vers,
    33  			Info:    &rpb.Info{Status: &rpb.Status{Code: code}},
    34  		}
    35  	}
    36  
    37  	// GetReleaseHistoryTests
    38  	tests := []struct {
    39  		desc string
    40  		req  *tpb.GetHistoryRequest
    41  		res  *tpb.GetHistoryResponse
    42  	}{
    43  		{
    44  			desc: "get release with history and default limit (max=256)",
    45  			req:  &tpb.GetHistoryRequest{Name: "angry-bird", Max: 256},
    46  			res: &tpb.GetHistoryResponse{Releases: []*rpb.Release{
    47  				mk("angry-bird", 4, rpb.Status_DEPLOYED),
    48  				mk("angry-bird", 3, rpb.Status_SUPERSEDED),
    49  				mk("angry-bird", 2, rpb.Status_SUPERSEDED),
    50  				mk("angry-bird", 1, rpb.Status_SUPERSEDED),
    51  			}},
    52  		},
    53  		{
    54  			desc: "get release with history using result limit (max=2)",
    55  			req:  &tpb.GetHistoryRequest{Name: "angry-bird", Max: 2},
    56  			res: &tpb.GetHistoryResponse{Releases: []*rpb.Release{
    57  				mk("angry-bird", 4, rpb.Status_DEPLOYED),
    58  				mk("angry-bird", 3, rpb.Status_SUPERSEDED),
    59  			}},
    60  		},
    61  	}
    62  
    63  	// test release history for release 'angry-bird'
    64  	hist := []*rpb.Release{
    65  		mk("angry-bird", 4, rpb.Status_DEPLOYED),
    66  		mk("angry-bird", 3, rpb.Status_SUPERSEDED),
    67  		mk("angry-bird", 2, rpb.Status_SUPERSEDED),
    68  		mk("angry-bird", 1, rpb.Status_SUPERSEDED),
    69  	}
    70  
    71  	srv := rsFixture()
    72  	for _, rls := range hist {
    73  		if err := srv.env.Releases.Create(rls); err != nil {
    74  			t.Fatalf("Failed to create release: %s", err)
    75  		}
    76  	}
    77  
    78  	// run tests
    79  	for _, tt := range tests {
    80  		res, err := srv.GetHistory(helm.NewContext(), tt.req)
    81  		if err != nil {
    82  			t.Fatalf("%s:\nFailed to get History of %q: %s", tt.desc, tt.req.Name, err)
    83  		}
    84  		if !reflect.DeepEqual(res, tt.res) {
    85  			t.Fatalf("%s:\nExpected:\n\t%+v\nActual\n\t%+v", tt.desc, tt.res, res)
    86  		}
    87  	}
    88  }
    89  
    90  func TestGetHistory_WithNoRevisions(t *testing.T) {
    91  	tests := []struct {
    92  		desc string
    93  		req  *tpb.GetHistoryRequest
    94  	}{
    95  		{
    96  			desc: "get release with no history",
    97  			req:  &tpb.GetHistoryRequest{Name: "sad-panda", Max: 256},
    98  		},
    99  	}
   100  
   101  	// create release 'sad-panda' with no revision history
   102  	rls := namedReleaseStub("sad-panda", rpb.Status_DEPLOYED)
   103  	srv := rsFixture()
   104  	srv.env.Releases.Create(rls)
   105  
   106  	for _, tt := range tests {
   107  		res, err := srv.GetHistory(helm.NewContext(), tt.req)
   108  		if err != nil {
   109  			t.Fatalf("%s:\nFailed to get History of %q: %s", tt.desc, tt.req.Name, err)
   110  		}
   111  		if len(res.Releases) > 1 {
   112  			t.Fatalf("%s:\nExpected zero items, got %d", tt.desc, len(res.Releases))
   113  		}
   114  	}
   115  }