/
src_test.go
66 lines (61 loc) · 1.91 KB
/
src_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package tarfile
import (
"bytes"
"context"
"io"
"strings"
"testing"
"github.com/containers/image/v5/manifest"
"github.com/containers/image/v5/pkg/blobinfocache/memory"
"github.com/containers/image/v5/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSourcePrepareLayerData(t *testing.T) {
// Just a smoke test to verify prepareLayerData does not crash on missing data
for _, c := range []struct {
config string
shouldFail bool
}{
{`{}`, true}, // No RootFS entry: can fail, shouldn’t crash
{`{"rootfs":{}}`, false}, // Useless no-layer configuration
} {
cache := memory.New()
var tarfileBuffer bytes.Buffer
ctx := context.Background()
writer := NewWriter(&tarfileBuffer)
dest := NewDestination(nil, writer, nil)
// No layers
configInfo, err := dest.PutBlob(ctx, strings.NewReader(c.config),
types.BlobInfo{Size: -1}, cache, true)
require.NoError(t, err, c.config)
manifest, err := manifest.Schema2FromComponents(
manifest.Schema2Descriptor{
MediaType: manifest.DockerV2Schema2ConfigMediaType,
Size: configInfo.Size,
Digest: configInfo.Digest,
}, []manifest.Schema2Descriptor{}).Serialize()
require.NoError(t, err, c.config)
err = dest.PutManifest(ctx, manifest, nil)
require.NoError(t, err, c.config)
err = writer.Close()
require.NoError(t, err, c.config)
reader, err := NewReaderFromStream(nil, &tarfileBuffer)
require.NoError(t, err, c.config)
src := NewSource(reader, true, nil, -1)
require.NoError(t, err, c.config)
defer src.Close()
configStream, _, err := src.GetBlob(ctx, types.BlobInfo{
Digest: configInfo.Digest,
Size: -1,
}, cache)
if !c.shouldFail {
require.NoError(t, err, c.config)
config2, err := io.ReadAll(configStream)
require.NoError(t, err, c.config)
assert.Equal(t, []byte(c.config), config2, c.config)
} else {
assert.Error(t, err, c.config)
}
}
}