github.com/lyraproj/hiera@v1.0.0-rc4/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 "github.com/lyraproj/dgo/test/require" 10 "github.com/lyraproj/dgo/typ" 11 "github.com/lyraproj/dgo/vf" 12 "github.com/lyraproj/hiera/api" 13 "github.com/lyraproj/hiera/hiera" 14 "github.com/lyraproj/hiera/provider" 15 sdk "github.com/lyraproj/hierasdk/hiera" 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_interpolateBadAlias(t *testing.T) { 80 require.Error(t, `'alias'/'strict_alias' interpolation is only permitted if the expression is equal to the entire string`, 81 hiera.TryWithParent(context.Background(), provider.YamlLookupKey, options, func(hs api.Session) error { 82 hiera.Lookup(hs.Invocation(nil, nil), `ipBadAlias`, nil, options) 83 return nil 84 })) 85 } 86 87 func TestLookup_interpolateBadFunction(t *testing.T) { 88 require.Error(t, `unknown interpolation method 'bad'`, 89 hiera.TryWithParent(context.Background(), provider.YamlLookupKey, options, func(hs api.Session) error { 90 hiera.Lookup(hs.Invocation(nil, nil), `ipBad`, nil, options) 91 return nil 92 })) 93 } 94 95 func TestLookup_notFoundWithoutDefault(t *testing.T) { 96 testOneLookup(t, func(iv api.Invocation) { 97 require.Nil(t, hiera.Lookup(iv, `nonexistent`, nil, options)) 98 }) 99 } 100 101 func TestLookup_notFoundDflt(t *testing.T) { 102 testOneLookup(t, func(iv api.Invocation) { 103 require.Equal(t, `default value`, hiera.Lookup(iv, `nonexistent`, vf.String(`default value`), options)) 104 }) 105 } 106 107 func TestLookup_notFoundDottedIdx(t *testing.T) { 108 testOneLookup(t, func(iv api.Invocation) { 109 require.Equal(t, `default value`, hiera.Lookup(iv, `array.3`, vf.String(`default value`), options)) 110 }) 111 } 112 113 func TestLookup_notFoundDottedMix(t *testing.T) { 114 testOneLookup(t, func(iv api.Invocation) { 115 require.Equal(t, `default value`, hiera.Lookup(iv, `hash.float`, vf.String(`default value`), options)) 116 }) 117 } 118 119 func TestLookup_badStringDig(t *testing.T) { 120 testOneLookup(t, func(iv api.Invocation) { 121 require.Nil(t, hiera.Lookup(iv, `hash.int.v`, nil, options)) 122 }) 123 } 124 125 func TestLookup_badIntDig(t *testing.T) { 126 testOneLookup(t, func(iv api.Invocation) { 127 require.Nil(t, hiera.Lookup(iv, `hash.int.3`, nil, options)) 128 }) 129 } 130 131 func TestLookup2_findFirst(t *testing.T) { 132 testOneLookup(t, func(iv api.Invocation) { 133 require.Equal(t, `value of first`, hiera.Lookup2(iv, []string{`first`, `second`}, typ.Any, nil, nil, nil, options, nil)) 134 }) 135 } 136 137 func TestLookup2_findSecond(t *testing.T) { 138 testOneLookup(t, func(iv api.Invocation) { 139 require.Equal(t, `includes 'value of first'`, hiera.Lookup2(iv, []string{`non existing`, `second`}, typ.Any, nil, nil, nil, options, nil)) 140 }) 141 } 142 143 func TestLookup2_notFoundWithoutDflt(t *testing.T) { 144 testOneLookup(t, func(iv api.Invocation) { 145 require.Nil(t, hiera.Lookup2(iv, []string{`non existing`, `not there`}, typ.Any, nil, nil, nil, options, nil)) 146 }) 147 } 148 149 func TestLookup2_notFoundDflt(t *testing.T) { 150 testOneLookup(t, func(iv api.Invocation) { 151 require.Equal(t, `default value`, hiera.Lookup2(iv, []string{`non existing`, `not there`}, typ.Any, vf.String(`default value`), nil, nil, options, nil)) 152 }) 153 } 154 155 func TestLookup_dottedStringInt(t *testing.T) { 156 testOneLookup(t, func(iv api.Invocation) { 157 require.Equal(t, `two`, hiera.Lookup(iv, `hash.array.0`, nil, options)) 158 }) 159 } 160 161 func ExampleLookup_mapProvider() { 162 sampleData := map[string]string{ 163 `a`: `value of a`, 164 `b`: `value of b`} 165 166 tp := func(ic sdk.ProviderContext, key string) dgo.Value { 167 if v, ok := sampleData[key]; ok { 168 return vf.String(v) 169 } 170 return nil 171 } 172 173 hiera.DoWithParent(context.Background(), tp, nil, func(hs api.Session) { 174 fmt.Println(hiera.Lookup(hs.Invocation(nil, nil), `a`, nil, nil)) 175 fmt.Println(hiera.Lookup(hs.Invocation(nil, nil), `b`, nil, nil)) 176 }) 177 178 // Output: 179 // value of a 180 // value of b 181 } 182 183 func testOneLookup(t *testing.T, f func(i api.Invocation)) { 184 t.Helper() 185 hiera.DoWithParent(context.Background(), provider.YamlLookupKey, options, func(hs api.Session) { 186 t.Helper() 187 f(hs.Invocation(nil, nil)) 188 }) 189 } 190 191 func testLookup(t *testing.T, f func(hs api.Session)) { 192 t.Helper() 193 hiera.DoWithParent(context.Background(), provider.YamlLookupKey, options, func(hs api.Session) { 194 t.Helper() 195 f(hs) 196 }) 197 }