|
4 | 4 | "regexp"
|
5 | 5 | "testing"
|
6 | 6 |
|
| 7 | + "github.com/coder/terraform-provider-coder/v2/provider" |
7 | 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
|
8 | 9 | "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
|
9 | 10 | "github.com/stretchr/testify/assert"
|
@@ -102,3 +103,122 @@ func TestWorkspace_MissingTemplateName(t *testing.T) {
|
102 | 103 | }},
|
103 | 104 | })
|
104 | 105 | }
|
| 106 | + |
| 107 | +// TestWorkspace_PrebuildEnv validates that our handling of input environment variables is correct. |
| 108 | +func TestWorkspace_PrebuildEnv(t *testing.T) { |
| 109 | + cases := []struct { |
| 110 | + name string |
| 111 | + envs map[string]string |
| 112 | + check func(state *terraform.State, resource *terraform.ResourceState) error |
| 113 | + }{ |
| 114 | + { |
| 115 | + name: "unused", |
| 116 | + envs: map[string]string{}, |
| 117 | + check: func(state *terraform.State, resource *terraform.ResourceState) error { |
| 118 | + attribs := resource.Primary.Attributes |
| 119 | + assert.Equal(t, "false", attribs["is_prebuild"]) |
| 120 | + assert.Equal(t, "0", attribs["prebuild_count"]) |
| 121 | + assert.Equal(t, "false", attribs["is_prebuild_claim"]) |
| 122 | + return nil |
| 123 | + }, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "prebuild=true", |
| 127 | + envs: map[string]string{ |
| 128 | + provider.IsPrebuildEnvironmentVariable(): "true", |
| 129 | + }, |
| 130 | + check: func(state *terraform.State, resource *terraform.ResourceState) error { |
| 131 | + attribs := resource.Primary.Attributes |
| 132 | + assert.Equal(t, "true", attribs["is_prebuild"]) |
| 133 | + assert.Equal(t, "1", attribs["prebuild_count"]) |
| 134 | + assert.Equal(t, "false", attribs["is_prebuild_claim"]) |
| 135 | + return nil |
| 136 | + }, |
| 137 | + }, |
| 138 | + { |
| 139 | + name: "prebuild=false", |
| 140 | + envs: map[string]string{ |
| 141 | + provider.IsPrebuildEnvironmentVariable(): "false", |
| 142 | + }, |
| 143 | + check: func(state *terraform.State, resource *terraform.ResourceState) error { |
| 144 | + attribs := resource.Primary.Attributes |
| 145 | + assert.Equal(t, "false", attribs["is_prebuild"]) |
| 146 | + assert.Equal(t, "0", attribs["prebuild_count"]) |
| 147 | + assert.Equal(t, "false", attribs["is_prebuild_claim"]) |
| 148 | + return nil |
| 149 | + }, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "prebuild_claim=true", |
| 153 | + envs: map[string]string{ |
| 154 | + provider.IsPrebuildClaimEnvironmentVariable(): "true", |
| 155 | + }, |
| 156 | + check: func(state *terraform.State, resource *terraform.ResourceState) error { |
| 157 | + attribs := resource.Primary.Attributes |
| 158 | + assert.Equal(t, "false", attribs["is_prebuild"]) |
| 159 | + assert.Equal(t, "0", attribs["prebuild_count"]) |
| 160 | + assert.Equal(t, "true", attribs["is_prebuild_claim"]) |
| 161 | + return nil |
| 162 | + }, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "prebuild_claim=false", |
| 166 | + envs: map[string]string{ |
| 167 | + provider.IsPrebuildClaimEnvironmentVariable(): "false", |
| 168 | + }, |
| 169 | + check: func(state *terraform.State, resource *terraform.ResourceState) error { |
| 170 | + attribs := resource.Primary.Attributes |
| 171 | + assert.Equal(t, "false", attribs["is_prebuild"]) |
| 172 | + assert.Equal(t, "0", attribs["prebuild_count"]) |
| 173 | + assert.Equal(t, "false", attribs["is_prebuild_claim"]) |
| 174 | + return nil |
| 175 | + }, |
| 176 | + }, |
| 177 | + { |
| 178 | + // Should not ever happen, but let's ensure our defensive check is activated. We can't ever have both flags |
| 179 | + // being true. |
| 180 | + name: "prebuild=true,prebuild_claim=true", |
| 181 | + envs: map[string]string{ |
| 182 | + provider.IsPrebuildEnvironmentVariable(): "true", |
| 183 | + provider.IsPrebuildClaimEnvironmentVariable(): "true", |
| 184 | + }, |
| 185 | + check: func(state *terraform.State, resource *terraform.ResourceState) error { |
| 186 | + attribs := resource.Primary.Attributes |
| 187 | + assert.Equal(t, "false", attribs["is_prebuild"]) |
| 188 | + assert.Equal(t, "0", attribs["prebuild_count"]) |
| 189 | + assert.Equal(t, "true", attribs["is_prebuild_claim"]) |
| 190 | + return nil |
| 191 | + }, |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + for _, tc := range cases { |
| 196 | + t.Run(tc.name, func(t *testing.T) { |
| 197 | + for k, v := range tc.envs { |
| 198 | + t.Setenv(k, v) |
| 199 | + } |
| 200 | + |
| 201 | + resource.Test(t, resource.TestCase{ |
| 202 | + ProviderFactories: coderFactory(), |
| 203 | + IsUnitTest: true, |
| 204 | + Steps: []resource.TestStep{{ |
| 205 | + Config: ` |
| 206 | +provider "coder" { |
| 207 | + url = "https://example.com:8080" |
| 208 | +} |
| 209 | +data "coder_workspace" "me" { |
| 210 | +}`, |
| 211 | + Check: func(state *terraform.State) error { |
| 212 | + // Baseline checks |
| 213 | + require.Len(t, state.Modules, 1) |
| 214 | + require.Len(t, state.Modules[0].Resources, 1) |
| 215 | + resource := state.Modules[0].Resources["data.coder_workspace.me"] |
| 216 | + require.NotNil(t, resource) |
| 217 | + |
| 218 | + return tc.check(state, resource) |
| 219 | + }, |
| 220 | + }}, |
| 221 | + }) |
| 222 | + }) |
| 223 | + } |
| 224 | +} |
0 commit comments