@@ -618,3 +618,162 @@ func TestDiffSetHashFailsOnNil(t *testing.T) {
618618 },
619619 )
620620}
621+
622+ func TestDefaultsChanges (t * testing.T ) {
623+ t .Parallel ()
624+
625+ for _ , defVal := range []string {"hello" , "" } {
626+ t .Run (fmt .Sprintf ("default value %q" , defVal ), func (t * testing.T ) {
627+ t .Parallel ()
628+
629+ for _ , useDefaultFunc := range []bool {true , false } {
630+ t .Run (fmt .Sprintf ("useDefaultFunc %t" , useDefaultFunc ), func (t * testing.T ) {
631+ t .Parallel ()
632+ defaultFunc := func () (interface {}, error ) {
633+ return defVal , nil
634+ }
635+
636+ var def any = defVal
637+
638+ if ! useDefaultFunc {
639+ defaultFunc = nil
640+ } else {
641+ def = nil
642+ }
643+
644+ res := & schema.Resource {
645+ Schema : map [string ]* schema.Schema {
646+ "test" : {
647+ Type : schema .TypeString ,
648+ Optional : true ,
649+ DefaultFunc : defaultFunc ,
650+ Default : def ,
651+ },
652+ },
653+ }
654+
655+ configNoValue := map [string ]cty.Value {}
656+ configWithValue := map [string ]cty.Value {
657+ "test" : cty .StringVal ("world" ),
658+ }
659+ configWithDefaultValue := map [string ]cty.Value {
660+ "test" : cty .StringVal (defVal ),
661+ }
662+
663+ t .Run ("value removed from config" , func (t * testing.T ) {
664+ t .Parallel ()
665+
666+ crosstests .Diff (t , res , configWithValue , configNoValue )
667+ })
668+
669+ t .Run ("default value removed from config" , func (t * testing.T ) {
670+ t .Parallel ()
671+
672+ crosstests .Diff (t , res , configWithDefaultValue , configNoValue )
673+ })
674+
675+ t .Run ("default value added to config" , func (t * testing.T ) {
676+ t .Parallel ()
677+
678+ crosstests .Diff (t , res , configNoValue , configWithDefaultValue )
679+ })
680+
681+ t .Run ("value added to config" , func (t * testing.T ) {
682+ t .Parallel ()
683+
684+ crosstests .Diff (t , res , configNoValue , configWithValue )
685+ })
686+ })
687+ }
688+ })
689+ }
690+ }
691+
692+ func TestDefaultSchemaChanged (t * testing.T ) {
693+ t .Parallel ()
694+
695+ res1 := & schema.Resource {
696+ Schema : map [string ]* schema.Schema {
697+ "test" : {
698+ Type : schema .TypeString ,
699+ Default : "hello" ,
700+ Optional : true ,
701+ },
702+ },
703+ }
704+
705+ res2 := & schema.Resource {
706+ Schema : map [string ]* schema.Schema {
707+ "test" : {
708+ Type : schema .TypeString ,
709+ Default : "world" ,
710+ Optional : true ,
711+ },
712+ },
713+ }
714+
715+ res3 := & schema.Resource {
716+ Schema : map [string ]* schema.Schema {
717+ "test" : {
718+ Type : schema .TypeString ,
719+ Optional : true ,
720+ },
721+ },
722+ }
723+ noValue := map [string ]cty.Value {}
724+ default1Value := map [string ]cty.Value {
725+ "test" : cty .StringVal ("hello" ),
726+ }
727+ default2Value := map [string ]cty.Value {
728+ "test" : cty .StringVal ("world" ),
729+ }
730+
731+ valuePairs := []struct {
732+ name string
733+ before map [string ]cty.Value
734+ after map [string ]cty.Value
735+ }{
736+ // TODO[pulumi/pulumi-terraform-bridge#3060]: Changing schema defaults is a DIFF_SOME in TF and a DIFF_NONE in Pulumi.
737+ // {name: "no value", before: noValue, after: noValue},
738+ {name : "no value, default1" , before : noValue , after : default1Value },
739+ {name : "no value, default2" , before : noValue , after : default2Value },
740+ {name : "default1, no value" , before : default1Value , after : noValue },
741+ {name : "default1, default1" , before : default1Value , after : default1Value },
742+ {name : "default1, default2" , before : default1Value , after : default2Value },
743+ {name : "default2, no value" , before : default2Value , after : noValue },
744+ {name : "default2, default1" , before : default2Value , after : default1Value },
745+ {name : "default2, default2" , before : default2Value , after : default2Value },
746+ }
747+
748+ schemaPairs := []struct {
749+ name string
750+ before * schema.Resource
751+ after * schema.Resource
752+ }{
753+ {name : "res1, res1" , before : res1 , after : res1 },
754+ {name : "res1, res2" , before : res1 , after : res2 },
755+ {name : "res1, res3" , before : res1 , after : res3 },
756+ {name : "res2, res1" , before : res2 , after : res1 },
757+ {name : "res2, res2" , before : res2 , after : res2 },
758+ {name : "res2, res3" , before : res2 , after : res3 },
759+ {name : "res3, res1" , before : res3 , after : res1 },
760+ {name : "res3, res2" , before : res3 , after : res2 },
761+ {name : "res3, res3" , before : res3 , after : res3 },
762+ }
763+
764+ for _ , valuePair := range valuePairs {
765+ for _ , schemaPair := range schemaPairs {
766+ t .Run (fmt .Sprintf ("%s, %s" , valuePair .name , schemaPair .name ), func (t * testing.T ) {
767+ t .Parallel ()
768+
769+ crosstests .Diff (
770+ t ,
771+ schemaPair .before ,
772+ valuePair .before ,
773+ valuePair .after ,
774+ crosstests .DiffProviderUpgradedSchema (schemaPair .after ),
775+ )
776+ })
777+ }
778+ }
779+ }
0 commit comments