forked from notaryproject/notation-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: improve test coverage to 80% (notaryproject#405)
Test: - improved test coverage to over 80.16% Fix: - added ORAS copy library to copy the OCI Layout test data to a temporary directory and avoid generating test time temporary files in the repository directory. --------- Signed-off-by: Junjie Gao <[email protected]>
- Loading branch information
Showing
11 changed files
with
839 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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.