github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/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 should not be removed from state yet") 53 } 54 55 // the root module should do all the module cleanup 56 node = nodeCloseModule{addrs.RootModule} 57 diags = node.Execute(ctx, walkApply) 58 if diags.HasErrors() { 59 t.Fatalf("unexpected error: %s", diags.Err()) 60 } 61 62 // Since module.child has no resources, it should be removed 63 if _, ok := state.Modules["module.child"]; ok { 64 t.Fatal("module.child was not removed from state") 65 } 66 }) 67 68 // walkImport is a no-op 69 t.Run("walkImport", func(t *testing.T) { 70 state := states.NewState() 71 state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey)) 72 ctx := &MockEvalContext{ 73 StateState: state.SyncWrapper(), 74 } 75 node := nodeCloseModule{addrs.Module{"child"}} 76 77 diags := node.Execute(ctx, walkImport) 78 if diags.HasErrors() { 79 t.Fatalf("unexpected error: %s", diags.Err()) 80 } 81 if _, ok := state.Modules["module.child"]; !ok { 82 t.Fatal("module.child was removed from state, expected no-op") 83 } 84 }) 85 } 86 87 func TestNodeValidateModuleExecute(t *testing.T) { 88 t.Run("success", func(t *testing.T) { 89 ctx := &MockEvalContext{ 90 InstanceExpanderExpander: instances.NewExpander(), 91 } 92 ctx.installSimpleEval() 93 node := nodeValidateModule{ 94 nodeExpandModule{ 95 Addr: addrs.Module{"child"}, 96 ModuleCall: &configs.ModuleCall{ 97 Count: hcltest.MockExprLiteral(cty.NumberIntVal(2)), 98 }, 99 }, 100 } 101 102 diags := node.Execute(ctx, walkApply) 103 if diags.HasErrors() { 104 t.Fatalf("unexpected error: %v", diags.Err()) 105 } 106 }) 107 108 t.Run("invalid count", func(t *testing.T) { 109 ctx := &MockEvalContext{ 110 InstanceExpanderExpander: instances.NewExpander(), 111 } 112 ctx.installSimpleEval() 113 node := nodeValidateModule{ 114 nodeExpandModule{ 115 Addr: addrs.Module{"child"}, 116 ModuleCall: &configs.ModuleCall{ 117 Count: hcltest.MockExprLiteral(cty.StringVal("invalid")), 118 }, 119 }, 120 } 121 122 err := node.Execute(ctx, walkApply) 123 if err == nil { 124 t.Fatal("expected error, got success") 125 } 126 }) 127 128 }