Commit 96cdbbe6 authored by Matt Butcher's avatar Matt Butcher Committed by GitHub

Merge pull request #1275 from technosophos/fix/srv-in-tmp

fix(helm): fix tests so that they do not write data into testdata
parents 837d0a88 fc160256
...@@ -29,19 +29,21 @@ import ( ...@@ -29,19 +29,21 @@ import (
var testName = "test-name" var testName = "test-name"
func TestRepoAddCmd(t *testing.T) { func TestRepoAddCmd(t *testing.T) {
srv := repotest.NewServer("testdata/testserver") srv, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
defer srv.Stop()
thome, err := tempHelmHome(t)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
oldhome := homePath() oldhome := homePath()
helmHome = thome helmHome = thome
defer func() { defer func() {
srv.Stop()
helmHome = oldhome helmHome = oldhome
os.Remove(thome) os.Remove(thome)
}() }()
if err := ensureTestHome(helmpath.Home(thome), t); err != nil {
t.Fatal(err)
}
tests := []releaseCase{ tests := []releaseCase{
{ {
...@@ -61,15 +63,22 @@ func TestRepoAddCmd(t *testing.T) { ...@@ -61,15 +63,22 @@ func TestRepoAddCmd(t *testing.T) {
} }
func TestRepoAdd(t *testing.T) { func TestRepoAdd(t *testing.T) {
ts := repotest.NewServer("testdata/testserver") ts, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
defer ts.Stop()
thome, err := tempHelmHome(t)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer os.Remove(thome)
oldhome := homePath()
helmHome = thome
hh := helmpath.Home(thome) hh := helmpath.Home(thome)
defer func() {
ts.Stop()
helmHome = oldhome
os.Remove(thome)
}()
if err := ensureTestHome(hh, t); err != nil {
t.Fatal(err)
}
if err := addRepository(testName, ts.URL(), hh); err != nil { if err := addRepository(testName, ts.URL(), hh); err != nil {
t.Error(err) t.Error(err)
......
...@@ -62,19 +62,21 @@ func TestUpdateCmd(t *testing.T) { ...@@ -62,19 +62,21 @@ func TestUpdateCmd(t *testing.T) {
} }
func TestUpdateCharts(t *testing.T) { func TestUpdateCharts(t *testing.T) {
srv := repotest.NewServer("testdata/testserver") srv, thome, err := repotest.NewTempServer("testdata/testserver/*.*")
defer srv.Stop()
thome, err := tempHelmHome(t)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
oldhome := homePath() oldhome := homePath()
helmHome = thome helmHome = thome
defer func() { defer func() {
srv.Stop()
helmHome = oldhome helmHome = oldhome
os.Remove(thome) os.Remove(thome)
}() }()
if err := ensureTestHome(helmpath.Home(thome), t); err != nil {
t.Fatal(err)
}
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
repos := []*repo.Entry{ repos := []*repo.Entry{
......
...@@ -27,6 +27,30 @@ import ( ...@@ -27,6 +27,30 @@ import (
"k8s.io/helm/pkg/repo" "k8s.io/helm/pkg/repo"
) )
// NewTempServer creates a server inside of a temp dir.
//
// If the passed in string is not "", it will be treated as a shell glob, and files
// will be copied from that path to the server's docroot.
//
// The caller is responsible for destroying the temp directory as well as stopping
// the server.
func NewTempServer(glob string) (*Server, string, error) {
tdir, err := ioutil.TempDir("", "helm-repotest-")
if err != nil {
return nil, tdir, err
}
srv := NewServer(tdir)
if glob != "" {
if _, err := srv.CopyCharts(glob); err != nil {
srv.Stop()
return srv, tdir, err
}
}
return srv, tdir, nil
}
// NewServer creates a repository server for testing. // NewServer creates a repository server for testing.
// //
// docroot should be a temp dir managed by the caller. // docroot should be a temp dir managed by the caller.
......
...@@ -102,3 +102,26 @@ func TestServer(t *testing.T) { ...@@ -102,3 +102,26 @@ func TestServer(t *testing.T) {
t.Errorf("Expected 404, got %d", res.StatusCode) t.Errorf("Expected 404, got %d", res.StatusCode)
} }
} }
func TestNewTempServer(t *testing.T) {
srv, tdir, err := NewTempServer("testdata/examplechart-0.1.0.tgz")
if err != nil {
t.Fatal(err)
}
defer func() {
srv.Stop()
os.RemoveAll(tdir)
}()
if _, err := os.Stat(tdir); err != nil {
t.Fatal(err)
}
res, err := http.Head(srv.URL() + "/examplechart-0.1.0.tgz")
if err != nil {
t.Error(err)
}
if res.StatusCode != 200 {
t.Errorf("Expected 200, got %d", res.StatusCode)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment