github.com/oam-dev/kubevela@v1.9.11/pkg/utils/registries/secret_authenticator_test.go (about)

     1  /*
     2  Copyright 2023 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package registries
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  	"github.com/google/go-containerregistry/pkg/authn"
    25  )
    26  
    27  func buildImageRegistry(registry, username, password string, insecure bool, useHTTP bool) *ImageRegistry {
    28  	imageRegistry := &ImageRegistry{
    29  		Registry: registry,
    30  		Auth:     Auth{Username: username, Password: password},
    31  		Insecure: insecure,
    32  		UseHTTP:  useHTTP,
    33  	}
    34  
    35  	return imageRegistry
    36  }
    37  
    38  func TestSecretAuthenticator(t *testing.T) {
    39  	imageRegistry := buildImageRegistry("dockerhub.qingcloud.com", "guest", "guest", false, false)
    40  
    41  	secretAuthenticator, err := NewSecretAuthenticator(imageRegistry)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  
    46  	auth, err := secretAuthenticator.Authorization()
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	expected := &authn.AuthConfig{
    52  		Username: "guest",
    53  		Password: "guest",
    54  		Auth:     "Z3Vlc3Q6Z3Vlc3Q=",
    55  	}
    56  
    57  	if diff := cmp.Diff(auth, expected); len(diff) != 0 {
    58  		t.Errorf("%T, got+ expected-, %s", expected, diff)
    59  	}
    60  }
    61  
    62  func TestAuthn(t *testing.T) {
    63  	testCases := []struct {
    64  		name          string
    65  		imageRegistry *ImageRegistry
    66  		auth          bool
    67  		expectErr     bool
    68  	}{
    69  		{
    70  			name:          "Should authenticate with correct credential",
    71  			imageRegistry: buildImageRegistry("dockerhub.qingcloud.com", "guest", "guest", false, false),
    72  			auth:          true,
    73  			expectErr:     false,
    74  		},
    75  		{
    76  			name:          "Shouldn't authenticate with incorrect credentials",
    77  			imageRegistry: buildImageRegistry("index.docker.io", "foo", "bar", false, false),
    78  			auth:          false,
    79  			expectErr:     true,
    80  		},
    81  		{
    82  			name:          "Shouldn't authenticate with no credentials",
    83  			imageRegistry: nil,
    84  			auth:          false,
    85  			expectErr:     true,
    86  		},
    87  	}
    88  
    89  	for _, testCase := range testCases {
    90  		t.Run(testCase.name, func(t *testing.T) {
    91  			secretAuthenticator, err := NewSecretAuthenticator(testCase.imageRegistry)
    92  			if err != nil {
    93  				t.Errorf("error creating secretAuthenticator, %v", err)
    94  			}
    95  
    96  			ok, err := secretAuthenticator.Auth(context.Background())
    97  			if testCase.auth != ok {
    98  				t.Errorf("expected auth result: %v, but got %v", testCase.auth, ok)
    99  			}
   100  
   101  			if testCase.expectErr && err == nil {
   102  				t.Errorf("expected error, but got nil")
   103  			}
   104  
   105  			if !testCase.expectErr && err != nil {
   106  				t.Errorf("authentication error, %v", err)
   107  			}
   108  		})
   109  	}
   110  }