github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/backend_test.go (about) 1 package schema 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/zclconf/go-cty/cty" 9 ) 10 11 func TestBackendPrepare(t *testing.T) { 12 cases := []struct { 13 Name string 14 B *Backend 15 Config map[string]cty.Value 16 Expect map[string]cty.Value 17 Err bool 18 }{ 19 { 20 "Basic required field", 21 &Backend{ 22 Schema: map[string]*Schema{ 23 "foo": { 24 Required: true, 25 Type: TypeString, 26 }, 27 }, 28 }, 29 map[string]cty.Value{}, 30 map[string]cty.Value{}, 31 true, 32 }, 33 34 { 35 "Null config", 36 &Backend{ 37 Schema: map[string]*Schema{ 38 "foo": { 39 Required: true, 40 Type: TypeString, 41 }, 42 }, 43 }, 44 nil, 45 map[string]cty.Value{}, 46 true, 47 }, 48 49 { 50 "Basic required field set", 51 &Backend{ 52 Schema: map[string]*Schema{ 53 "foo": { 54 Required: true, 55 Type: TypeString, 56 }, 57 }, 58 }, 59 map[string]cty.Value{ 60 "foo": cty.StringVal("bar"), 61 }, 62 map[string]cty.Value{ 63 "foo": cty.StringVal("bar"), 64 }, 65 false, 66 }, 67 68 { 69 "unused default", 70 &Backend{ 71 Schema: map[string]*Schema{ 72 "foo": { 73 Optional: true, 74 Type: TypeString, 75 Default: "baz", 76 }, 77 }, 78 }, 79 map[string]cty.Value{ 80 "foo": cty.StringVal("bar"), 81 }, 82 map[string]cty.Value{ 83 "foo": cty.StringVal("bar"), 84 }, 85 false, 86 }, 87 88 { 89 "default", 90 &Backend{ 91 Schema: map[string]*Schema{ 92 "foo": { 93 Type: TypeString, 94 Optional: true, 95 Default: "baz", 96 }, 97 }, 98 }, 99 map[string]cty.Value{}, 100 map[string]cty.Value{ 101 "foo": cty.StringVal("baz"), 102 }, 103 false, 104 }, 105 106 { 107 "default func", 108 &Backend{ 109 Schema: map[string]*Schema{ 110 "foo": { 111 Type: TypeString, 112 Optional: true, 113 DefaultFunc: func() (interface{}, error) { 114 return "baz", nil 115 }, 116 }, 117 }, 118 }, 119 map[string]cty.Value{}, 120 map[string]cty.Value{ 121 "foo": cty.StringVal("baz"), 122 }, 123 false, 124 }, 125 } 126 127 for i, tc := range cases { 128 t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) { 129 cfgVal := cty.NullVal(cty.Object(map[string]cty.Type{})) 130 if tc.Config != nil { 131 cfgVal = cty.ObjectVal(tc.Config) 132 } 133 configVal, diags := tc.B.PrepareConfig(cfgVal) 134 if diags.HasErrors() != tc.Err { 135 for _, d := range diags { 136 t.Error(d.Description()) 137 } 138 } 139 140 if tc.Err { 141 return 142 } 143 144 expect := cty.ObjectVal(tc.Expect) 145 if !expect.RawEquals(configVal) { 146 t.Fatalf("\nexpected: %#v\ngot: %#v\n", expect, configVal) 147 } 148 }) 149 } 150 } 151 152 func TestBackendConfigure(t *testing.T) { 153 cases := []struct { 154 Name string 155 B *Backend 156 Config map[string]cty.Value 157 Err bool 158 }{ 159 { 160 "Basic config", 161 &Backend{ 162 Schema: map[string]*Schema{ 163 "foo": { 164 Type: TypeInt, 165 Optional: true, 166 }, 167 }, 168 169 ConfigureFunc: func(ctx context.Context) error { 170 d := FromContextBackendConfig(ctx) 171 if d.Get("foo").(int) != 42 { 172 return fmt.Errorf("bad config data") 173 } 174 175 return nil 176 }, 177 }, 178 map[string]cty.Value{ 179 "foo": cty.NumberIntVal(42), 180 }, 181 false, 182 }, 183 } 184 185 for i, tc := range cases { 186 t.Run(fmt.Sprintf("%d-%s", i, tc.Name), func(t *testing.T) { 187 diags := tc.B.Configure(cty.ObjectVal(tc.Config)) 188 if diags.HasErrors() != tc.Err { 189 t.Errorf("wrong number of diagnostics") 190 } 191 }) 192 } 193 }