github.com/avenga/couper@v1.12.2/config/body/body_test.go (about) 1 package body_test 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/hcl/v2/hclsyntax" 7 "github.com/zclconf/go-cty/cty" 8 9 "github.com/avenga/couper/config/body" 10 "github.com/avenga/couper/eval" 11 ) 12 13 func TestBody_MergeBodies(t *testing.T) { 14 tests := []struct { 15 name string 16 src *hclsyntax.Body 17 replace bool 18 expAttrs map[string]string 19 expBlocksTotal int 20 expBlocksOfType map[string]int 21 }{ 22 { 23 "merge, replace", 24 &hclsyntax.Body{ 25 Attributes: hclsyntax.Attributes{ 26 "a": &hclsyntax.Attribute{Name: "a", Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("a2")}}, 27 "c": &hclsyntax.Attribute{Name: "c", Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("c")}}, 28 }, 29 Blocks: hclsyntax.Blocks{ 30 &hclsyntax.Block{Type: "a", Labels: []string{}, Body: &hclsyntax.Body{}}, 31 &hclsyntax.Block{Type: "b", Labels: []string{"label"}, Body: &hclsyntax.Body{}}, 32 &hclsyntax.Block{Type: "c", Labels: []string{"label"}, Body: &hclsyntax.Body{}}, 33 }, 34 }, 35 true, 36 map[string]string{"a": "a2", "b": "b", "c": "c"}, 37 6, 38 map[string]int{"a": 2, "b": 2, "c": 2}, 39 }, 40 { 41 "merge, no replace", 42 &hclsyntax.Body{ 43 Attributes: hclsyntax.Attributes{ 44 "a": &hclsyntax.Attribute{Name: "a", Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("a2")}}, 45 "c": &hclsyntax.Attribute{Name: "c", Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("c")}}, 46 }, 47 Blocks: hclsyntax.Blocks{ 48 &hclsyntax.Block{Type: "a", Labels: []string{}, Body: &hclsyntax.Body{}}, 49 &hclsyntax.Block{Type: "b", Labels: []string{"label"}, Body: &hclsyntax.Body{}}, 50 &hclsyntax.Block{Type: "c", Labels: []string{"label"}, Body: &hclsyntax.Body{}}, 51 }, 52 }, 53 false, 54 map[string]string{"a": "a1", "b": "b", "c": "c"}, 55 6, 56 map[string]int{"a": 2, "b": 2, "c": 2}, 57 }, 58 { 59 "'merge' with self, replace", 60 nil, 61 true, 62 map[string]string{"a": "a1", "b": "b"}, 63 3, 64 map[string]int{"a": 1, "b": 1, "c": 1}, 65 }, 66 { 67 "'merge' with self, no replace", 68 nil, 69 false, 70 map[string]string{"a": "a1", "b": "b"}, 71 3, 72 map[string]int{"a": 1, "b": 1, "c": 1}, 73 }, 74 } 75 76 hclcontext := eval.NewDefaultContext().HCLContext() 77 78 for _, tt := range tests { 79 t.Run(tt.name, func(subT *testing.T) { 80 dest := &hclsyntax.Body{ 81 Attributes: hclsyntax.Attributes{ 82 "a": &hclsyntax.Attribute{Name: "a", Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("a1")}}, 83 "b": &hclsyntax.Attribute{Name: "b", Expr: &hclsyntax.LiteralValueExpr{Val: cty.StringVal("b")}}, 84 }, 85 Blocks: hclsyntax.Blocks{ 86 &hclsyntax.Block{Type: "a", Labels: []string{}, Body: &hclsyntax.Body{}}, 87 &hclsyntax.Block{Type: "b", Labels: []string{"label"}, Body: &hclsyntax.Body{}}, 88 &hclsyntax.Block{Type: "c", Labels: []string{""}, Body: &hclsyntax.Body{}}, 89 }, 90 } 91 src := tt.src 92 if tt.src == nil { 93 src = dest 94 } 95 merged := body.MergeBodies(dest, src, tt.replace) 96 if len(merged.Attributes) != len(tt.expAttrs) { 97 subT.Fatalf("expected %d attributes, was %d", len(tt.expAttrs), len(merged.Attributes)) 98 } 99 for k, expV := range tt.expAttrs { 100 attr, set := merged.Attributes[k] 101 if !set { 102 subT.Errorf("expected attribute %q set", k) 103 } 104 val, diags := attr.Expr.Value(hclcontext) 105 if diags.HasErrors() { 106 subT.Error(diags) 107 } 108 sVal := val.AsString() 109 if val.AsString() != expV { 110 subT.Errorf("attribute value for %q:\nwant: %q\ngot: %q", k, expV, sVal) 111 } 112 } 113 if len(merged.Blocks) != tt.expBlocksTotal { 114 subT.Fatalf("expected %d blocks, was %d", tt.expBlocksTotal, len(merged.Blocks)) 115 } 116 if len(body.BlocksOfType(merged, "a")) != tt.expBlocksOfType["a"] { 117 subT.Errorf("expected %d blocks of type a", tt.expBlocksOfType["a"]) 118 } 119 if len(body.BlocksOfType(merged, "b")) != tt.expBlocksOfType["b"] { 120 subT.Errorf("expected %d blocks of type b", tt.expBlocksOfType["b"]) 121 } 122 if len(body.BlocksOfType(merged, "c")) != tt.expBlocksOfType["c"] { 123 subT.Errorf("expected %d blocks of type c", tt.expBlocksOfType["c"]) 124 } 125 }) 126 } 127 }