Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
test: add tests of graylog_dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jan 12, 2020
1 parent 75d5412 commit 04e0866
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 93 deletions.
220 changes: 130 additions & 90 deletions terraform/graylog/resource_dashboard_test.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,138 @@
package graylog

import (
"io/ioutil"
"net/http"
"strings"
"testing"
)

//func testDeleteDashboard(
// ctx context.Context, cl *client.Client, key string,
//) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// id, err := getIDFromTfState(tfState, key)
// if err != nil {
// return err
// }
// if _, _, err := cl.GetDashboard(ctx, id); err == nil {
// return fmt.Errorf(`dashboard "%s" must be deleted`, id)
// }
// return nil
// }
//}
//
//func testCreateDashboard(
// ctx context.Context, cl *client.Client, key string,
//) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// id, err := getIDFromTfState(tfState, key)
// if err != nil {
// return err
// }
// _, _, err = cl.GetDashboard(ctx, id)
// return err
// }
//}
//
//func testUpdateDashboard(
// ctx context.Context, cl *client.Client, key, title string,
//) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// id, err := getIDFromTfState(tfState, key)
// if err != nil {
// return err
// }
// db, _, err := cl.GetDashboard(ctx, id)
// if err != nil {
// return err
// }
// if db.Title != title {
// return errors.New("db.Title is not updated")
// }
// return nil
// }
//}
"github.com/hashicorp/terraform/helper/resource"
"github.com/stretchr/testify/require"
"github.com/suzuki-shunsuke/flute/flute"

"github.com/suzuki-shunsuke/go-graylog/v9/testdata"
)

func TestAccDashboard(t *testing.T) {
// ctx := context.Background()
// cl, err := setEnv()
// if err != nil {
// t.Fatal(err)
// }
//
// testAccProvider := Provider()
// testAccProviders := map[string]terraform.ResourceProvider{
// "graylog": testAccProvider,
// }
//
// title := "test-dashboard"
// updatedTitle := "test-dashboard changed"
//
// dbTf := fmt.Sprintf(`
// resource "graylog_dashboard" "zoo" {
// title = "%s"
// description = "test dashboard"
// }`, title)
// updateTf := fmt.Sprintf(`
// resource "graylog_dashboard" "zoo" {
// title = "%s"
// description = "test dashboard"
// }`, updatedTitle)
// key := "graylog_dashboard.zoo"
// resource.Test(t, resource.TestCase{
// Providers: testAccProviders,
// CheckDestroy: testDeleteDashboard(ctx, cl, key),
// Steps: []resource.TestStep{
// {
// Config: dbTf,
// Check: resource.ComposeTestCheckFunc(
// testCreateDashboard(ctx, cl, key),
// ),
// },
// {
// Config: updateTf,
// Check: resource.ComposeTestCheckFunc(
// testUpdateDashboard(ctx, cl, key, updatedTitle),
// ),
// },
// },
// })
setEnv()

createRespBody, err := ioutil.ReadFile("../../testdata/dashboard/create_dashboard_response.json")
require.Nil(t, err)

getBody, err := ioutil.ReadFile("../../testdata/dashboard/dashboard.json")
require.Nil(t, err)

updatedGetBody, err := ioutil.ReadFile("../../testdata/dashboard/updated_dashboard.json")
require.Nil(t, err)

createTF, err := ioutil.ReadFile("../../testdata/dashboard/dashboard.tf")
require.Nil(t, err)

updateTF, err := ioutil.ReadFile("../../testdata/dashboard/update_dashboard.tf")
require.Nil(t, err)

store := newBodyStore("")

ds := testdata.Dashboard()

dsPath := "/api/dashboards/" + ds.ID

defaultTransport := http.DefaultClient.Transport
defer func() {
http.DefaultClient.Transport = defaultTransport
}()
http.DefaultClient.Transport = &flute.Transport{
T: t,
Services: []flute.Service{
{
Endpoint: "http://example.com",
Routes: []flute.Route{
{
Name: "get a dashboard",
Matcher: &flute.Matcher{
Method: "GET",
},
Tester: &flute.Tester{
Path: dsPath,
PartOfHeader: getTestHeader(),
},
Response: &flute.Response{
Response: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader(store.Get())),
}, nil
},
},
},
{
Name: "create a dashboard",
Matcher: &flute.Matcher{
Method: "POST",
},
Tester: &flute.Tester{
Path: "/api/dashboards",
PartOfHeader: getTestHeader(),
Test: genTestBody(map[string]interface{}{
"title": "test",
"description": "test",
}, string(getBody), store),
},
Response: &flute.Response{
Base: http.Response{
StatusCode: 201,
},
BodyString: string(createRespBody),
},
},
{
Name: "update a dashboard",
Matcher: &flute.Matcher{
Method: "PUT",
},
Tester: &flute.Tester{
Path: dsPath,
PartOfHeader: getTestHeader(),
Test: genTestBody(map[string]interface{}{
"title": "updated title",
"description": "updated description",
}, string(updatedGetBody), store),
},
Response: &flute.Response{
Base: http.Response{
StatusCode: 204,
},
},
},
{
Name: "delete a dashboard",
Matcher: &flute.Matcher{
Method: "DELETE",
},
Tester: &flute.Tester{
Path: dsPath,
PartOfHeader: getTestHeader(),
},
Response: &flute.Response{
Base: http.Response{
StatusCode: 204,
},
},
},
},
},
},
}

resource.Test(t, resource.TestCase{
Providers: getTestProviders(),
Steps: []resource.TestStep{
{
Config: string(createTF),
},
{
Config: string(updateTF),
},
},
})
}
14 changes: 14 additions & 0 deletions terraform/graylog/util_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package graylog

import (
"encoding/json"
"net/http"
"sync"
"testing"

"github.com/hashicorp/terraform/terraform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/suzuki-shunsuke/flute/flute"
)

func genTestBody(exp map[string]interface{}, bodyString string, store *bodyStore) func(t *testing.T, req *http.Request, svc *flute.Service, route *flute.Route) {
return func(t *testing.T, req *http.Request, svc *flute.Service, route *flute.Route) {
body := map[string]interface{}{}
require.Nil(t, json.NewDecoder(req.Body).Decode(&body))
assert.Equal(t, exp, body)
store.Set(bodyString)
}
}

func getTestProviders() map[string]terraform.ResourceProvider {
return map[string]terraform.ResourceProvider{
"graylog": Provider(),
Expand Down
3 changes: 3 additions & 0 deletions testdata/dashboard/create_dashboard_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"dashboard_id": "5d84c1a92ab79c000d35d6c7"
}
6 changes: 3 additions & 3 deletions testdata/dashboard/dashboard.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"creator_user_id": "admin",
"id": "5d84c1a92ab79c000d35d6c7",
"title": "test",
"description": "test",
"creator_user_id": "admin",
"created_at": "2019-09-20T12:10:17.486Z",
"positions": {
"78ae7029-0eb4-4064-b3a0-c51306093877": {
Expand All @@ -16,8 +18,6 @@
"height": 1
}
},
"id": "5d84c1a92ab79c000d35d6c7",
"title": "test",
"widgets": [
{
"creator_user_id": "admin",
Expand Down
4 changes: 4 additions & 0 deletions testdata/dashboard/dashboard.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "graylog_dashboard" "foo" {
title = "test"
description = "test"
}
4 changes: 4 additions & 0 deletions testdata/dashboard/update_dashboard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"title": "updated title",
"description": "updated description"
}
4 changes: 4 additions & 0 deletions testdata/dashboard/update_dashboard.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "graylog_dashboard" "foo" {
title = "updated title"
description = "updated description"
}
62 changes: 62 additions & 0 deletions testdata/dashboard/updated_dashboard.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"id": "5d84c1a92ab79c000d35d6c7",
"title": "updated title",
"description": "updated description",
"creator_user_id": "admin",
"created_at": "2019-09-20T12:10:17.486Z",
"positions": {
"78ae7029-0eb4-4064-b3a0-c51306093877": {
"width": 2,
"col": 1,
"row": 0,
"height": 2
},
"ede5fd51-6286-40ee-9b82-249207808344": {
"width": 1,
"col": 0,
"row": 0,
"height": 1
}
},
"widgets": [
{
"creator_user_id": "admin",
"cache_time": 10,
"description": "Quick values",
"id": "78ae7029-0eb4-4064-b3a0-c51306093877",
"type": "QUICKVALUES",
"config": {
"timerange": {
"type": "relative",
"range": 300
},
"field": "status",
"stream_id": "5d84c1a92ab79c000d35d6ca",
"query": "",
"show_data_table": true,
"limit": 5,
"sort_order": "desc",
"show_pie_chart": true,
"data_table_limit": 60
}
},
{
"creator_user_id": "admin",
"cache_time": 10,
"description": "Stream search result count change",
"id": "ede5fd51-6286-40ee-9b82-249207808344",
"type": "STREAM_SEARCH_RESULT_COUNT",
"config": {
"timerange": {
"type": "relative",
"range": 400
},
"lower_is_better": true,
"stream_id": "5d84c1a92ab79c000d35d6ca",
"trend": true,
"query": ""
}
}
]
}

0 comments on commit 04e0866

Please sign in to comment.