docker_test: Add a sample testdir.tar.gz

Signed-off-by: Simarpreet Singh <simar@linux.com>
This commit is contained in:
Simarpreet Singh
2019-11-25 10:04:55 -08:00
parent 55e97e97ce
commit 511e061c0c
2 changed files with 21 additions and 32 deletions

View File

@@ -227,8 +227,9 @@ func TestDockerExtractor_Extract(t *testing.T) {
name string name string
imageName string imageName string
manifestResp string manifestResp string
layerData []byte fileName string
blobData string blobData string
fileToExtract []string
expectedFileMap extractor.FileMap expectedFileMap extractor.FileMap
expectedError string expectedError string
}{ }{
@@ -245,24 +246,12 @@ func TestDockerExtractor_Extract(t *testing.T) {
} }
] ]
}`, }`,
layerData: []byte{ // this is hello.txt containing "hello world\n" POSIX tar'd and then gzipped fileName: "testdata/testdir.tar.gz", // includes helloworld.txt and badworld.txt
0x1f, 0x8b, 0x8, 0x8, 0xfa, 0x33, 0xd4, 0x5d, 0x0, 0x3, 0x68, 0x65, blobData: "foo",
0x6c, 0x6c, 0x6f, 0x0, 0xd3, 0xd3, 0xcf, 0x48, 0xcd, 0xc9, 0xc9, 0xd7, fileToExtract: []string{"testdir/helloworld.txt"},
0x2b, 0xa9, 0x28, 0x61, 0xa0, 0x15, 0x30, 0x30, 0x30, 0x30, 0x33, 0x31,
0x51, 0x0, 0xd1, 0xe6, 0x66, 0xa6, 0x60, 0xda, 0xc0, 0x8, 0xc2, 0x87, 0x0,
0x53, 0x53, 0x5, 0x43, 0x63, 0x53, 0x33, 0x53, 0x3, 0x63, 0x43, 0x33, 0x23,
0x23, 0x5, 0x3, 0x43, 0x63, 0x63, 0x13, 0x3, 0x6, 0x5, 0x3, 0x9a, 0xb9, 0x8,
0x9, 0x94, 0x16, 0x97, 0x24, 0x16, 0x1, 0x9d, 0x52, 0x9c, 0x99, 0xb, 0xa4,
0x71, 0x3, 0xa0, 0xb2, 0xb4, 0x34, 0x3c, 0xf2, 0x10, 0x9f, 0x28, 0xc0, 0xe9,
0x21, 0x2, 0xe4, 0xbb, 0x39, 0x18, 0x26, 0x19, 0x5f, 0x89, 0x65, 0x60, 0x7e,
0x7b, 0x90, 0x91, 0x17, 0x28, 0x70, 0x68, 0xc1, 0x77, 0xff, 0x5c, 0x3e, 0xf3,
0x5, 0x70, 0x15, 0xd, 0xe6, 0xcc, 0xb3, 0xee, 0xc9, 0xaa, 0x33, 0x68, 0xc, 0x98,
0x1b, 0x47, 0xc1, 0x28, 0x18, 0x5, 0xa3, 0x60, 0x14, 0x50, 0x1f, 0x0, 0x0,
0x7b, 0x78, 0xf9, 0x4b, 0x0, 0x8, 0x0, 0x0,
},
blobData: "foo",
expectedFileMap: extractor.FileMap{ expectedFileMap: extractor.FileMap{
"/config": []uint8{0x66, 0x6f, 0x6f}, "/config": []uint8{0x66, 0x6f, 0x6f},
"testdir/helloworld.txt": []uint8{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0xa},
}, },
}, },
{ {
@@ -278,20 +267,20 @@ func TestDockerExtractor_Extract(t *testing.T) {
{ {
name: "sad path: corrupt layer data invalid gzip", name: "sad path: corrupt layer data invalid gzip",
manifestResp: `{ manifestResp: `{
"schemaVersion": 2, "schemaVersion": 2,
"mediaType": "application/vnd.docker.distribution.manifest.v2+json", "mediaType": "application/vnd.docker.distribution.manifest.v2+json",
"layers": [ "layers": [
{ {
"mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip", "mediaType": "application/vnd.docker.image.rootfs.diff.tar.gzip",
"size": 153263, "size": 153263,
"digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b" "digest": "sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"
} }
] ]
}`, }`,
layerData: []byte{0xde, 0xad, 0xbe, 0xef}, fileName: "testdata/opq.tar",
blobData: "foo", blobData: "foo",
expectedFileMap: extractor.FileMap(nil), expectedFileMap: extractor.FileMap(nil),
expectedError: "invalid gzip: unexpected EOF", expectedError: "invalid gzip: gzip: invalid header",
}, },
} }
@@ -303,7 +292,8 @@ func TestDockerExtractor_Extract(t *testing.T) {
w.Header().Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json") w.Header().Set("Content-Type", "application/vnd.docker.distribution.manifest.v2+json")
_, _ = fmt.Fprint(w, tc.manifestResp) _, _ = fmt.Fprint(w, tc.manifestResp)
case strings.Contains(httpPath, "/v2/library/fooimage/blobs/sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"): case strings.Contains(httpPath, "/v2/library/fooimage/blobs/sha256:62d8908bee94c202b2d35224a221aaa2058318bfa9879fa541efaecba272331b"):
_, _ = w.Write(tc.layerData) b, _ := ioutil.ReadFile(tc.fileName)
_, _ = w.Write(b)
case strings.Contains(httpPath, "/v2/library/fooimage/blobs/"): case strings.Contains(httpPath, "/v2/library/fooimage/blobs/"):
_, _ = w.Write([]byte(tc.blobData)) _, _ = w.Write([]byte(tc.blobData))
default: default:
@@ -332,7 +322,6 @@ func TestDockerExtractor_Extract(t *testing.T) {
Cache: cache.Initialize(tempCacheDir), Cache: cache.Initialize(tempCacheDir),
} }
filesToExtract := []string{"file1", "file2", "file3"}
tsURL := strings.TrimPrefix(ts.URL, "http://") tsURL := strings.TrimPrefix(ts.URL, "http://")
var imageName string var imageName string
@@ -342,7 +331,7 @@ func TestDockerExtractor_Extract(t *testing.T) {
default: default:
imageName = tsURL + "/library/fooimage" imageName = tsURL + "/library/fooimage"
} }
fm, err := de.Extract(context.TODO(), imageName, filesToExtract) fm, err := de.Extract(context.TODO(), imageName, tc.fileToExtract)
switch { switch {
case tc.expectedError != "": case tc.expectedError != "":

BIN
extractor/docker/testdata/testdir.tar.gz vendored Normal file

Binary file not shown.