1
15
16 package content
17
18 import (
19 "context"
20 "fmt"
21 "io/ioutil"
22 "os"
23 "path/filepath"
24 "testing"
25
26 "github.com/containerd/containerd/remotes"
27 digest "github.com/opencontainers/go-digest"
28 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
29 "github.com/stretchr/testify/suite"
30 )
31
32 type ContentTestSuite struct {
33 suite.Suite
34 TestMemoryStore *Memory
35 TestFileStore *File
36 }
37
38 var (
39 testDirRoot, _ = filepath.Abs("../../.test")
40 testFileName = filepath.Join(testDirRoot, "testfile")
41 testRef = "abc123"
42 testContent = []byte("Hello World!")
43 testDescriptor = ocispec.Descriptor{
44 MediaType: ocispec.MediaTypeImageConfig,
45 Digest: digest.FromBytes(testContent),
46 Size: int64(len(testContent)),
47 Annotations: map[string]string{
48 ocispec.AnnotationTitle: testRef,
49 },
50 }
51 testBadContent = []byte("doesnotexist")
52 testBadDescriptor = ocispec.Descriptor{
53 MediaType: ocispec.MediaTypeImageConfig,
54 Digest: digest.FromBytes(testBadContent),
55 Size: int64(len(testBadContent)),
56 }
57 )
58
59 func (suite *ContentTestSuite) SetupSuite() {
60 testMemoryStore := NewMemory()
61 desc, err := testMemoryStore.Add(testRef, "", testContent)
62 suite.Nil(err, "no error adding testContent to memory store")
63 manifest, manifestDesc, config, configDesc, err := GenerateManifestAndConfig(nil, nil, desc)
64 suite.Nil(err, "no error creating config and manifest")
65 testMemoryStore.Set(configDesc, config)
66 err = testMemoryStore.StoreManifest(testRef, manifestDesc, manifest)
67 suite.Nil(err, "no error adding ref to memory store")
68 suite.TestMemoryStore = testMemoryStore
69
70 os.Remove(testFileName)
71 err = ioutil.WriteFile(testFileName, testContent, 0644)
72 suite.Nil(err, "no error creating test file on disk")
73 testFileStore := NewFile(testDirRoot, WithErrorOnNoName())
74 desc, err = testFileStore.Add(testRef, "", testFileName)
75 suite.Nil(err, "no error adding item to file store")
76 err = testFileStore.Load(configDesc, config)
77 suite.Nil(err, "no error adding config to file store")
78 err = testFileStore.StoreManifest(testRef, manifestDesc, manifest)
79 suite.Nil(err, "no error adding ref to file store")
80 suite.TestFileStore = testFileStore
81 }
82
83
84 func (suite *ContentTestSuite) Test_0_Ingesters() {
85
86 ctx := context.Background()
87 memPusher, _ := suite.TestMemoryStore.Pusher(ctx, "")
88 filePusher, _ := suite.TestFileStore.Pusher(ctx, "")
89 ingesters := map[string]remotes.Pusher{
90 "memory": memPusher,
91 "file": filePusher,
92 }
93
94 for key, ingester := range ingesters {
95
96
97 writer, err := ingester.Push(ctx, testBadDescriptor)
98 if key == "file" {
99 suite.NotNil(err, fmt.Sprintf("no error getting writer w bad ref for %s store", key))
100 }
101
102
103 ctx = context.Background()
104 writer, err = ingester.Push(ctx, testDescriptor)
105 suite.Nil(err, fmt.Sprintf("no error getting writer w good ref for %s store", key))
106 _, err = writer.Write(testContent)
107 suite.Nil(err, fmt.Sprintf("no error using writer.Write w good ref for %s store", key))
108 err = writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest)
109 suite.Nil(err, fmt.Sprintf("no error using writer.Commit w good ref for %s store", key))
110
111 digest := writer.Digest()
112 suite.Equal(testDescriptor.Digest, digest, fmt.Sprintf("correct digest for %s store", key))
113 status, err := writer.Status()
114 suite.Nil(err, fmt.Sprintf("no error retrieving writer status for %s store", key))
115 suite.Equal(testRef, status.Ref, fmt.Sprintf("correct status for %s store", key))
116
117
118 err = writer.Close()
119 suite.Nil(err, fmt.Sprintf("no error closing writer w bad ref for %s store", key))
120 err = writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest)
121 suite.NotNil(err, fmt.Sprintf("error using writer.Commit when closed w good ref for %s store", key))
122
123
124 writer, _ = ingester.Push(ctx, testDescriptor)
125 writer.Write(testContent)
126
127
128 err = writer.Truncate(123456789)
129 suite.NotNil(err, fmt.Sprintf("error using writer.Truncate w invalid size, good ref for %s store", key))
130
131
132 err = writer.Truncate(0)
133 suite.Nil(err, fmt.Sprintf("no error using writer.Truncate w valid size, good ref for %s store", key))
134
135 writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest)
136
137
138 err = writer.Commit(ctx, 1, testDescriptor.Digest)
139 fmt.Println(err)
140 suite.NotNil(err, fmt.Sprintf("error using writer.Commit w bad size, good ref for %s store", key))
141
142
143 err = writer.Commit(ctx, 0, testBadDescriptor.Digest)
144 suite.NotNil(err, fmt.Sprintf("error using writer.Commit w bad digest, good ref for %s store", key))
145 }
146 }
147
148
149 func (suite *ContentTestSuite) Test_1_Providers() {
150 ctx := context.Background()
151 memFetcher, _ := suite.TestMemoryStore.Fetcher(ctx, testRef)
152 fileFetcher, _ := suite.TestFileStore.Fetcher(ctx, testRef)
153 providers := map[string]remotes.Fetcher{
154 "memory": memFetcher,
155 "file": fileFetcher,
156 }
157
158
159 for key, provider := range providers {
160
161
162 ctx := context.Background()
163 _, err := provider.Fetch(ctx, testBadDescriptor)
164 suite.NotNil(err, fmt.Sprintf("error with bad ref for %s store", key))
165
166
167 ctx = context.Background()
168 reader, err := provider.Fetch(ctx, testDescriptor)
169 suite.Nil(err, fmt.Sprintf("no error with good ref for %s store", key))
170
171
172 err = reader.Close()
173 suite.Nil(err, fmt.Sprintf("no error closing reader for %s store", key))
174
175
176 if key == "file" {
177 os.Remove(testFileName)
178 ctx := context.Background()
179 _, err := provider.Fetch(ctx, testDescriptor)
180 suite.NotNil(err, fmt.Sprintf("error with good ref for %s store (file missing)", key))
181 }
182 }
183 }
184
185 func (suite *ContentTestSuite) Test_2_GetByName() {
186
187 _, _, ok := suite.TestMemoryStore.GetByName("doesnotexist")
188 suite.False(ok, "unable to find non-existant ref by name for memory store")
189
190
191 _, _, ok = suite.TestMemoryStore.GetByName(testRef)
192 suite.True(ok, "able to find non-existant ref by name for memory store")
193 }
194
195 func TestContentTestSuite(t *testing.T) {
196 suite.Run(t, new(ContentTestSuite))
197 }
198
View as plain text