Skip to content

Commit

Permalink
test(unit): increase coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah authored and TerryHowe committed Aug 7, 2024
1 parent 1c60b17 commit 270ad71
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 15 deletions.
32 changes: 17 additions & 15 deletions cmd/oras/internal/display/status/text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"testing"

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content/memory"
"oras.land/oras/cmd/oras/internal/output"
"oras.land/oras/internal/testutils"
"os"
"strings"
"testing"
)

var (
Expand All @@ -36,37 +37,38 @@ var (
printer *output.Printer
bogus ocispec.Descriptor
memStore *memory.Store
memDesc ocispec.Descriptor
layerDesc ocispec.Descriptor
manifestDesc ocispec.Descriptor
wantedError = fmt.Errorf("wanted error")
)

func TestMain(m *testing.M) {
// memory store for testing
memStore = memory.New()
content := []byte("test")
r := bytes.NewReader(content)
memDesc = ocispec.Descriptor{
layerContent := []byte("test")
r := bytes.NewReader(layerContent)
layerDesc = ocispec.Descriptor{
MediaType: "application/octet-stream",
Digest: digest.FromBytes(content),
Size: int64(len(content)),
Digest: digest.FromBytes(layerContent),
Size: int64(len(layerContent)),
}
if err := memStore.Push(context.Background(), memDesc, r); err != nil {
if err := memStore.Push(context.Background(), layerDesc, r); err != nil {
fmt.Println("Setup failed:", err)
os.Exit(1)
}
if err := memStore.Tag(context.Background(), memDesc, memDesc.Digest.String()); err != nil {
if err := memStore.Tag(context.Background(), layerDesc, layerDesc.Digest.String()); err != nil {
fmt.Println("Setup failed:", err)
os.Exit(1)
}

layer1Desc := memDesc
layer1Desc := layerDesc
layer1Desc.Annotations = map[string]string{ocispec.AnnotationTitle: "layer1"}
layer2Desc := memDesc
layer2Desc := layerDesc
layer2Desc.Annotations = map[string]string{ocispec.AnnotationTitle: "layer2"}
manifest := ocispec.Manifest{
MediaType: ocispec.MediaTypeImageManifest,
Layers: []ocispec.Descriptor{layer1Desc, layer2Desc},
Config: memDesc,
Config: layerDesc,
}
manifestContent, err := json.Marshal(&manifest)
if err != nil {
Expand All @@ -82,7 +84,7 @@ func TestMain(m *testing.M) {
fmt.Println("Setup failed:", err)
os.Exit(1)
}
if err := memStore.Tag(context.Background(), memDesc, memDesc.Digest.String()); err != nil {
if err := memStore.Tag(context.Background(), layerDesc, layerDesc.Digest.String()); err != nil {
fmt.Println("Setup failed:", err)
os.Exit(1)
}
Expand Down
48 changes: 48 additions & 0 deletions cmd/oras/internal/display/status/tty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ limitations under the License.
package status

import (
"context"
"io"
"os"
"testing"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
)

func TestTTYPushHandler_OnFileLoading(t *testing.T) {
Expand Down Expand Up @@ -63,3 +66,48 @@ func TestTTYPullHandler_OnNodeProcessing(t *testing.T) {
t.Error("OnNodeProcessing() should not return an error")
}
}

// ErrorFetcher implements content.Fetcher.
type ErrorFetcher struct{}

// Fetch returns an error.
func (f *ErrorFetcher) Fetch(context.Context, ocispec.Descriptor) (io.ReadCloser, error) {
return nil, wantedError
}

func TestTTYPushHandler_errGetSuccessor(t *testing.T) {
ph := NewTTYPushHandler(nil)
opts := oras.CopyGraphOptions{}
ph.UpdateCopyOptions(&opts, &ErrorFetcher{})
if err := opts.PostCopy(context.Background(), ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageManifest,
}); err != wantedError {
t.Error("PostCopy() should return expected error")
}
}

// ErrorPromptor mocks trackable GraphTarget.
type ErrorPromptor struct {
oras.GraphTarget
io.Closer
}

// Prompt mocks an errored prompt.
func (p *ErrorPromptor) Prompt(ocispec.Descriptor, string) error {
return wantedError
}

func TestTTYPushHandler_errPrompt(t *testing.T) {
ph := TTYPushHandler{
tracked: &ErrorPromptor{},
}
opts := oras.CopyGraphOptions{}
ph.UpdateCopyOptions(&opts, memStore)
if err := opts.OnCopySkipped(ctx, layerDesc); err != wantedError {
t.Error("OnCopySkipped() should return expected error")
}
// test
if err := opts.PostCopy(context.Background(), manifestDesc); err != wantedError {
t.Error("PostCopy() should return expected error")
}
}

0 comments on commit 270ad71

Please sign in to comment.