...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/remote/options_test.go

Documentation: github.com/sigstore/cosign/v2/pkg/oci/remote

     1  //
     2  // Copyright 2021 The Sigstore 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  package remote
    17  
    18  import (
    19  	"errors"
    20  	"os"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/google/go-containerregistry/pkg/authn"
    25  	"github.com/google/go-containerregistry/pkg/name"
    26  	"github.com/google/go-containerregistry/pkg/v1/remote"
    27  )
    28  
    29  func TestOptions(t *testing.T) {
    30  	repo, err := name.NewRepository("gcr.io/projectsigstore")
    31  	if err != nil {
    32  		t.Errorf("NewRepository() = %v", err)
    33  	}
    34  
    35  	overrideRepo, err := name.NewRepository("gcr.io/distroless")
    36  	if err != nil {
    37  		t.Errorf("NewRepository() = %v", err)
    38  	}
    39  
    40  	otherROpt := []remote.Option{
    41  		remote.WithAuthFromKeychain(authn.DefaultKeychain),
    42  		// TODO(mattmoor): Incorporate user agent.
    43  	}
    44  
    45  	tests := []struct {
    46  		name string
    47  		opts []Option
    48  		want *options
    49  	}{{
    50  		name: "no options",
    51  		want: &options{
    52  			SignatureSuffix:   SignatureTagSuffix,
    53  			AttestationSuffix: AttestationTagSuffix,
    54  			SBOMSuffix:        SBOMTagSuffix,
    55  			TargetRepository:  repo,
    56  			ROpt:              defaultOptions,
    57  		},
    58  	}, {
    59  		name: "signature option",
    60  		opts: []Option{WithSignatureSuffix("pig")},
    61  		want: &options{
    62  			SignatureSuffix:   "pig",
    63  			AttestationSuffix: AttestationTagSuffix,
    64  			SBOMSuffix:        SBOMTagSuffix,
    65  			TargetRepository:  repo,
    66  			ROpt:              defaultOptions,
    67  		},
    68  	}, {
    69  		name: "attestation option",
    70  		opts: []Option{WithAttestationSuffix("pig")},
    71  		want: &options{
    72  			SignatureSuffix:   SignatureTagSuffix,
    73  			AttestationSuffix: "pig",
    74  			SBOMSuffix:        SBOMTagSuffix,
    75  			TargetRepository:  repo,
    76  			ROpt:              defaultOptions,
    77  		},
    78  	}, {
    79  		name: "sbom option",
    80  		opts: []Option{WithSBOMSuffix("pig")},
    81  		want: &options{
    82  			SignatureSuffix:   SignatureTagSuffix,
    83  			AttestationSuffix: AttestationTagSuffix,
    84  			SBOMSuffix:        "pig",
    85  			TargetRepository:  repo,
    86  			ROpt:              defaultOptions,
    87  		},
    88  	}, {
    89  		name: "target repo option",
    90  		opts: []Option{WithTargetRepository(overrideRepo)},
    91  		want: &options{
    92  			SignatureSuffix:   SignatureTagSuffix,
    93  			AttestationSuffix: AttestationTagSuffix,
    94  			SBOMSuffix:        SBOMTagSuffix,
    95  			TargetRepository:  overrideRepo,
    96  			ROpt:              defaultOptions,
    97  		},
    98  	}, {
    99  		name: "remote options option",
   100  		opts: []Option{WithRemoteOptions(otherROpt...)},
   101  		want: &options{
   102  			SignatureSuffix:   SignatureTagSuffix,
   103  			AttestationSuffix: AttestationTagSuffix,
   104  			SBOMSuffix:        SBOMTagSuffix,
   105  			TargetRepository:  repo,
   106  			ROpt:              otherROpt,
   107  		},
   108  	}}
   109  
   110  	for _, test := range tests {
   111  		t.Run(test.name, func(t *testing.T) {
   112  			got := makeOptions(repo, test.opts...)
   113  			test.want.OriginalOptions = test.opts
   114  
   115  			if !reflect.DeepEqual(got, test.want) {
   116  				t.Errorf("makeOptions() = %#v, wanted %#v", got, test.want)
   117  			}
   118  		})
   119  	}
   120  }
   121  
   122  func TestGetEnvTargetRepository(t *testing.T) {
   123  	tests := []struct {
   124  		desc string
   125  
   126  		envVal string
   127  
   128  		want    name.Repository
   129  		wantErr error
   130  	}{
   131  		{
   132  			desc: "good",
   133  
   134  			envVal: "gcr.io/distroless",
   135  
   136  			want: name.MustParseReference("gcr.io/distroless").Context(),
   137  		},
   138  		{
   139  			desc: "bad",
   140  
   141  			envVal:  "bad$repo",
   142  			wantErr: errors.New("parsing $COSIGN_REPOSITORY: repository can only contain the characters `abcdefghijklmnopqrstuvwxyz0123456789_-./`: bad$repo"),
   143  		},
   144  		{
   145  			desc: "empty",
   146  
   147  			envVal: "",
   148  			want:   name.Repository{},
   149  		},
   150  	}
   151  
   152  	for _, tc := range tests {
   153  		t.Run(tc.desc, func(t *testing.T) {
   154  			ev := os.Getenv("COSIGN_REPOSITORY")
   155  			defer os.Setenv("COSIGN_REPOSITORY", ev)
   156  			os.Setenv("COSIGN_REPOSITORY", tc.envVal)
   157  
   158  			got, err := GetEnvTargetRepository()
   159  
   160  			if !errors.Is(err, tc.wantErr) {
   161  				if tc.wantErr == nil || err == nil || tc.wantErr.Error() != err.Error() {
   162  					t.Fatalf("GetEnvTargetRepository() returned error %v, wanted %v", err, tc.wantErr)
   163  				}
   164  				return
   165  			}
   166  
   167  			if tc.want != got {
   168  				t.Errorf("GetEnvTargetRepository() returned %#v, wanted %#v", got, tc.want)
   169  			}
   170  		})
   171  	}
   172  }
   173  

View as plain text