From 68db2aa81435596204567c85d340fbd2410d45f4 Mon Sep 17 00:00:00 2001 From: Michael Schurter Date: Fri, 26 May 2017 16:52:47 -0700 Subject: [PATCH] Fix getter tests And use an interface for ReplaceEnv since its all getter needs. --- client/getter/getter.go | 11 ++++++++--- client/getter/getter_test.go | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/client/getter/getter.go b/client/getter/getter.go index 6b0394e98..c55dafd27 100644 --- a/client/getter/getter.go +++ b/client/getter/getter.go @@ -8,7 +8,6 @@ import ( "sync" gg "github.com/hashicorp/go-getter" - "github.com/hashicorp/nomad/client/driver/env" "github.com/hashicorp/nomad/nomad/structs" ) @@ -27,6 +26,12 @@ const ( gitSSHPrefix = "git@github.com:" ) +// EnvReplacer is an interface which can interpolate environment variables and +// is usually satisfied by env.TaskEnv. +type EnvReplacer interface { + ReplaceEnv(string) string +} + // getClient returns a client that is suitable for Nomad downloading artifacts. func getClient(src, dst string) *gg.Client { lock.Lock() @@ -51,7 +56,7 @@ func getClient(src, dst string) *gg.Client { } // getGetterUrl returns the go-getter URL to download the artifact. -func getGetterUrl(taskEnv *env.TaskEnv, artifact *structs.TaskArtifact) (string, error) { +func getGetterUrl(taskEnv EnvReplacer, artifact *structs.TaskArtifact) (string, error) { source := taskEnv.ReplaceEnv(artifact.GetterSource) // Handle an invalid URL when given a go-getter url such as @@ -84,7 +89,7 @@ func getGetterUrl(taskEnv *env.TaskEnv, artifact *structs.TaskArtifact) (string, } // GetArtifact downloads an artifact into the specified task directory. -func GetArtifact(taskEnv *env.TaskEnv, artifact *structs.TaskArtifact, taskDir string) error { +func GetArtifact(taskEnv EnvReplacer, artifact *structs.TaskArtifact, taskDir string) error { url, err := getGetterUrl(taskEnv, artifact) if err != nil { return newGetError(artifact.GetterSource, err, false) diff --git a/client/getter/getter_test.go b/client/getter/getter_test.go index becbd946c..7333f6efb 100644 --- a/client/getter/getter_test.go +++ b/client/getter/getter_test.go @@ -16,6 +16,15 @@ import ( "github.com/hashicorp/nomad/nomad/structs" ) +// fakeReplacer is a noop version of env.TaskEnv.ReplanceEnv +type fakeReplacer struct{} + +func (fakeReplacer) ReplaceEnv(s string) string { + return s +} + +var taskEnv = fakeReplacer{} + func TestGetArtifact_FileAndChecksum(t *testing.T) { // Create the test server hosting the file to download ts := httptest.NewServer(http.FileServer(http.Dir(filepath.Dir("./test-fixtures/")))) @@ -38,7 +47,6 @@ func TestGetArtifact_FileAndChecksum(t *testing.T) { } // Download the artifact - taskEnv := env.NewTaskEnvironment(mock.Node()) if err := GetArtifact(taskEnv, artifact, taskDir); err != nil { t.Fatalf("GetArtifact failed: %v", err) } @@ -73,7 +81,6 @@ func TestGetArtifact_File_RelativeDest(t *testing.T) { } // Download the artifact - taskEnv := env.NewTaskEnvironment(mock.Node()) if err := GetArtifact(taskEnv, artifact, taskDir); err != nil { t.Fatalf("GetArtifact failed: %v", err) } @@ -91,7 +98,11 @@ func TestGetGetterUrl_Interprolation(t *testing.T) { } url := "foo.com" - taskEnv := env.NewTaskEnvironment(mock.Node()).SetTaskMeta(map[string]string{"artifact": url}) + alloc := mock.Alloc() + task := alloc.Job.TaskGroups[0].Tasks[0] + task.Meta = map[string]string{"artifact": url} + taskEnv := env.NewBuilder(mock.Node(), alloc, task, "global").Build() + act, err := getGetterUrl(taskEnv, artifact) if err != nil { t.Fatalf("getGetterUrl() failed: %v", err) @@ -124,7 +135,6 @@ func TestGetArtifact_InvalidChecksum(t *testing.T) { } // Download the artifact and expect an error - taskEnv := env.NewTaskEnvironment(mock.Node()) if err := GetArtifact(taskEnv, artifact, taskDir); err == nil { t.Fatalf("GetArtifact should have failed") } @@ -190,7 +200,6 @@ func TestGetArtifact_Archive(t *testing.T) { }, } - taskEnv := env.NewTaskEnvironment(mock.Node()) if err := GetArtifact(taskEnv, artifact, taskDir); err != nil { t.Fatalf("GetArtifact failed: %v", err) } @@ -206,7 +215,6 @@ func TestGetArtifact_Archive(t *testing.T) { } func TestGetGetterUrl_Queries(t *testing.T) { - taskEnv := env.NewTaskEnvironment(mock.Node()) cases := []struct { name string artifact *structs.TaskArtifact