Skip to content

Commit

Permalink
pkg/content unit tests (oras-project#24)
Browse files Browse the repository at this point in the history
* make test to test all pkg

* begin testing on content pkg

* add more test cases

* readerat tests

* add file missing test
  • Loading branch information
jdolitsky authored Jan 7, 2019
1 parent c4f2a91 commit 9499859
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ debug
.idea

# Custom
coverage.*
.cover/
.test/
hello.txt
bin/
dist/
Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ fix-deps:

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

.PHONY: covhtml
covhtml:
open coverage.html
open .cover/coverage.html

.PHONY: clean
clean:
Expand Down
172 changes: 172 additions & 0 deletions pkg/content/content_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package content

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/containerd/containerd/content"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/suite"
)

type ContentTestSuite struct {
suite.Suite
TestMemoryStore *Memorystore
TestFileStore *FileStore
}

var (
testDirRoot, _ = filepath.Abs("../../.test")
testFileName = filepath.Join(testDirRoot, "testfile")
testRef = "abc123"
testContent = []byte("Hello World!")
testDescriptor = ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageConfig,
Digest: digest.FromBytes(testContent),
Size: int64(len(testContent)),
Annotations: map[string]string{
ocispec.AnnotationTitle: testRef,
},
}
testBadContent = []byte("doesnotexist")
testBadDescriptor = ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageConfig,
Digest: digest.FromBytes(testBadContent),
Size: int64(len(testBadContent)),
}
)

func (suite *ContentTestSuite) SetupSuite() {
testMemoryStore := NewMemoryStore()
testMemoryStore.Add(testRef, "", testContent)
suite.TestMemoryStore = testMemoryStore

os.Remove(testFileName)
err := ioutil.WriteFile(testFileName, testContent, 0644)
suite.Nil(err, "no error creating test file on disk")
testFileStore := NewFileStore(testDirRoot)
_, err = testFileStore.Add(testRef, "", testFileName)
suite.Nil(err, "no error adding item to file store")
suite.TestFileStore = testFileStore
}

// Tests all Writers (Ingesters)
func (suite *ContentTestSuite) Test_0_Ingesters() {
ingesters := map[string]content.Ingester{
"memory": suite.TestMemoryStore,
"file": suite.TestFileStore,
}

for key, ingester := range ingesters {

// Bad ref
ctx := context.Background()
refOpt := content.WithDescriptor(testBadDescriptor)
writer, err := ingester.Writer(ctx, refOpt)
if key == "file" {
suite.NotNil(err, fmt.Sprintf("no error getting writer w bad ref for %s store", key))
}

// Good ref
ctx = context.Background()
refOpt = content.WithDescriptor(testDescriptor)
writer, err = ingester.Writer(ctx, refOpt)
suite.Nil(err, fmt.Sprintf("no error getting writer w good ref for %s store", key))
_, err = writer.Write(testContent)
suite.Nil(err, fmt.Sprintf("no error using writer.Write w good ref for %s store", key))
err = writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest)
suite.Nil(err, fmt.Sprintf("no error using writer.Commit w good ref for %s store", key))

digest := writer.Digest()
suite.Equal(testDescriptor.Digest, digest, fmt.Sprintf("correct digest for %s store", key))
status, err := writer.Status()
suite.Nil(err, fmt.Sprintf("no error retrieving writer status for %s store", key))
suite.Equal(testRef, status.Ref, fmt.Sprintf("correct status for %s store", key))

// close writer
err = writer.Close()
suite.Nil(err, fmt.Sprintf("no error closing writer w bad ref for %s store", key))
err = writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest)
suite.NotNil(err, fmt.Sprintf("error using writer.Commit when closed w good ref for %s store", key))

// re-init writer after closing
writer, _ = ingester.Writer(ctx, refOpt)
writer.Write(testContent)

// invalid truncate size
err = writer.Truncate(123456789)
suite.NotNil(err, fmt.Sprintf("error using writer.Truncate w invalid size, good ref for %s store", key))

// valid truncate size
err = writer.Truncate(0)
suite.Nil(err, fmt.Sprintf("no error using writer.Truncate w valid size, good ref for %s store", key))

writer.Commit(ctx, testDescriptor.Size, testDescriptor.Digest)

// bad size
err = writer.Commit(ctx, 1, testDescriptor.Digest)
fmt.Println(err)
suite.NotNil(err, fmt.Sprintf("error using writer.Commit w bad size, good ref for %s store", key))

// bad digest
writer, _ = ingester.Writer(ctx, refOpt)
err = writer.Commit(ctx, 0, testBadDescriptor.Digest)
suite.NotNil(err, fmt.Sprintf("error using writer.Commit w bad digest, good ref for %s store", key))
}
}

// Tests all Readers (Providers)
func (suite *ContentTestSuite) Test_1_Providers() {
providers := map[string]content.Provider{
"memory": suite.TestMemoryStore,
"file": suite.TestFileStore,
}

// Readers (Providers)
for key, provider := range providers {

// Bad ref
ctx := context.Background()
_, err := provider.ReaderAt(ctx, testBadDescriptor)
suite.NotNil(err, fmt.Sprintf("error with bad ref for %s store", key))

// Good ref
ctx = context.Background()
readerAt, err := provider.ReaderAt(ctx, testDescriptor)
suite.Nil(err, fmt.Sprintf("no error with good ref for %s store", key))

// readerat Size()
suite.Equal(testDescriptor.Size, readerAt.Size(), fmt.Sprintf("readerat size matches for %s store", key))

// readerat Close()
err = readerAt.Close()
suite.Nil(err, fmt.Sprintf("no error closing readerat for %s store", key))

// file missing
if key == "file" {
os.Remove(testFileName)
ctx := context.Background()
_, err := provider.ReaderAt(ctx, testDescriptor)
suite.NotNil(err, fmt.Sprintf("error with good ref for %s store (file missing)", key))
}
}
}

func (suite *ContentTestSuite) Test_2_GetByName() {
// NotFound
_, _, ok := suite.TestMemoryStore.GetByName("doesnotexist")
suite.False(ok, "unable to find non-existant ref by name for memory store")

// Found
_, _, ok = suite.TestMemoryStore.GetByName(testRef)
suite.True(ok, "able to find non-existant ref by name for memory store")
}

func TestContentTestSuite(t *testing.T) {
suite.Run(t, new(ContentTestSuite))
}
19 changes: 19 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash -ex

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR/../

rm -rf .cover/ .test/
mkdir .cover/ .test/
trap "rm -rf .test/" EXIT

export CGO_ENABLED=0
for pkg in `go list ./pkg/... | grep -v /vendor/`; do
go test -v -covermode=atomic \
-coverprofile=".cover/$(echo $pkg | sed 's/\//_/g').cover.out" $pkg
done

echo "mode: set" > .cover/cover.out && cat .cover/*.cover.out | grep -v mode: | sort -r | \
awk '{if($1 != last) {print $0;last=$1}}' >> .cover/cover.out

go tool cover -html=.cover/cover.out -o=.cover/coverage.html

0 comments on commit 9499859

Please sign in to comment.