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