2018-02-05 16:05:59 -05:00
|
|
|
package archive // import "github.com/docker/docker/pkg/archive"
|
2015-04-24 11:03:33 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
2017-08-08 22:27:01 -04:00
|
|
|
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
2015-04-24 11:03:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGenerateEmptyFile(t *testing.T) {
|
|
|
|
archive, err := Generate("emptyFile")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
if archive == nil {
|
|
|
|
t.Fatal("The generated archive should not be nil.")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFiles := [][]string{
|
|
|
|
{"emptyFile", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(archive)
|
|
|
|
actualFiles := make([][]string, 0, 10)
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(tr)
|
|
|
|
content := buf.String()
|
|
|
|
actualFiles = append(actualFiles, []string{hdr.Name, content})
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if len(actualFiles) != len(expectedFiles) {
|
|
|
|
t.Fatalf("Number of expected file %d, got %d.", len(expectedFiles), len(actualFiles))
|
|
|
|
}
|
|
|
|
for i := 0; i < len(expectedFiles); i++ {
|
|
|
|
actual := actualFiles[i]
|
|
|
|
expected := expectedFiles[i]
|
|
|
|
if actual[0] != expected[0] {
|
|
|
|
t.Fatalf("Expected name '%s', Actual name '%s'", expected[0], actual[0])
|
|
|
|
}
|
|
|
|
if actual[1] != expected[1] {
|
|
|
|
t.Fatalf("Expected content '%s', Actual content '%s'", expected[1], actual[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGenerateWithContent(t *testing.T) {
|
|
|
|
archive, err := Generate("file", "content")
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
if archive == nil {
|
|
|
|
t.Fatal("The generated archive should not be nil.")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFiles := [][]string{
|
|
|
|
{"file", "content"},
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := tar.NewReader(archive)
|
|
|
|
actualFiles := make([][]string, 0, 10)
|
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(t, err)
|
2015-04-24 11:03:33 -04:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(tr)
|
|
|
|
content := buf.String()
|
|
|
|
actualFiles = append(actualFiles, []string{hdr.Name, content})
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
if len(actualFiles) != len(expectedFiles) {
|
|
|
|
t.Fatalf("Number of expected file %d, got %d.", len(expectedFiles), len(actualFiles))
|
|
|
|
}
|
|
|
|
for i := 0; i < len(expectedFiles); i++ {
|
|
|
|
actual := actualFiles[i]
|
|
|
|
expected := expectedFiles[i]
|
|
|
|
if actual[0] != expected[0] {
|
|
|
|
t.Fatalf("Expected name '%s', Actual name '%s'", expected[0], actual[0])
|
|
|
|
}
|
|
|
|
if actual[1] != expected[1] {
|
|
|
|
t.Fatalf("Expected content '%s', Actual content '%s'", expected[1], actual[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|