英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论