...

Source file src/github.com/sigstore/cosign/v2/pkg/providers/envvar/env_test.go

Documentation: github.com/sigstore/cosign/v2/pkg/providers/envvar

     1  //
     2  // Copyright 2023 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 envvar
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/sigstore/cosign/v2/pkg/cosign/env"
    24  )
    25  
    26  func TestEnvVar(t *testing.T) {
    27  	ctx := context.Background()
    28  	token := "tacocat"
    29  
    30  	for _, tc := range []struct {
    31  		envmap map[string]string
    32  		want   bool
    33  	}{
    34  		{
    35  			envmap: map[string]string{
    36  				env.VariableSigstoreIDToken.String(): token,
    37  			},
    38  			want: true,
    39  		},
    40  		{
    41  			want: false,
    42  		},
    43  	} {
    44  		t.Run(fmt.Sprint(tc.want), func(t *testing.T) {
    45  			for k, v := range tc.envmap {
    46  				t.Setenv(k, v)
    47  			}
    48  			e := &envvar{}
    49  
    50  			if enabled := e.Enabled(ctx); enabled != tc.want {
    51  				t.Errorf("Enabled: want %t, got %t", tc.want, enabled)
    52  			}
    53  
    54  			got, err := e.Provide(ctx, "")
    55  			if err != nil {
    56  				t.Fatalf("Provide: %v", err)
    57  			}
    58  			want := ""
    59  			if tc.want {
    60  				want = token
    61  			}
    62  			if got != want {
    63  				t.Fatalf("Provide: want %s, got %s", want, got)
    64  			}
    65  		})
    66  	}
    67  }
    68  

View as plain text