...

Source file src/github.com/google/go-containerregistry/pkg/authn/multikeychain_test.go

Documentation: github.com/google/go-containerregistry/pkg/authn

     1  // Copyright 2018 Google LLC All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package authn
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-containerregistry/pkg/name"
    21  )
    22  
    23  func TestMultiKeychain(t *testing.T) {
    24  	one := &Basic{Username: "one", Password: "secret"}
    25  	two := &Basic{Username: "two", Password: "secret"}
    26  	three := &Basic{Username: "three", Password: "secret"}
    27  
    28  	regOne, _ := name.NewRegistry("one.gcr.io", name.StrictValidation)
    29  	regTwo, _ := name.NewRegistry("two.gcr.io", name.StrictValidation)
    30  	regThree, _ := name.NewRegistry("three.gcr.io", name.StrictValidation)
    31  
    32  	tests := []struct {
    33  		name string
    34  		reg  name.Registry
    35  		kc   Keychain
    36  		want Authenticator
    37  	}{{
    38  		// Make sure our test keychain WAI
    39  		name: "simple fixed test (match)",
    40  		reg:  regOne,
    41  		kc:   fixedKeychain{regOne: one},
    42  		want: one,
    43  	}, {
    44  		// Make sure our test keychain WAI
    45  		name: "simple fixed test (no match)",
    46  		reg:  regTwo,
    47  		kc:   fixedKeychain{regOne: one},
    48  		want: Anonymous,
    49  	}, {
    50  		name: "match first keychain",
    51  		reg:  regOne,
    52  		kc: NewMultiKeychain(
    53  			fixedKeychain{regOne: one},
    54  			fixedKeychain{regOne: three, regTwo: two},
    55  		),
    56  		want: one,
    57  	}, {
    58  		name: "match second keychain",
    59  		reg:  regTwo,
    60  		kc: NewMultiKeychain(
    61  			fixedKeychain{regOne: one},
    62  			fixedKeychain{regOne: three, regTwo: two},
    63  		),
    64  		want: two,
    65  	}, {
    66  		name: "match no keychain",
    67  		reg:  regThree,
    68  		kc: NewMultiKeychain(
    69  			fixedKeychain{regOne: one},
    70  			fixedKeychain{regOne: three, regTwo: two},
    71  		),
    72  		want: Anonymous,
    73  	}}
    74  
    75  	for _, test := range tests {
    76  		t.Run(test.name, func(t *testing.T) {
    77  			got, err := test.kc.Resolve(test.reg)
    78  			if err != nil {
    79  				t.Errorf("Resolve() = %v", err)
    80  			}
    81  			if got != test.want {
    82  				t.Errorf("Resolve() = %v, wanted %v", got, test.want)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  type fixedKeychain map[Resource]Authenticator
    89  
    90  var _ Keychain = (fixedKeychain)(nil)
    91  
    92  // Resolve implements Keychain.
    93  func (fk fixedKeychain) Resolve(target Resource) (Authenticator, error) {
    94  	if auth, ok := fk[target]; ok {
    95  		return auth, nil
    96  	}
    97  	return Anonymous, nil
    98  }
    99  

View as plain text