github.com/yanndegat/hiera@v0.6.8/hiera/hiera_test.go (about) 1 package hiera_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/lyraproj/dgo/dgo" 9 require "github.com/lyraproj/dgo/dgo_test" 10 "github.com/lyraproj/dgo/typ" 11 "github.com/lyraproj/dgo/vf" 12 sdk "github.com/lyraproj/hierasdk/hiera" 13 "github.com/yanndegat/hiera/api" 14 "github.com/yanndegat/hiera/hiera" 15 "github.com/yanndegat/hiera/provider" 16 ) 17 18 var options = vf.Map(`path`, `./testdata/sample_data.yaml`) 19 20 func TestLookup_first(t *testing.T) { 21 testOneLookup(t, func(iv api.Invocation) { 22 require.Equal(t, `value of first`, hiera.Lookup(iv, `first`, nil, nil)) 23 }) 24 } 25 26 func TestLookup_dottedInt(t *testing.T) { 27 testOneLookup(t, func(iv api.Invocation) { 28 require.Equal(t, `two`, hiera.Lookup(iv, `array.1`, nil, nil).String()) 29 }) 30 } 31 32 func TestLookup_dottedMix(t *testing.T) { 33 testOneLookup(t, func(iv api.Invocation) { 34 require.Equal(t, `value of first`, 35 hiera.Lookup(iv, `hash.array.1`, nil, nil).String()) 36 }) 37 } 38 39 func TestLookup_interpolate(t *testing.T) { 40 testOneLookup(t, func(iv api.Invocation) { 41 require.Equal(t, `includes 'value of first'`, 42 hiera.Lookup(iv, `second`, nil, nil).String()) 43 }) 44 } 45 46 func TestLookup_interpolateScope(t *testing.T) { 47 s := map[string]string{ 48 `world`: `cruel world`, 49 } 50 testLookup(t, func(hs api.Session) { 51 require.Equal(t, `hello cruel world`, hiera.Lookup(hs.Invocation(s, nil), `ipScope`, nil, nil)) 52 require.Equal(t, `hello cruel world`, hiera.Lookup(hs.Invocation(s, nil), `ipScope2`, nil, nil)) 53 }) 54 } 55 56 func TestLookup_interpolateEmpty(t *testing.T) { 57 testLookup(t, func(hs api.Session) { 58 require.Equal(t, `StartEnd`, hiera.Lookup(hs.Invocation(nil, nil), `empty1`, nil, nil)) 59 require.Equal(t, `StartEnd`, hiera.Lookup(hs.Invocation(nil, nil), `empty2`, nil, nil)) 60 require.Equal(t, `StartEnd`, hiera.Lookup(hs.Invocation(nil, nil), `empty3`, nil, nil)) 61 require.Equal(t, `StartEnd`, hiera.Lookup(hs.Invocation(nil, nil), `empty4`, nil, nil)) 62 require.Equal(t, `StartEnd`, hiera.Lookup(hs.Invocation(nil, nil), `empty5`, nil, nil)) 63 require.Equal(t, `StartEnd`, hiera.Lookup(hs.Invocation(nil, nil), `empty6`, nil, nil)) 64 }) 65 } 66 67 func TestLookup_interpolateLiteral(t *testing.T) { 68 testOneLookup(t, func(iv api.Invocation) { 69 require.Equal(t, `some literal text`, hiera.Lookup(iv, `ipLiteral`, nil, options)) 70 }) 71 } 72 73 func TestLookup_interpolateAlias(t *testing.T) { 74 testOneLookup(t, func(iv api.Invocation) { 75 require.Equal(t, vf.Strings("one", "two", "three"), hiera.Lookup(iv, `ipAlias`, nil, options)) 76 }) 77 } 78 79 func TestLookup_interpolateAliasYaml(t *testing.T) { 80 testOneLookup(t, func(iv api.Invocation) { 81 require.Equal(t, "- one\n- two\n- three\n", hiera.Lookup(iv, `array_yaml`, nil, options)) 82 }) 83 } 84 85 func TestLookup_interpolateAliasJson(t *testing.T) { 86 testOneLookup(t, func(iv api.Invocation) { 87 require.Equal(t, `["one","two","three"]`, hiera.Lookup(iv, `array_json`, nil, options)) 88 }) 89 } 90 91 func TestLookup_interpolateBadAlias(t *testing.T) { 92 require.NotOk(t, `'alias'/'strict_alias' interpolation is only permitted if the expression is equal to the entire string`, 93 hiera.TryWithParent(context.Background(), provider.YamlLookupKey, options, func(hs api.Session) error { 94 hiera.Lookup(hs.Invocation(nil, nil), `ipBadAlias`, nil, options) 95 return nil 96 })) 97 } 98 99 func TestLookup_interpolateBadFunction(t *testing.T) { 100 require.NotOk(t, `unknown interpolation method 'bad'`, 101 hiera.TryWithParent(context.Background(), provider.YamlLookupKey, options, func(hs api.Session) error { 102 hiera.Lookup(hs.Invocation(nil, nil), `ipBad`, nil, options) 103 return nil 104 })) 105 } 106 107 func TestLookup_notFoundWithoutDefault(t *testing.T) { 108 testOneLookup(t, func(iv api.Invocation) { 109 require.Nil(t, hiera.Lookup(iv, `nonexistent`, nil, options)) 110 }) 111 } 112 113 func TestLookup_notFoundDflt(t *testing.T) { 114 testOneLookup(t, func(iv api.Invocation) { 115 require.Equal(t, `default value`, hiera.Lookup(iv, `nonexistent`, vf.String(`default value`), options)) 116 }) 117 } 118 119 func TestLookup_notFoundDottedIdx(t *testing.T) { 120 testOneLookup(t, func(iv api.Invocation) { 121 require.Equal(t, `default value`, hiera.Lookup(iv, `array.3`, vf.String(`default value`), options)) 122 }) 123 } 124 125 func TestLookup_notFoundDottedMix(t *testing.T) { 126 testOneLookup(t, func(iv api.Invocation) { 127 require.Equal(t, `default value`, hiera.Lookup(iv, `hash.float`, vf.String(`default value`), options)) 128 }) 129 } 130 131 func TestLookup_badStringDig(t *testing.T) { 132 testOneLookup(t, func(iv api.Invocation) { 133 require.Nil(t, hiera.Lookup(iv, `hash.int.v`, nil, options)) 134 }) 135 } 136 137 func TestLookup_badIntDig(t *testing.T) { 138 testOneLookup(t, func(iv api.Invocation) { 139 require.Nil(t, hiera.Lookup(iv, `hash.int.3`, nil, options)) 140 }) 141 } 142 143 func TestLookup2_findFirst(t *testing.T) { 144 testOneLookup(t, func(iv api.Invocation) { 145 require.Equal(t, `value of first`, hiera.Lookup2(iv, []string{`first`, `second`}, typ.Any, nil, nil, nil, options, nil)) 146 }) 147 } 148 149 func TestLookup2_findSecond(t *testing.T) { 150 testOneLookup(t, func(iv api.Invocation) { 151 require.Equal(t, `includes 'value of first'`, hiera.Lookup2(iv, []string{`non existing`, `second`}, typ.Any, nil, nil, nil, options, nil)) 152 }) 153 } 154 155 func TestLookup2_notFoundWithoutDflt(t *testing.T) { 156 testOneLookup(t, func(iv api.Invocation) { 157 require.Nil(t, hiera.Lookup2(iv, []string{`non existing`, `not there`}, typ.Any, nil, nil, nil, options, nil)) 158 }) 159 } 160 161 func TestLookup2_notFoundDflt(t *testing.T) { 162 testOneLookup(t, func(iv api.Invocation) { 163 require.Equal(t, `default value`, hiera.Lookup2(iv, []string{`non existing`, `not there`}, typ.Any, vf.String(`default value`), nil, nil, options, nil)) 164 }) 165 } 166 167 func TestLookup_dottedStringInt(t *testing.T) { 168 testOneLookup(t, func(iv api.Invocation) { 169 require.Equal(t, `two`, hiera.Lookup(iv, `hash.array.0`, nil, options)) 170 }) 171 } 172 173 func ExampleLookup_mapProvider() { 174 sampleData := map[string]string{ 175 `a`: `value of a`, 176 `b`: `value of b`} 177 178 tp := func(ic sdk.ProviderContext, key string) dgo.Value { 179 if v, ok := sampleData[key]; ok { 180 return vf.String(v) 181 } 182 return nil 183 } 184 185 hiera.DoWithParent(context.Background(), tp, nil, func(hs api.Session) { 186 fmt.Println(hiera.Lookup(hs.Invocation(nil, nil), `a`, nil, nil)) 187 fmt.Println(hiera.Lookup(hs.Invocation(nil, nil), `b`, nil, nil)) 188 }) 189 190 // Output: 191 // value of a 192 // value of b 193 } 194 195 func testOneLookup(t *testing.T, f func(i api.Invocation)) { 196 t.Helper() 197 hiera.DoWithParent(context.Background(), provider.YamlLookupKey, options, func(hs api.Session) { 198 t.Helper() 199 f(hs.Invocation(nil, nil)) 200 }) 201 } 202 203 func testLookup(t *testing.T, f func(hs api.Session)) { 204 t.Helper() 205 hiera.DoWithParent(context.Background(), provider.YamlLookupKey, options, func(hs api.Session) { 206 t.Helper() 207 f(hs) 208 }) 209 }