github.com/yanndegat/hiera@v0.6.8/examples/merge_test.go (about) 1 package examples_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/lyraproj/dgo/vf" 8 "github.com/yanndegat/hiera/api" 9 "github.com/yanndegat/hiera/hiera" 10 "github.com/yanndegat/hiera/merge" 11 "github.com/yanndegat/hiera/provider" 12 ) 13 14 // TestMerge_default tests the default merge strategy which is "first found" 15 func TestMerge_default(t *testing.T) { 16 configOptions := map[string]string{api.HieraConfig: `testdata/merge.yaml`} 17 hiera.DoWithParent(context.Background(), provider.ConfigLookupKey, configOptions, func(hs api.Session) { 18 // m.a only exists in the first provider 19 result := hiera.Lookup(hs.Invocation(nil, nil), `m.a`, nil, nil) 20 if result == nil || `first value of a` != result.String() { 21 t.Fatalf("unexpected result %v", result) 22 } 23 24 // m.b only exists in the second provider and is hence not found since the hashes are not merged 25 result = hiera.Lookup(hs.Invocation(nil, nil), `m.b`, nil, nil) 26 if result != nil { 27 t.Fatalf("unexpected result %v", result) 28 } 29 30 // m.c exists in both but since no merge occurs, the first one is selected 31 result = hiera.Lookup(hs.Invocation(nil, nil), `m.c`, nil, nil) 32 if result == nil || `first value of c` != result.String() { 33 t.Fatalf("unexpected result %v", result) 34 } 35 }) 36 } 37 38 // TestMerge_deep shows how to pass a merge option in a lookup. The possible merge options are: First, 39 // Unique, Hash, and Deep. Their behaviour should correspond to Puppet Hiera except for the current 40 // limitation that Deep cannot be fine tuned with additional options. So no "knock_out_prefix" etc. just 41 // yet. 42 // 43 // As with Puppet Hiera, merge options can also be specified as lookup_options in the data files. 44 func TestMerge_deep(t *testing.T) { 45 configOptions := map[string]string{api.HieraConfig: `testdata/merge.yaml`} 46 hiera.DoWithParent(context.Background(), provider.ConfigLookupKey, configOptions, func(hs api.Session) { 47 // options containing the merge option "deep" 48 opts := map[string]string{`merge`: `deep`} 49 50 // m.a only exists in the first provider 51 result := hiera.Lookup(hs.Invocation(nil, nil), `m.a`, nil, opts) 52 if result == nil || `first value of a` != result.String() { 53 t.Fatalf("unexpected result %v", result) 54 } 55 56 // m.b only exists in the second provider 57 result = hiera.Lookup(hs.Invocation(nil, nil), `m.b`, nil, opts) 58 if result == nil || `second value of b` != result.String() { 59 t.Fatalf("unexpected result %v", result) 60 } 61 62 // m.c exists in both and since a merge occurs, the first one has precedence 63 result = hiera.Lookup(hs.Invocation(nil, nil), `m.c`, nil, opts) 64 if result == nil || `first value of c` != result.String() { 65 t.Fatalf("unexpected result %v", result) 66 } 67 68 // obtain the full map and compare 69 result = hiera.Lookup(hs.Invocation(nil, nil), `m`, nil, opts) 70 if !vf.Value(map[string]string{`a`: `first value of a`, `b`: `second value of b`, `c`: `first value of c`}).Equals(result) { 71 t.Fatalf("unexpected result %v", result) 72 } 73 74 // obtain m and m2 using first found strategy (no options). Then merge them with an explicit call to DeepMerge 75 m := hiera.Lookup(hs.Invocation(nil, nil), `m`, nil, nil) 76 m2 := hiera.Lookup(hs.Invocation(nil, nil), `m2`, nil, nil) 77 result, ok := merge.Deep(m, m2, opts) 78 if !ok { 79 t.Fatal("DeepMerge failed") 80 } 81 if !vf.Value(map[string]string{`a`: `first value of a`, `b`: `third value of b`, `c`: `first value of c`}).Equals(result) { 82 t.Fatalf("unexpected result %v", result) 83 } 84 }) 85 }