github.com/Racer159/helm-experiment@v0.0.0-20230822001441-1eb31183f614/src/install_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 cmd
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"path/filepath"
    24  	"testing"
    25  
    26  	"helm.sh/helm/v3/pkg/repo/repotest"
    27  )
    28  
    29  func TestInstall(t *testing.T) {
    30  	srv, err := repotest.NewTempServerWithCleanup(t, "testdata/testcharts/*.tgz*")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer srv.Stop()
    35  
    36  	srv.WithMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    37  		username, password, ok := r.BasicAuth()
    38  		if !ok || username != "username" || password != "password" {
    39  			t.Errorf("Expected request to use basic auth and for username == 'username' and password == 'password', got '%v', '%s', '%s'", ok, username, password)
    40  		}
    41  	}))
    42  
    43  	srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    44  		http.FileServer(http.Dir(srv.Root())).ServeHTTP(w, r)
    45  	}))
    46  	defer srv2.Close()
    47  
    48  	if err := srv.LinkIndices(); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  
    52  	repoFile := filepath.Join(srv.Root(), "repositories.yaml")
    53  
    54  	tests := []cmdTestCase{
    55  		// Install, base case
    56  		{
    57  			name:   "basic install",
    58  			cmd:    "install aeneas testdata/testcharts/empty --namespace default",
    59  			golden: "output/install.txt",
    60  		},
    61  
    62  		// Install, values from cli
    63  		{
    64  			name:   "install with values",
    65  			cmd:    "install virgil testdata/testcharts/alpine --set test.Name=bar",
    66  			golden: "output/install-with-values.txt",
    67  		},
    68  		// Install, values from cli via multiple --set
    69  		{
    70  			name:   "install with multiple values",
    71  			cmd:    "install virgil testdata/testcharts/alpine --set test.Color=yellow --set test.Name=banana",
    72  			golden: "output/install-with-multiple-values.txt",
    73  		},
    74  		// Install, values from yaml
    75  		{
    76  			name:   "install with values file",
    77  			cmd:    "install virgil testdata/testcharts/alpine -f testdata/testcharts/alpine/extra_values.yaml",
    78  			golden: "output/install-with-values-file.txt",
    79  		},
    80  		// Install, no hooks
    81  		{
    82  			name:   "install without hooks",
    83  			cmd:    "install aeneas testdata/testcharts/alpine --no-hooks --set test.Name=hello",
    84  			golden: "output/install-no-hooks.txt",
    85  		},
    86  		// Install, values from multiple yaml
    87  		{
    88  			name:   "install with values",
    89  			cmd:    "install virgil testdata/testcharts/alpine -f testdata/testcharts/alpine/extra_values.yaml -f testdata/testcharts/alpine/more_values.yaml",
    90  			golden: "output/install-with-multiple-values-files.txt",
    91  		},
    92  		// Install, no charts
    93  		{
    94  			name:      "install with no chart specified",
    95  			cmd:       "install",
    96  			golden:    "output/install-no-args.txt",
    97  			wantError: true,
    98  		},
    99  		// Install, re-use name
   100  		{
   101  			name:   "install and replace release",
   102  			cmd:    "install aeneas testdata/testcharts/empty --replace",
   103  			golden: "output/install-and-replace.txt",
   104  		},
   105  		// Install, with timeout
   106  		{
   107  			name:   "install with a timeout",
   108  			cmd:    "install foobar testdata/testcharts/empty --timeout 120s",
   109  			golden: "output/install-with-timeout.txt",
   110  		},
   111  		// Install, with wait
   112  		{
   113  			name:   "install with a wait",
   114  			cmd:    "install apollo testdata/testcharts/empty --wait",
   115  			golden: "output/install-with-wait.txt",
   116  		},
   117  		// Install, with wait-for-jobs
   118  		{
   119  			name:   "install with wait-for-jobs",
   120  			cmd:    "install apollo testdata/testcharts/empty --wait --wait-for-jobs",
   121  			golden: "output/install-with-wait-for-jobs.txt",
   122  		},
   123  		// Install, using the name-template
   124  		{
   125  			name:   "install with name-template",
   126  			cmd:    "install testdata/testcharts/empty --name-template '{{ \"foobar\"}}'",
   127  			golden: "output/install-name-template.txt",
   128  		},
   129  		// Install, perform chart verification along the way.
   130  		{
   131  			name:      "install with verification, missing provenance",
   132  			cmd:       "install bogus testdata/testcharts/compressedchart-0.1.0.tgz --verify --keyring testdata/helm-test-key.pub",
   133  			wantError: true,
   134  		},
   135  		{
   136  			name:      "install with verification, directory instead of file",
   137  			cmd:       "install bogus testdata/testcharts/signtest --verify --keyring testdata/helm-test-key.pub",
   138  			wantError: true,
   139  		},
   140  		{
   141  			name: "install with verification, valid",
   142  			cmd:  "install signtest testdata/testcharts/signtest-0.1.0.tgz --verify --keyring testdata/helm-test-key.pub",
   143  		},
   144  		// Install, chart with missing dependencies in /charts
   145  		{
   146  			name:      "install chart with missing dependencies",
   147  			cmd:       "install nodeps testdata/testcharts/chart-missing-deps",
   148  			wantError: true,
   149  		},
   150  		// Install chart with update-dependency
   151  		{
   152  			name:   "install chart with missing dependencies",
   153  			cmd:    "install --dependency-update updeps testdata/testcharts/chart-with-subchart-update",
   154  			golden: "output/chart-with-subchart-update.txt",
   155  		},
   156  		// Install, chart with bad dependencies in Chart.yaml in /charts
   157  		{
   158  			name:      "install chart with bad dependencies in Chart.yaml",
   159  			cmd:       "install badreq testdata/testcharts/chart-bad-requirements",
   160  			wantError: true,
   161  		},
   162  		// Install, chart with library chart dependency
   163  		{
   164  			name: "install chart with library chart dependency",
   165  			cmd:  "install withlibchartp testdata/testcharts/chart-with-lib-dep",
   166  		},
   167  		// Install, library chart
   168  		{
   169  			name:      "install library chart",
   170  			cmd:       "install libchart testdata/testcharts/lib-chart",
   171  			wantError: true,
   172  			golden:    "output/install-lib-chart.txt",
   173  		},
   174  		// Install, chart with bad type
   175  		{
   176  			name:      "install chart with bad type",
   177  			cmd:       "install badtype testdata/testcharts/chart-bad-type",
   178  			wantError: true,
   179  			golden:    "output/install-chart-bad-type.txt",
   180  		},
   181  		// Install, values from yaml, schematized
   182  		{
   183  			name:   "install with schema file",
   184  			cmd:    "install schema testdata/testcharts/chart-with-schema",
   185  			golden: "output/schema.txt",
   186  		},
   187  		// Install, values from yaml, schematized with errors
   188  		{
   189  			name:      "install with schema file, with errors",
   190  			cmd:       "install schema testdata/testcharts/chart-with-schema-negative",
   191  			wantError: true,
   192  			golden:    "output/schema-negative.txt",
   193  		},
   194  		// Install, values from yaml, extra values from yaml, schematized with errors
   195  		{
   196  			name:      "install with schema file, extra values from yaml, with errors",
   197  			cmd:       "install schema testdata/testcharts/chart-with-schema -f testdata/testcharts/chart-with-schema/extra-values.yaml",
   198  			wantError: true,
   199  			golden:    "output/schema-negative.txt",
   200  		},
   201  		// Install, values from yaml, extra values from cli, schematized with errors
   202  		{
   203  			name:      "install with schema file, extra values from cli, with errors",
   204  			cmd:       "install schema testdata/testcharts/chart-with-schema --set age=-5",
   205  			wantError: true,
   206  			golden:    "output/schema-negative-cli.txt",
   207  		},
   208  		// Install with subchart, values from yaml, schematized with errors
   209  		{
   210  			name:      "install with schema file and schematized subchart, with errors",
   211  			cmd:       "install schema testdata/testcharts/chart-with-schema-and-subchart",
   212  			wantError: true,
   213  			golden:    "output/subchart-schema-negative.txt",
   214  		},
   215  		// Install with subchart, values from yaml, extra values from cli, schematized with errors
   216  		{
   217  			name:   "install with schema file and schematized subchart, extra values from cli",
   218  			cmd:    "install schema testdata/testcharts/chart-with-schema-and-subchart --set lastname=doe --set subchart-with-schema.age=25",
   219  			golden: "output/subchart-schema-cli.txt",
   220  		},
   221  		// Install with subchart, values from yaml, extra values from cli, schematized with errors
   222  		{
   223  			name:      "install with schema file and schematized subchart, extra values from cli, with errors",
   224  			cmd:       "install schema testdata/testcharts/chart-with-schema-and-subchart --set lastname=doe --set subchart-with-schema.age=-25",
   225  			wantError: true,
   226  			golden:    "output/subchart-schema-cli-negative.txt",
   227  		},
   228  		// Install deprecated chart
   229  		{
   230  			name:   "install with warning about deprecated chart",
   231  			cmd:    "install aeneas testdata/testcharts/deprecated --namespace default",
   232  			golden: "output/deprecated-chart.txt",
   233  		},
   234  		// Install chart with only crds
   235  		{
   236  			name: "install chart with only crds",
   237  			cmd:  "install crd-test testdata/testcharts/chart-with-only-crds --namespace default",
   238  		},
   239  		// Verify the user/pass works
   240  		{
   241  			name:   "basic install with credentials",
   242  			cmd:    "install aeneas reqtest --namespace default --repo " + srv.URL() + " --username username --password password",
   243  			golden: "output/install.txt",
   244  		},
   245  		{
   246  			name:   "basic install with credentials",
   247  			cmd:    "install aeneas reqtest --namespace default --repo " + srv2.URL + " --username username --password password --pass-credentials",
   248  			golden: "output/install.txt",
   249  		},
   250  		{
   251  			name:   "basic install with credentials and no repo",
   252  			cmd:    fmt.Sprintf("install aeneas test/reqtest --username username --password password --repository-config %s --repository-cache %s", repoFile, srv.Root()),
   253  			golden: "output/install.txt",
   254  		},
   255  	}
   256  
   257  	runTestCmd(t, tests)
   258  }
   259  
   260  func TestInstallOutputCompletion(t *testing.T) {
   261  	outputFlagCompletionTest(t, "install")
   262  }
   263  
   264  func TestInstallVersionCompletion(t *testing.T) {
   265  	repoFile := "testdata/helmhome/helm/repositories.yaml"
   266  	repoCache := "testdata/helmhome/helm/repository"
   267  
   268  	repoSetup := fmt.Sprintf("--repository-config %s --repository-cache %s", repoFile, repoCache)
   269  
   270  	tests := []cmdTestCase{{
   271  		name:   "completion for install version flag with release name",
   272  		cmd:    fmt.Sprintf("%s __complete install releasename testing/alpine --version ''", repoSetup),
   273  		golden: "output/version-comp.txt",
   274  	}, {
   275  		name:   "completion for install version flag with generate-name",
   276  		cmd:    fmt.Sprintf("%s __complete install --generate-name testing/alpine --version ''", repoSetup),
   277  		golden: "output/version-comp.txt",
   278  	}, {
   279  		name:   "completion for install version flag, no filter",
   280  		cmd:    fmt.Sprintf("%s __complete install releasename testing/alpine --version 0.3", repoSetup),
   281  		golden: "output/version-comp.txt",
   282  	}, {
   283  		name:   "completion for install version flag too few args",
   284  		cmd:    fmt.Sprintf("%s __complete install testing/alpine --version ''", repoSetup),
   285  		golden: "output/version-invalid-comp.txt",
   286  	}, {
   287  		name:   "completion for install version flag too many args",
   288  		cmd:    fmt.Sprintf("%s __complete install releasename testing/alpine badarg --version ''", repoSetup),
   289  		golden: "output/version-invalid-comp.txt",
   290  	}, {
   291  		name:   "completion for install version flag invalid chart",
   292  		cmd:    fmt.Sprintf("%s __complete install releasename invalid/invalid --version ''", repoSetup),
   293  		golden: "output/version-invalid-comp.txt",
   294  	}}
   295  	runTestCmd(t, tests)
   296  }
   297  
   298  func TestInstallFileCompletion(t *testing.T) {
   299  	checkFileCompletion(t, "install", false)
   300  	checkFileCompletion(t, "install --generate-name", true)
   301  	checkFileCompletion(t, "install myname", true)
   302  	checkFileCompletion(t, "install myname mychart", false)
   303  }