@@ -860,7 +860,7 @@ func TestApply_plan_remoteState(t *testing.T) {
860
860
func TestApply_planWithVarFile (t * testing.T ) {
861
861
varFileDir := testTempDir (t )
862
862
varFilePath := filepath .Join (varFileDir , "terraform.tfvars" )
863
- if err := ioutil .WriteFile (varFilePath , []byte (applyVarFile ), 0644 ); err != nil {
863
+ if err := os .WriteFile (varFilePath , []byte (applyVarFile ), 0644 ); err != nil {
864
864
t .Fatalf ("err: %s" , err )
865
865
}
866
866
@@ -878,6 +878,19 @@ func TestApply_planWithVarFile(t *testing.T) {
878
878
defer os .Chdir (cwd )
879
879
880
880
p := applyFixtureProvider ()
881
+ p .GetProviderSchemaResponse = & providers.GetProviderSchemaResponse {
882
+ ResourceTypes : map [string ]providers.Schema {
883
+ "test_instance" : {
884
+ Block : & configschema.Block {
885
+ Attributes : map [string ]* configschema.Attribute {
886
+ "id" : {Type : cty .String , Computed : true },
887
+ "value" : {Type : cty .String , Optional : true },
888
+ },
889
+ },
890
+ },
891
+ },
892
+ }
893
+
881
894
view , done := testView (t )
882
895
c := & ApplyCommand {
883
896
Meta : Meta {
@@ -906,59 +919,14 @@ func TestApply_planWithVarFile(t *testing.T) {
906
919
}
907
920
}
908
921
909
- func TestApply_planWithVarFilePreviouslyUnset (t * testing.T ) {
910
- varFileDir := testTempDir (t )
911
- varFilePath := filepath .Join (varFileDir , "terraform.tfvars" )
912
- if err := ioutil .WriteFile (varFilePath , []byte (applyVarFile ), 0644 ); err != nil {
913
- t .Fatalf ("err: %s" , err )
914
- }
915
-
916
- // The value of foo is not set
917
- planPath := applyFixturePlanFile (t )
918
- statePath := testTempFile (t )
919
-
920
- cwd , err := os .Getwd ()
921
- if err != nil {
922
- t .Fatalf ("err: %s" , err )
923
- }
924
- if err := os .Chdir (varFileDir ); err != nil {
925
- t .Fatalf ("err: %s" , err )
926
- }
927
- defer os .Chdir (cwd )
928
-
929
- p := applyFixtureProvider ()
930
- view , done := testView (t )
931
- c := & ApplyCommand {
932
- Meta : Meta {
933
- testingOverrides : metaOverridesForProvider (p ),
934
- View : view ,
935
- },
936
- }
937
-
938
- args := []string {
939
- "-state-out" , statePath ,
940
- planPath ,
941
- }
942
- code := c .Run (args )
943
- output := done (t )
944
- if code == 0 {
945
- t .Fatalf ("expected to fail, but succeeded. \n \n %s" , output .All ())
946
- }
947
-
948
- expectedTitle := "Can't set variable when applying a saved plan"
949
- if ! strings .Contains (output .Stderr (), expectedTitle ) {
950
- t .Fatalf ("Expected stderr to contain %q, got %q" , expectedTitle , output .Stderr ())
951
- }
952
- }
953
-
954
922
func TestApply_planWithVarFileChangingVariableValue (t * testing.T ) {
955
923
varFileDir := testTempDir (t )
956
924
varFilePath := filepath .Join (varFileDir , "terraform.tfvars" )
957
- if err := ioutil .WriteFile (varFilePath , []byte (applyVarFile ), 0644 ); err != nil {
925
+ if err := os .WriteFile (varFilePath , []byte (applyVarFile ), 0644 ); err != nil {
958
926
t .Fatalf ("err: %s" , err )
959
927
}
960
928
961
- // The value of foo is differnet from the var file
929
+ // The value of foo is different from the var file
962
930
planPath := applyFixturePlanFileWithVariableValue (t , "lorem ipsum" )
963
931
statePath := testTempFile (t )
964
932
@@ -1289,6 +1257,114 @@ foo = "bar"
1289
1257
}
1290
1258
}
1291
1259
1260
+ // Variables can be passed to apply now for ephemeral usage, but we need to
1261
+ // ensure that the legacy handling of undeclared variables remains intact
1262
+ func TestApply_changedVars_applyTime (t * testing.T ) {
1263
+ t .Run ("undeclared-config-var" , func (t * testing.T ) {
1264
+ // an undeclared config variable is a warning, just like during plan
1265
+ varFileDir := testTempDir (t )
1266
+ varFilePath := filepath .Join (varFileDir , "terraform.tfvars" )
1267
+ if err := os .WriteFile (varFilePath , []byte (`undeclared = true` ), 0644 ); err != nil {
1268
+ t .Fatalf ("err: %s" , err )
1269
+ }
1270
+
1271
+ // The value of foo is not set
1272
+ planPath := applyFixturePlanFile (t )
1273
+ statePath := testTempFile (t )
1274
+
1275
+ cwd , err := os .Getwd ()
1276
+ if err != nil {
1277
+ t .Fatalf ("err: %s" , err )
1278
+ }
1279
+ if err := os .Chdir (varFileDir ); err != nil {
1280
+ t .Fatalf ("err: %s" , err )
1281
+ }
1282
+ defer os .Chdir (cwd )
1283
+
1284
+ p := applyFixtureProvider ()
1285
+ view , done := testView (t )
1286
+ c := & ApplyCommand {
1287
+ Meta : Meta {
1288
+ testingOverrides : metaOverridesForProvider (p ),
1289
+ View : view ,
1290
+ },
1291
+ }
1292
+
1293
+ args := []string {
1294
+ "-state-out" , statePath ,
1295
+ planPath ,
1296
+ }
1297
+ code := c .Run (args )
1298
+ output := done (t )
1299
+ if code != 0 {
1300
+ t .Fatalf ("unexpected exit code %d:\n \n %s" , code , output .All ())
1301
+ }
1302
+
1303
+ if ! strings .Contains (output .All (), `Value for undeclared variable` ) {
1304
+ t .Fatalf ("missing undeclared warning:\n %s" , output .All ())
1305
+ }
1306
+ })
1307
+
1308
+ t .Run ("undeclared-cli-var" , func (t * testing.T ) {
1309
+ // an undeclared cli variable is an error, just like during plan
1310
+ planPath := applyFixturePlanFile (t )
1311
+ statePath := testTempFile (t )
1312
+
1313
+ p := applyFixtureProvider ()
1314
+ view , done := testView (t )
1315
+ c := & ApplyCommand {
1316
+ Meta : Meta {
1317
+ testingOverrides : metaOverridesForProvider (p ),
1318
+ View : view ,
1319
+ },
1320
+ }
1321
+
1322
+ args := []string {
1323
+ "-var" , "undeclared=true" ,
1324
+ "-state-out" , statePath ,
1325
+ planPath ,
1326
+ }
1327
+ code := c .Run (args )
1328
+ output := done (t )
1329
+ if code != 1 {
1330
+ t .Fatalf ("unexpected exit code %d:\n \n %s" , code , output .All ())
1331
+ }
1332
+
1333
+ if ! strings .Contains (output .Stderr (), `Value for undeclared variable` ) {
1334
+ t .Fatalf ("missing undeclared warning:\n %s" , output .All ())
1335
+ }
1336
+ })
1337
+
1338
+ t .Run ("changed-cli-var" , func (t * testing.T ) {
1339
+ planPath := applyFixturePlanFileWithVariableValue (t , "orig" )
1340
+ statePath := testTempFile (t )
1341
+
1342
+ p := applyFixtureProvider ()
1343
+ view , done := testView (t )
1344
+ c := & ApplyCommand {
1345
+ Meta : Meta {
1346
+ testingOverrides : metaOverridesForProvider (p ),
1347
+ View : view ,
1348
+ },
1349
+ }
1350
+
1351
+ args := []string {
1352
+ "-var" , "foo=new" ,
1353
+ "-state-out" , statePath ,
1354
+ planPath ,
1355
+ }
1356
+ code := c .Run (args )
1357
+ output := done (t )
1358
+ if code != 1 {
1359
+ t .Fatalf ("unexpected exit code %d:\n \n %s" , code , output .All ())
1360
+ }
1361
+
1362
+ if ! strings .Contains (output .Stderr (), `Can't change variable when applying a saved plan` ) {
1363
+ t .Fatalf ("missing undeclared warning:\n %s" , output .All ())
1364
+ }
1365
+ })
1366
+ }
1367
+
1292
1368
// we should be able to apply a plan file with no other file dependencies
1293
1369
func TestApply_planNoModuleFiles (t * testing.T ) {
1294
1370
// temporary data directory which we can remove between commands
@@ -1800,7 +1876,7 @@ func TestApply_varFileDefault(t *testing.T) {
1800
1876
defer testChdir (t , td )()
1801
1877
1802
1878
varFilePath := filepath .Join (td , "terraform.tfvars" )
1803
- if err := ioutil .WriteFile (varFilePath , []byte (applyVarFile ), 0644 ); err != nil {
1879
+ if err := os .WriteFile (varFilePath , []byte (applyVarFile ), 0644 ); err != nil {
1804
1880
t .Fatalf ("err: %s" , err )
1805
1881
}
1806
1882
@@ -2595,10 +2671,10 @@ func applyFixturePlanFileMatchState(t *testing.T, stateMeta statemgr.SnapshotMet
2595
2671
// a single change to create the test_instance.foo and a variable value that is included in the
2596
2672
// "apply" test fixture, returning the location of that plan file.
2597
2673
func applyFixturePlanFileWithVariableValue (t * testing.T , value string ) string {
2598
- _ , snap := testModuleWithSnapshot (t , "apply" )
2674
+ _ , snap := testModuleWithSnapshot (t , "apply-vars " )
2599
2675
plannedVal := cty .ObjectVal (map [string ]cty.Value {
2600
- "id" : cty .UnknownVal (cty .String ),
2601
- "ami " : cty .StringVal ("bar" ),
2676
+ "id" : cty .UnknownVal (cty .String ),
2677
+ "value " : cty .StringVal ("bar" ),
2602
2678
})
2603
2679
priorValRaw , err := plans .NewDynamicValue (cty .NullVal (plannedVal .Type ()), plannedVal .Type ())
2604
2680
if err != nil {
0 commit comments