如何解决如果使用自定义客户端,如何为 Kubernetes client-go 编写单元测试?
我正在使用 client-go 来读取 Kubernetes 对象。有一些电话我也在阅读自定义资源。以下是代码片段:
func GetComCR(kubeClient *v1alpha1.ExampleV1Alpha1Client) []byte {
compute,err := kubeClient.ComputeMons("default").List(context.Todo(),Metav1.ListOptions{})
if err != nil {
panic(err)
}
json_data,err := json.Marshal(compute)
if err != nil {
panic(err)
}
fmt.Println("compute_data: ",string(json_data))
return json_data
}
func GetClusterclient() *v1alpha1.ExampleV1Alpha1Client {
config := GetClusterConfig()
config.ContentConfig.GroupVersion = &schema.GroupVersion{Group: "acme.com",Version: "v1alpha1"}
config.APIPath = "/apis"
config.NegotiatedSerializer = scheme.Codecs.WithoutConversion()
config.UserAgent = rest.DefaultKubernetesUserAgent()
monitoringv1alpha1.AddToScheme(scheme.Scheme)
clientSet,_ := v1alpha1.NewForConfig(config)
return clientSet
}
func (this *ComputeController) Get() {
kubeclient := GetClusterclient()
json_data := GetComCR(kubeclient)
var resp interface{}
json.Unmarshal(json_data,&resp)
this.Data["json"] = resp
this.ServeJSON()
this.StopRun()
}
type ExampleV1Alpha1Client struct {
restClient rest.Interface
}
现在我想在这里为 GetComCR 方法编写 UT。我在这里找到了一个关于 UT 的主题:
How to write simple tests for client-go using a fake client?
但这对我没有帮助。它询问 kubeClient kubernetes.Interface 但在我的例子中是 ExampleV1Alpha1Client。
有人可以帮助我如何伪造客户吗?
我尝试了以下测试:
func TestCRRead(t *testing.T) {
kubeClient := fake.NewSimpleClientset()
resp := GetComCR(kubeClient)
logs.Info("resp >>>",resp)
}
但编译失败,因为 NewSimpleClientset 给出了 *fake.Clientset。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。