github.com/metasources/buildx@v0.0.0-20230418141019-7aa1459cedea/internal/config/registry_test.go (about) 1 package config 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 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 53 for _, test := range tests { 54 t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) { 55 assert.Equal(t, test.expected, hasNonEmptyCredentials(test.username, test.password, test.token)) 56 }) 57 } 58 } 59 60 func Test_registry_ToOptions(t *testing.T) { 61 tests := []struct { 62 name string 63 input registry 64 expected image.RegistryOptions 65 }{ 66 { 67 name: "no registry options", 68 input: registry{}, 69 expected: image.RegistryOptions{ 70 Credentials: []image.RegistryCredentials{}, 71 }, 72 }, 73 { 74 name: "set InsecureSkipTLSVerify", 75 input: registry{ 76 InsecureSkipTLSVerify: true, 77 }, 78 expected: image.RegistryOptions{ 79 InsecureSkipTLSVerify: true, 80 Credentials: []image.RegistryCredentials{}, 81 }, 82 }, 83 { 84 name: "set InsecureUseHTTP", 85 input: registry{ 86 InsecureUseHTTP: true, 87 }, 88 expected: image.RegistryOptions{ 89 InsecureUseHTTP: true, 90 Credentials: []image.RegistryCredentials{}, 91 }, 92 }, 93 { 94 name: "set all bool options", 95 input: registry{ 96 InsecureSkipTLSVerify: true, 97 InsecureUseHTTP: true, 98 }, 99 expected: image.RegistryOptions{ 100 InsecureSkipTLSVerify: true, 101 InsecureUseHTTP: true, 102 Credentials: []image.RegistryCredentials{}, 103 }, 104 }, 105 } 106 107 for _, test := range tests { 108 t.Run(test.name, func(t *testing.T) { 109 assert.Equal(t, &test.expected, test.input.ToOptions()) 110 }) 111 } 112 }