在我的代码中,我有一个查询函数,使用Factory.NewBuilder().Do()ink8s.io/kubectl/pkg/cmd/util/factory.go来查询 K8S 中的 CR。
现在我想用一些 UT 来测试我的查询功能,所以我使用TestFactoryink8s.io/kubectl/pkg/cmd/testing/fake.go来查询一些假资源,但是:
FakeDynamicClient无法被找到NewBuilder().Do()UnstructuredClientfake Response,还提示“错误:服务器没有资源类型“myCRDresource””import (
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
...
)
//create my fake CR lists
getFakeClusterVersionList := func(clusterVersions ...*appsv1alpha1.ClusterVersion) *appsv1alpha1.ClusterVersionList {
res := &appsv1alpha1.ClusterVersionList{
ListMeta: metav1.ListMeta{
ResourceVersion: "1",
},
Items: make([]appsv1alpha1.ClusterVersion, 0),
}
for _, cv := range clusterVersions {
res.Items = append(res.Items, *cv.DeepCopy())
}
return res
}
cvlists := getFakeClusterVersionList(cv1, cv2)
tf = cmdtesting.NewTestFactory()
tf.UnstructuredClient = &fake.RESTClient{
NegotiatedSerializer: resource.UnstructuredPlusDefaultContentConfig().NegotiatedSerializer,
Resp: &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: cmdtesting.ObjBody(codec, cvlists)},
}
// do my query but fail
...
Run Code Online (Sandbox Code Playgroud)
我使用的测试工厂
type TestFactory struct {
cmdutil.Factory
kubeConfigFlags *genericclioptions.TestConfigFlags
Client RESTClient
ScaleGetter scaleclient.ScalesGetter
UnstructuredClient RESTClient
ClientConfigVal *restclient.Config
FakeDynamicClient *fakedynamic.FakeDynamicClient
tempConfigFile *os.File
UnstructuredClientForMappingFunc resource.FakeClientFunc
OpenAPISchemaFunc func() (openapi.Resources, error)
FakeOpenAPIGetter discovery.OpenAPISchemaInterface
}
Run Code Online (Sandbox Code Playgroud)
我想为我的 CRS 查询做一些 UT,并且不需要启动真正的 K8S 集群。
| 归档时间: |
|
| 查看次数: |
125 次 |
| 最近记录: |