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