From 751b11b5bdca673cfdf1e418e056943c0f9fa171 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Tue, 17 Dec 2024 08:00:55 +0300 Subject: [PATCH] fix test --- pkg/tests/apis/helper.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/pkg/tests/apis/helper.go b/pkg/tests/apis/helper.go index 9bfed788070..50bad1e1da7 100644 --- a/pkg/tests/apis/helper.go +++ b/pkg/tests/apis/helper.go @@ -13,7 +13,6 @@ import ( "testing" "time" - "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/api/errors" @@ -27,6 +26,7 @@ import ( "k8s.io/client-go/discovery" "k8s.io/client-go/dynamic" "k8s.io/client-go/rest" + "k8s.io/kube-openapi/pkg/spec3" "github.com/grafana/grafana/pkg/apimachinery/identity" "github.com/grafana/grafana/pkg/infra/localcache" @@ -184,23 +184,25 @@ func (c *K8sTestHelper) VerifyStaticOpenAPISpec(gv schema.GroupVersion, fpath st require.NotNil(c.t, rsp.Response) require.Equal(c.t, 200, rsp.Response.StatusCode) - var prettyJSON bytes.Buffer - err := json.Indent(&prettyJSON, rsp.Body, "", " ") + spec := spec3.OpenAPI{} + err := spec.UnmarshalJSON(rsp.Body) require.NoError(c.t, err) - pretty := prettyJSON.String() // nolint:gosec // We can ignore the gosec G304 warning since this is a test and the function is only called with explicit paths body, err := os.ReadFile(fpath) if err == nil { - if !assert.JSONEq(c.t, pretty, string(body)) { - if diff := cmp.Diff(pretty, string(body)); diff != "" { - err = fmt.Errorf("body mismatch (-want +got):\n%s", diff) - } + specSaved := spec3.OpenAPI{} + err := specSaved.UnmarshalJSON(body) + assert.NoError(c.t, err, "error reading") + + if !assert.Equal(c.t, specSaved, spec) { + err = fmt.Errorf("mismatch") } } if err != nil { + pretty, err := json.MarshalIndent(spec, "", " ") e2 := os.WriteFile(fpath, []byte(pretty), 0644) if e2 != nil { c.t.Errorf("error writing file: %s", e2.Error())