-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathociindex_test.go
More file actions
153 lines (129 loc) · 5.58 KB
/
Copy pathociindex_test.go
File metadata and controls
153 lines (129 loc) · 5.58 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package desync
import (
"encoding/json"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// newTestOCIIndexStore starts a fake registry and returns an index store and a
// chunk store pointed at the same repository.
func newTestOCIIndexStore(t *testing.T) (OCIIndexStore, OCIStore, *testOCIRegistry) {
t.Helper()
reg := newTestOCIRegistry()
srv := httptest.NewServer(reg)
t.Cleanup(srv.Close)
u, err := url.Parse("oci+" + srv.URL + "/user/repo")
require.NoError(t, err)
is, err := NewOCIIndexStore(u, nil, StoreOptions{})
require.NoError(t, err)
cs, err := NewOCIStore(u, nil, StoreOptions{})
require.NoError(t, err)
return is, cs, reg
}
func testIndex(t *testing.T) Index {
t.Helper()
f, err := os.Open("testdata/blob1.caibx")
require.NoError(t, err)
defer f.Close()
idx, err := IndexFromReader(f)
require.NoError(t, err)
return idx
}
// Covers the round trip, the manifest the index is stored under, and the
// streaming path taken by indexes too large to buffer, which has to produce
// the same blob as the buffered one.
func TestOCIIndexStoreRoundtrip(t *testing.T) {
s, _, reg := newTestOCIIndexStore(t)
idx := testIndex(t)
_, err := s.GetIndex("blob1.caibx")
require.ErrorAs(t, err, &NoSuchObject{})
require.NoError(t, s.StoreIndex("blob1.caibx", idx))
got, err := s.GetIndex("blob1.caibx")
require.NoError(t, err)
require.Equal(t, idx.Index, got.Index)
require.Equal(t, idx.Chunks, got.Chunks)
// The manifest carries the shape of the index, so it can be read without
// pulling what may be a very large blob.
m, ok := reg.manifests["blob1.caibx"]
require.True(t, ok, "index not tagged with its name")
var manifest ocispec.Manifest
require.NoError(t, json.Unmarshal(m.content, &manifest))
assert.Equal(t, OCIIndexArtifactType, manifest.ArtifactType)
require.Len(t, manifest.Layers, 1)
assert.Equal(t, "blob1.caibx", manifest.Annotations[ocispec.AnnotationTitle])
assert.Equal(t, "161", manifest.Annotations[ociIndexChunksAnnotation])
assert.Equal(t, "2097152", manifest.Annotations[ociIndexBlobSizeAnnotation])
assert.Equal(t, "2048:8192:32768", manifest.Annotations[ociIndexChunkSizeAnnotation])
// Same index again over the streaming path.
orig := maxInMemoryIndex
maxInMemoryIndex = 1
defer func() { maxInMemoryIndex = orig }()
require.NoError(t, s.StoreIndex("streamed.caibx", idx))
got, err = s.GetIndex("streamed.caibx")
require.NoError(t, err)
require.Equal(t, idx.Chunks, got.Chunks)
var streamed ocispec.Manifest
require.NoError(t, json.Unmarshal(reg.manifests["streamed.caibx"].content, &streamed))
assert.Equal(t, manifest.Layers[0].Digest, streamed.Layers[0].Digest)
assert.Equal(t, manifest.Layers[0].Size, streamed.Layers[0].Size)
}
// Indexes and chunks can share a repository: pruning the chunk store must not
// touch indexes, and neither kind may be mistaken for the other.
func TestOCIIndexStoreSharesRepoWithChunks(t *testing.T) {
is, cs, _ := newTestOCIIndexStore(t)
idx := testIndex(t)
require.NoError(t, is.StoreIndex("blob1.caibx", idx))
chunk := NewChunk([]byte("some chunk data"))
require.NoError(t, cs.StoreChunk(chunk))
// A chunk is not an index, even asked for under its own tag.
_, err := is.GetIndex(chunk.ID().String() + ".cacnk")
require.ErrorAs(t, err, &NoSuchObject{})
// Prune the chunk store down to nothing. The index survives: its tag
// doesn't parse as a chunk ID, and it carries a different artifact type.
require.NoError(t, cs.Prune(t.Context(), map[ChunkID]struct{}{}))
got, err := is.GetIndex("blob1.caibx")
require.NoError(t, err)
assert.Equal(t, idx.Chunks, got.Chunks)
has, err := cs.HasChunk(chunk.ID())
require.NoError(t, err)
assert.False(t, has, "chunk should have been pruned")
}
// Names that can't be an OCI tag, and options an index store can't honor, have
// to be rejected rather than passed to the registry or silently ignored.
func TestOCIIndexStoreRejects(t *testing.T) {
for _, name := range []string{"a", "file.iso.caibx", "rootfs-v2.caidx", "a_b.c-d"} {
assert.NoError(t, ValidateOCIIndexName(name), name)
}
for _, name := range []string{"", "sub/dir.caibx", ".hidden", "-lead", "with space", strings.Repeat("a", 129)} {
assert.Error(t, ValidateOCIIndexName(name), name)
}
// The store surfaces that check on both paths.
s, _, _ := newTestOCIIndexStore(t)
require.ErrorContains(t, s.StoreIndex(".hidden.caibx", Index{}), "cannot be used in an OCI registry")
_, err := s.GetIndex(".hidden.caibx")
require.ErrorContains(t, err, "cannot be used in an OCI registry")
// Indexes are stored in plain form, so encryption can't be honored.
u, err := url.Parse("oci+https://registry.example.com/user/repo")
require.NoError(t, err)
_, err = NewOCIIndexStore(u, nil, StoreOptions{Encryption: true})
require.Error(t, err)
}
// oras returns the manifest response body unbounded, so an endless or
// oversized body has to be cut off rather than read until memory runs out.
func TestOCIManifestSizeLimit(t *testing.T) {
// A body larger than the limit is rejected rather than consumed.
huge := strings.NewReader("{" + strings.Repeat(" ", maxOCIManifestSize+16) + "}")
_, err := readOCIManifest(huge)
require.ErrorContains(t, err, "exceeds")
// Anything trailing a valid manifest is rejected too.
_, err = readOCIManifest(strings.NewReader(`{"schemaVersion":2} trailing`))
require.Error(t, err)
m, err := readOCIManifest(strings.NewReader(`{"schemaVersion":2,"artifactType":"` + OCIIndexArtifactType + `"}`))
require.NoError(t, err)
assert.Equal(t, OCIIndexArtifactType, m.ArtifactType)
}