Debian Codegraph @

content/oci/readonlystorage_test.go

raw
1 /*
2 Copyright The ORAS Authors.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15
16 package oci
17
18 import (
19 "bytes"
20 "context"
21 "errors"
22 "io"
23 "os"
24 "path/filepath"
25 "strings"
26 "testing"
27 "testing/fstest"
28
29 "github.com/opencontainers/go-digest"
30 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
31 "oras.land/oras-go/v2/content"
32 "oras.land/oras-go/v2/errdef"
33 "oras.land/oras-go/v2/internal/docker"
34 )
35
36 func TestReadOnlyStorage_Exists(t *testing.T) {
37 blob := []byte("test")
38 dgst := digest.FromBytes(blob)
39 desc := content.NewDescriptorFromBytes("", blob)
40 fsys := fstest.MapFS{
41 strings.Join([]string{"blobs", dgst.Algorithm().String(), dgst.Encoded()}, "/"): {},
42 }
43 s := NewStorageFromFS(fsys)
44 ctx := context.Background()
45
46 // test sha256
47 got, err := s.Exists(ctx, desc)
48 if err != nil {
49 t.Fatal("ReadOnlyStorage.Exists() error =", err)
50 }
51 if want := true; got != want {
52 t.Errorf("ReadOnlyStorage.Exists() = %v, want %v", got, want)
53 }
54
55 // test not found
56 blob = []byte("whaterver")
57 desc = content.NewDescriptorFromBytes("", blob)
58 got, err = s.Exists(ctx, desc)
59 if err != nil {
60 t.Fatal("ReadOnlyStorage.Exists() error =", err)
61 }
62 if want := false; got != want {
63 t.Errorf("ReadOnlyStorage.Exists() = %v, want %v", got, want)
64 }
65
66 // test invalid digest
67 desc = ocispec.Descriptor{Digest: "not a digest"}
68 _, err = s.Exists(ctx, desc)
69 if err == nil {
70 t.Fatalf("ReadOnlyStorage.Exists() error = %v, wantErr %v", err, true)
71 }
72 }
73
74 func TestReadOnlyStorage_Fetch(t *testing.T) {
75 blob := []byte("test")
76 dgst := digest.FromBytes(blob)
77 desc := content.NewDescriptorFromBytes("", blob)
78 fsys := fstest.MapFS{
79 strings.Join([]string{"blobs", dgst.Algorithm().String(), dgst.Encoded()}, "/"): {
80 Data: blob,
81 },
82 }
83
84 s := NewStorageFromFS(fsys)
85 ctx := context.Background()
86
87 // test fetch
88 rc, err := s.Fetch(ctx, desc)
89 if err != nil {
90 t.Fatal("ReadOnlyStorage.Fetch() error =", err)
91 }
92 got, err := io.ReadAll(rc)
93 if err != nil {
94 t.Fatal("ReadOnlyStorage.Fetch().Read() error =", err)
95 }
96 err = rc.Close()
97 if err != nil {
98 t.Error("ReadOnlyStorage.Fetch().Close() error =", err)
99 }
100 if !bytes.Equal(got, blob) {
101 t.Errorf("ReadOnlyStorage.Fetch() = %v, want %v", got, blob)
102 }
103
104 // test not found
105 anotherBlob := []byte("whatever")
106 desc = content.NewDescriptorFromBytes("", anotherBlob)
107 _, err = s.Fetch(ctx, desc)
108 if !errors.Is(err, errdef.ErrNotFound) {
109 t.Fatalf("ReadOnlyStorage.Fetch() error = %v, wantErr %v", err, errdef.ErrNotFound)
110 }
111
112 // test invalid digest
113 desc = ocispec.Descriptor{Digest: "not a digest"}
114 _, err = s.Fetch(ctx, desc)
115 if err == nil {
116 t.Fatalf("ReadOnlyStorage.Fetch() error = %v, wantErr %v", err, true)
117 }
118 }
119
120 func TestReadOnlyStorage_DirFS(t *testing.T) {
121 tempDir := t.TempDir()
122 blob := []byte("test")
123 dgst := digest.FromBytes(blob)
124 desc := content.NewDescriptorFromBytes("test", blob)
125 // write blob to disk
126 path := filepath.Join(tempDir, "blobs", dgst.Algorithm().String(), dgst.Encoded())
127 if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
128 t.Fatal("error calling Mkdir(), error =", err)
129 }
130 if err := os.WriteFile(path, blob, 0444); err != nil {
131 t.Fatal("error calling WriteFile(), error =", err)
132 }
133
134 s := NewStorageFromFS(os.DirFS(tempDir))
135 ctx := context.Background()
136
137 // test Exists
138 exists, err := s.Exists(ctx, desc)
139 if err != nil {
140 t.Fatal("ReadOnlyStorage.Exists() error =", err)
141 }
142 if !exists {
143 t.Errorf("ReadOnlyStorage.Exists() = %v, want %v", exists, true)
144 }
145
146 // test Fetch
147 rc, err := s.Fetch(ctx, desc)
148 if err != nil {
149 t.Fatal("ReadOnlyStorage.Fetch() error =", err)
150 }
151 got, err := io.ReadAll(rc)
152 if err != nil {
153 t.Fatal("ReadOnlyStorage.Fetch().Read() error =", err)
154 }
155 err = rc.Close()
156 if err != nil {
157 t.Error("ReadOnlyStorage.Fetch().Close() error =", err)
158 }
159 if !bytes.Equal(got, blob) {
160 t.Errorf("ReadOnlyStorage.Fetch() = %v, want %v", got, blob)
161 }
162 }
163
164 /*
165 === Contents of testdata/hello-world.tar ===
166
167 blobs/
168 blobs/sha256/
169 blobs/sha256/2db29710123e3e53a794f2694094b9b4338aa9ee5c40b930cb8063a1be392c54
170 blobs/sha256/f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
171 blobs/sha256/faa03e786c97f07ef34423fccceeec2398ec8a5759259f94d99078f264e9d7af
172 blobs/sha256/feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
173 index.json
174 manifest.json
175 oci-layout
176
177 === Contents of testdata/hello-world-prefixed-path.tar ===
178
179 ./
180 ./blobs/
181 ./blobs/sha256/
182 ./blobs/sha256/2db29710123e3e53a794f2694094b9b4338aa9ee5c40b930cb8063a1be392c54
183 ./blobs/sha256/f54a58bc1aac5ea1a25d796ae155dc228b3f0e11d046ae276b39c4bf2f13d8c4
184 ./blobs/sha256/faa03e786c97f07ef34423fccceeec2398ec8a5759259f94d99078f264e9d7af
185 ./blobs/sha256/feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
186 ./index.json
187 ./manifest.json
188 ./oci-layout
189
190 */
191
192 func TestReadOnlyStorage_TarFS(t *testing.T) {
193 tarPaths := []string{
194 "testdata/hello-world.tar",
195 "testdata/hello-world-prefixed-path.tar",
196 }
197 for _, tarPath := range tarPaths {
198 t.Run(tarPath, func(t *testing.T) {
199 s, err := NewStorageFromTar(tarPath)
200 if err != nil {
201 t.Fatal("NewStorageFromTar() error =", err)
202 }
203 ctx := context.Background()
204
205 // test data in testdata/hello-world.tar and testdata/hello-world-prefixed-path.tar
206 blob := []byte(`{"architecture":"amd64","config":{"Hostname":"","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/hello"],"Image":"sha256:b9935d4e8431fb1a7f0989304ec86b3329a99a25f5efdc7f09f3f8c41434ca6d","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":null},"container":"8746661ca3c2f215da94e6d3f7dfdcafaff5ec0b21c9aff6af3dc379a82fbc72","container_config":{"Hostname":"8746661ca3c2","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/bin/sh","-c","#(nop) ","CMD [\"/hello\"]"],"Image":"sha256:b9935d4e8431fb1a7f0989304ec86b3329a99a25f5efdc7f09f3f8c41434ca6d","Volumes":null,"WorkingDir":"","Entrypoint":null,"OnBuild":null,"Labels":{}},"created":"2021-09-23T23:47:57.442225064Z","docker_version":"20.10.7","history":[{"created":"2021-09-23T23:47:57.098990892Z","created_by":"/bin/sh -c #(nop) COPY file:50563a97010fd7ce1ceebd1fa4f4891ac3decdf428333fb2683696f4358af6c2 in / "},{"created":"2021-09-23T23:47:57.442225064Z","created_by":"/bin/sh -c #(nop) CMD [\"/hello\"]","empty_layer":true}],"os":"linux","rootfs":{"type":"layers","diff_ids":["sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359"]}}`)
207 desc := content.NewDescriptorFromBytes(docker.MediaTypeManifest, blob)
208
209 // test Exists
210 exists, err := s.Exists(ctx, desc)
211 if err != nil {
212 t.Fatal("ReadOnlyStorage.Exists() error =", err)
213 }
214 if want := true; exists != want {
215 t.Errorf("ReadOnlyStorage.Exists() = %v, want %v", exists, want)
216 }
217
218 // test Fetch
219 rc, err := s.Fetch(ctx, desc)
220 if err != nil {
221 t.Fatal("ReadOnlyStorage.Fetch() error =", err)
222 }
223 got, err := io.ReadAll(rc)
224 if err != nil {
225 t.Fatal("ReadOnlyStorage.Fetch().Read() error =", err)
226 }
227 err = rc.Close()
228 if err != nil {
229 t.Error("ReadOnlyStorage.Fetch().Close() error =", err)
230 }
231 if !bytes.Equal(got, blob) {
232 t.Errorf("ReadOnlyStorage.Fetch() = %v, want %v", got, blob)
233 }
234
235 // test Exists against a non-existing content
236 blob = []byte("whatever")
237 desc = content.NewDescriptorFromBytes("", blob)
238 exists, err = s.Exists(ctx, desc)
239 if err != nil {
240 t.Fatal("ReadOnlyStorage.Exists() error =", err)
241 }
242 if want := false; exists != want {
243 t.Errorf("ReadOnlyStorage.Exists() = %v, want %v", exists, want)
244 }
245
246 // test Fetch against a non-existing content
247 _, err = s.Fetch(ctx, desc)
248 if want := errdef.ErrNotFound; !errors.Is(err, want) {
249 t.Errorf("ReadOnlyStorage.Fetch() error = %v, wantErr %v", err, want)
250 }
251 })
252 }
253 }