-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathenv_test.go
121 lines (110 loc) · 2.98 KB
/
env_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package provider_test
import (
"regexp"
"testing"
"github.com/coder/terraform-provider-coder/provider"
"github.com/stretchr/testify/require"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestEnv(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
resource "coder_env" "example" {
agent_id = "king"
name = "MESSAGE"
value = "Believe in yourself and there will come a day when others will have no choice but to believe with you."
}
`,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)
script := state.Modules[0].Resources["coder_env.example"]
require.NotNil(t, script)
t.Logf("script attributes: %#v", script.Primary.Attributes)
for key, expected := range map[string]string{
"agent_id": "king",
"name": "MESSAGE",
"value": "Believe in yourself and there will come a day when others will have no choice but to believe with you.",
} {
require.Equal(t, expected, script.Primary.Attributes[key])
}
return nil
},
}},
})
}
func TestEnvEmptyValue(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
resource "coder_env" "example" {
agent_id = "king"
name = "MESSAGE"
}
`,
Check: func(state *terraform.State) error {
require.Len(t, state.Modules, 1)
require.Len(t, state.Modules[0].Resources, 1)
script := state.Modules[0].Resources["coder_env.example"]
require.NotNil(t, script)
t.Logf("script attributes: %#v", script.Primary.Attributes)
for key, expected := range map[string]string{
"agent_id": "king",
"name": "MESSAGE",
"value": "",
} {
require.Equal(t, expected, script.Primary.Attributes[key])
}
return nil
},
}},
})
}
func TestEnvBadName(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
resource "coder_env" "example" {
agent_id = ""
name = "bad-name"
}
`,
ExpectError: regexp.MustCompile(`must be a valid environment variable name`),
}},
})
}
func TestEnvNoName(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `
resource "coder_env" "example" {
agent_id = ""
}
`,
ExpectError: regexp.MustCompile(`The argument "name" is required, but no definition was found.`),
}},
})
}