github.com/lyraproj/hiera@v1.0.0-rc4/examples/module_test.go (about)

     1  package examples
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/lyraproj/dgo/vf"
     9  	"github.com/lyraproj/hiera/api"
    10  	"github.com/lyraproj/hiera/hiera"
    11  	"github.com/lyraproj/hiera/provider"
    12  	sdk "github.com/lyraproj/hierasdk/hiera"
    13  )
    14  
    15  // TestHelloWorld_globalAndModules uses the MuxLookupKey to inject two lookup_key functions. The ConfigLookupKey
    16  // that consults the yaml config and the ModuleLookupKey that consults the provider.ModulePath to find modules
    17  // that in turn contains additional configuration and data.
    18  func TestHelloWorld_globalAndModules(t *testing.T) {
    19  	configOptions := vf.Map(
    20  		provider.LookupKeyFunctions, []sdk.LookupKey{provider.ConfigLookupKey, provider.ModuleLookupKey},
    21  		api.HieraRoot, `testdata`,
    22  		provider.ModulePath, filepath.Join(`testdata`, `modules`))
    23  
    24  	// Initialize a Hiera session with the MuxLookupKey as the top-level function configured using the configOptions.
    25  	hiera.DoWithParent(context.Background(), provider.MuxLookupKey, configOptions, func(hs api.Session) {
    26  		// A lookup of just "hello" should hit the first provider, the ConfigLookupKey.
    27  		result := hiera.Lookup(hs.Invocation(nil, nil), `hello`, nil, nil)
    28  		if result == nil || `yaml data says hello` != result.String() {
    29  			t.Fatalf("unexpected result %v", result)
    30  		}
    31  
    32  		// A lookup of "one::a" is found in the module "one"
    33  		result = hiera.Lookup(hs.Invocation(nil, nil), `one::a`, nil, nil)
    34  		if result == nil || `value of one::a` != result.String() {
    35  			t.Fatalf("unexpected result %v", result)
    36  		}
    37  
    38  		// A lookup of "one::merge" is found in the ConfigLookupKey provider and in module "one". The lookup_options
    39  		// declared in the module "one" stipulates a deep merge.
    40  		result = hiera.Lookup(hs.Invocation(nil, nil), `one::merge`, nil, nil)
    41  		if result == nil || `{"a":"value of one::merge a","b":"value of one::merge b"}` != result.String() {
    42  			t.Fatalf("unexpected result %v", result)
    43  		}
    44  
    45  		// A lookup of "three::a" will not find a value because the "three" directory does not contain a hiera.yaml
    46  		result = hiera.Lookup(hs.Invocation(nil, nil), `three::a`, nil, nil)
    47  		if result != nil {
    48  			t.Fatalf("unexpected result %v", result)
    49  		}
    50  	})
    51  }
    52  
    53  // TestHelloWorld_globalAndModules_nonExistentPath uses a path that doesn't appoint a directory.
    54  func TestHelloWorld_globalAndModules_nonExistentPath(t *testing.T) {
    55  	configOptions := vf.Map(
    56  		provider.LookupKeyFunctions, []sdk.LookupKey{provider.ConfigLookupKey, provider.ModuleLookupKey},
    57  		api.HieraRoot, `testdata`,
    58  		provider.ModulePath, filepath.Join(`testdata`, `nomodules`))
    59  
    60  	hiera.DoWithParent(context.Background(), provider.MuxLookupKey, configOptions, func(hs api.Session) {
    61  		result := hiera.Lookup(hs.Invocation(nil, nil), `one::a`, nil, nil)
    62  		if result != nil {
    63  			t.Fatalf("unexpected result %v", result)
    64  		}
    65  	})
    66  }