2015-11-03 21:15:40 +00:00
|
|
|
package getter
|
|
|
|
|
|
|
|
import (
|
2015-11-03 21:16:17 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-11-03 21:15:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetArtifact_basic(t *testing.T) {
|
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
logger := log.New(os.Stderr, "", log.LstdFlags)
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
// 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",
|
|
|
|
"",
|
|
|
|
},
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
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)
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
path, err := GetArtifact(destDir, p.Source, p.Checksum, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("TestGetArtifact_basic unexpected failure here: %s", err)
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
if p.Checksum != "" {
|
|
|
|
if ok := strings.Contains(path, p.Checksum); ok {
|
|
|
|
t.Fatalf("path result should not contain the checksum, got: %s", path)
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
// verify artifact exists
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
t.Fatalf("source path error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetArtifact_fails(t *testing.T) {
|
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
logger := log.New(os.Stderr, "", log.LstdFlags)
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
failing := []struct {
|
|
|
|
Source, Checksum string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
"sha256:66aa0f05fc0cfcf1e5ed8cc5307b5d",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/u/47675/jar_thing/hi_darwin_amd64",
|
|
|
|
"sha256:66aa0f05fc0cfcf1e5ed8cc5307b5d",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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)
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
|
2015-11-03 21:16:17 +00:00
|
|
|
_, err = GetArtifact(destDir, p.Source, p.Checksum, logger)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("TestGetArtifact_basic expected failure, but got none")
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 21:15:40 +00:00
|
|
|
}
|