github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/client/getter/getter_test.go (about)

     1  package getter
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestGetArtifact_basic(t *testing.T) {
    13  
    14  	logger := log.New(os.Stderr, "", log.LstdFlags)
    15  
    16  	// TODO: Use http.TestServer to serve these files from fixtures dir
    17  	passing := []struct {
    18  		Source, Checksum string
    19  	}{
    20  		{
    21  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_darwin_amd64",
    22  			"sha256:66aa0f05fc0cfcf1e5ed8cc5307b5df51e33871d6b295a60e0f9f6dd573da977",
    23  		},
    24  		{
    25  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64",
    26  			"sha256:6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c",
    27  		},
    28  		{
    29  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64",
    30  			"md5:a9b14903a8942748e4f8474e11f795d3",
    31  		},
    32  		{
    33  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64?checksum=sha256:6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c",
    34  			"",
    35  		},
    36  		{
    37  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64",
    38  			"",
    39  		},
    40  	}
    41  
    42  	for i, p := range passing {
    43  		destDir, err := ioutil.TempDir("", fmt.Sprintf("nomad-test-%d", i))
    44  		if err != nil {
    45  			t.Fatalf("Error in TestGetArtifact_basic makeing TempDir: %s", err)
    46  		}
    47  
    48  		path, err := GetArtifact(destDir, p.Source, p.Checksum, logger)
    49  		if err != nil {
    50  			t.Fatalf("TestGetArtifact_basic unexpected failure here: %s", err)
    51  		}
    52  
    53  		if p.Checksum != "" {
    54  			if ok := strings.Contains(path, p.Checksum); ok {
    55  				t.Fatalf("path result should not contain the checksum, got: %s", path)
    56  			}
    57  		}
    58  
    59  		// verify artifact exists
    60  		if _, err := os.Stat(path); err != nil {
    61  			t.Fatalf("source path error: %s", err)
    62  		}
    63  	}
    64  }
    65  
    66  func TestGetArtifact_fails(t *testing.T) {
    67  
    68  	logger := log.New(os.Stderr, "", log.LstdFlags)
    69  
    70  	failing := []struct {
    71  		Source, Checksum string
    72  	}{
    73  		{
    74  			"",
    75  			"sha256:66aa0f05fc0cfcf1e5ed8cc5307b5d",
    76  		},
    77  		{
    78  			"/u/47675/jar_thing/hi_darwin_amd64",
    79  			"sha256:66aa0f05fc0cfcf1e5ed8cc5307b5d",
    80  		},
    81  		{
    82  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_darwin_amd64",
    83  			"sha256:66aa0f05fc0cfcf1e5ed8cc5307b5d",
    84  		},
    85  		{
    86  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64",
    87  			"sha257:6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c",
    88  		},
    89  		// malformed checksum
    90  		{
    91  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd64",
    92  			"6f99b4c5184726e601ecb062500aeb9537862434dfe1898dbe5c68d9f50c179c",
    93  		},
    94  		// 404
    95  		{
    96  			"https://dl.dropboxusercontent.com/u/47675/jar_thing/hi_linux_amd86",
    97  			"",
    98  		},
    99  	}
   100  	for i, p := range failing {
   101  		destDir, err := ioutil.TempDir("", fmt.Sprintf("nomad-test-%d", i))
   102  		if err != nil {
   103  			t.Fatalf("Error in TestGetArtifact_basic makeing TempDir: %s", err)
   104  		}
   105  
   106  		_, err = GetArtifact(destDir, p.Source, p.Checksum, logger)
   107  		if err == nil {
   108  			t.Fatalf("TestGetArtifact_basic expected failure, but got none")
   109  		}
   110  	}
   111  }