|
| 1 | +package content |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/containerd/containerd/content" |
| 12 | + "github.com/opencontainers/go-digest" |
| 13 | + ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 14 | + "github.com/stretchr/testify/suite" |
| 15 | +) |
| 16 | + |
| 17 | +type ContentTestSuite struct { |
| 18 | + suite.Suite |
| 19 | + TestMemoryStore *Memorystore |
| 20 | + TestFileStore *FileStore |
| 21 | +} |
| 22 | + |
| 23 | +var ( |
| 24 | + testDirRoot, _ = filepath.Abs("../../.test") |
| 25 | + testFileName = filepath.Join(testDirRoot, "testfile") |
| 26 | + testRef = "abc123" |
| 27 | + testContent = []byte("Hello World!") |
| 28 | + testDescriptor = ocispec.Descriptor{ |
| 29 | + MediaType: ocispec.MediaTypeImageConfig, |
| 30 | + Digest: digest.FromBytes(testContent), |
| 31 | + Size: int64(len(testContent)), |
| 32 | + Annotations: map[string]string{ |
| 33 | + ocispec.AnnotationTitle: testRef, |
| 34 | + }, |
| 35 | + } |
| 36 | + testBadContent = []byte("doesnotexist") |
| 37 | + testBadDescriptor = ocispec.Descriptor{ |
| 38 | + MediaType: ocispec.MediaTypeImageConfig, |
| 39 | + Digest: digest.FromBytes(testBadContent), |
| 40 | + Size: int64(len(testBadContent)), |
| 41 | + } |
| 42 | +) |
| 43 | + |
| 44 | +func (suite *ContentTestSuite) SetupSuite() { |
| 45 | + testMemoryStore := NewMemoryStore() |
| 46 | + testMemoryStore.Add(testRef, "", testContent) |
| 47 | + suite.TestMemoryStore = testMemoryStore |
| 48 | + |
| 49 | + os.Remove(testFileName) |
| 50 | + err := ioutil.WriteFile(testFileName, testContent, 0644) |
| 51 | + suite.Nil(err, "no error creating test file on disk") |
| 52 | + testFileStore := NewFileStore(testDirRoot) |
| 53 | + _, err = testFileStore.Add(testRef, "", testFileName) |
| 54 | + suite.Nil(err, "no error adding item to file store") |
| 55 | + suite.TestFileStore = testFileStore |
| 56 | +} |
| 57 | + |
| 58 | +// Tests all Writers (Ingesters) |
| 59 | +func (suite *ContentTestSuite) Test_0_Ingesters() { |
| 60 | + ingesters := map[string]content.Ingester{ |
| 61 | + "memory": suite.TestMemoryStore, |
| 62 | + "file": suite.TestFileStore, |
| 63 | + } |
| 64 | + |
| 65 | + for key, ingester := range ingesters { |
| 66 | + |
| 67 | + // Bad ref |
| 68 | + ctx := context.Background() |
| 69 | + refOpt := content.WithDescriptor(testBadDescriptor) |
| 70 | + writer, err := ingester.Writer(ctx, refOpt) |
| 71 | + if key == "file" { |
| 72 | + suite.NotNil(err, fmt.Sprintf("no error getting writer w bad ref for %s store", key)) |
| 73 | + } |
| 74 | + |
| 75 | + // Good ref |
| 76 | + ctx = context.Background() |
| 77 | + refOpt = content.WithDescriptor(testDescriptor) |
| 78 | + writer, err = ingester.Writer(ctx, refOpt) |
| 79 | + suite.Nil(err, fmt.Sprintf("no error getting writer w good ref for %s store", key)) |
| 80 | + _, err = writer.Write(testContent) |
| 81 | + suite.Nil(err, fmt.Sprintf("no error using writer.Write w good ref for %s store", key)) |
| 82 | + err = writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest) |
| 83 | + suite.Nil(err, fmt.Sprintf("no error using writer.Commit w good ref for %s store", key)) |
| 84 | + |
| 85 | + digest := writer.Digest() |
| 86 | + suite.Equal(testDescriptor.Digest, digest, fmt.Sprintf("correct digest for %s store", key)) |
| 87 | + status, err := writer.Status() |
| 88 | + suite.Nil(err, fmt.Sprintf("no error retrieving writer status for %s store", key)) |
| 89 | + suite.Equal(testRef, status.Ref, fmt.Sprintf("correct status for %s store", key)) |
| 90 | + |
| 91 | + // close writer |
| 92 | + err = writer.Close() |
| 93 | + suite.Nil(err, fmt.Sprintf("no error closing writer w bad ref for %s store", key)) |
| 94 | + err = writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest) |
| 95 | + suite.NotNil(err, fmt.Sprintf("error using writer.Commit when closed w good ref for %s store", key)) |
| 96 | + |
| 97 | + // re-init writer after closing |
| 98 | + writer, _ = ingester.Writer(ctx, refOpt) |
| 99 | + writer.Write(testContent) |
| 100 | + |
| 101 | + // invalid truncate size |
| 102 | + err = writer.Truncate(123456789) |
| 103 | + suite.NotNil(err, fmt.Sprintf("error using writer.Truncate w invalid size, good ref for %s store", key)) |
| 104 | + |
| 105 | + // valid truncate size |
| 106 | + err = writer.Truncate(0) |
| 107 | + suite.Nil(err, fmt.Sprintf("no error using writer.Truncate w valid size, good ref for %s store", key)) |
| 108 | + |
| 109 | + writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest) |
| 110 | + |
| 111 | + // bad size |
| 112 | + err = writer.Commit(ctx, 1, testDescriptor.Digest) |
| 113 | + fmt.Println(err) |
| 114 | + suite.NotNil(err, fmt.Sprintf("error using writer.Commit w bad size, good ref for %s store", key)) |
| 115 | + |
| 116 | + // bad digest |
| 117 | + writer, _ = ingester.Writer(ctx, refOpt) |
| 118 | + err = writer.Commit(ctx, 0, testBadDescriptor.Digest) |
| 119 | + suite.NotNil(err, fmt.Sprintf("error using writer.Commit w bad digest, good ref for %s store", key)) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// Tests all Readers (Providers) |
| 124 | +func (suite *ContentTestSuite) Test_1_Providers() { |
| 125 | + providers := map[string]content.Provider{ |
| 126 | + "memory": suite.TestMemoryStore, |
| 127 | + "file": suite.TestFileStore, |
| 128 | + } |
| 129 | + |
| 130 | + // Readers (Providers) |
| 131 | + for key, provider := range providers { |
| 132 | + |
| 133 | + // Bad ref |
| 134 | + ctx := context.Background() |
| 135 | + _, err := provider.ReaderAt(ctx, testBadDescriptor) |
| 136 | + suite.NotNil(err, fmt.Sprintf("error with bad ref for %s store", key)) |
| 137 | + |
| 138 | + // Good ref |
| 139 | + ctx = context.Background() |
| 140 | + readerAt, err := provider.ReaderAt(ctx, testDescriptor) |
| 141 | + suite.Nil(err, fmt.Sprintf("no error with good ref for %s store", key)) |
| 142 | + |
| 143 | + // readerat Size() |
| 144 | + suite.Equal(testDescriptor.Size, readerAt.Size(), fmt.Sprintf("readerat size matches for %s store", key)) |
| 145 | + |
| 146 | + // readerat Close() |
| 147 | + err = readerAt.Close() |
| 148 | + suite.Nil(err, fmt.Sprintf("no error closing readerat for %s store", key)) |
| 149 | + |
| 150 | + // file missing |
| 151 | + if key == "file" { |
| 152 | + os.Remove(testFileName) |
| 153 | + ctx := context.Background() |
| 154 | + _, err := provider.ReaderAt(ctx, testDescriptor) |
| 155 | + suite.NotNil(err, fmt.Sprintf("error with good ref for %s store (file missing)", key)) |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func (suite *ContentTestSuite) Test_2_GetByName() { |
| 161 | + // NotFound |
| 162 | + _, _, ok := suite.TestMemoryStore.GetByName("doesnotexist") |
| 163 | + suite.False(ok, "unable to find non-existant ref by name for memory store") |
| 164 | + |
| 165 | + // Found |
| 166 | + _, _, ok = suite.TestMemoryStore.GetByName(testRef) |
| 167 | + suite.True(ok, "able to find non-existant ref by name for memory store") |
| 168 | +} |
| 169 | + |
| 170 | +func TestContentTestSuite(t *testing.T) { |
| 171 | + suite.Run(t, new(ContentTestSuite)) |
| 172 | +} |
0 commit comments