-
Notifications
You must be signed in to change notification settings - Fork 46
test: improve test coverage to 80% #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
priteshbandi
merged 18 commits into
notaryproject:main
from
JeyJeyGao:test/increase_coverage
May 28, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
da398fe
fix: error message for dangling reference index
JeyJeyGao 269845c
test: add unit test
JeyJeyGao 7ab5919
Merge remote-tracking branch 'upstream/main' into fix/dangling_referr…
JeyJeyGao d28f182
fix: use remote.ReferrersError type
JeyJeyGao 6664b27
test: add test cases
JeyJeyGao 51a3e6b
test: add test cases
JeyJeyGao d8c8f29
fix: add license
JeyJeyGao d444452
test: add test cases
JeyJeyGao 83b28aa
test: update test cases
JeyJeyGao 12dfced
fix: update
JeyJeyGao 7647253
fix: update code
JeyJeyGao 519099d
fiX: update code
JeyJeyGao 49fa8a2
test: improve test coverage
JeyJeyGao 07d9719
Merge remote-tracking branch 'upstream/main' into test/increase_coverage
JeyJeyGao 7812144
fix: update test
JeyJeyGao 25d37ee
fix(test): update
JeyJeyGao cdc359e
fix: simplify Copy function
JeyJeyGao 39452a5
fix: simplify code
JeyJeyGao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright The Notary Project Authors. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package notation | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestErrorMessages(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| err error | ||
| want string | ||
| }{ | ||
| { | ||
| name: "ErrorPushSignatureFailed with message", | ||
| err: ErrorPushSignatureFailed{Msg: "test message"}, | ||
| want: "failed to push signature to registry with error: test message", | ||
| }, | ||
| { | ||
| name: "ErrorPushSignatureFailed without message", | ||
| err: ErrorPushSignatureFailed{}, | ||
| want: "failed to push signature to registry", | ||
| }, | ||
| { | ||
| name: "ErrorVerificationInconclusive with message", | ||
| err: ErrorVerificationInconclusive{Msg: "test message"}, | ||
| want: "test message", | ||
| }, | ||
| { | ||
| name: "ErrorVerificationInconclusive without message", | ||
| err: ErrorVerificationInconclusive{}, | ||
| want: "signature verification was inclusive due to an unexpected error", | ||
| }, | ||
| { | ||
| name: "ErrorNoApplicableTrustPolicy with message", | ||
| err: ErrorNoApplicableTrustPolicy{Msg: "test message"}, | ||
| want: "test message", | ||
| }, | ||
| { | ||
| name: "ErrorNoApplicableTrustPolicy without message", | ||
| err: ErrorNoApplicableTrustPolicy{}, | ||
| want: "there is no applicable trust policy for the given artifact", | ||
| }, | ||
| { | ||
| name: "ErrorSignatureRetrievalFailed with message", | ||
| err: ErrorSignatureRetrievalFailed{Msg: "test message"}, | ||
| want: "test message", | ||
| }, | ||
| { | ||
| name: "ErrorSignatureRetrievalFailed without message", | ||
| err: ErrorSignatureRetrievalFailed{}, | ||
| want: "unable to retrieve the digital signature from the registry", | ||
| }, | ||
| { | ||
| name: "ErrorVerificationFailed with message", | ||
| err: ErrorVerificationFailed{Msg: "test message"}, | ||
| want: "test message", | ||
| }, | ||
| { | ||
| name: "ErrorVerificationFailed without message", | ||
| err: ErrorVerificationFailed{}, | ||
| want: "signature verification failed", | ||
| }, | ||
| { | ||
| name: "ErrorUserMetadataVerificationFailed with message", | ||
| err: ErrorUserMetadataVerificationFailed{Msg: "test message"}, | ||
| want: "test message", | ||
| }, | ||
| { | ||
| name: "ErrorUserMetadataVerificationFailed without message", | ||
| err: ErrorUserMetadataVerificationFailed{}, | ||
| want: "unable to find specified metadata in the signature", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := tt.err.Error(); got != tt.want { | ||
| t.Errorf("Error() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright The Notary Project Authors. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package ocilayout | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
|
|
||
| "oras.land/oras-go/v2" | ||
| "oras.land/oras-go/v2/content/oci" | ||
| ) | ||
|
|
||
| // Copy creates a temporary OCI layout for testing | ||
| // and returns the path to the layout. | ||
| func Copy(sourcePath, destPath, tag string) error { | ||
| ctx := context.Background() | ||
|
|
||
| srcStore, err := oci.NewFromFS(ctx, os.DirFS(sourcePath)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // create a dest store for store the generated oci layout. | ||
| destStore, err := oci.New(destPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // copy data | ||
| _, err = oras.ExtendedCopy(ctx, srcStore, tag, destStore, "", oras.DefaultExtendedCopyOptions) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright The Notary Project Authors. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package ocilayout | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestCopy(t *testing.T) { | ||
| t.Run("empty oci layout", func(t *testing.T) { | ||
| err := Copy("", "", "v2") | ||
| if err == nil { | ||
| t.Errorf("expected error, got nil") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("invalid target path", func(t *testing.T) { | ||
| tempDir := t.TempDir() | ||
| // change the permission of the tempDir to make it invalid | ||
| if err := os.Chmod(tempDir, 0); err != nil { | ||
| t.Fatalf("failed to change the permission of the tempDir: %v", err) | ||
| } | ||
| err := Copy("../../testdata/oci-layout", tempDir, "v2") | ||
| if err == nil { | ||
| t.Errorf("expected error, got nil") | ||
| } | ||
|
|
||
| if err := os.Chmod(tempDir, 0755); err != nil { | ||
| t.Fatalf("failed to change the permission of the tempDir: %v", err) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("copy failed", func(t *testing.T) { | ||
| tempDir := t.TempDir() | ||
| err := Copy("../../testdata/oci-layout", tempDir, "v3") | ||
| if err == nil { | ||
| t.Errorf("expected error, got nil") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("copy success", func(t *testing.T) { | ||
| tempDir := t.TempDir() | ||
| err := Copy("../../testdata/oci-layout", tempDir, "v2") | ||
| if err != nil { | ||
| t.Errorf("expected nil, got %v", err) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright The Notary Project Authors. | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Package log provides logging functionality to notation. | ||
| // Users who want to enable logging option in notation should implement the | ||
| // log.Logger interface and include it in context by calling log.WithLogger. | ||
| // 3rd party loggers that implement log.Logger: github.com/uber-go/zap.SugaredLogger | ||
| // and github.com/sirupsen/logrus.Logger. | ||
| package log | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestWithLoggerAndGetLogger(t *testing.T) { | ||
| tl := &discardLogger{} | ||
| ctx := WithLogger(context.Background(), tl) | ||
|
|
||
| if got := GetLogger(ctx); got != tl { | ||
| t.Errorf("GetLogger() = %v, want %v", got, tl) | ||
| } | ||
| } | ||
|
|
||
| func TestGetLoggerWithNoLogger(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| if got := GetLogger(ctx); got != Discard { | ||
| t.Errorf("GetLogger() = %v, want Discard", got) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.