From 270ad717de5182e2fd58fb60e726b36735902d1e Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Tue, 6 Aug 2024 05:34:07 +0000 Subject: [PATCH] test(unit): increase coverage Signed-off-by: Billy Zha --- cmd/oras/internal/display/status/text_test.go | 32 +++++++------ cmd/oras/internal/display/status/tty_test.go | 48 +++++++++++++++++++ 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/cmd/oras/internal/display/status/text_test.go b/cmd/oras/internal/display/status/text_test.go index 47d9d6282..ca05177ed 100644 --- a/cmd/oras/internal/display/status/text_test.go +++ b/cmd/oras/internal/display/status/text_test.go @@ -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 ( @@ -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 { @@ -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) } diff --git a/cmd/oras/internal/display/status/tty_test.go b/cmd/oras/internal/display/status/tty_test.go index f611bcd33..9cb31f49b 100644 --- a/cmd/oras/internal/display/status/tty_test.go +++ b/cmd/oras/internal/display/status/tty_test.go @@ -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) { @@ -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") + } +}