github.com/strongmonkey/helm@v2.7.2+incompatible/cmd/helm/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 main
    18  
    19  import (
    20  	"bytes"
    21  	"regexp"
    22  	"testing"
    23  
    24  	"k8s.io/helm/pkg/helm"
    25  	rpb "k8s.io/helm/pkg/proto/hapi/release"
    26  )
    27  
    28  func TestHistoryCmd(t *testing.T) {
    29  	mk := func(name string, vers int32, code rpb.Status_Code) *rpb.Release {
    30  		return releaseMock(&releaseOptions{
    31  			name:       name,
    32  			version:    vers,
    33  			statusCode: code,
    34  		})
    35  	}
    36  
    37  	tests := []struct {
    38  		cmds string
    39  		desc string
    40  		args []string
    41  		resp []*rpb.Release
    42  		xout string
    43  	}{
    44  		{
    45  			cmds: "helm history RELEASE_NAME",
    46  			desc: "get history for release",
    47  			args: []string{"angry-bird"},
    48  			resp: []*rpb.Release{
    49  				mk("angry-bird", 4, rpb.Status_DEPLOYED),
    50  				mk("angry-bird", 3, rpb.Status_SUPERSEDED),
    51  				mk("angry-bird", 2, rpb.Status_SUPERSEDED),
    52  				mk("angry-bird", 1, rpb.Status_SUPERSEDED),
    53  			},
    54  			xout: "REVISION\tUPDATED                 \tSTATUS    \tCHART           \tDESCRIPTION \n1       \t(.*)\tSUPERSEDED\tfoo-0.1.0-beta.1\tRelease mock\n2       \t(.*)\tSUPERSEDED\tfoo-0.1.0-beta.1\tRelease mock\n3       \t(.*)\tSUPERSEDED\tfoo-0.1.0-beta.1\tRelease mock\n4       \t(.*)\tDEPLOYED  \tfoo-0.1.0-beta.1\tRelease mock\n",
    55  		},
    56  		{
    57  			cmds: "helm history --max=MAX RELEASE_NAME",
    58  			desc: "get history with max limit set",
    59  			args: []string{"--max=2", "angry-bird"},
    60  			resp: []*rpb.Release{
    61  				mk("angry-bird", 4, rpb.Status_DEPLOYED),
    62  				mk("angry-bird", 3, rpb.Status_SUPERSEDED),
    63  			},
    64  			xout: "REVISION\tUPDATED                 \tSTATUS    \tCHART           \tDESCRIPTION \n3       \t(.*)\tSUPERSEDED\tfoo-0.1.0-beta.1\tRelease mock\n4       \t(.*)\tDEPLOYED  \tfoo-0.1.0-beta.1\tRelease mock\n",
    65  		},
    66  	}
    67  
    68  	var buf bytes.Buffer
    69  	for _, tt := range tests {
    70  		frc := &helm.FakeClient{Rels: tt.resp}
    71  		cmd := newHistoryCmd(frc, &buf)
    72  		cmd.ParseFlags(tt.args)
    73  
    74  		if err := cmd.RunE(cmd, tt.args); err != nil {
    75  			t.Fatalf("%q\n\t%s: unexpected error: %v", tt.cmds, tt.desc, err)
    76  		}
    77  		re := regexp.MustCompile(tt.xout)
    78  		if !re.Match(buf.Bytes()) {
    79  			t.Fatalf("%q\n\t%s:\nexpected\n\t%q\nactual\n\t%q", tt.cmds, tt.desc, tt.xout, buf.String())
    80  		}
    81  		buf.Reset()
    82  	}
    83  }