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

Commit

Permalink
Merge pull request #226 from suzuki-shunsuke/test/graylog_stream_rule
Browse files Browse the repository at this point in the history
test: add tests of graylog_stream_rule
  • Loading branch information
suzuki-shunsuke committed Jan 13, 2020
2 parents 414ced6 + f29cf8b commit 44a5fb8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 126 deletions.
148 changes: 22 additions & 126 deletions terraform/graylog/resource_stream_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,31 @@ package graylog

import (
"testing"

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

// func testDeleteStreamRule(
// ctx context.Context, cl *client.Client, key string,
// ) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// rs, ok := tfState.RootModule().Resources[key]
// if !ok {
// return errors.New("Not found: " + key)
// }
// id := rs.Primary.ID
// streamID, ok := rs.Primary.Attributes["stream_id"]
// if !ok {
// return errors.New("stream_id is not found: " + key)
// }
// if _, _, err := cl.GetStreamRule(ctx, streamID, id); err == nil {
// return fmt.Errorf(`stream rule "%s" must be deleted`, id)
// }
// return nil
// }
// }
func TestAccStreamRule(t *testing.T) {
setEnv()

rule := testdata.StreamRule()

// func testCreateStreamRule(
// ctx context.Context, cl *client.Client, key string,
// ) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// rs, ok := tfState.RootModule().Resources[key]
// if !ok {
// return errors.New("Not found: " + key)
// }
// id := rs.Primary.ID
// streamID, ok := rs.Primary.Attributes["stream_id"]
// if !ok {
// return errors.New("stream_id is not found: " + key)
// }
// _, _, err := cl.GetStreamRule(ctx, streamID, id)
// return err
// }
// }
tc := &testCase{
t: t,
Name: "stream rule",
CreatePath: "/api/streams/" + rule.StreamID + "/rules",
GetPath: "/api/streams/" + rule.StreamID + "/rules/" + rule.ID,

// func testUpdateStreamRule(
// ctx context.Context, cl *client.Client, key, desc string,
// ) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// rs, ok := tfState.RootModule().Resources[key]
// if !ok {
// return errors.New("Not found: " + key)
// }
// id := rs.Primary.ID
// streamID, ok := rs.Primary.Attributes["stream_id"]
// if !ok {
// return errors.New("stream_id is not found: " + key)
// }
// rule, _, err := cl.GetStreamRule(ctx, streamID, id)
// if err != nil {
// return err
// }
// if rule.Description != desc {
// return fmt.Errorf("rule.Description == %s, wanted %s", rule.Description, desc)
// }
// return nil
// }
// }
CreateReqBodyMap: testutil.ConvertIntToFloat64OfMap(testdata.CreateStreamRuleReqBodyMap()),
UpdateReqBodyMap: testutil.ConvertIntToFloat64OfMap(testdata.UpdateStreamRuleReqBodyMap()),
CreatedDataPath: "stream_rule/stream_rule.json",
UpdatedDataPath: "stream_rule/updated_stream_rule.json",
CreateRespBodyPath: "stream_rule/create_response.json",
UpdateRespBodyPath: "stream_rule/create_response.json",
CreateTFPath: "stream_rule/create.tf",
UpdateTFPath: "stream_rule/update.tf",
}

func TestAccStreamRule(t *testing.T) {
// ctx := context.Background()
// cl, err := setEnv()
// if err != nil {
// t.Fatal(err)
// }
//
// testAccProvider := Provider()
// testAccProviders := map[string]terraform.ResourceProvider{
// "graylog": testAccProvider,
// }
//
// roleTf := `
// resource "graylog_index_set" "test" {
// title = "terraform test index set"
// description = "terraform test index set description"
// index_prefix = "test"
// shards = 4
// replicas = 0
// rotation_strategy_class = "org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategy"
// rotation_strategy {
// type = "org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategyConfig"
// }
// retention_strategy_class = "org.graylog2.indexer.retention.strategies.DeletionRetentionStrategy"
// retention_strategy {
// type = "org.graylog2.indexer.retention.strategies.DeletionRetentionStrategyConfig"
// }
// index_analyzer = "standard"
// writable = true
// index_optimization_max_num_segments = 1
// }
//
// resource "graylog_stream" "test" {
// title = "stream test"
// index_set_id = "${graylog_index_set.test.id}"
// matching_type = "AND"
// }
//
// resource "graylog_stream_rule" "test" {
// field = "tag"
// stream_id = "${graylog_stream.test.id}"
// description = "%s"
// type = 1
// value = "stream_rule.test"
// }`
// createDesc := "terraform stream rule test"
// updateDesc := "terraform stream rule test updated"
//
// key := "graylog_stream_rule.test"
// resource.Test(t, resource.TestCase{
// Providers: testAccProviders,
// CheckDestroy: testDeleteStreamRule(ctx, cl, key),
// Steps: []resource.TestStep{
// {
// Config: fmt.Sprintf(roleTf, createDesc),
// Check: resource.ComposeTestCheckFunc(
// testCreateStreamRule(ctx, cl, key),
// ),
// },
// {
// Config: fmt.Sprintf(roleTf, updateDesc),
// Check: resource.ComposeTestCheckFunc(
// testUpdateStreamRule(ctx, cl, key, updateDesc),
// ),
// },
// },
// })
tc.Test()
}
20 changes: 20 additions & 0 deletions testdata/stream_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,23 @@ func StreamRule() *graylog.StreamRule {
Inverted: false,
}
}

func CreateStreamRuleReqBodyMap() map[string]interface{} {
return map[string]interface{}{
"field": "tag",
"value": "4",
"description": "test",
"type": 1,
"inverted": false,
}
}

func UpdateStreamRuleReqBodyMap() map[string]interface{} {
return map[string]interface{}{
"field": "tag",
"value": "4",
"description": "updated description",
"type": 1,
"inverted": false,
}
}
8 changes: 8 additions & 0 deletions testdata/stream_rule/create.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "graylog_stream_rule" "test" {
field = "tag"
value = "4"
stream_id = "5d84c1a92ab79c000d35d6ca"
description = "test"
type = 1
inverted = false
}
3 changes: 3 additions & 0 deletions testdata/stream_rule/create_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"streamrule_id": "5d84c1a92ab79c000d35d6d7"
}
8 changes: 8 additions & 0 deletions testdata/stream_rule/update.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "graylog_stream_rule" "test" {
field = "tag"
value = "4"
stream_id = "5d84c1a92ab79c000d35d6ca"
description = "updated description"
type = 1
inverted = false
}
9 changes: 9 additions & 0 deletions testdata/stream_rule/updated_stream_rule.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"field": "tag",
"stream_id": "5d84c1a92ab79c000d35d6ca",
"description": "updated description",
"id": "5d84c1a92ab79c000d35d6d7",
"type": 1,
"inverted": false,
"value": "4"
}

0 comments on commit 44a5fb8

Please sign in to comment.