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

Commit

Permalink
test: refactor test of graylog_dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Jan 13, 2020
1 parent 14111f4 commit f5f9096
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 130 deletions.
139 changes: 18 additions & 121 deletions terraform/graylog/resource_dashboard_test.go
Original file line number Diff line number Diff line change
@@ -1,138 +1,35 @@
package graylog

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

"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) {
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
tc := &testCase{
t: t,
Name: "dashboard",
CreatePath: "/api/dashboards",
GetPath: "/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,
},
},
},
},
},
CreateReqBodyMap: map[string]interface{}{
"title": "test",
"description": "test",
},
}

resource.Test(t, resource.TestCase{
Providers: getTestProviders(),
Steps: []resource.TestStep{
{
Config: string(createTF),
},
{
Config: string(updateTF),
},
UpdateReqBodyMap: map[string]interface{}{
"title": "updated title",
"description": "updated description",
},
})
CreatedDataPath: "dashboard/dashboard.json",
UpdatedDataPath: "dashboard/updated_dashboard.json",
CreateRespBodyPath: "dashboard/create_dashboard_response.json",
CreateTFPath: "dashboard/dashboard.tf",
UpdateTFPath: "dashboard/update_dashboard.tf",
}
tc.Test()
}
9 changes: 0 additions & 9 deletions terraform/graylog/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,6 @@ func (tc *testCase) Get() (http.RoundTripper, resource.TestCase, error) {
}, nil
}

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

0 comments on commit f5f9096

Please sign in to comment.