lib/promscrape/discovery/kubernetes/kubeconfig_test.go: make TestParseKubeConfigSuccess test code easier to follow

This commit is contained in:
Aliaksandr Valialkin 2023-10-25 23:17:18 +02:00
parent eed5206376
commit c22e3e7b1d
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -8,57 +8,39 @@ import (
)
func TestParseKubeConfigSuccess(t *testing.T) {
f := func(configFile string, expectedConfig *kubeConfig) {
t.Helper()
type testCase struct {
name string
kubeConfigFile string
expectedConfig *kubeConfig
config, err := newKubeConfig(configFile)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !reflect.DeepEqual(config, expectedConfig) {
t.Fatalf("unexpected result, got: %v, want: %v", config, expectedConfig)
}
}
var cases = []testCase{
{
name: "token",
kubeConfigFile: "testdata/good_kubeconfig/with_token.yaml",
expectedConfig: &kubeConfig{
server: "http://some-server:8080",
token: "abc",
},
f("testdata/good_kubeconfig/with_token.yaml", &kubeConfig{
server: "http://some-server:8080",
token: "abc",
})
f("testdata/good_kubeconfig/with_tls.yaml", &kubeConfig{
server: "https://localhost:6443",
tlsConfig: &promauth.TLSConfig{
CA: "authority",
Cert: "certificate",
Key: "key",
},
{
name: "cert",
kubeConfigFile: "testdata/good_kubeconfig/with_tls.yaml",
expectedConfig: &kubeConfig{
server: "https://localhost:6443",
tlsConfig: &promauth.TLSConfig{
CA: "authority",
Cert: "certificate",
Key: "key",
},
},
})
f("testdata/good_kubeconfig/with_basic.yaml", &kubeConfig{
server: "http://some-server:8080",
basicAuth: &promauth.BasicAuthConfig{
Password: promauth.NewSecret("secret"),
Username: "user1",
},
{
name: "basic",
kubeConfigFile: "testdata/good_kubeconfig/with_basic.yaml",
expectedConfig: &kubeConfig{
server: "http://some-server:8080",
basicAuth: &promauth.BasicAuthConfig{
Password: promauth.NewSecret("secret"),
Username: "user1",
},
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ac, err := newKubeConfig(tc.kubeConfigFile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !reflect.DeepEqual(ac, tc.expectedConfig) {
t.Fatalf("unexpected result, got: %v, want: %v", ac, tc.expectedConfig)
}
})
}
})
}
func TestParseKubeConfigFail(t *testing.T) {