test(contrib): add unit test for contrib/config/k8s (#2140)

* test(contrib): add unit test for contrib/config/k8s

* style(contrib): import style change

Co-authored-by: rogerogers <rogers@rogerogers.com>
pull/2151/head
rogerogers 2 years ago committed by GitHub
parent ed8b2352b7
commit 8c0a354797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 168
      contrib/config/kubernetes/config_test.go

@ -1,18 +1,44 @@
package kubernetes package kubernetes
import ( import (
"context"
"log" "log"
"path/filepath" "path/filepath"
"reflect"
"strings"
"testing" "testing"
"github.com/go-kratos/kratos/v2/config" "github.com/go-kratos/kratos/v2/config"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir" "k8s.io/client-go/util/homedir"
) )
const (
testKey = "test_config.json"
namespace = "default"
name = "test"
)
var (
keyPath = strings.Join([]string{namespace, name, testKey}, "/")
objectMeta = metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
"app": "test",
},
}
)
func TestSource(t *testing.T) { func TestSource(t *testing.T) {
home := homedir.HomeDir() home := homedir.HomeDir()
s := NewSource( s := NewSource(
Namespace("mesh"), Namespace("default"),
LabelSelector(""), LabelSelector(""),
KubeConfig(filepath.Join(home, ".kube", "config")), KubeConfig(filepath.Join(home, ".kube", "config")),
) )
@ -40,3 +66,143 @@ func ExampleNewSource() {
log.Panic(err) log.Panic(err)
} }
} }
func TestConfig(t *testing.T) {
restConfig, err := rest.InClusterConfig()
home := homedir.HomeDir()
options := []Option{
Namespace(namespace),
LabelSelector("app=test"),
}
if err != nil {
kubeconfig := filepath.Join(home, ".kube", "config")
restConfig, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
t.Fatal(err)
}
options = append(options, KubeConfig(kubeconfig))
}
clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
t.Fatal(err)
}
clientSetConfigMaps := clientSet.CoreV1().ConfigMaps(namespace)
source := NewSource(options...)
if _, err = clientSetConfigMaps.Create(context.Background(), &v1.ConfigMap{
ObjectMeta: objectMeta,
Data: map[string]string{
testKey: "test config",
},
}, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
defer func() {
if err = clientSetConfigMaps.Delete(context.Background(), name, metav1.DeleteOptions{}); err != nil {
t.Error(err)
}
}()
kvs, err := source.Load()
if err != nil {
t.Fatal(err)
}
if len(kvs) != 1 || kvs[0].Key != keyPath || string(kvs[0].Value) != "test config" {
t.Fatal("config error")
}
w, err := source.Watch()
if err != nil {
t.Fatal(err)
}
defer func() {
_ = w.Stop()
}()
// create also produce an event, discard it
if _, err = w.Next(); err != nil {
t.Fatal(err)
}
if _, err = clientSetConfigMaps.Update(context.Background(), &v1.ConfigMap{
ObjectMeta: objectMeta,
Data: map[string]string{
testKey: "new config",
},
}, metav1.UpdateOptions{}); err != nil {
t.Error(err)
}
if kvs, err = w.Next(); err != nil {
t.Fatal(err)
}
if len(kvs) != 1 || kvs[0].Key != keyPath || string(kvs[0].Value) != "new config" {
t.Fatal("config error")
}
}
func TestExtToFormat(t *testing.T) {
restConfig, err := rest.InClusterConfig()
home := homedir.HomeDir()
options := []Option{
Namespace(namespace),
LabelSelector("app=test"),
}
if err != nil {
kubeconfig := filepath.Join(home, ".kube", "config")
restConfig, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
t.Fatal(err)
}
options = append(options, KubeConfig(kubeconfig))
}
clientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
t.Fatal(err)
}
clientSetConfigMaps := clientSet.CoreV1().ConfigMaps(namespace)
tc := `{"a":1}`
if _, err = clientSetConfigMaps.Create(context.Background(), &v1.ConfigMap{
ObjectMeta: objectMeta,
Data: map[string]string{
testKey: tc,
},
}, metav1.CreateOptions{}); err != nil {
t.Fatal(err)
}
defer func() {
if err = clientSetConfigMaps.Delete(context.Background(), name, metav1.DeleteOptions{}); err != nil {
t.Error(err)
}
}()
source := NewSource(options...)
if err != nil {
t.Fatal(err)
}
kvs, err := source.Load()
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(len(kvs), 1) {
t.Errorf("len(kvs) = %d", len(kvs))
}
if !reflect.DeepEqual(keyPath, kvs[0].Key) {
t.Errorf("kvs[0].Key is %s", kvs[0].Key)
}
if !reflect.DeepEqual(tc, string(kvs[0].Value)) {
t.Errorf("kvs[0].Value is %s", kvs[0].Value)
}
if !reflect.DeepEqual("json", kvs[0].Format) {
t.Errorf("kvs[0].Format is %s", kvs[0].Format)
}
}

Loading…
Cancel
Save