Skip to content

Commit 8dee653

Browse files
authored
Update the system_user acceptance tests to utilise the config directory pattern (#1404)
We want to standardise on this, since this resource is being held up as an example to follow we should ensure to follows the desired design
1 parent 5aa03af commit 8dee653

File tree

5 files changed

+100
-45
lines changed

5 files changed

+100
-45
lines changed
Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
11
package system_user_test
22

33
import (
4+
_ "embed"
45
"regexp"
56
"testing"
67

78
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
89
"github.com/elastic/terraform-provider-elasticstack/internal/acctest/checks"
10+
"github.com/hashicorp/terraform-plugin-testing/config"
911
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1012
)
1113

14+
const remoteMonitoringUser = "remote_monitoring_user"
15+
1216
func TestAccResourceSecuritySystemUser(t *testing.T) {
17+
newPassword := "new_password"
1318
resource.Test(t, resource.TestCase{
14-
PreCheck: func() { acctest.PreCheck(t) },
15-
ProtoV6ProviderFactories: acctest.Providers,
19+
PreCheck: func() { acctest.PreCheck(t) },
1620
Steps: []resource.TestStep{
1721
{
18-
Config: testAccResourceSecuritySystemUserCreate,
22+
ProtoV6ProviderFactories: acctest.Providers,
23+
ConfigDirectory: acctest.NamedTestCaseDirectory("create"),
24+
ConfigVariables: config.Variables{
25+
"username": config.StringVariable(remoteMonitoringUser),
26+
},
1927
Check: resource.ComposeTestCheckFunc(
20-
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "username", "remote_monitoring_user"),
28+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "username", remoteMonitoringUser),
2129
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "enabled", "true"),
2230
),
2331
},
2432
{
25-
Config: testAccResourceSecuritySystemUserUpdate,
33+
ProtoV6ProviderFactories: acctest.Providers,
34+
ConfigDirectory: acctest.NamedTestCaseDirectory("update"),
35+
ConfigVariables: config.Variables{
36+
"username": config.StringVariable(remoteMonitoringUser),
37+
"password": config.StringVariable(newPassword),
38+
},
2639
Check: resource.ComposeTestCheckFunc(
27-
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "username", "remote_monitoring_user"),
40+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "username", remoteMonitoringUser),
2841
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "enabled", "true"),
29-
checks.CheckUserCanAuthenticate("remote_monitoring_user", "new_password"),
42+
checks.CheckUserCanAuthenticate(remoteMonitoringUser, newPassword),
3043
),
3144
},
3245
},
@@ -35,17 +48,24 @@ func TestAccResourceSecuritySystemUser(t *testing.T) {
3548

3649
func TestAccResourceSecuritySystemUserNotFound(t *testing.T) {
3750
resource.Test(t, resource.TestCase{
38-
PreCheck: func() { acctest.PreCheck(t) },
39-
ProtoV6ProviderFactories: acctest.Providers,
51+
PreCheck: func() { acctest.PreCheck(t) },
4052
Steps: []resource.TestStep{
4153
{
42-
Config: testAccResourceSecuritySystemUserNotFound,
54+
ProtoV6ProviderFactories: acctest.Providers,
55+
ConfigDirectory: acctest.NamedTestCaseDirectory(""),
56+
ConfigVariables: config.Variables{
57+
"username": config.StringVariable("not_system_user"),
58+
"password": config.StringVariable("new_password"),
59+
},
4360
ExpectError: regexp.MustCompile(`System user "not_system_user" not found`),
4461
},
4562
},
4663
})
4764
}
4865

66+
//go:embed testdata/TestAccResourceSecuritySystemUserFromSDK/system_user.tf
67+
var sdkCreateTestConfig string
68+
4969
func TestAccResourceSecuritySystemUserFromSDK(t *testing.T) {
5070
resource.Test(t, resource.TestCase{
5171
PreCheck: func() { acctest.PreCheck(t) },
@@ -58,51 +78,26 @@ func TestAccResourceSecuritySystemUserFromSDK(t *testing.T) {
5878
VersionConstraint: "0.11.15",
5979
},
6080
},
61-
Config: testAccResourceSecuritySystemUserCreate,
81+
ConfigVariables: config.Variables{
82+
"username": config.StringVariable(remoteMonitoringUser),
83+
},
84+
Config: sdkCreateTestConfig,
6285
Check: resource.ComposeTestCheckFunc(
63-
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "username", "remote_monitoring_user"),
86+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "username", remoteMonitoringUser),
6487
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "enabled", "true"),
6588
),
6689
},
6790
{
6891
ProtoV6ProviderFactories: acctest.Providers,
69-
Config: testAccResourceSecuritySystemUserCreate,
92+
ConfigDirectory: acctest.NamedTestCaseDirectory(""),
93+
ConfigVariables: config.Variables{
94+
"username": config.StringVariable(remoteMonitoringUser),
95+
},
7096
Check: resource.ComposeTestCheckFunc(
71-
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "username", "remote_monitoring_user"),
97+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "username", remoteMonitoringUser),
7298
resource.TestCheckResourceAttr("elasticstack_elasticsearch_security_system_user.remote_monitoring_user", "enabled", "true"),
7399
),
74100
},
75101
},
76102
})
77103
}
78-
79-
const testAccResourceSecuritySystemUserCreate = `
80-
provider "elasticstack" {
81-
elasticsearch {}
82-
}
83-
84-
resource "elasticstack_elasticsearch_security_system_user" "remote_monitoring_user" {
85-
username = "remote_monitoring_user"
86-
}
87-
`
88-
89-
const testAccResourceSecuritySystemUserUpdate = `
90-
provider "elasticstack" {
91-
elasticsearch {}
92-
}
93-
94-
resource "elasticstack_elasticsearch_security_system_user" "remote_monitoring_user" {
95-
username = "remote_monitoring_user"
96-
password = "new_password"
97-
}
98-
`
99-
const testAccResourceSecuritySystemUserNotFound = `
100-
provider "elasticstack" {
101-
elasticsearch {}
102-
}
103-
104-
resource "elasticstack_elasticsearch_security_system_user" "test" {
105-
username = "not_system_user"
106-
password = "new_password"
107-
}
108-
`
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "username" {
2+
description = "The system username"
3+
type = string
4+
}
5+
6+
provider "elasticstack" {
7+
elasticsearch {}
8+
}
9+
10+
resource "elasticstack_elasticsearch_security_system_user" "remote_monitoring_user" {
11+
username = var.username
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
variable "username" {
2+
description = "The system username"
3+
type = string
4+
}
5+
6+
variable "password" {
7+
description = "The password for the system user"
8+
type = string
9+
}
10+
11+
provider "elasticstack" {
12+
elasticsearch {}
13+
}
14+
15+
resource "elasticstack_elasticsearch_security_system_user" "remote_monitoring_user" {
16+
username = var.username
17+
password = var.password
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "username" {
2+
description = "The system username"
3+
type = string
4+
}
5+
6+
provider "elasticstack" {
7+
elasticsearch {}
8+
}
9+
10+
resource "elasticstack_elasticsearch_security_system_user" "remote_monitoring_user" {
11+
username = var.username
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
variable "username" {
2+
description = "The system username"
3+
type = string
4+
}
5+
6+
variable "password" {
7+
description = "The password for the system user"
8+
type = string
9+
}
10+
11+
provider "elasticstack" {
12+
elasticsearch {}
13+
}
14+
15+
resource "elasticstack_elasticsearch_security_system_user" "test" {
16+
username = var.username
17+
password = var.password
18+
}

0 commit comments

Comments
 (0)