github.com/whiteboxio/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/cfg/env_provider_test.go (about)

     1  package cfg
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/awesome-flow/flow/pkg/types"
    10  )
    11  
    12  func TestEnvProviderSetUp(t *testing.T) {
    13  	tests := []struct {
    14  		name         string
    15  		regs         []string
    16  		wantRegistry map[string]types.Value
    17  		wantRegs     []string
    18  	}{
    19  		{
    20  			"Empty set",
    21  			[]string{},
    22  			map[string]types.Value{},
    23  			[]string{},
    24  		},
    25  		{
    26  			"Simple KV pair not preffixed with FLOW_",
    27  			[]string{"FOO=BAR"},
    28  			map[string]types.Value{},
    29  			[]string{},
    30  		},
    31  		{
    32  			"Simple KV pair preffixed with FLOW_",
    33  			[]string{"FLOW_FOO=BAR"},
    34  			map[string]types.Value{"foo": "BAR"},
    35  			[]string{"foo"},
    36  		},
    37  		{
    38  			"A KV with nested key",
    39  			[]string{"FLOW_FOO_BAR_BAZ=moo"},
    40  			map[string]types.Value{"foo.bar.baz": "moo"},
    41  			[]string{"foo.bar.baz"},
    42  		},
    43  		{
    44  			"A value with underscore",
    45  			[]string{"FLOW_FOO=bar_baz_moo"},
    46  			map[string]types.Value{"foo": "bar_baz_moo"},
    47  			[]string{"foo"},
    48  		},
    49  		{
    50  			"A key with double underscore",
    51  			[]string{"FLOW_FOO__BAR=moo__baz"},
    52  			map[string]types.Value{"foo_bar": "moo__baz"},
    53  			[]string{"foo_bar"},
    54  		},
    55  	}
    56  
    57  	t.Parallel()
    58  
    59  	for _, testCase := range tests {
    60  		t.Run(testCase.name, func(t *testing.T) {
    61  			// Redefined function
    62  			oldEnvVars := envVars
    63  			envVars = func() []string { return testCase.regs }
    64  
    65  			repo := NewRepository()
    66  			prov, err := NewEnvProvider(repo, 0)
    67  			if err != nil {
    68  				t.Fatalf("Failed to initialize a new env provider: %s", err)
    69  			}
    70  			if err := prov.SetUp(repo); err != nil {
    71  				t.Fatalf("Failed to set up env provider: %s", err)
    72  			}
    73  
    74  			gotRegs := flattenRepo(repo)
    75  			for _, k := range testCase.wantRegs {
    76  				provs, ok := gotRegs[k]
    77  				if !ok {
    78  					t.Fatalf("Failed to find a registration for key %q: got regs: %#v", k, gotRegs)
    79  				}
    80  				if !reflect.DeepEqual(provs, []Provider{prov}) {
    81  					t.Fatalf("Unexpected provider list for key %q: %#v, want: %#v", k, provs, []Provider{prov})
    82  				}
    83  				delete(gotRegs, k)
    84  			}
    85  			if len(gotRegs) > 0 {
    86  				extraKeys := make([]string, 0, len(gotRegs))
    87  				for k := range gotRegs {
    88  					extraKeys = append(extraKeys, k)
    89  				}
    90  				sort.Strings(extraKeys)
    91  				t.Fatalf("Unexpected registration keys: %s", strings.Join(extraKeys, ", "))
    92  			}
    93  
    94  			if !reflect.DeepEqual(prov.registry, testCase.wantRegistry) {
    95  				t.Fatalf("Unexpected state for CliProvider.registry: want: %#v, got: %#v", testCase.wantRegistry, prov.registry)
    96  			}
    97  
    98  			envVars = oldEnvVars
    99  		})
   100  	}
   101  }