github.com/koderover/helm@v2.17.0+incompatible/cmd/helm/inspect_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  	"bytes"
    21  	"io/ioutil"
    22  	"os"
    23  	"strings"
    24  	"testing"
    25  
    26  	"k8s.io/helm/pkg/repo/repotest"
    27  )
    28  
    29  func TestInspect(t *testing.T) {
    30  	b := bytes.NewBuffer(nil)
    31  
    32  	insp := &inspectCmd{
    33  		chartpath: "testdata/testcharts/alpine",
    34  		output:    all,
    35  		out:       b,
    36  	}
    37  	insp.run()
    38  
    39  	// Load the data from the textfixture directly.
    40  	cdata, err := ioutil.ReadFile("testdata/testcharts/alpine/Chart.yaml")
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	data, err := ioutil.ReadFile("testdata/testcharts/alpine/values.yaml")
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	readmeData, err := ioutil.ReadFile("testdata/testcharts/alpine/README.md")
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	parts := strings.SplitN(b.String(), "---", 3)
    53  	if len(parts) != 3 {
    54  		t.Fatalf("Expected 2 parts, got %d", len(parts))
    55  	}
    56  
    57  	expect := []string{
    58  		strings.Replace(strings.TrimSpace(string(cdata)), "\r", "", -1),
    59  		strings.Replace(strings.TrimSpace(string(data)), "\r", "", -1),
    60  		strings.Replace(strings.TrimSpace(string(readmeData)), "\r", "", -1),
    61  	}
    62  
    63  	// Problem: ghodss/yaml doesn't marshal into struct order. To solve, we
    64  	// have to carefully craft the Chart.yaml to match.
    65  	for i, got := range parts {
    66  		got = strings.Replace(strings.TrimSpace(got), "\r", "", -1)
    67  		if got != expect[i] {
    68  			t.Errorf("Expected\n%q\nGot\n%q\n", expect[i], got)
    69  		}
    70  	}
    71  
    72  	// Regression tests for missing values. See issue #1024.
    73  	b.Reset()
    74  	insp = &inspectCmd{
    75  		chartpath: "testdata/testcharts/novals",
    76  		output:    "values",
    77  		out:       b,
    78  	}
    79  	insp.run()
    80  	if b.Len() != 0 {
    81  		t.Errorf("expected empty values buffer, got %q", b.String())
    82  	}
    83  }
    84  
    85  func TestInspectPreReleaseChart(t *testing.T) {
    86  	hh, err := tempHelmHome(t)
    87  	if err != nil {
    88  		t.Fatal(err)
    89  	}
    90  	cleanup := resetEnv()
    91  	defer func() {
    92  		os.RemoveAll(hh.String())
    93  		cleanup()
    94  	}()
    95  
    96  	settings.Home = hh
    97  
    98  	srv := repotest.NewServer(hh.String())
    99  	defer srv.Stop()
   100  
   101  	if _, err := srv.CopyCharts("testdata/testcharts/*.tgz*"); err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if err := srv.LinkIndices(); err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	tests := []struct {
   109  		name        string
   110  		args        []string
   111  		flags       []string
   112  		fail        bool
   113  		expectedErr string
   114  	}{
   115  		{
   116  			name:        "inspect pre-release chart",
   117  			args:        []string{"prerelease"},
   118  			fail:        true,
   119  			expectedErr: "chart \"prerelease\" not found",
   120  		},
   121  		{
   122  			name:  "inspect pre-release chart with 'devel' flag",
   123  			args:  []string{"prerelease"},
   124  			flags: []string{"--devel"},
   125  			fail:  false,
   126  		},
   127  	}
   128  
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			tt.flags = append(tt.flags, "--repo", srv.URL())
   132  			cmd := newInspectCmd(ioutil.Discard)
   133  			cmd.SetArgs(tt.args)
   134  			cmd.ParseFlags(tt.flags)
   135  			if err := cmd.RunE(cmd, tt.args); err != nil {
   136  				if tt.fail {
   137  					if !strings.Contains(err.Error(), tt.expectedErr) {
   138  						t.Errorf("%q expected error: %s, got: %s", tt.name, tt.expectedErr, err.Error())
   139  					}
   140  					return
   141  				}
   142  				t.Errorf("%q reported error: %s", tt.name, err)
   143  			}
   144  		})
   145  	}
   146  }