Skip to content

Commit

Permalink
Fix tests after openapi client merge (#269)
Browse files Browse the repository at this point in the history
* Fix test and bug

* Make tests work
  • Loading branch information
malcolmholmes authored Nov 23, 2023
1 parent 91aa73b commit 2d61537
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 47 deletions.
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ go 1.20
require (
github.com/fatih/color v1.15.0
github.com/gdamore/tcell v1.4.0
github.com/go-chi/chi v1.5.5
github.com/go-clix/cli v0.2.0
github.com/go-openapi/errors v0.20.4
github.com/go-openapi/runtime v0.26.0
github.com/gobwas/glob v0.2.3
github.com/google/go-jsonnet v0.20.0
github.com/grafana/grafana-openapi-client-go v0.0.0-20231016093917-88fc2f84f257
github.com/gorilla/websocket v1.5.1
github.com/grafana/grafana-openapi-client-go v0.0.0-20231123143558-91f2985bb3ef
github.com/grafana/synthetic-monitoring-agent v0.16.5
github.com/grafana/synthetic-monitoring-api-go-client v0.7.0
github.com/grafana/tanka v0.25.0
github.com/pmezard/go-difflib v1.0.0
github.com/rivo/tview v0.0.0-20200818120338-53d50e499bf9
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/unknwon/log v0.0.0-20200308114134-929b1006e34a
golang.org/x/crypto v0.14.0
gopkg.in/fsnotify.v1 v1.4.7
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -34,10 +37,10 @@ require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.4 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/runtime v0.26.0 // indirect
github.com/go-openapi/spec v0.20.9 // indirect
github.com/go-openapi/strfmt v0.21.7 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
Expand All @@ -46,7 +49,6 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
Expand All @@ -64,6 +66,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/rivo/uniseg v0.1.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
Expand Down
246 changes: 243 additions & 3 deletions go.sum

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions pkg/grafana/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestDatasources(t *testing.T) {

require.Equal(t, "grizzly.grafana.com/v1alpha1", resource.APIVersion())
require.Equal(t, "AppDynamics", resource.Name())
require.Len(t, resource.Spec(), 12)
require.Len(t, resource.Spec(), 13)
})

t.Run("get remote datasource - not found", func(t *testing.T) {
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestDatasources(t *testing.T) {
t.Run("put remote datasource - update", func(t *testing.T) {
ds.SetSpecString("type", "new-type")

err := handler.Add(*ds)
err := handler.Update(nil, *ds)
require.NoError(t, err)

updatedDS, err := handler.GetByUID("appdynamics")
Expand All @@ -91,10 +91,9 @@ func TestDatasources(t *testing.T) {
resource.SetSpecString("name", "AppDynamics")

err = handler.Add(resource)
apiError := err.(APIResponse)

var non200ResponseErr ErrNon200Response
require.ErrorAs(t, err, &non200ResponseErr)
require.Equal(t, 409, non200ResponseErr.Response.StatusCode)
require.Equal(t, 409, apiError.Code())
})

t.Run("Check getUID is functioning correctly", func(t *testing.T) {
Expand Down
23 changes: 4 additions & 19 deletions pkg/grafana/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package grafana

import (
"fmt"
"io"
"net/http"
"strings"
)

Expand All @@ -14,21 +12,8 @@ func (e ErrUidsMissing) Error() string {
return fmt.Sprintf("One or more dashboards have no UID set. UIDs are required for Grizzly to operate properly:\n - %s", strings.Join(e, "\n - "))
}

type ErrNon200Response struct {
Type string
UID string
Response *http.Response
}

func NewErrNon200Response(typ, uid string, resp *http.Response) ErrNon200Response {
return ErrNon200Response{
Type: typ,
UID: uid,
Response: resp,
}
}
func (e ErrNon200Response) Error() string {
body, _ := io.ReadAll(e.Response.Body)
status := e.Response.Status
return fmt.Sprintf("Non-200 response from Grafana while applying %s %s: %s %s", e.Type, e.UID, status, string(body))
type APIResponse interface {
Code() int
Error() string
String() string
}
8 changes: 4 additions & 4 deletions pkg/grafana/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ func getRemoteFolder(client *gclient.GrafanaHTTPAPI, uid string) (*grizzly.Resou
params := folders.NewGetFolderByUIDParams().WithFolderUID(uid)
folderOk, err := client.Folders.GetFolderByUID(params, nil)
if err != nil {
var gErr *folders.GetFolderByUIDNotFound
if errors.As(err, &gErr) {
return nil, fmt.Errorf("couldn't fetch folder '%s' from remote: %w", uid, grizzly.ErrNotFound)
var gErrNotFound *folders.GetFolderByUIDNotFound
var gErrForbidden *folders.GetFolderByUIDForbidden
if errors.As(err, &gErrNotFound) || errors.As(err, &gErrForbidden) {
return nil, fmt.Errorf("Couldn't fetch folder '%s' from remote: %w", uid, grizzly.ErrNotFound)
}
return nil, err
}
Expand Down Expand Up @@ -118,7 +119,6 @@ func putFolder(client *gclient.GrafanaHTTPAPI, resource grizzly.Resource) error

body := models.UpdateFolderCommand{
Title: folder.Title,
UID: folder.UID,
}
params := folders.NewUpdateFolderParams().WithBody(&body)
_, err = client.Folders.UpdateFolder(params, nil)
Expand Down
18 changes: 7 additions & 11 deletions pkg/grafana/folders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"github.com/go-openapi/runtime"
"github.com/grafana/grizzly/pkg/grizzly"
. "github.com/grafana/grizzly/pkg/internal/testutil"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestFolders(t *testing.T) {

t.Run("get remote folder - not found", func(t *testing.T) {
_, err := handler.GetByUID("dummy")
require.ErrorContains(t, err, "couldn't fetch folder 'dummy' from remote: not found")
require.ErrorContains(t, err, "Couldn't fetch folder 'dummy' from remote: not found")
})

t.Run("get folders list", func(t *testing.T) {
Expand Down Expand Up @@ -66,16 +67,12 @@ func TestFolders(t *testing.T) {
require.NotNil(t, remoteFolder)
require.Equal(t, "/dashboards/f/newFolder/new-folder", remoteFolder.Spec()["url"])

t.Run("put remote folder - update uid", func(t *testing.T) {
t.Run("conflict: put remote folder - update uid", func(t *testing.T) {
remoteFolder.SetSpecString("uid", "dummyUid")

err := handler.Add(*remoteFolder)
require.NoError(t, err)

updatedFolder, err := handler.GetByUID("dummyUid")
require.NoError(t, err)

require.Equal(t, "dummyUid", updatedFolder.Spec()["uid"])
apiError := err.(APIResponse)
require.Equal(t, 409, apiError.Code())
})
})

Expand All @@ -91,8 +88,7 @@ func TestFolders(t *testing.T) {
resource.SetSpecString("title", "Azure Data Explorer")

err = handler.Add(resource)
var non200ResponseErr ErrNon200Response
require.ErrorAs(t, err, &non200ResponseErr)
require.Equal(t, 409, non200ResponseErr.Response.StatusCode)
apiError := err.(*runtime.APIError)
require.Equal(t, 412, apiError.Code)
})
}
2 changes: 1 addition & 1 deletion pkg/grafana/testdata/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM grafana/grafana:9.1.5
FROM grafana/grafana:10.2.0

ADD custom.ini /etc/grafana/grafana.ini
ADD provisioning /etc/grafana/provisioning
Expand Down

0 comments on commit 2d61537

Please sign in to comment.