mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 01:15:43 +03:00
Fix getter tests
And use an interface for ReplaceEnv since its all getter needs.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user