github.com/kanishk98/terraform@v1.3.0-dev.0.20220917174235-661ca8088a6a/internal/terraform/node_module_expand_test.go (about) 1 package terraform 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/hcl/v2/hcltest" 7 "github.com/hashicorp/terraform/internal/addrs" 8 "github.com/hashicorp/terraform/internal/configs" 9 "github.com/hashicorp/terraform/internal/instances" 10 "github.com/hashicorp/terraform/internal/states" 11 "github.com/zclconf/go-cty/cty" 12 ) 13 14 func TestNodeExpandModuleExecute(t *testing.T) { 15 ctx := &MockEvalContext{ 16 InstanceExpanderExpander: instances.NewExpander(), 17 } 18 ctx.installSimpleEval() 19 20 node := nodeExpandModule{ 21 Addr: addrs.Module{"child"}, 22 ModuleCall: &configs.ModuleCall{ 23 Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)), 24 }, 25 } 26 27 err := node.Execute(ctx, walkApply) 28 if err != nil { 29 t.Fatalf("unexpected error: %s", err) 30 } 31 32 if !ctx.InstanceExpanderCalled { 33 t.Fatal("did not expand") 34 } 35 } 36 37 func TestNodeCloseModuleExecute(t *testing.T) { 38 t.Run("walkApply", func(t *testing.T) { 39 state := states.NewState() 40 state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey)) 41 ctx := &MockEvalContext{ 42 StateState: state.SyncWrapper(), 43 } 44 node := nodeCloseModule{addrs.Module{"child"}} 45 diags := node.Execute(ctx, walkApply) 46 if diags.HasErrors() { 47 t.Fatalf("unexpected error: %s", diags.Err()) 48 } 49 50 // Since module.child has no resources, it should be removed 51 if _, ok := state.Modules["module.child"]; ok { 52 t.Fatal("module.child was not removed from state") 53 } 54 }) 55 56 // walkImport is a no-op 57 t.Run("walkImport", func(t *testing.T) { 58 state := states.NewState() 59 state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey)) 60 ctx := &MockEvalContext{ 61 StateState: state.SyncWrapper(), 62 } 63 node := nodeCloseModule{addrs.Module{"child"}} 64 65 diags := node.Execute(ctx, walkImport) 66 if diags.HasErrors() { 67 t.Fatalf("unexpected error: %s", diags.Err()) 68 } 69 if _, ok := state.Modules["module.child"]; !ok { 70 t.Fatal("module.child was removed from state, expected no-op") 71 } 72 }) 73 } 74 75 func TestNodeValidateModuleExecute(t *testing.T) { 76 t.Run("success", func(t *testing.T) { 77 ctx := &MockEvalContext{ 78 InstanceExpanderExpander: instances.NewExpander(), 79 } 80 ctx.installSimpleEval() 81 node := nodeValidateModule{ 82 nodeExpandModule{ 83 Addr: addrs.Module{"child"}, 84 ModuleCall: &configs.ModuleCall{ 85 Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)), 86 }, 87 }, 88 } 89 90 diags := node.Execute(ctx, walkApply) 91 if diags.HasErrors() { 92 t.Fatalf("unexpected error: %v", diags.Err()) 93 } 94 }) 95 96 t.Run("invalid count", func(t *testing.T) { 97 ctx := &MockEvalContext{ 98 InstanceExpanderExpander: instances.NewExpander(), 99 } 100 ctx.installSimpleEval() 101 node := nodeValidateModule{ 102 nodeExpandModule{ 103 Addr: addrs.Module{"child"}, 104 ModuleCall: &configs.ModuleCall{ 105 Count: hcltest.MockExprLiteral(cty.StringVal("invalid")), 106 }, 107 }, 108 } 109 110 err := node.Execute(ctx, walkApply) 111 if err == nil { 112 t.Fatal("expected error, got success") 113 } 114 }) 115 116 }