github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/image/registry/google/google_test.go (about)

     1  package google
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/GoogleCloudPlatform/docker-credential-gcr/store"
     9  
    10  	"github.com/devseccon/trivy/pkg/fanal/types"
    11  )
    12  
    13  func TestCheckOptions(t *testing.T) {
    14  	var tests = map[string]struct {
    15  		domain  string
    16  		opt     types.RegistryOptions
    17  		gcr     *Registry
    18  		wantErr error
    19  	}{
    20  		"InvalidURL": {
    21  			domain:  "alpine:3.9",
    22  			wantErr: types.InvalidURLPattern,
    23  		},
    24  		"NoOption": {
    25  			domain: "gcr.io",
    26  			gcr:    &Registry{domain: "gcr.io"},
    27  		},
    28  		"CredOption": {
    29  			domain: "gcr.io",
    30  			opt:    types.RegistryOptions{GCPCredPath: "/path/to/file.json"},
    31  			gcr: &Registry{
    32  				domain: "gcr.io",
    33  				Store:  store.NewGCRCredStore("/path/to/file.json"),
    34  			},
    35  		},
    36  	}
    37  
    38  	for testname, v := range tests {
    39  		g := &Registry{}
    40  		err := g.CheckOptions(v.domain, v.opt)
    41  		if v.wantErr != nil {
    42  			if err == nil {
    43  				t.Errorf("%s : expected error but no error", testname)
    44  				continue
    45  			}
    46  			if !errors.Is(err, v.wantErr) {
    47  				t.Errorf("[%s]\nexpected error based on %v\nactual : %v", testname, v.wantErr, err)
    48  			}
    49  			continue
    50  		}
    51  		if !reflect.DeepEqual(v.gcr, g) {
    52  			t.Errorf("[%s]\nexpected : %v\nactual : %v", testname, v.gcr, g)
    53  		}
    54  	}
    55  }