github.com/anchore/syft@v1.38.2/test/cli/config_test.go (about) 1 package cli 2 3 import ( 4 "os" 5 "path/filepath" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 "go.yaml.in/yaml/v3" 11 ) 12 13 func Test_configLoading(t *testing.T) { 14 cwd, err := os.Getwd() 15 require.NoError(t, err) 16 defer func() { require.NoError(t, os.Chdir(cwd)) }() 17 18 configsDir := filepath.Join(cwd, "test-fixtures", "configs") 19 path := func(path string) string { 20 return filepath.Join(configsDir, filepath.Join(strings.Split(path, "/")...)) 21 } 22 23 type creds struct { 24 Authority string `yaml:"authority"` 25 } 26 27 type registry struct { 28 Credentials []creds `yaml:"auth"` 29 } 30 31 type config struct { 32 Registry registry `yaml:"registry"` 33 } 34 35 tests := []struct { 36 name string 37 home string 38 cwd string 39 args []string 40 expected []creds 41 err string 42 }{ 43 { 44 name: "single explicit config", 45 home: configsDir, 46 cwd: cwd, 47 args: []string{ 48 "-c", 49 path("dir1/.syft.yaml"), 50 }, 51 expected: []creds{ 52 { 53 Authority: "dir1-authority", 54 }, 55 }, 56 }, 57 { 58 name: "multiple explicit config", 59 home: configsDir, 60 cwd: cwd, 61 args: []string{ 62 "-c", 63 path("dir1/.syft.yaml"), 64 "-c", 65 path("dir2/.syft.yaml"), 66 }, 67 expected: []creds{ 68 { 69 Authority: "dir1-authority", 70 }, 71 { 72 Authority: "dir2-authority", 73 }, 74 }, 75 }, 76 { 77 name: "empty profile override", 78 home: configsDir, 79 cwd: cwd, 80 args: []string{ 81 "-c", 82 path("dir1/.syft.yaml"), 83 "-c", 84 path("dir2/.syft.yaml"), 85 "--profile", 86 "no-auth", 87 }, 88 expected: []creds{}, 89 }, 90 { 91 name: "no profiles defined", 92 home: configsDir, 93 cwd: configsDir, 94 args: []string{ 95 "--profile", 96 "invalid", 97 }, 98 err: "not found in any configuration files", 99 }, 100 { 101 name: "invalid profile name", 102 home: configsDir, 103 cwd: cwd, 104 args: []string{ 105 "-c", 106 path("dir1/.syft.yaml"), 107 "-c", 108 path("dir2/.syft.yaml"), 109 "--profile", 110 "alt", 111 }, 112 err: "profile not found", 113 }, 114 { 115 name: "explicit with profile override", 116 home: configsDir, 117 cwd: cwd, 118 args: []string{ 119 "-c", 120 path("dir1/.syft.yaml"), 121 "-c", 122 path("dir2/.syft.yaml"), 123 "--profile", 124 "alt-auth", 125 }, 126 expected: []creds{ 127 { 128 Authority: "dir1-alt-authority", // dir1 is still first 129 }, 130 { 131 Authority: "dir2-alt-authority", 132 }, 133 }, 134 }, 135 { 136 name: "single in cwd", 137 home: configsDir, 138 cwd: path("dir2"), 139 args: []string{}, 140 expected: []creds{ 141 { 142 Authority: "dir2-authority", 143 }, 144 }, 145 }, 146 { 147 name: "single in home", 148 home: path("dir2"), 149 cwd: configsDir, 150 args: []string{}, 151 expected: []creds{ 152 { 153 Authority: "dir2-authority", 154 }, 155 }, 156 }, 157 { 158 name: "inherited in cwd", 159 home: path("dir1"), 160 cwd: path("dir2"), 161 args: []string{}, 162 expected: []creds{ 163 { 164 Authority: "dir2-authority", // dir2 is in cwd, giving higher priority 165 }, 166 { 167 Authority: "dir1-authority", // home has "lower priority and should be after" 168 }, 169 }, 170 }, 171 { 172 name: "inherited profile override", 173 home: path("dir1"), 174 cwd: path("dir2"), 175 args: []string{ 176 "--profile", 177 "alt-auth", 178 }, 179 expected: []creds{ 180 { 181 Authority: "dir2-alt-authority", // dir2 is in cwd, giving higher priority 182 }, 183 { 184 Authority: "dir1-alt-authority", // dir1 is home, lower priority 185 }, 186 }, 187 }, 188 } 189 190 for _, test := range tests { 191 t.Run(test.name, func(t *testing.T) { 192 require.NoError(t, os.Chdir(test.cwd)) 193 defer func() { require.NoError(t, os.Chdir(cwd)) }() 194 env := map[string]string{ 195 "HOME": test.home, 196 "XDG_CONFIG_HOME": test.home, 197 } 198 _, stdout, stderr := runSyft(t, env, append([]string{"config", "--load"}, test.args...)...) 199 if test.err != "" { 200 require.Contains(t, stderr, test.err) 201 return 202 } else { 203 require.Empty(t, stderr) 204 } 205 got := config{} 206 err = yaml.NewDecoder(strings.NewReader(stdout)).Decode(&got) 207 require.NoError(t, err) 208 require.Equal(t, test.expected, got.Registry.Credentials) 209 }) 210 } 211 }