fix test
This commit is contained in:
1 parent
a75c39bfe7
commit
751b11b5bd
1 file changed
+10
-8
@@ -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())
|
||||
|
||||
Reference in new issue
Block a user