github.com/damirazo/docker@v1.9.0/utils/utils_test.go (about) 1 package utils 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 ) 10 11 func TestReplaceAndAppendEnvVars(t *testing.T) { 12 var ( 13 d = []string{"HOME=/"} 14 o = []string{"HOME=/root", "TERM=xterm"} 15 ) 16 17 env := ReplaceOrAppendEnvValues(d, o) 18 if len(env) != 2 { 19 t.Fatalf("expected len of 2 got %d", len(env)) 20 } 21 if env[0] != "HOME=/root" { 22 t.Fatalf("expected HOME=/root got '%s'", env[0]) 23 } 24 if env[1] != "TERM=xterm" { 25 t.Fatalf("expected TERM=xterm got '%s'", env[1]) 26 } 27 } 28 29 func TestImageReference(t *testing.T) { 30 tests := []struct { 31 repo string 32 ref string 33 expected string 34 }{ 35 {"repo", "tag", "repo:tag"}, 36 {"repo", "sha256:c100b11b25d0cacd52c14e0e7bf525e1a4c0e6aec8827ae007055545909d1a64", "repo@sha256:c100b11b25d0cacd52c14e0e7bf525e1a4c0e6aec8827ae007055545909d1a64"}, 37 } 38 39 for i, test := range tests { 40 actual := ImageReference(test.repo, test.ref) 41 if test.expected != actual { 42 t.Errorf("%d: expected %q, got %q", i, test.expected, actual) 43 } 44 } 45 } 46 47 func TestDigestReference(t *testing.T) { 48 input := "sha256:c100b11b25d0cacd52c14e0e7bf525e1a4c0e6aec8827ae007055545909d1a64" 49 if !DigestReference(input) { 50 t.Errorf("Expected DigestReference=true for input %q", input) 51 } 52 53 input = "latest" 54 if DigestReference(input) { 55 t.Errorf("Unexpected DigestReference=true for input %q", input) 56 } 57 } 58 59 func TestReadDockerIgnore(t *testing.T) { 60 tmpDir, err := ioutil.TempDir("", "dockerignore-test") 61 if err != nil { 62 t.Fatal(err) 63 } 64 defer os.RemoveAll(tmpDir) 65 66 di, err := ReadDockerIgnore(nil) 67 if err != nil { 68 t.Fatalf("Expected not to have error, got %v", err) 69 } 70 71 if diLen := len(di); diLen != 0 { 72 t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen) 73 } 74 75 diName := filepath.Join(tmpDir, ".dockerignore") 76 content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile") 77 err = ioutil.WriteFile(diName, []byte(content), 0777) 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 diFd, err := os.Open(diName) 83 if err != nil { 84 t.Fatal(err) 85 } 86 di, err = ReadDockerIgnore(diFd) 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 if di[0] != "test1" { 92 t.Fatalf("First element is not test1") 93 } 94 if di[1] != "/test2" { 95 t.Fatalf("Second element is not /test2") 96 } 97 if di[2] != "/a/file/here" { 98 t.Fatalf("Third element is not /a/file/here") 99 } 100 if di[3] != "lastfile" { 101 t.Fatalf("Fourth element is not lastfile") 102 } 103 }