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 #224 from suzuki-shunsuke/test/test-graylog_input
Browse files Browse the repository at this point in the history
test: add tests of graylog_ldap_setting
  • Loading branch information
suzuki-shunsuke committed Jan 13, 2020
2 parents 565e202 + f5a7dac commit 08ba9bc
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 115 deletions.
31 changes: 16 additions & 15 deletions terraform/graylog/resource_index_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

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

func TestAccIndexSet(t *testing.T) {
Expand All @@ -32,53 +33,53 @@ func TestAccIndexSet(t *testing.T) {
return m, nil
},

CreateReqBodyMap: map[string]interface{}{
CreateReqBodyMap: testutil.ConvertIntToFloat64OfMap(map[string]interface{}{
"title": "test",
"description": "The Graylog default index set",

"index_prefix": "1234-test",
"rotation_strategy_class": "org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategy",
"rotation_strategy": map[string]interface{}{
"type": "org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategyConfig",
"max_docs_per_index": float64(20000000),
"max_docs_per_index": 20000000,
},
"retention_strategy_class": "org.graylog2.indexer.retention.strategies.DeletionRetentionStrategy",
"retention_strategy": map[string]interface{}{
"type": "org.graylog2.indexer.retention.strategies.DeletionRetentionStrategyConfig",
"max_number_of_indices": float64(20),
"max_number_of_indices": 20,
},
"creation_date": creationDate,
"index_analyzer": "standard",
"shards": float64(4),
"index_optimization_max_num_segments": float64(1),
"field_type_refresh_interval": float64(5000),
"shards": 4,
"index_optimization_max_num_segments": 1,
"field_type_refresh_interval": 5000,
"writable": true,
"index_optimization_disabled": false,
"default": false,
},
UpdateReqBodyMap: map[string]interface{}{
}),
UpdateReqBodyMap: testutil.ConvertIntToFloat64OfMap(map[string]interface{}{
"title": "updated title",
"description": "updated description",

"index_prefix": "1234-test",
"rotation_strategy_class": "org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategy",
"rotation_strategy": map[string]interface{}{
"type": "org.graylog2.indexer.rotation.strategies.MessageCountRotationStrategyConfig",
"max_docs_per_index": float64(20000000),
"max_docs_per_index": 20000000,
},
"retention_strategy_class": "org.graylog2.indexer.retention.strategies.DeletionRetentionStrategy",
"retention_strategy": map[string]interface{}{
"type": "org.graylog2.indexer.retention.strategies.DeletionRetentionStrategyConfig",
"max_number_of_indices": float64(20),
"max_number_of_indices": 20,
},
"index_analyzer": "standard",
"shards": float64(4),
"index_optimization_max_num_segments": float64(1),
"replicas": float64(0),
"field_type_refresh_interval": float64(5000),
"shards": 4,
"index_optimization_max_num_segments": 1,
"replicas": 0,
"field_type_refresh_interval": 5000,
"writable": true,
"index_optimization_disabled": false,
},
}),
CreatedDataPath: "index_set/create_index_set_response.json",
UpdatedDataPath: "index_set/update_response.json",
CreateRespBodyPath: "index_set/create_index_set_response.json",
Expand Down
117 changes: 17 additions & 100 deletions terraform/graylog/resource_ldap_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,24 @@ package graylog

import (
"testing"
)

// func testDeleteLDAPSetting(
// ctx context.Context, cl *client.Client,
// ) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// ls, _, err := cl.GetLDAPSetting(ctx)
// if err != nil {
// return err
// }
// if ls.DisplayNameAttribute != "" {
// return fmt.Errorf(
// `display_name_attribute = "%s", wanted ""`,
// ls.DisplayNameAttribute)
// }
// return nil
// }
// }
//
// func testCreateLDAPSetting(
// ctx context.Context, cl *client.Client, exp string,
// ) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// ls, _, err := cl.GetLDAPSetting(ctx)
// if err != nil {
// return err
// }
// if ls.DisplayNameAttribute != exp {
// return fmt.Errorf(
// `display_name_attribute = "%s", wanted "%s"`,
// ls.DisplayNameAttribute, exp)
// }
// return nil
// }
// }
//
// func testUpdateLDAPSetting(
// ctx context.Context, cl *client.Client, exp string,
// ) resource.TestCheckFunc {
// return func(tfState *terraform.State) error {
// ls, _, err := cl.GetLDAPSetting(ctx)
// if err != nil {
// return err
// }
// if ls.DisplayNameAttribute != exp {
// return fmt.Errorf(
// `display_name_attribute = "%s", wanted "%s"`,
// ls.DisplayNameAttribute, exp)
// }
// return nil
// }
// }
"github.com/suzuki-shunsuke/go-graylog/v9/testdata"
)

func TestAccLDAPSetting(t *testing.T) {
// ctx := context.Background()
// cl, err := setEnv()
// if err != nil {
// t.Fatal(err)
// }
//
// testAccProvider := Provider()
// testAccProviders := map[string]terraform.ResourceProvider{
// "graylog": testAccProvider,
// }
//
// createTf := `
// resource "graylog_ldap_setting" "test-terraform" {
// system_username = "admin"
// system_password = "password"
// ldap_uri = "ldap://localhost:389"
// display_name_attribute = "displayname"
// search_base = "OU=user,OU=foo,DC=example,DC=com"
// search_pattern = "(cn={0})"
// default_group = "Reader"
// }`
// updateTf := `
// resource "graylog_ldap_setting" "test-terraform" {
// system_username = "admin"
// system_password = "password"
// ldap_uri = "ldap://localhost:389"
// display_name_attribute = "displayname_updated"
// search_base = "OU=user,OU=foo,DC=example,DC=com"
// search_pattern = "(cn={0})"
// default_group = "Reader"
// }`
// resource.Test(t, resource.TestCase{
// Providers: testAccProviders,
// CheckDestroy: testDeleteLDAPSetting(ctx, cl),
// Steps: []resource.TestStep{
// {
// Config: createTf,
// Check: resource.ComposeTestCheckFunc(
// testCreateLDAPSetting(ctx, cl, "displayname"),
// ),
// },
// {
// Config: updateTf,
// Check: resource.ComposeTestCheckFunc(
// testUpdateLDAPSetting(ctx, cl, "displayname_updated"),
// ),
// },
// },
// })
setEnv()

tc := &testCase{
t: t,
Name: "ldap setting",
GetPath: "/api/system/ldap/settings",

CreateReqBodyMap: map[string]interface{}{},
UpdateReqBodyMap: testdata.CreateLDAPSettingMap(),
CreatedDataPath: "ldap_setting/create.json",
UpdatedDataPath: "ldap_setting/create.json",
CreateTFPath: "ldap_setting/create.tf",
UpdateTFPath: "ldap_setting/create.tf",
}
tc.Test()
}
17 changes: 17 additions & 0 deletions testdata/ldap_setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package testdata

func CreateLDAPSettingMap() map[string]interface{} {
return map[string]interface{}{
"enabled": true,
"system_username": "",
"system_password": "",
"ldap_uri": "ldap://localhost:389",
"use_start_tls": false,
"trust_all_certificates": false,
"active_directory": false,
"search_base": "",
"search_pattern": "",
"display_name_attribute": "",
"default_group": "",
}
}
18 changes: 18 additions & 0 deletions testdata/ldap_setting/create.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"enabled": true,
"system_username": "",
"ldap_uri": "ldap://localhost:389",
"use_start_tls": false,
"trust_all_certificates": false,
"active_directory": false,
"search_base": "",
"search_pattern": "",
"display_name_attribute": "",
"default_group": "",
"group_mapping": null,
"group_search_base": null,
"group_id_attribute": null,
"additional_default_groups": null,
"group_search_pattern": null,
"system_password_set": false
}
16 changes: 16 additions & 0 deletions testdata/ldap_setting/create.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "graylog_ldap_setting" "foo" {
enabled = true
system_username = ""
system_password = ""
ldap_uri = "ldap://localhost:389"
use_start_tls = false
trust_all_certificates = false
active_directory = false
search_base = ""
search_pattern = ""
display_name_attribute = ""
default_group = ""
group_search_base = ""
group_id_attribute = ""
group_search_pattern = ""
}
25 changes: 25 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package testutil

func ConvertIntToFloat64(data interface{}) interface{} {
switch value := data.(type) {
case int:
return float64(value)
case map[string]interface{}:
for k, v := range value {
value[k] = ConvertIntToFloat64(v)
}
case []interface{}:
for i, v := range value {
value[i] = ConvertIntToFloat64(v)
}
}
return data
}

func ConvertIntToFloat64OfMap(data map[string]interface{}) map[string]interface{} {
m := make(map[string]interface{}, len(data))
for k, v := range data {
m[k] = ConvertIntToFloat64(v)
}
return m
}

0 comments on commit 08ba9bc

Please sign in to comment.