github.com/Beeketing/helm@v2.12.1+incompatible/cmd/helm/list_test.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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  	"io"
    21  	"regexp"
    22  	"testing"
    23  
    24  	"github.com/spf13/cobra"
    25  
    26  	"io/ioutil"
    27  	"os"
    28  
    29  	"k8s.io/helm/pkg/chartutil"
    30  	"k8s.io/helm/pkg/helm"
    31  	"k8s.io/helm/pkg/proto/hapi/chart"
    32  	"k8s.io/helm/pkg/proto/hapi/release"
    33  )
    34  
    35  func TestListCmd(t *testing.T) {
    36  	tmpChart, _ := ioutil.TempDir("testdata", "tmp")
    37  	defer os.RemoveAll(tmpChart)
    38  	cfile := &chart.Metadata{
    39  		Name:        "foo",
    40  		Description: "A Helm chart for Kubernetes",
    41  		Version:     "0.1.0-beta.1",
    42  		AppVersion:  "2.X.A",
    43  	}
    44  	chartPath, err := chartutil.Create(cfile, tmpChart)
    45  	if err != nil {
    46  		t.Errorf("Error creating chart for list: %v", err)
    47  	}
    48  	ch, _ := chartutil.Load(chartPath)
    49  
    50  	tests := []releaseCase{
    51  		{
    52  			name:     "empty",
    53  			rels:     []*release.Release{},
    54  			expected: "",
    55  		},
    56  		{
    57  			name: "with a release",
    58  			rels: []*release.Release{
    59  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}),
    60  			},
    61  			expected: "thomas-guide",
    62  		},
    63  		{
    64  			name: "list",
    65  			rels: []*release.Release{
    66  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas"}),
    67  			},
    68  			expected: "NAME \tREVISION\tUPDATED                 \tSTATUS  \tCHART           \tAPP VERSION\tNAMESPACE\natlas\t1       \t(.*)\tDEPLOYED\tfoo-0.1.0-beta.1\t           \tdefault  \n",
    69  		},
    70  		{
    71  			name: "list with appVersion",
    72  			rels: []*release.Release{
    73  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas", Chart: ch}),
    74  			},
    75  			expected: "NAME \tREVISION\tUPDATED                 \tSTATUS  \tCHART           \tAPP VERSION\tNAMESPACE\natlas\t1       \t(.*)\tDEPLOYED\tfoo-0.1.0-beta.1\t2.X.A      \tdefault  \n",
    76  		},
    77  		{
    78  			name:  "with json output",
    79  			flags: []string{"--max", "1", "--output", "json"},
    80  			rels: []*release.Release{
    81  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}),
    82  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide"}),
    83  			},
    84  			expected: regexp.QuoteMeta(`{"Next":"atlas-guide","Releases":[{"Name":"thomas-guide","Revision":1,"Updated":"`) + `([^"]*)` + regexp.QuoteMeta(`","Status":"DEPLOYED","Chart":"foo-0.1.0-beta.1","AppVersion":"","Namespace":"default"}]}
    85  `),
    86  		},
    87  		{
    88  			name:  "with yaml output",
    89  			flags: []string{"--max", "1", "--output", "yaml"},
    90  			rels: []*release.Release{
    91  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}),
    92  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide"}),
    93  			},
    94  			expected: regexp.QuoteMeta(`Next: atlas-guide
    95  Releases:
    96  - AppVersion: ""
    97    Chart: foo-0.1.0-beta.1
    98    Name: thomas-guide
    99    Namespace: default
   100    Revision: 1
   101    Status: DEPLOYED
   102    Updated: `) + `(.*)` + `
   103  
   104  `,
   105  		},
   106  		{
   107  			name:  "with short json output",
   108  			flags: []string{"-q", "--output", "json"},
   109  			rels: []*release.Release{
   110  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas"}),
   111  			},
   112  			expected: regexp.QuoteMeta(`["atlas"]
   113  `),
   114  		},
   115  		{
   116  			name:  "with short yaml output",
   117  			flags: []string{"-q", "--output", "yaml"},
   118  			rels: []*release.Release{
   119  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas"}),
   120  			},
   121  			expected: regexp.QuoteMeta(`- atlas
   122  
   123  `),
   124  		},
   125  		{
   126  			name:  "with json output without next",
   127  			flags: []string{"--output", "json"},
   128  			rels:  []*release.Release{},
   129  			expected: regexp.QuoteMeta(`{"Next":"","Releases":[]}
   130  `),
   131  		},
   132  		{
   133  			name:  "with yaml output without next",
   134  			flags: []string{"--output", "yaml"},
   135  			rels:  []*release.Release{},
   136  			expected: regexp.QuoteMeta(`Next: ""
   137  Releases: []
   138  
   139  `),
   140  		},
   141  		{
   142  			name:     "with unknown output format",
   143  			flags:    []string{"--output", "_unknown_"},
   144  			rels:     []*release.Release{},
   145  			err:      true,
   146  			expected: regexp.QuoteMeta(``),
   147  		},
   148  		{
   149  			name:  "list, one deployed, one failed",
   150  			flags: []string{"-q"},
   151  			rels: []*release.Release{
   152  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", StatusCode: release.Status_FAILED}),
   153  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", StatusCode: release.Status_DEPLOYED}),
   154  			},
   155  			expected: "thomas-guide\natlas-guide",
   156  		},
   157  		{
   158  			name:  "with a release, multiple flags",
   159  			flags: []string{"--deleted", "--deployed", "--failed", "-q"},
   160  			rels: []*release.Release{
   161  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", StatusCode: release.Status_DELETED}),
   162  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", StatusCode: release.Status_DEPLOYED}),
   163  			},
   164  			// Note: We're really only testing that the flags parsed correctly. Which results are returned
   165  			// depends on the backend. And until pkg/helm is done, we can't mock this.
   166  			expected: "thomas-guide\natlas-guide",
   167  		},
   168  		{
   169  			name:  "with a release, multiple flags",
   170  			flags: []string{"--all", "-q"},
   171  			rels: []*release.Release{
   172  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", StatusCode: release.Status_DELETED}),
   173  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", StatusCode: release.Status_DEPLOYED}),
   174  			},
   175  			// See note on previous test.
   176  			expected: "thomas-guide\natlas-guide",
   177  		},
   178  		{
   179  			name:  "with a release, multiple flags, deleting",
   180  			flags: []string{"--all", "-q"},
   181  			rels: []*release.Release{
   182  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", StatusCode: release.Status_DELETING}),
   183  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", StatusCode: release.Status_DEPLOYED}),
   184  			},
   185  			// See note on previous test.
   186  			expected: "thomas-guide\natlas-guide",
   187  		},
   188  		{
   189  			name:  "namespace defined, multiple flags",
   190  			flags: []string{"--all", "-q", "--namespace test123"},
   191  			rels: []*release.Release{
   192  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", Namespace: "test123"}),
   193  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", Namespace: "test321"}),
   194  			},
   195  			// See note on previous test.
   196  			expected: "thomas-guide",
   197  		},
   198  		{
   199  			name:  "with a pending release, multiple flags",
   200  			flags: []string{"--all", "-q"},
   201  			rels: []*release.Release{
   202  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", StatusCode: release.Status_PENDING_INSTALL}),
   203  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", StatusCode: release.Status_DEPLOYED}),
   204  			},
   205  			expected: "thomas-guide\natlas-guide",
   206  		},
   207  		{
   208  			name:  "with a pending release, pending flag",
   209  			flags: []string{"--pending", "-q"},
   210  			rels: []*release.Release{
   211  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", StatusCode: release.Status_PENDING_INSTALL}),
   212  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "wild-idea", StatusCode: release.Status_PENDING_UPGRADE}),
   213  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "crazy-maps", StatusCode: release.Status_PENDING_ROLLBACK}),
   214  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "atlas-guide", StatusCode: release.Status_DEPLOYED}),
   215  			},
   216  			expected: "thomas-guide\nwild-idea\ncrazy-maps",
   217  		},
   218  		{
   219  			name: "with old releases",
   220  			rels: []*release.Release{
   221  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide"}),
   222  				helm.ReleaseMock(&helm.MockReleaseOptions{Name: "thomas-guide", StatusCode: release.Status_FAILED}),
   223  			},
   224  			expected: "thomas-guide",
   225  		},
   226  	}
   227  
   228  	runReleaseCases(t, tests, func(c *helm.FakeClient, out io.Writer) *cobra.Command {
   229  		return newListCmd(c, out)
   230  	})
   231  }