github.com/wulonghui/docker@v1.8.0-rc2/pkg/archive/wrap_test.go (about) 1 package archive 2 3 import ( 4 "archive/tar" 5 "bytes" 6 "io" 7 "testing" 8 ) 9 10 func TestGenerateEmptyFile(t *testing.T) { 11 archive, err := Generate("emptyFile") 12 if err != nil { 13 t.Fatal(err) 14 } 15 if archive == nil { 16 t.Fatal("The generated archive should not be nil.") 17 } 18 19 expectedFiles := [][]string{ 20 {"emptyFile", ""}, 21 } 22 23 tr := tar.NewReader(archive) 24 actualFiles := make([][]string, 0, 10) 25 i := 0 26 for { 27 hdr, err := tr.Next() 28 if err == io.EOF { 29 break 30 } 31 if err != nil { 32 t.Fatal(err) 33 } 34 buf := new(bytes.Buffer) 35 buf.ReadFrom(tr) 36 content := buf.String() 37 actualFiles = append(actualFiles, []string{hdr.Name, content}) 38 i++ 39 } 40 if len(actualFiles) != len(expectedFiles) { 41 t.Fatalf("Number of expected file %d, got %d.", len(expectedFiles), len(actualFiles)) 42 } 43 for i := 0; i < len(expectedFiles); i++ { 44 actual := actualFiles[i] 45 expected := expectedFiles[i] 46 if actual[0] != expected[0] { 47 t.Fatalf("Expected name '%s', Actual name '%s'", expected[0], actual[0]) 48 } 49 if actual[1] != expected[1] { 50 t.Fatalf("Expected content '%s', Actual content '%s'", expected[1], actual[1]) 51 } 52 } 53 } 54 55 func TestGenerateWithContent(t *testing.T) { 56 archive, err := Generate("file", "content") 57 if err != nil { 58 t.Fatal(err) 59 } 60 if archive == nil { 61 t.Fatal("The generated archive should not be nil.") 62 } 63 64 expectedFiles := [][]string{ 65 {"file", "content"}, 66 } 67 68 tr := tar.NewReader(archive) 69 actualFiles := make([][]string, 0, 10) 70 i := 0 71 for { 72 hdr, err := tr.Next() 73 if err == io.EOF { 74 break 75 } 76 if err != nil { 77 t.Fatal(err) 78 } 79 buf := new(bytes.Buffer) 80 buf.ReadFrom(tr) 81 content := buf.String() 82 actualFiles = append(actualFiles, []string{hdr.Name, content}) 83 i++ 84 } 85 if len(actualFiles) != len(expectedFiles) { 86 t.Fatalf("Number of expected file %d, got %d.", len(expectedFiles), len(actualFiles)) 87 } 88 for i := 0; i < len(expectedFiles); i++ { 89 actual := actualFiles[i] 90 expected := expectedFiles[i] 91 if actual[0] != expected[0] { 92 t.Fatalf("Expected name '%s', Actual name '%s'", expected[0], actual[0]) 93 } 94 if actual[1] != expected[1] { 95 t.Fatalf("Expected content '%s', Actual content '%s'", expected[1], actual[1]) 96 } 97 } 98 }