...

Source file src/github.com/sigstore/cosign/v2/pkg/oci/empty/empty_test.go

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

     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 empty
    17  
    18  import (
    19  	"os"
    20  	"testing"
    21  
    22  	"github.com/google/go-containerregistry/pkg/v1/types"
    23  
    24  	"github.com/sigstore/cosign/v2/pkg/cosign/env"
    25  )
    26  
    27  func TestEmptyImage(t *testing.T) {
    28  	tests := []struct {
    29  		name         string
    30  		value        string
    31  		wantMT       types.MediaType
    32  		wantConfigMT types.MediaType
    33  	}{{
    34  		name:         "unset",
    35  		wantMT:       types.OCIManifestSchema1,
    36  		wantConfigMT: types.OCIConfigJSON,
    37  	}, {
    38  		name:         "set false",
    39  		value:        "false",
    40  		wantMT:       types.OCIManifestSchema1,
    41  		wantConfigMT: types.OCIConfigJSON,
    42  	}, {
    43  		name:         "set true",
    44  		value:        "true",
    45  		wantMT:       types.DockerManifestSchema2,
    46  		wantConfigMT: types.DockerConfigJSON,
    47  	}}
    48  
    49  	for _, test := range tests {
    50  		t.Run(test.name, func(t *testing.T) {
    51  			if err := os.Setenv(env.VariableDockerMediaTypes.String(), test.value); err != nil {
    52  				t.Fatalf("Setenv() = %v", err)
    53  			}
    54  
    55  			img := Signatures()
    56  
    57  			if mt, err := img.MediaType(); err != nil {
    58  				t.Errorf("MediaType() = %v", err)
    59  			} else if mt != test.wantMT {
    60  				t.Errorf("MediaType() = %v, wanted %v", mt, test.wantMT)
    61  			}
    62  
    63  			m, err := img.Manifest()
    64  			if err != nil {
    65  				t.Fatalf("ConfigFile() = %v", err)
    66  			}
    67  
    68  			if mt := m.Config.MediaType; mt != test.wantConfigMT {
    69  				t.Errorf("Config.MediaType = %v, wanted %v", mt, test.wantConfigMT)
    70  			}
    71  		})
    72  	}
    73  }
    74  

View as plain text