对于进行身份验证并返回 Kubernetes 客户端集的 client-go 函数进行单元测试。

huangapple go评论80阅读模式
英文:

Unit testing client-go function that auths and returns a k8s clientset

问题

我正在使用client-go编写最简单的函数,它只执行集群内身份验证并返回指向kubernetes.Clientset对象的指针。

// getInClusterAuth 执行集群内身份验证并返回clientset
func getInClusterAuth() (*kubernetes.Clientset, error) {
	// 创建集群内配置
	config, err := rest.InClusterConfig()
	if err != nil {
		return nil, err
	}
	// 创建clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, err
	}

	return clientset, nil
}

由于这不仅针对外部系统(k8s API服务器)运行,而且还应该在部署后从Pod内部运行,那么适当的单元测试方式是什么?

是否可以在端到端或集成测试中覆盖此情况?

英文:

I am writing the simplest fxn possible using client-go that justs performs in-cluster authentication and returns a pointer to the kubernetes.Clientset object

// getInClusterAuth performs in cluster authentication and returns the clientset
func getInClusterAuth() (*kubernetes.Clientset, error) {
	// creates the in-cluster config
	config, err := rest.InClusterConfig()
	if err != nil {
		return nil, err
	}
	// create the clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		return nil, err
	}

	return clientset, nil
}

Since this is sth that not only does it run against an external system (a k8s api server) but it is also supposed to be running from within a Pod once deployed, what is the appropriate way of unit-testing it?

Could it be that it is an acceptable practice to cover the case in e2e or integration tests?

答案1

得分: 2

你可以在单元测试中使用k8s.io/client-go/kubernetes/fake.NewSimpleClientset来模拟一个clientSet

import (
        "context"
        "testing"

        v1 "k8s.io/api/core/v1"
        "k8s.io/apimachinery/pkg/api/errors"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/client-go/kubernetes/fake"
)

func TestHelloWorld(t *testing.T) {
        clientset := fake.NewSimpleClientset(&v1.Pod{
                ObjectMeta: metav1.ObjectMeta{
                        Name:        "influxdb-v2",
                        Namespace:   "default",
                        Annotations: map[string]string{},
                },
        }, &v1.Pod{
                ObjectMeta: metav1.ObjectMeta{
                        Name:        "chronograf",
                        Namespace:   "default",
                        Annotations: map[string]string{},
                },
        })

        _, err := clientset.CoreV1().Pods(v1.NamespaceDefault).Get(context.Background(), "influxdb-v2", metav1.GetOptions{})
        if err != nil {
                if errors.IsNotFound(err) {
                        t.Error(err)
                } else {
                        t.Errorf("failed to get service from apiserver: %s", err)
                }
        }

        p := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "helloooooo"}}
        _, err = clientset.CoreV1().Pods(v1.NamespaceDefault).Create(context.Background(), p, metav1.CreateOptions{})
        if err != nil {
            t.Fatalf("error injecting pod add: %v", err)
        }
}

这是一个示例:一个fakeclient的案例

英文:

you can use k8s.io/client-go/kubernetes/fake.NewSimpleClientset to mock a clientSet in your unit test .


import (
        "context"
        "testing"

        v1 "k8s.io/api/core/v1"
        "k8s.io/apimachinery/pkg/api/errors"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/client-go/kubernetes/fake"
)

func TestHelloWorld(t *testing.T) {
        clientset := fake.NewSimpleClientset(&v1.Pod{
                ObjectMeta: metav1.ObjectMeta{
                        Name:        "influxdb-v2",
                        Namespace:   "default",
                        Annotations: map[string]string{},
                },
        }, &v1.Pod{
                ObjectMeta: metav1.ObjectMeta{
                        Name:        "chronograf",
                        Namespace:   "default",
                        Annotations: map[string]string{},
                },
        })

        _, err := clientset.CoreV1().Pods(v1.NamespaceDefault).Get(context.Background(), "influxdb-v2", metav1.GetOptions{})
        if err != nil {
                if errors.IsNotFound(err) {
                        t.Error(err)
                } else {
                        t.Errorf("failed to get service from apiserver: %s", err)
                }
        }

        p := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "helloooooo"}}
        _, err = clientset.CoreV1().Pods(v1.NamespaceDefault).Create(context.Background(), p, metav1.CreateOptions{})
        if err != nil {
            t.Fatalf("error injecting pod add: %v", err)
        }
}

this is a sample: a fakeclient case

huangapple
  • 本文由 发表于 2021年8月9日 07:19:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/68705402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定