func TestDownloadTo(t *testing.T) {
	hh, err := ioutil.TempDir("", "helm-downloadto-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(hh)

	dest := filepath.Join(hh, "dest")
	os.MkdirAll(dest, 0755)

	// Set up a fake repo
	srv := repotest.NewServer(hh)
	defer srv.Stop()
	if _, err := srv.CopyCharts("testdata/*.tgz*"); err != nil {
		t.Error(err)
		return
	}

	c := ChartDownloader{
		HelmHome: helmpath.Home("testdata/helmhome"),
		Out:      os.Stderr,
		Verify:   VerifyAlways,
		Keyring:  "testdata/helm-test-key.pub",
	}
	cname := "/signtest-0.1.0.tgz"
	where, v, err := c.DownloadTo(srv.URL()+cname, "", dest)
	if err != nil {
		t.Error(err)
		return
	}

	if expect := filepath.Join(dest, cname); where != expect {
		t.Errorf("Expected download to %s, got %s", expect, where)
	}

	if v.FileHash == "" {
		t.Error("File hash was empty, but verification is required.")
	}

	if _, err := os.Stat(filepath.Join(dest, cname)); err != nil {
		t.Error(err)
		return
	}
}
Пример #2
0
func TestDownloadTo_VerifyLater(t *testing.T) {
	hh, err := ioutil.TempDir("", "helm-downloadto-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(hh)

	dest := filepath.Join(hh, "dest")
	os.MkdirAll(dest, 0755)

	// Set up a fake repo
	srv := repotest.NewServer(hh)
	defer srv.Stop()
	if _, err := srv.CopyCharts("testdata/*.tgz*"); err != nil {
		t.Error(err)
		return
	}

	c := ChartDownloader{
		HelmHome: helmpath.Home("testdata/helmhome"),
		Out:      os.Stderr,
		Verify:   VerifyLater,
	}
	cname := "/signtest-0.1.0.tgz"
	where, _, err := c.DownloadTo(srv.URL()+cname, "", dest)
	if err != nil {
		t.Error(err)
		return
	}

	if expect := filepath.Join(dest, cname); where != expect {
		t.Errorf("Expected download to %s, got %s", expect, where)
	}

	if _, err := os.Stat(filepath.Join(dest, cname)); err != nil {
		t.Error(err)
		return
	}
	if _, err := os.Stat(filepath.Join(dest, cname+".prov")); err != nil {
		t.Error(err)
		return
	}
}
Пример #3
0
func TestDependencyUpdateCmd(t *testing.T) {
	// Set up a testing helm home
	oldhome := helmHome
	hh, err := tempHelmHome()
	if err != nil {
		t.Fatal(err)
	}
	helmHome = hh
	defer func() {
		os.RemoveAll(hh)
		helmHome = oldhome
	}()

	srv := repotest.NewServer(hh)
	defer srv.Stop()
	copied, err := srv.CopyCharts("testdata/testcharts/*.tgz")
	t.Logf("Copied charts:\n%s", strings.Join(copied, "\n"))
	t.Logf("Listening on directory %s", srv.Root())

	chartname := "depup"
	if err := createTestingChart(hh, chartname, srv.URL()); err != nil {
		t.Fatal(err)
	}

	out := bytes.NewBuffer(nil)
	duc := &dependencyUpdateCmd{out: out}
	duc.helmhome = helmpath.Home(hh)
	duc.chartpath = filepath.Join(hh, chartname)

	if err := duc.run(); err != nil {
		output := out.String()
		t.Logf("Output: %s", output)
		t.Fatal(err)
	}

	output := out.String()
	// This is written directly to stdout, so we have to capture as is.
	if !strings.Contains(output, `update from the "test" chart repository`) {
		t.Errorf("Repo did not get updated\n%s", output)
	}

	// Make sure the actual file got downloaded.
	expect := filepath.Join(hh, chartname, "charts/reqtest-0.1.0.tgz")
	if _, err := os.Stat(expect); err != nil {
		t.Fatal(err)
	}

	hash, err := provenance.DigestFile(expect)
	if err != nil {
		t.Fatal(err)
	}

	i, err := repo.LoadIndexFile(cacheIndexFile("test"))
	if err != nil {
		t.Fatal(err)
	}

	if h := i.Entries["reqtest-0.1.0"].Digest; h != hash {
		t.Errorf("Failed hash match: expected %s, got %s", hash, h)
	}

	t.Logf("Results: %s", out.String())
}
func TestDependencyBuildCmd(t *testing.T) {
	oldhome := helmHome
	hh, err := tempHelmHome(t)
	if err != nil {
		t.Fatal(err)
	}
	helmHome = hh
	defer func() {
		os.RemoveAll(hh)
		helmHome = oldhome
	}()

	srv := repotest.NewServer(hh)
	defer srv.Stop()
	_, err = srv.CopyCharts("testdata/testcharts/*.tgz")
	if err != nil {
		t.Fatal(err)
	}

	chartname := "depbuild"
	if err := createTestingChart(hh, chartname, srv.URL()); err != nil {
		t.Fatal(err)
	}

	out := bytes.NewBuffer(nil)
	dbc := &dependencyBuildCmd{out: out}
	dbc.helmhome = helmpath.Home(hh)
	dbc.chartpath = filepath.Join(hh, chartname)

	// In the first pass, we basically want the same results as an update.
	if err := dbc.run(); err != nil {
		output := out.String()
		t.Logf("Output: %s", output)
		t.Fatal(err)
	}

	output := out.String()
	if !strings.Contains(output, `update from the "test" chart repository`) {
		t.Errorf("Repo did not get updated\n%s", output)
	}

	// Make sure the actual file got downloaded.
	expect := filepath.Join(hh, chartname, "charts/reqtest-0.1.0.tgz")
	if _, err := os.Stat(expect); err != nil {
		t.Fatal(err)
	}

	// In the second pass, we want to remove the chart's request dependency,
	// then see if it restores from the lock.
	lockfile := filepath.Join(hh, chartname, "requirements.lock")
	if _, err := os.Stat(lockfile); err != nil {
		t.Fatal(err)
	}
	if err := os.RemoveAll(expect); err != nil {
		t.Fatal(err)
	}

	if err := dbc.run(); err != nil {
		output := out.String()
		t.Logf("Output: %s", output)
		t.Fatal(err)
	}

	// Now repeat the test that the dependency exists.
	expect = filepath.Join(hh, chartname, "charts/reqtest-0.1.0.tgz")
	if _, err := os.Stat(expect); err != nil {
		t.Fatal(err)
	}

	// Make sure that build is also fetching the correct version.
	hash, err := provenance.DigestFile(expect)
	if err != nil {
		t.Fatal(err)
	}

	i, err := repo.LoadIndexFile(dbc.helmhome.CacheIndex("test"))
	if err != nil {
		t.Fatal(err)
	}

	reqver := i.Entries["reqtest"][0]
	if h := reqver.Digest; h != hash {
		t.Errorf("Failed hash match: expected %s, got %s", hash, h)
	}
	if v := reqver.Version; v != "0.1.0" {
		t.Errorf("mismatched versions. Expected %q, got %q", "0.1.0", v)
	}

}
Пример #5
0
func TestFetchCmd(t *testing.T) {
	hh, err := tempHelmHome(t)
	if err != nil {
		t.Fatal(err)
	}
	old := homePath()
	helmHome = hh
	defer func() {
		helmHome = old
		os.RemoveAll(hh)
	}()

	// all flags will get "--home=TMDIR -d outdir" appended.
	tests := []struct {
		name         string
		chart        string
		flags        []string
		fail         bool
		failExpect   string
		expectFile   string
		expectDir    bool
		expectVerify bool
	}{
		{
			name:       "Basic chart fetch",
			chart:      "test/signtest",
			expectFile: "./signtest-0.1.0.tgz",
		},
		{
			name:       "Chart fetch with version",
			chart:      "test/signtest",
			flags:      []string{"--version", "0.1.0"},
			expectFile: "./signtest-0.1.0.tgz",
		},
		{
			name:       "Fail chart fetch with non-existent version",
			chart:      "test/signtest",
			flags:      []string{"--version", "99.1.0"},
			fail:       true,
			failExpect: "no such chart",
		},
		{
			name:       "Fail fetching non-existent chart",
			chart:      "test/nosuchthing",
			failExpect: "Failed to fetch",
			fail:       true,
		},
		{
			name:         "Fetch and verify",
			chart:        "test/signtest",
			flags:        []string{"--verify", "--keyring", "testdata/helm-test-key.pub"},
			expectFile:   "./signtest-0.1.0.tgz",
			expectVerify: true,
		},
		{
			name:       "Fetch and fail verify",
			chart:      "test/reqtest",
			flags:      []string{"--verify", "--keyring", "testdata/helm-test-key.pub"},
			failExpect: "Failed to fetch provenance",
			fail:       true,
		},
		{
			name:       "Fetch and untar",
			chart:      "test/signtest",
			flags:      []string{"--untar", "--untardir", "signtest"},
			expectFile: "./signtest",
			expectDir:  true,
		},
		{
			name:         "Fetch, verify, untar",
			chart:        "test/signtest",
			flags:        []string{"--verify", "--keyring", "testdata/helm-test-key.pub", "--untar", "--untardir", "signtest"},
			expectFile:   "./signtest",
			expectDir:    true,
			expectVerify: true,
		},
	}

	srv := repotest.NewServer(hh)
	defer srv.Stop()

	if _, err := srv.CopyCharts("testdata/testcharts/*.tgz*"); err != nil {
		t.Fatal(err)
	}
	if err := srv.LinkIndices(); err != nil {
		t.Fatal(err)
	}

	for _, tt := range tests {
		outdir := filepath.Join(hh, "testout")
		os.RemoveAll(outdir)
		os.Mkdir(outdir, 0755)

		buf := bytes.NewBuffer(nil)
		cmd := newFetchCmd(buf)
		tt.flags = append(tt.flags, "-d", outdir)
		cmd.ParseFlags(tt.flags)
		if err := cmd.RunE(cmd, []string{tt.chart}); err != nil {
			if tt.fail {
				continue
			}
			t.Errorf("%q reported error: %s", tt.name, err)
			continue
		}
		if tt.expectVerify {
			pointerAddressPattern := "0[xX][A-Fa-f0-9]+"
			sha256Pattern := "[A-Fa-f0-9]{64}"
			verificationRegex := regexp.MustCompile(
				fmt.Sprintf("Verification: &{%s sha256:%s signtest-0.1.0.tgz}\n", pointerAddressPattern, sha256Pattern))
			if !verificationRegex.MatchString(buf.String()) {
				t.Errorf("%q: expected match for regex %s, got %s", tt.name, verificationRegex, buf.String())
			}
		}

		ef := filepath.Join(outdir, tt.expectFile)
		fi, err := os.Stat(ef)
		if err != nil {
			t.Errorf("%q: expected a file at %s. %s", tt.name, ef, err)
		}
		if fi.IsDir() != tt.expectDir {
			t.Errorf("%q: expected directory=%t, but it's not.", tt.name, tt.expectDir)
		}
	}
}