From 321eced643758809d2a5eee01f298355a39631d2 Mon Sep 17 00:00:00 2001 From: Sean Sullivan Date: Tue, 14 Feb 2023 18:00:08 -0800 Subject: [PATCH] Refactor to use openapi fake client Kubernetes-commit: e352d94e45cdd7f92574ac02e37fc3ab78a34798 --- pkg/explain/v2/explain_test.go | 59 +++++++--------------------------- 1 file changed, 12 insertions(+), 47 deletions(-) diff --git a/pkg/explain/v2/explain_test.go b/pkg/explain/v2/explain_test.go index a6d5a3bd..327e3a65 100644 --- a/pkg/explain/v2/explain_test.go +++ b/pkg/explain/v2/explain_test.go @@ -24,7 +24,6 @@ import ( "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/openapi" "k8s.io/client-go/openapi/openapitest" ) @@ -39,22 +38,24 @@ var apiGroupsGVR = schema.GroupVersionResource{ func TestExplainErrors(t *testing.T) { var buf bytes.Buffer - // Validate error when "Paths()" returns error. - err := PrintModelDescription(nil, &buf, &forceErrorClient{}, apiGroupsGVR, false, "unknown-format") - require.ErrorContains(t, err, "failed to fetch list of groupVersions") - // Validate error when GVR is not found in returned paths map. - emptyClient := &emptyPathsClient{} - err = PrintModelDescription(nil, &buf, emptyClient, schema.GroupVersionResource{ + fakeClient := openapitest.NewFakeClient() + err := PrintModelDescription(nil, &buf, fakeClient, schema.GroupVersionResource{ Group: "test0.example.com", Version: "v1", Resource: "doesntmatter", }, false, "unknown-format") require.ErrorContains(t, err, "could not locate schema") + // Validate error when openapi client returns error. + fakeClient.ForcedErr = fmt.Errorf("Always fails") + err = PrintModelDescription(nil, &buf, fakeClient, apiGroupsGVR, false, "unknown-format") + require.ErrorContains(t, err, "failed to fetch list of groupVersions") + // Validate error when GroupVersion "Schema()" call returns error. - fakeClient := &fakeOpenAPIClient{values: make(map[string]openapi.GroupVersion)} - fakeClient.values["apis/test1.example.com/v1"] = &forceErrorGV{} + fakeClient = openapitest.NewFakeClient() + forceErrorGV := openapitest.FakeGroupVersion{ForcedErr: fmt.Errorf("Always fails")} + fakeClient.PathsMap["apis/test1.example.com/v1"] = &forceErrorGV err = PrintModelDescription(nil, &buf, fakeClient, schema.GroupVersionResource{ Group: "test1.example.com", Version: "v1", @@ -63,7 +64,8 @@ func TestExplainErrors(t *testing.T) { require.ErrorContains(t, err, "failed to fetch openapi schema ") // Validate error when returned bytes from GroupVersion "Schema" are invalid. - fakeClient.values["apis/test2.example.com/v1"] = &parseErrorGV{} + parseErrorGV := openapitest.FakeGroupVersion{GVSpec: []byte(``)} + fakeClient.PathsMap["apis/test2.example.com/v1"] = &parseErrorGV err = PrintModelDescription(nil, &buf, fakeClient, schema.GroupVersionResource{ Group: "test2.example.com", Version: "v1", @@ -113,40 +115,3 @@ func TestExplainOpenAPIClient(t *testing.T) { require.NoError(t, err) require.Equal(t, expectedContext, actualContext) } - -// forceErrorClient always returns an error for "Paths()". -type forceErrorClient struct{} - -func (f *forceErrorClient) Paths() (map[string]openapi.GroupVersion, error) { - return nil, fmt.Errorf("Always fails") -} - -// emptyPathsClient returns an empty map for "Paths()". -type emptyPathsClient struct{} - -func (f *emptyPathsClient) Paths() (map[string]openapi.GroupVersion, error) { - return map[string]openapi.GroupVersion{}, nil -} - -// fakeOpenAPIClient returns hard-coded map for "Paths()". -type fakeOpenAPIClient struct { - values map[string]openapi.GroupVersion -} - -func (f *fakeOpenAPIClient) Paths() (map[string]openapi.GroupVersion, error) { - return f.values, nil -} - -// forceErrorGV always returns an error for "Schema()". -type forceErrorGV struct{} - -func (f *forceErrorGV) Schema(contentType string) ([]byte, error) { - return nil, fmt.Errorf("Always fails") -} - -// parseErrorGV always returns invalid JSON for "Schema()". -type parseErrorGV struct{} - -func (f *parseErrorGV) Schema(contentType string) ([]byte, error) { - return []byte(``), nil -}