Skip to content

Commit

Permalink
Slightly better tests for datasources-related client functions
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Dec 20, 2021
1 parent c64e208 commit ce24cc0
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 52 deletions.
65 changes: 17 additions & 48 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,7 @@ func (client *Client) CreateFolder(ctx context.Context, name string) (*Folder, e
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return nil, fmt.Errorf("could not create folder: %s (HTTP status %d)", body, resp.StatusCode)
return nil, client.httpError(resp)
}

var folder Folder
Expand All @@ -157,12 +152,7 @@ func (client *Client) GetFolderByTitle(ctx context.Context, title string) (*Fold
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return nil, fmt.Errorf("could list folders: %s (HTTP status %d)", body, resp.StatusCode)
return nil, client.httpError(resp)
}

var folders []Folder
Expand All @@ -189,12 +179,7 @@ func (client *Client) GetAlertChannelByName(ctx context.Context, name string) (*
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return nil, fmt.Errorf("could lookup alert channels: %s (HTTP status %d)", body, resp.StatusCode)
return nil, client.httpError(resp)
}

var channels []alert.Channel
Expand Down Expand Up @@ -234,12 +219,7 @@ func (client *Client) UpsertDashboard(ctx context.Context, folder *Folder, build
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

return nil, fmt.Errorf("could not create dashboard: %s", body)
return nil, client.httpError(resp)
}

var model Dashboard
Expand All @@ -263,12 +243,7 @@ func (client *Client) DeleteDashboard(ctx context.Context, uid string) error {
return ErrDashboardNotFound
}
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

return fmt.Errorf("could not delete dashboard: %s", body)
return client.httpError(resp)
}

return nil
Expand Down Expand Up @@ -301,12 +276,7 @@ func (client *Client) UpsertDatasource(ctx context.Context, datasource datasourc
defer func() { _ = resp.Body.Close() }()

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

return fmt.Errorf("could not create datasource: %s", body)
return client.httpError(resp)
}

return nil
Expand All @@ -330,12 +300,7 @@ func (client *Client) DeleteDatasource(ctx context.Context, name string) error {
return ErrDatasourceNotFound
}
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

return fmt.Errorf("could not delete datasource: %s", body)
return client.httpError(resp)
}

return nil
Expand All @@ -355,12 +320,7 @@ func (client *Client) getDatasourceIDByName(ctx context.Context, name string) (i
}

if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}

return 0, fmt.Errorf("could not query datasources: %s (HTTP status %d)", body, resp.StatusCode)
return 0, client.httpError(resp)
}

response := struct {
Expand All @@ -373,6 +333,15 @@ func (client *Client) getDatasourceIDByName(ctx context.Context, name string) (i
return response.ID, nil
}

func (client Client) httpError(resp *http.Response) error {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

return fmt.Errorf("could not query grafana: %s (HTTP status %d)", body, resp.StatusCode)
}

func (client Client) delete(ctx context.Context, path string) (*http.Response, error) {
request, err := http.NewRequestWithContext(ctx, http.MethodDelete, client.url(path), nil)
if err != nil {
Expand Down
153 changes: 149 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,21 +335,166 @@ func TestDeletingANonExistingDashboardReturnsSpecificError(t *testing.T) {
req.Equal(ErrDashboardNotFound, err)
}

func TestDatasourceUpsertCanFail(t *testing.T) {
func TestDatasourceUpsertCanCreateANewDatasource(t *testing.T) {
req := require.New(t)
datasourcePosted := false

datasource := prometheus.New("name", "address")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// "Datasource ID by name" call
if strings.HasPrefix(r.URL.Path, "/api/datasources/id/") {
w.WriteHeader(http.StatusNotFound)
return
}

datasourcePosted = true

req.Equal(http.MethodPost, r.Method)
req.Equal("/api/datasources", r.URL.Path)

w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{}`)
}))
defer ts.Close()

client := NewClient(http.DefaultClient, ts.URL)

err := client.UpsertDatasource(context.TODO(), datasource)

req.NoError(err)
req.True(datasourcePosted)
}

func TestDatasourceUpsertCanUpdateADatasource(t *testing.T) {
req := require.New(t)
datasourceUpdated := false

datasource := prometheus.New("name", "address")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// "Datasource ID by name" call
if strings.HasPrefix(r.URL.Path, "/api/datasources/id/") {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"id": 2}`)
return
}

datasourceUpdated = true

req.Equal(http.MethodPut, r.Method)
req.Equal("/api/datasources/2", r.URL.Path)

w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{}`)
}))
defer ts.Close()

client := NewClient(http.DefaultClient, ts.URL)

err := client.UpsertDatasource(context.TODO(), datasource)

req.NoError(err)
req.True(datasourceUpdated)
}

func TestUpsertDatasourceForwardsErrorsOnFailure(t *testing.T) {
req := require.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// "Datasource ID by name" call
if strings.HasPrefix(r.URL.Path, "/api/datasources/id/") {
w.WriteHeader(http.StatusNotFound)
return
}

w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, `{
"message": "The datasource creation failed for some reason",
"status": "version-mismatch"
"message": "something when wrong"
}`)
}))
defer ts.Close()

client := NewClient(http.DefaultClient, ts.URL)

err := client.UpsertDatasource(context.TODO(), datasource)
err := client.UpsertDatasource(context.TODO(), prometheus.New("name", "address"))

req.Error(err)
req.Contains(err.Error(), "something when wrong")
}

func TestDeleteDatasource(t *testing.T) {
req := require.New(t)
datasourceDeleted := false

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// "Datasource ID by name" call
if strings.HasPrefix(r.URL.Path, "/api/datasources/id/") {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"id": 2}`)
return
}

datasourceDeleted = true

req.Equal(http.MethodDelete, r.Method)
req.Equal("/api/datasources/2", r.URL.Path)

w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{}`)
}))
defer ts.Close()

client := NewClient(http.DefaultClient, ts.URL)

err := client.DeleteDatasource(context.TODO(), "test-ds")

req.NoError(err)
req.True(datasourceDeleted)
}

func TestDeleteDatasourceReturnsKnownErrorIfDatasourceDoesNotExist(t *testing.T) {
req := require.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// "Datasource ID by name" call
if strings.HasPrefix(r.URL.Path, "/api/datasources/id/") {
w.WriteHeader(http.StatusNotFound)
return
}

w.WriteHeader(http.StatusOK)
}))
defer ts.Close()

client := NewClient(http.DefaultClient, ts.URL)

err := client.DeleteDatasource(context.TODO(), "test-ds")

req.Error(err)
req.Equal(ErrDatasourceNotFound, err)
}

func TestDeleteDatasourceForwardsErrorOnFailure(t *testing.T) {
req := require.New(t)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// "Datasource ID by name" call
if strings.HasPrefix(r.URL.Path, "/api/datasources/id/") {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"id": 2}`)
return
}

w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, `{
"message": "something when wrong"
}`)
}))
defer ts.Close()

client := NewClient(http.DefaultClient, ts.URL)

err := client.DeleteDatasource(context.TODO(), "test-ds")

req.Error(err)
req.Contains(err.Error(), "something when wrong")
}

0 comments on commit ce24cc0

Please sign in to comment.