From 057e4efe4208af94ee55e9baeb0d4a8817f32c5f Mon Sep 17 00:00:00 2001 From: Jim Date: Fri, 28 Oct 2016 21:12:31 -0400 Subject: [PATCH] fix(helm): fix tests if tmp directory is on another block device Two non-exported helper functions were added to the repo index test file. They first try to link the file, since this is optimal. If the link fails a copy occurs. Fixes #1472 --- cmd/helm/repo_index_test.go | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/cmd/helm/repo_index_test.go b/cmd/helm/repo_index_test.go index 4f2a69eec..bd1010dd7 100644 --- a/cmd/helm/repo_index_test.go +++ b/cmd/helm/repo_index_test.go @@ -18,6 +18,7 @@ package main import ( "bytes" + "io" "io/ioutil" "os" "path/filepath" @@ -35,11 +36,11 @@ func TestRepoIndexCmd(t *testing.T) { defer os.RemoveAll(dir) comp := filepath.Join(dir, "compressedchart-0.1.0.tgz") - if err := os.Link("testdata/testcharts/compressedchart-0.1.0.tgz", comp); err != nil { + if err := linkOrCopy("testdata/testcharts/compressedchart-0.1.0.tgz", comp); err != nil { t.Fatal(err) } comp2 := filepath.Join(dir, "compressedchart-0.2.0.tgz") - if err := os.Link("testdata/testcharts/compressedchart-0.2.0.tgz", comp2); err != nil { + if err := linkOrCopy("testdata/testcharts/compressedchart-0.2.0.tgz", comp2); err != nil { t.Fatal(err) } @@ -81,10 +82,10 @@ func TestRepoIndexCmd(t *testing.T) { t.Fatal(err) } // Add a new chart and a new version of an existing chart - if err := os.Link("testdata/testcharts/reqtest-0.1.0.tgz", filepath.Join(dir, "reqtest-0.1.0.tgz")); err != nil { + if err := linkOrCopy("testdata/testcharts/reqtest-0.1.0.tgz", filepath.Join(dir, "reqtest-0.1.0.tgz")); err != nil { t.Fatal(err) } - if err := os.Link("testdata/testcharts/compressedchart-0.3.0.tgz", filepath.Join(dir, "compressedchart-0.3.0.tgz")); err != nil { + if err := linkOrCopy("testdata/testcharts/compressedchart-0.3.0.tgz", filepath.Join(dir, "compressedchart-0.3.0.tgz")); err != nil { t.Fatal(err) } @@ -112,3 +113,29 @@ func TestRepoIndexCmd(t *testing.T) { t.Errorf("expected %q, got %q", expectedVersion, vs[0].Version) } } + +func linkOrCopy(old, new string) error { + if err := os.Link(old, new); err != nil { + return copyFile(old, new) + } + + return nil +} + +func copyFile(dst, src string) error { + i, err := os.Open(dst) + if err != nil { + return err + } + defer i.Close() + + o, err := os.Create(src) + if err != nil { + return err + } + defer o.Close() + + _, err = io.Copy(o, i) + + return err +}