VictoriaMetrics/lib/promscrape/discovery/kubernetes/kubeconfig_test.go
Aliaksandr Valialkin eb2a7ced9c
lib/promauth: properly parse string contents for ca, cert and key fields at tls_config
Previously yaml parser wasn't accepting string values for these fields,
because it was mistakenly expecting a list of uint8 values instead.
2023-10-27 14:23:15 +02:00

75 lines
1.8 KiB
Go

package kubernetes
import (
"reflect"
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
)
func TestParseKubeConfigSuccess(t *testing.T) {
type testCase struct {
name string
kubeConfigFile string
expectedConfig *kubeConfig
}
var cases = []testCase{
{
name: "token",
kubeConfigFile: "testdata/good_kubeconfig/with_token.yaml",
expectedConfig: &kubeConfig{
server: "http://some-server:8080",
token: "abc",
},
},
{
name: "cert",
kubeConfigFile: "testdata/good_kubeconfig/with_tls.yaml",
expectedConfig: &kubeConfig{
server: "https://localhost:6443",
tlsConfig: &promauth.TLSConfig{
CA: "authority",
Cert: "certificate",
Key: "key",
},
},
},
{
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) {
f := func(name, kubeConfigFile string) {
t.Helper()
t.Run(name, func(t *testing.T) {
if _, err := newKubeConfig(kubeConfigFile); err == nil {
t.Fatalf("unexpected result for config file: %s, must return error", kubeConfigFile)
}
})
}
f("unsupported options", "testdata/bad_kubeconfig/unsupported_fields")
f("missing server address", "testdata/bad_kubeconfig/missing_server.yaml")
}