-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getter package, a thin wrapper of go-getter
- Loading branch information
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/url" | ||
"path" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
"syscall" | ||
|
||
gg "github.com/hashicorp/go-getter" | ||
) | ||
|
||
func GetArtifact(destDir, source, checksum string, logger *log.Logger) (string, error) { | ||
// We use go-getter to support a variety of protocols, but need to change | ||
// file permissions of the resulted download to be executable | ||
|
||
u, err := url.Parse(source) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// look for checksum, apply to URL | ||
if checksum != "" { | ||
source = strings.Join([]string{source, fmt.Sprintf("checksum=%s", checksum)}, "?") | ||
logger.Printf("[DEBUG] Applying checksum to Artifact Source URL, new url: %s", source) | ||
} | ||
|
||
artifactName := path.Base(u.Path) | ||
artifactFile := filepath.Join(destDir, artifactName) | ||
if err := gg.GetFile(artifactFile, source); err != nil { | ||
return "", fmt.Errorf("Error downloading artifact: %s", err) | ||
} | ||
|
||
// Add execution permissions to the newly downloaded artifact | ||
if runtime.GOOS != "windows" { | ||
if err := syscall.Chmod(artifactFile, 0755); err != nil { | ||
logger.Printf("[ERR] driver.raw_exec: Error making artifact executable: %s", err) | ||
} | ||
} | ||
return artifactFile, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package getter | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetArtifact_basic(t *testing.T) { | ||
|
||
logger := log.New(os.Stderr, "", log.LstdFlags) | ||
|
||
// TODO: Use http.TestServer to serve these files from fixtures dir | ||
passing := []struct { | ||
Source, Checksum string | ||
}{ | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_darwin_amd64", | ||
"sha256:66aa0f05fc0cfcf1e5ed8cc5307b5df51e33871d6b295a60e0f9f6dd573da977", | ||
}, | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64", | ||
"sha256:6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c", | ||
}, | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64", | ||
"md5:a9b14903a8942748e4f8474e11f795d3", | ||
}, | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64?checksum=sha256:6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c", | ||
"", | ||
}, | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64", | ||
"", | ||
}, | ||
} | ||
|
||
for i, p := range passing { | ||
destDir, err := ioutil.TempDir("", fmt.Sprintf("nomad-test-%d", i)) | ||
if err != nil { | ||
t.Fatalf("Error in TestGetArtifact_basic makeing TempDir: %s", err) | ||
} | ||
|
||
path, err := GetArtifact(destDir, p.Source, p.Checksum, logger) | ||
if err != nil { | ||
t.Fatalf("TestGetArtifact_basic unexpected failure here: %s", err) | ||
} | ||
|
||
if p.Checksum != "" { | ||
if ok := strings.Contains(path, p.Checksum); ok { | ||
t.Fatalf("path result should not contain the checksum, got: %s", path) | ||
} | ||
} | ||
|
||
// verify artifact exists | ||
if _, err := os.Stat(path); err != nil { | ||
t.Fatalf("source path error: %s", err) | ||
} | ||
} | ||
} | ||
|
||
func TestGetArtifact_fails(t *testing.T) { | ||
|
||
logger := log.New(os.Stderr, "", log.LstdFlags) | ||
|
||
failing := []struct { | ||
Source, Checksum string | ||
}{ | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_darwin_amd64", | ||
"sha256:66aa0f05fc0cfcf1e5ed8cc5307b5d", | ||
}, | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64", | ||
"sha257:6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c", | ||
}, | ||
// malformed checksum | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64", | ||
"6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c", | ||
}, | ||
// 404 | ||
{ | ||
"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd86", | ||
"", | ||
}, | ||
} | ||
for i, p := range failing { | ||
destDir, err := ioutil.TempDir("", fmt.Sprintf("nomad-test-%d", i)) | ||
if err != nil { | ||
t.Fatalf("Error in TestGetArtifact_basic makeing TempDir: %s", err) | ||
} | ||
|
||
_, err = GetArtifact(destDir, p.Source, p.Checksum, logger) | ||
if err == nil { | ||
t.Fatalf("TestGetArtifact_basic expected failure, but got none") | ||
} | ||
} | ||
} |