This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests of graylog_dashboard
- Loading branch information
1 parent
75d5412
commit 04e0866
Showing
8 changed files
with
224 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"dashboard_id": "5d84c1a92ab79c000d35d6c7" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "graylog_dashboard" "foo" { | ||
title = "test" | ||
description = "test" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"title": "updated title", | ||
"description": "updated description" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
resource "graylog_dashboard" "foo" { | ||
title = "updated title" | ||
description = "updated description" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "" | ||
} | ||
} | ||
] | ||
} | ||
|