@@ -7,9 +7,11 @@ import (
77 "testing"
88
99 "github.com/go-test/deep"
10+ "github.com/zclconf/go-cty/cty"
1011
12+ version "github.com/hashicorp/go-version"
1113 "github.com/hashicorp/terraform/internal/addrs"
12- "github.com/zclconf/go-cty/cty "
14+ "github.com/hashicorp/terraform/internal/configs/configschema "
1315)
1416
1517func TestProviderAddrs (t * testing.T ) {
@@ -105,6 +107,188 @@ func TestProviderAddrs(t *testing.T) {
105107 }
106108}
107109
110+ func TestBackend_Validate (t * testing.T ) {
111+
112+ typeName := "foobar"
113+ workspace := "default"
114+ config := cty .ObjectVal (map [string ]cty.Value {
115+ "bool" : cty .BoolVal (true ),
116+ })
117+ schema := & configschema.Block {
118+ Attributes : map [string ]* configschema.Attribute {
119+ "bool" : {
120+ Type : cty .Bool ,
121+ },
122+ },
123+ }
124+
125+ // Not-empty cases
126+ t .Run ("backend is not valid if all values are set" , func (t * testing.T ) {
127+ b , err := NewBackend (typeName , config , schema , workspace )
128+ if err != nil {
129+ t .Fatalf ("unexpected error: %s" , err )
130+ }
131+ if err := b .Validate (); err != nil {
132+ t .Fatalf ("expected the Backend to be valid, given all input values were provided: %s" , err )
133+ }
134+ })
135+ t .Run ("backend is not empty if the schema contains no attributes or blocks" , func (t * testing.T ) {
136+ emptyConfig := cty .ObjectVal (map [string ]cty.Value {})
137+ emptySchema := & configschema.Block {
138+ Attributes : map [string ]* configschema.Attribute {
139+ // No attributes
140+ },
141+ }
142+ b , err := NewBackend (typeName , emptyConfig , emptySchema , workspace )
143+ if err != nil {
144+ t .Fatalf ("unexpected error: %s" , err )
145+ }
146+ if err := b .Validate (); err != nil {
147+ t .Fatalf ("expected the Backend to be valid, as empty schemas should be tolerated: %s" , err )
148+ }
149+ })
150+
151+ // Empty cases
152+ t .Run ("backend is empty if type name is missing" , func (t * testing.T ) {
153+ b , err := NewBackend ("" , config , schema , workspace )
154+ if err != nil {
155+ t .Fatalf ("unexpected error: %s" , err )
156+ }
157+ if err := b .Validate (); err == nil {
158+ t .Fatalf ("expected the Backend to be invalid, given the type being unset: %#v" , b )
159+ }
160+ })
161+ t .Run ("backend is empty if workspace name is missing" , func (t * testing.T ) {
162+ b , err := NewBackend (typeName , config , schema , "" )
163+ if err != nil {
164+ t .Fatalf ("unexpected error: %s" , err )
165+ }
166+ if err := b .Validate (); err == nil {
167+ t .Fatalf ("expected the Backend to be invalid, given the type being unset: %#v" , b )
168+ }
169+ })
170+ }
171+
172+ func TestStateStore_Validate (t * testing.T ) {
173+ typeName := "test_store"
174+ providerVersion := version .Must (version .NewSemver ("1.2.3" ))
175+ source := addrs .MustParseProviderSourceString ("hashicorp/test" )
176+ workspace := "default"
177+ config := cty .ObjectVal (map [string ]cty.Value {
178+ "bool" : cty .BoolVal (true ),
179+ })
180+ schema := & configschema.Block {
181+ Attributes : map [string ]* configschema.Attribute {
182+ "bool" : {
183+ Type : cty .Bool ,
184+ },
185+ },
186+ }
187+
188+ // Not-empty cases
189+ t .Run ("state store is not empty if all values are set" , func (t * testing.T ) {
190+ s , err := NewStateStore (typeName , providerVersion , & source , config , schema , config , schema , workspace )
191+ if err != nil {
192+ t .Fatalf ("unexpected error: %s" , err )
193+ }
194+ if err := s .Validate (); err != nil {
195+ t .Fatalf ("expected the StateStore to be valid, given all input values were provided: %s" , err )
196+ }
197+ })
198+ t .Run ("state store is not empty if the state store config is present but contains all null values" , func (t * testing.T ) {
199+ nullConfig := cty .ObjectVal (map [string ]cty.Value {
200+ "bool" : cty .NullVal (cty .Bool ),
201+ })
202+ s , err := NewStateStore (typeName , providerVersion , & source , nullConfig , schema , config , schema , workspace )
203+ if err != nil {
204+ t .Fatalf ("unexpected error: %s" , err )
205+ }
206+ if err := s .Validate (); err != nil {
207+ t .Fatalf ("expected the StateStore to be valid, despite the state store config containing only null values: %s" , err )
208+ }
209+ })
210+ t .Run ("state store is not empty if the provider config is present but contains all null values" , func (t * testing.T ) {
211+ nullConfig := cty .ObjectVal (map [string ]cty.Value {
212+ "bool" : cty .NullVal (cty .Bool ),
213+ })
214+ s , err := NewStateStore (typeName , providerVersion , & source , nullConfig , schema , config , schema , workspace )
215+ if err != nil {
216+ t .Fatalf ("unexpected error: %s" , err )
217+ }
218+ if err := s .Validate (); err != nil {
219+ t .Fatalf ("expected the StateStore to be valid, despite the provider config containing only null values: %s" , err )
220+ }
221+ })
222+ t .Run ("state store is not incorrectly identified as empty if the state store's schema contains no attributes or blocks" , func (t * testing.T ) {
223+ emptyConfig := cty .ObjectVal (map [string ]cty.Value {})
224+ emptySchema := & configschema.Block {
225+ Attributes : map [string ]* configschema.Attribute {
226+ // No attributes
227+ },
228+ }
229+ s , err := NewStateStore (typeName , providerVersion , & source , emptyConfig , emptySchema , config , schema , workspace )
230+ if err != nil {
231+ t .Fatalf ("unexpected error: %s" , err )
232+ }
233+ if err := s .Validate (); err != nil {
234+ t .Fatalf ("expected the StateStore to be valid, as empty schemas should be tolerated: %s" , err )
235+ }
236+ })
237+ t .Run ("state store is not incorrectly identified as empty if the provider's schema contains no attributes or blocks" , func (t * testing.T ) {
238+ emptyConfig := cty .ObjectVal (map [string ]cty.Value {})
239+ emptySchema := & configschema.Block {
240+ Attributes : map [string ]* configschema.Attribute {
241+ // No attributes
242+ },
243+ }
244+ s , err := NewStateStore (typeName , providerVersion , & source , config , schema , emptyConfig , emptySchema , workspace )
245+ if err != nil {
246+ t .Fatalf ("unexpected error: %s" , err )
247+ }
248+ if err := s .Validate (); err != nil {
249+ t .Fatalf ("expected the StateStore to be valid, as empty schemas should be tolerated: %s" , err )
250+ }
251+ })
252+
253+ // Empty cases
254+ t .Run ("state store is empty if the type is missing" , func (t * testing.T ) {
255+ s , err := NewStateStore ("" , providerVersion , & source , config , schema , config , schema , workspace )
256+ if err != nil {
257+ t .Fatalf ("unexpected error: %s" , err )
258+ }
259+ if err := s .Validate (); err == nil {
260+ t .Fatalf ("expected the StateStore to be invalid, given the type name is missing: %s" , err )
261+ }
262+ })
263+ t .Run ("state store is empty if the provider version is missing" , func (t * testing.T ) {
264+ s , err := NewStateStore (typeName , nil , & source , config , schema , config , schema , workspace )
265+ if err != nil {
266+ t .Fatalf ("unexpected error: %s" , err )
267+ }
268+ if err := s .Validate (); err == nil {
269+ t .Fatalf ("expected the StateStore to be invalid, given the version is missing: %s" , err )
270+ }
271+ })
272+ t .Run ("state store is empty if the provider source is missing" , func (t * testing.T ) {
273+ s , err := NewStateStore (typeName , providerVersion , nil , config , schema , config , schema , workspace )
274+ if err != nil {
275+ t .Fatalf ("unexpected error: %s" , err )
276+ }
277+ if err := s .Validate (); err == nil {
278+ t .Fatalf ("expected the StateStore to be invalid, given the version is missing: %s" , err )
279+ }
280+ })
281+ t .Run ("state store is empty if the workspace name is missing" , func (t * testing.T ) {
282+ s , err := NewStateStore (typeName , providerVersion , & source , cty .NilVal , schema , config , schema , "" )
283+ if err != nil {
284+ t .Fatalf ("unexpected error: %s" , err )
285+ }
286+ if err := s .Validate (); err == nil {
287+ t .Fatalf ("expected the StateStore to be invalid, given the workspace name is missing: %s" , err )
288+ }
289+ })
290+ }
291+
108292// Module outputs should not effect the result of Empty
109293func TestModuleOutputChangesEmpty (t * testing.T ) {
110294 changes := & ChangesSrc {
0 commit comments