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

     1  package cfg
     2  
     3  import (
     4  	"reflect"
     5  	"sort"
     6  	"strings"
     7  	"testing"
     8  
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  const (
    13  	DefaultWeight = 10
    14  	sampleYaml    = `
    15  system:
    16    maxprocs: 4
    17    admin:
    18      enabled: true
    19  components:
    20    udp_rcv:
    21      module: receiver.udp
    22      params:
    23        bind_addr: localhost:3101
    24    fanout:
    25      module: link.fanout
    26    tcp_sink_7222:
    27      module: sink.tcp
    28      params:
    29        bind_addr: localhost:7222
    30    tcp_sink_7223:
    31      module: sink.tcp
    32      params:
    33        bind_addr: localhost:7223
    34  pipeline:
    35    udp_rcv:
    36      connect: fanout
    37    fanout:
    38      links:
    39        - tcp_sink_7222
    40        - tcp_sink_7223
    41  `
    42  )
    43  
    44  func TestYamlProviderSetUp(t *testing.T) {
    45  	tests := []struct {
    46  		name     string
    47  		src      []byte
    48  		options  *YamlProviderOptions
    49  		wantRegs []string
    50  	}{
    51  		{
    52  			"empty yaml",
    53  			[]byte(""),
    54  			&YamlProviderOptions{},
    55  			[]string{},
    56  		},
    57  		{
    58  			"Sample yaml",
    59  			[]byte(sampleYaml),
    60  			&YamlProviderOptions{},
    61  			[]string{
    62  				"system.maxprocs",
    63  				"system.admin.enabled",
    64  				"components.udp_rcv.params.bind_addr",
    65  				"components.udp_rcv.module",
    66  				"components.tcp_sink_7223.params.bind_addr",
    67  				"components.tcp_sink_7223.module",
    68  				"components.tcp_sink_7222.params.bind_addr",
    69  				"components.tcp_sink_7222.module",
    70  				"components.fanout.module",
    71  				"pipeline.udp_rcv.connect",
    72  				"pipeline.fanout.links",
    73  			},
    74  		},
    75  	}
    76  
    77  	t.Parallel()
    78  
    79  	for _, testCase := range tests {
    80  		t.Run(testCase.name, func(t *testing.T) {
    81  
    82  			// Redefining the original value
    83  			oldReadRaw := readRaw
    84  			readRaw = func(source string) (map[interface{}]interface{}, error) {
    85  				out := make(map[interface{}]interface{})
    86  				if err := yaml.Unmarshal(testCase.src, &out); err != nil {
    87  					return nil, err
    88  				}
    89  				return out, nil
    90  			}
    91  
    92  			repo := NewRepository()
    93  			prov, err := NewYamlProviderFromSource(repo, 0, testCase.options, "dummy.dummy")
    94  			if err != nil {
    95  				t.Fatalf("Failed to initialize a new yaml provider: %s", err)
    96  			}
    97  			if err := prov.SetUp(repo); err != nil {
    98  				t.Fatalf("Failed to set up yaml provider: %s", err)
    99  			}
   100  			gotRegs := flattenRepo(repo)
   101  			for _, k := range testCase.wantRegs {
   102  				provs, ok := gotRegs[k]
   103  				if !ok {
   104  					t.Fatalf("Failed to find a registration for key %q", k)
   105  				}
   106  				if !reflect.DeepEqual(provs, []Provider{prov}) {
   107  					t.Fatalf("Unexpected provider list for key %q: %#v, want: %#v", k, provs, []Provider{prov})
   108  				}
   109  				delete(gotRegs, k)
   110  			}
   111  			if len(gotRegs) > 0 {
   112  				extraKeys := make([]string, 0, len(gotRegs))
   113  				for k := range gotRegs {
   114  					extraKeys = append(extraKeys, k)
   115  				}
   116  				sort.Strings(extraKeys)
   117  				t.Fatalf("Unexpected registration keys: %s", strings.Join(extraKeys, ", "))
   118  			}
   119  
   120  			readRaw = oldReadRaw
   121  		})
   122  	}
   123  }