github.com/yanndegat/hiera@v0.6.8/api/config_test.go (about)

     1  package api_test
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  	"github.com/yanndegat/hiera/api"
    11  	"github.com/yanndegat/hiera/hiera"
    12  )
    13  
    14  func TestConfigLookup_default(t *testing.T) {
    15  	wd, err := os.Getwd()
    16  	require.NoError(t, err)
    17  	options := map[string]string{api.HieraRoot: filepath.Join(wd, `testdata`, `defaultconfig`)}
    18  	hiera.DoWithParent(context.Background(), nil, options, func(hs api.Session) {
    19  		require.Equal(t, `value of first`, hiera.Lookup(hs.Invocation(nil, nil), `first`, nil, nil).String())
    20  	})
    21  }
    22  
    23  func TestConfigLookup_lyra_default(t *testing.T) {
    24  	wd, err := os.Getwd()
    25  	require.NoError(t, err)
    26  	options := map[string]string{api.HieraRoot: filepath.Join(wd, `testdata`, `defaultlyraconfig`)}
    27  	hiera.DoWithParent(context.Background(), nil, options, func(hs api.Session) {
    28  		require.Equal(t, `value of first`, hiera.Lookup(hs.Invocation(nil, nil), `first`, nil, nil).String())
    29  	})
    30  }
    31  
    32  func TestConfigLookup_explicit(t *testing.T) {
    33  	testExplicit(t, `first`, `first`, `value of first`)
    34  }
    35  
    36  func TestConfigLookup_hash_merge(t *testing.T) {
    37  	testExplicit(t, `hash`,
    38  		`hash`, `{"one":1,"three":{"a":"A","c":"C"},"two":"two"}`)
    39  }
    40  
    41  func TestConfigLookup_deep_merge(t *testing.T) {
    42  	testExplicit(t, `hash`,
    43  		``, `{"one":1,"two":"two","three":{"a":"A","c":"C","b":"B"}}`)
    44  }
    45  
    46  func TestConfigLookup_unique(t *testing.T) {
    47  	testExplicit(t, `array`,
    48  		`unique`, `{"one","two","three","four","five"}`)
    49  }
    50  
    51  func TestConfigLookup_sensitive(t *testing.T) {
    52  	testExplicit(t, `sense`,
    53  		``, `sensitive [value redacted]`)
    54  }
    55  
    56  func testExplicit(t *testing.T, key, merge, expected string) {
    57  	t.Helper()
    58  	wd, err := os.Getwd()
    59  	require.NoError(t, err)
    60  	options := map[string]string{api.HieraRoot: filepath.Join(wd, `testdata`, `explicit`)}
    61  	var luOpts map[string]string
    62  	if merge != `` {
    63  		luOpts = map[string]string{`merge`: merge}
    64  	}
    65  	hiera.DoWithParent(context.Background(), nil, options, func(hs api.Session) {
    66  		require.Equal(t, expected, hiera.Lookup(hs.Invocation(nil, nil), key, nil, luOpts).String())
    67  	})
    68  }