github.com/skatsuta/docker@v1.8.1/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 diName := filepath.Join(tmpDir, ".dockerignore") 67 68 di, err := ReadDockerIgnore(diName) 69 if err != nil { 70 t.Fatalf("Expected not to have error, got %s", err) 71 } 72 73 if diLen := len(di); diLen != 0 { 74 t.Fatalf("Expected to have zero dockerignore entry, got %d", diLen) 75 } 76 77 content := fmt.Sprintf("test1\n/test2\n/a/file/here\n\nlastfile") 78 err = ioutil.WriteFile(diName, []byte(content), 0777) 79 if err != nil { 80 t.Fatal(err) 81 } 82 83 di, err = ReadDockerIgnore(diName) 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 if di[0] != "test1" { 89 t.Fatalf("First element is not test1") 90 } 91 if di[1] != "/test2" { 92 t.Fatalf("Second element is not /test2") 93 } 94 if di[2] != "/a/file/here" { 95 t.Fatalf("Third element is not /a/file/here") 96 } 97 if di[3] != "lastfile" { 98 t.Fatalf("Fourth element is not lastfile") 99 } 100 }