Skip to content
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

Add unit tests for internal packages #376

Merged
merged 3 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions internal/credential/credential.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
/*
Copyright The ORAS 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 credential

import "oras.land/oras-go/v2/registry/remote/auth"

// Credential converts user input username and password to a credential.
func Credential(username, password string) auth.Credential {
if username == "" {
return auth.Credential{
Expand Down
51 changes: 51 additions & 0 deletions internal/credential/credential_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright The ORAS 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 credential_test

import (
"testing"

"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras/internal/credential"
)

func Test_Credential_emptyCredential(t *testing.T) {
cred := credential.Credential("", "")
if cred != auth.EmptyCredential {
t.Fatalf("Expect empty credential but got %v", cred)
}
}

func Test_Credential_usernamePassword(t *testing.T) {
expected := auth.Credential{
Username: "username",
Password: "password",
}
cred := credential.Credential(expected.Username, expected.Password)
if cred != expected {
t.Fatalf("Expected credential to be '%v' but got '%v'", expected, cred)
}
}

func Test_Credential_refreshToken(t *testing.T) {
expected := auth.Credential{
RefreshToken: "mocked",
}
cred := credential.Credential("", expected.RefreshToken)
if cred != expected {
t.Fatalf("Expected credential to be '%v' but got '%v'", expected, cred)
}
}
85 changes: 85 additions & 0 deletions internal/credential/store_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright The ORAS 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 credential_test

import (
"context"
"math/rand"
"path/filepath"
"strconv"
"testing"

"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras/internal/credential"
)

func TestStore_storeGetErase(t *testing.T) {
tempDir := t.TempDir()
confFileName := "test.json"
configPath := filepath.Join(tempDir, confFileName)
regName := "test"
cred := auth.Credential{
Username: "username",
Password: "password",
}

// store
store, err := credential.NewStore(configPath)
if err != nil {
t.Fatalf("unexpected error: %v", err.Error())
}
err = store.Store(regName, cred)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

// get cred
got, err := store.Credential(context.Background(), regName)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != cred {
t.Fatalf("expect: %v, got: %v", cred, got)
}

// erase
err = store.Erase(regName)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
got, err = store.Credential(context.Background(), regName)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != auth.EmptyCredential {
t.Fatalf("expect: %v, got: %v", auth.EmptyCredential, got)
}
}

func TestStore_getEmptyCred(t *testing.T) {
store, err := credential.NewStore()
if err != nil {
t.Fatalf("Failed to create store with default config path: %v", err.Error())
}

got, err := store.Credential(context.Background(), strconv.Itoa(rand.Int()))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != auth.EmptyCredential {
t.Fatalf("expect: %v, got: %v", auth.EmptyCredential, got)
}
}
56 changes: 56 additions & 0 deletions internal/http/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright The ORAS 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 http_test

import (
"testing"

nhttp "net/http"

"oras.land/oras-go/v2/registry/remote/auth"
"oras.land/oras/internal/http"
)

func Test_NewClient_credential(t *testing.T) {
wanted := auth.Credential{
Username: "username",
}
opts := http.ClientOptions{
Credential: wanted,
}
client := http.NewClient(opts)
got, err := client.(*auth.Client).Credential(nil, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if got != wanted {
t.Fatalf("expect: %v, got: %v", wanted, got)
}
}

func Test_NewClient_tlsConfig(t *testing.T) {
opts := http.ClientOptions{
SkipTLSVerify: true,
}

wanted := opts.SkipTLSVerify
client := http.NewClient(opts)
config := client.(*auth.Client).Client.Transport.(*nhttp.Transport).TLSClientConfig
got := config.InsecureSkipVerify
if got != wanted {
t.Fatalf("expect: %v, got: %v", wanted, got)
}
}
1 change: 1 addition & 0 deletions internal/trace/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func WithLoggerLevel(ctx context.Context, level logrus.Level) (context.Context,
return context.WithValue(ctx, loggerKey, entry), entry
}

// Logger return the logger attached to context or the standard one.
func Logger(ctx context.Context) logrus.FieldLogger {
logger, ok := ctx.Value(loggerKey).(logrus.FieldLogger)
if !ok {
Expand Down