github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/cmd/syft/cli/options/registry_test.go (about)

     1  package options
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/anchore/stereoscope/pkg/image"
    10  )
    11  
    12  func TestHasNonEmptyCredentials(t *testing.T) {
    13  	tests := []struct {
    14  		username, password, token, cert, key string
    15  		expected                             bool
    16  	}{
    17  
    18  		{
    19  			"", "", "", "", "",
    20  			false,
    21  		},
    22  		{
    23  			"user", "", "", "", "",
    24  			false,
    25  		},
    26  		{
    27  			"", "pass", "", "", "",
    28  			false,
    29  		},
    30  		{
    31  			"", "pass", "tok", "", "",
    32  			true,
    33  		},
    34  		{
    35  			"user", "", "tok", "", "",
    36  			true,
    37  		},
    38  		{
    39  			"", "", "tok", "", "",
    40  			true,
    41  		},
    42  		{
    43  			"user", "pass", "tok", "", "",
    44  			true,
    45  		},
    46  
    47  		{
    48  			"user", "pass", "", "", "",
    49  			true,
    50  		},
    51  		{
    52  			"", "", "", "cert", "key",
    53  			true,
    54  		},
    55  		{
    56  			"", "", "", "cert", "",
    57  			false,
    58  		},
    59  		{
    60  			"", "", "", "", "key",
    61  			false,
    62  		},
    63  	}
    64  
    65  	for _, test := range tests {
    66  		t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) {
    67  			assert.Equal(t, test.expected, hasNonEmptyCredentials(test.username, test.password, test.token, test.cert, test.key))
    68  		})
    69  	}
    70  }
    71  
    72  func Test_registry_ToOptions(t *testing.T) {
    73  	tests := []struct {
    74  		name     string
    75  		input    registry
    76  		expected image.RegistryOptions
    77  	}{
    78  		{
    79  			name:  "no registry options",
    80  			input: registry{},
    81  			expected: image.RegistryOptions{
    82  				Credentials: []image.RegistryCredentials{},
    83  			},
    84  		},
    85  		{
    86  			name: "set InsecureSkipTLSVerify",
    87  			input: registry{
    88  				InsecureSkipTLSVerify: true,
    89  			},
    90  			expected: image.RegistryOptions{
    91  				InsecureSkipTLSVerify: true,
    92  				Credentials:           []image.RegistryCredentials{},
    93  			},
    94  		},
    95  		{
    96  			name: "set InsecureUseHTTP",
    97  			input: registry{
    98  				InsecureUseHTTP: true,
    99  			},
   100  			expected: image.RegistryOptions{
   101  				InsecureUseHTTP: true,
   102  				Credentials:     []image.RegistryCredentials{},
   103  			},
   104  		},
   105  		{
   106  			name: "set all bool options",
   107  			input: registry{
   108  				InsecureSkipTLSVerify: true,
   109  				InsecureUseHTTP:       true,
   110  			},
   111  			expected: image.RegistryOptions{
   112  				InsecureSkipTLSVerify: true,
   113  				InsecureUseHTTP:       true,
   114  				Credentials:           []image.RegistryCredentials{},
   115  			},
   116  		},
   117  		{
   118  			name: "provide all tls configuration",
   119  			input: registry{
   120  				CACert:                "ca.crt",
   121  				InsecureSkipTLSVerify: true,
   122  				Auth: []RegistryCredentials{
   123  					{
   124  						TLSCert: "client.crt",
   125  						TLSKey:  "client.key",
   126  					},
   127  				},
   128  			},
   129  			expected: image.RegistryOptions{
   130  				CAFileOrDir:           "ca.crt",
   131  				InsecureSkipTLSVerify: true,
   132  				Credentials: []image.RegistryCredentials{
   133  					{
   134  						ClientCert: "client.crt",
   135  						ClientKey:  "client.key",
   136  					},
   137  				},
   138  			},
   139  		},
   140  	}
   141  
   142  	for _, test := range tests {
   143  		t.Run(test.name, func(t *testing.T) {
   144  			assert.Equal(t, &test.expected, test.input.ToOptions())
   145  		})
   146  	}
   147  }