Skip to content

Commit

Permalink
increase test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
  • Loading branch information
Xiaoxuan Wang committed Nov 11, 2024
1 parent 20ac24b commit 97ba0a1
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 86 deletions.
9 changes: 4 additions & 5 deletions cmd/oras/root/manifest/index/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"oras.land/oras/cmd/oras/internal/argument"
"oras.land/oras/cmd/oras/internal/command"
"oras.land/oras/cmd/oras/internal/display"
"oras.land/oras/cmd/oras/internal/display/metadata"
"oras.land/oras/cmd/oras/internal/display/status"
oerrors "oras.land/oras/cmd/oras/internal/errors"
"oras.land/oras/cmd/oras/internal/option"
Expand Down Expand Up @@ -123,11 +122,11 @@ func updateIndex(cmd *cobra.Command, opts updateOptions) error {
if err != nil {
return err
}
manifests, err = addManifests(ctx, displayStatus, displayMetadata, manifests, target, opts.addArguments)
manifests, err = addManifests(ctx, displayStatus, manifests, target, opts.addArguments)
if err != nil {
return err
}
manifests, err = mergeIndexes(ctx, displayStatus, displayMetadata, manifests, target, opts.mergeArguments)
manifests, err = mergeIndexes(ctx, displayStatus, manifests, target, opts.mergeArguments)
if err != nil {
return err
}
Expand Down Expand Up @@ -173,7 +172,7 @@ func fetchIndex(ctx context.Context, handler status.ManifestIndexUpdateHandler,
return index, nil
}

func addManifests(ctx context.Context, displayStatus status.ManifestIndexUpdateHandler, displayMetadata metadata.ManifestIndexUpdateHandler, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, addArguments []string) ([]ocispec.Descriptor, error) {
func addManifests(ctx context.Context, displayStatus status.ManifestIndexUpdateHandler, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, addArguments []string) ([]ocispec.Descriptor, error) {
for _, manifestRef := range addArguments {
if err := displayStatus.OnFetching(manifestRef); err != nil {
return nil, err
Expand All @@ -199,7 +198,7 @@ func addManifests(ctx context.Context, displayStatus status.ManifestIndexUpdateH
return manifests, nil
}

func mergeIndexes(ctx context.Context, displayStatus status.ManifestIndexUpdateHandler, displayMetadata metadata.ManifestIndexUpdateHandler, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, mergeArguments []string) ([]ocispec.Descriptor, error) {
func mergeIndexes(ctx context.Context, displayStatus status.ManifestIndexUpdateHandler, manifests []ocispec.Descriptor, target oras.ReadOnlyTarget, mergeArguments []string) ([]ocispec.Descriptor, error) {
for _, indexRef := range mergeArguments {
if err := displayStatus.OnFetching(indexRef); err != nil {
return nil, err
Expand Down
226 changes: 145 additions & 81 deletions cmd/oras/root/manifest/index/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ package index
import (
"context"
"fmt"
"os"
"reflect"
"testing"

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras/cmd/oras/internal/display/status"
"oras.land/oras/cmd/oras/internal/output"
)

// // for quick drafting
Expand All @@ -37,6 +35,77 @@ import (
// return 0, fmt.Errorf("test error")
// }

type testUpdateDisplayStatus struct {
onFetchingError bool
onFetchedError bool
onIndexPackedError bool
onIndexPushedError bool
onManifestRemovedError bool
onManifestAddedError bool
onIndexMergedError bool
}

func (tds *testUpdateDisplayStatus) OnFetching(manifestRef string) error {
if tds.onFetchingError {
return fmt.Errorf("OnFetching error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnFetched(manifestRef string, desc ocispec.Descriptor) error {
if tds.onFetchedError {
return fmt.Errorf("OnFetched error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnIndexPacked(desc ocispec.Descriptor) error {
if tds.onIndexPackedError {
return fmt.Errorf("error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnIndexPushed(path string) error {
if tds.onIndexPushedError {
return fmt.Errorf("error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnManifestRemoved(digest digest.Digest) error {
if tds.onManifestRemovedError {
return fmt.Errorf("OnManifestRemoved error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnManifestAdded(manifestRef string, desc ocispec.Descriptor) error {
if tds.onManifestAddedError {
return fmt.Errorf("error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnIndexMerged(indexRef string, desc ocispec.Descriptor) error {
if tds.onIndexMergedError {
return fmt.Errorf("error")
}
return nil
}

func NewTestUpdateDisplayStatus(onFetching, onFetched, onIndexPacked, onIndexPushed, onManifestRemoved, onManifestAdded, onIndexMerged bool) status.ManifestIndexUpdateHandler {
return &testUpdateDisplayStatus{
onFetchingError: onFetching,
onFetchedError: onFetched,
onIndexPackedError: onIndexPacked,
onIndexPushedError: onIndexPushed,
onManifestRemovedError: onManifestRemoved,
onManifestAddedError: onManifestAdded,
onIndexMergedError: onIndexMerged,
}
}

var (
A = ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageManifest,
Expand Down Expand Up @@ -69,7 +138,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove one matched item",
manifests: []ocispec.Descriptor{A, B, C},
digestSet: map[digest.Digest]bool{B.Digest: false},
displayStatus: status.NewTextManifestIndexUpdateHandler(output.NewPrinter(os.Stdout, os.Stderr, false)),
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
indexRef: "test01",
want: []ocispec.Descriptor{A, C},
wantErr: false,
Expand All @@ -78,7 +147,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove all matched items",
manifests: []ocispec.Descriptor{A, B, A, C, A, A, A},
digestSet: map[digest.Digest]bool{A.Digest: false},
displayStatus: status.NewTextManifestIndexUpdateHandler(output.NewPrinter(os.Stdout, os.Stderr, false)),
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
indexRef: "test02",
want: []ocispec.Descriptor{B, C},
wantErr: false,
Expand All @@ -87,7 +156,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove correctly when there is only one item",
manifests: []ocispec.Descriptor{A},
digestSet: map[digest.Digest]bool{A.Digest: false},
displayStatus: status.NewTextManifestIndexUpdateHandler(output.NewPrinter(os.Stdout, os.Stderr, false)),
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
indexRef: "test03",
want: []ocispec.Descriptor{},
wantErr: false,
Expand All @@ -96,7 +165,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove multiple distinct manifests",
manifests: []ocispec.Descriptor{A, B, C},
digestSet: map[digest.Digest]bool{A.Digest: false, C.Digest: false},
displayStatus: status.NewTextManifestIndexUpdateHandler(output.NewPrinter(os.Stdout, os.Stderr, false)),
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
indexRef: "test04",
want: []ocispec.Descriptor{B},
wantErr: false,
Expand All @@ -105,17 +174,26 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove multiple duplicate manifests",
manifests: []ocispec.Descriptor{A, B, C, C, B, A, B},
digestSet: map[digest.Digest]bool{A.Digest: false, C.Digest: false},
displayStatus: status.NewTextManifestIndexUpdateHandler(output.NewPrinter(os.Stdout, os.Stderr, false)),
indexRef: "test04",
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
indexRef: "test05",
want: []ocispec.Descriptor{B, B, B},
wantErr: false,
},
{
name: "return error when deleting a nonexistent item",
manifests: []ocispec.Descriptor{A, C},
digestSet: map[digest.Digest]bool{B.Digest: false},
displayStatus: status.NewTextManifestIndexUpdateHandler(output.NewPrinter(os.Stdout, os.Stderr, false)),
indexRef: "test04",
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
indexRef: "test06",
want: nil,
wantErr: true,
},
{
name: "handler error",
manifests: []ocispec.Descriptor{A, B, C},
digestSet: map[digest.Digest]bool{B.Digest: false},
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, true, false, false),
indexRef: "test07",
want: nil,
wantErr: true,
},
Expand Down Expand Up @@ -143,77 +221,6 @@ func Test_doRemoveManifests(t *testing.T) {
}
}

type testUpdateDisplayStatus struct {
onFetchingError bool
onFetchedError bool
onIndexPackedError bool
onIndexPushedError bool
onManifestRemovedError bool
onManifestAddedError bool
onIndexMergedError bool
}

func (tds *testUpdateDisplayStatus) OnFetching(manifestRef string) error {
if tds.onFetchingError {
return fmt.Errorf("OnFetching error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnFetched(manifestRef string, desc ocispec.Descriptor) error {
if tds.onFetchedError {
return fmt.Errorf("OnFetched error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnIndexPacked(desc ocispec.Descriptor) error {
if tds.onIndexPackedError {
return fmt.Errorf("error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnIndexPushed(path string) error {
if tds.onIndexPushedError {
return fmt.Errorf("error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnManifestRemoved(digest digest.Digest) error {
if tds.onManifestRemovedError {
return fmt.Errorf("error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnManifestAdded(manifestRef string, desc ocispec.Descriptor) error {
if tds.onManifestAddedError {
return fmt.Errorf("error")
}
return nil
}

func (tds *testUpdateDisplayStatus) OnIndexMerged(indexRef string, desc ocispec.Descriptor) error {
if tds.onIndexMergedError {
return fmt.Errorf("error")
}
return nil
}

func NewTestUpdateDisplayStatus(onFetching, onFetched, onIndexPacked, onIndexPushed, onManifestRemoved, onManifestAdded, onIndexMerged bool) status.ManifestIndexUpdateHandler {
return &testUpdateDisplayStatus{
onFetchingError: onFetching,
onFetchedError: onFetched,
onIndexPackedError: onIndexPacked,
onIndexPushedError: onIndexPushed,
onManifestRemovedError: onManifestRemoved,
onManifestAddedError: onManifestAdded,
onIndexMergedError: onIndexMerged,
}
}

func Test_fetchIndex(t *testing.T) {
testContext := context.Background()
tests := []struct {
Expand Down Expand Up @@ -266,3 +273,60 @@ func Test_fetchIndex(t *testing.T) {
})
}
}

func Test_mergeIndexes(t *testing.T) {
testContext := context.Background()
tests := []struct {
name string
ctx context.Context
displayStatus status.ManifestIndexUpdateHandler
manifests []ocispec.Descriptor
target oras.ReadOnlyTarget
mergeArguments []string
want []ocispec.Descriptor
wantErr bool
}{
{
name: "OnFetching error",
ctx: testContext,
displayStatus: NewTestUpdateDisplayStatus(true, false, false, false, false, false, false),
manifests: []ocispec.Descriptor{},
target: NewTestReadOnlyTarget("index"),
mergeArguments: []string{"test"},
want: nil,
wantErr: true,
},
{
name: "OnFetched error",
ctx: testContext,
displayStatus: NewTestUpdateDisplayStatus(false, true, false, false, false, false, false),
manifests: []ocispec.Descriptor{},
target: NewTestReadOnlyTarget("index"),
mergeArguments: []string{"test"},
want: nil,
wantErr: true,
},
{
name: "unmarshall error",
ctx: testContext,
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
manifests: []ocispec.Descriptor{},
target: NewTestReadOnlyTarget("index"),
mergeArguments: []string{"test"},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := mergeIndexes(tt.ctx, tt.displayStatus, tt.manifests, tt.target, tt.mergeArguments)
if (err != nil) != tt.wantErr {
t.Errorf("mergeIndexes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("mergeIndexes() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 97ba0a1

Please sign in to comment.