问题描述
我正在使用 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 (将#修改为@)