Skip to content

Commit 9499859

Browse files
authored
pkg/content unit tests (#24)
* make test to test all pkg * begin testing on content pkg * add more test cases * readerat tests * add file missing test
1 parent c4f2a91 commit 9499859

File tree

4 files changed

+195
-4
lines changed

4 files changed

+195
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ debug
1919
.idea
2020

2121
# Custom
22-
coverage.*
22+
.cover/
23+
.test/
2324
hello.txt
2425
bin/
2526
dist/

Makefile

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ fix-deps:
1010

1111
.PHONY: test
1212
test:
13-
CGO_ENABLED=0 go test -v -covermode=atomic -coverprofile=coverage.out github.com/shizhMSFT/oras/pkg/oras
14-
go tool cover -html=coverage.out -o=coverage.html
13+
./scripts/test.sh
1514

1615
.PHONY: covhtml
1716
covhtml:
18-
open coverage.html
17+
open .cover/coverage.html
1918

2019
.PHONY: clean
2120
clean:

pkg/content/content_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}

scripts/test.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash -ex
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
cd $DIR/../
5+
6+
rm -rf .cover/ .test/
7+
mkdir .cover/ .test/
8+
trap "rm -rf .test/" EXIT
9+
10+
export CGO_ENABLED=0
11+
for pkg in `go list ./pkg/... | grep -v /vendor/`; do
12+
go test -v -covermode=atomic \
13+
-coverprofile=".cover/$(echo $pkg | sed 's/\//_/g').cover.out" $pkg
14+
done
15+
16+
echo "mode: set" > .cover/cover.out && cat .cover/*.cover.out | grep -v mode: | sort -r | \
17+
awk '{if($1 != last) {print $0;last=$1}}' >> .cover/cover.out
18+
19+
go tool cover -html=.cover/cover.out -o=.cover/coverage.html

0 commit comments

Comments
 (0)