github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/structs/connect_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestConnectManagedProxy_ParseConfig(t *testing.T) {
     9  	tests := []struct {
    10  		name    string
    11  		config  map[string]interface{}
    12  		want    *ConnectManagedProxyConfig
    13  		wantErr bool
    14  	}{
    15  		{
    16  			name:    "empty",
    17  			config:  nil,
    18  			want:    &ConnectManagedProxyConfig{},
    19  			wantErr: false,
    20  		},
    21  		{
    22  			name: "specified",
    23  			config: map[string]interface{}{
    24  				"bind_address": "127.0.0.1",
    25  				"bind_port":    1234,
    26  			},
    27  			want: &ConnectManagedProxyConfig{
    28  				BindAddress: "127.0.0.1",
    29  				BindPort:    1234,
    30  			},
    31  			wantErr: false,
    32  		},
    33  		{
    34  			name: "stringy port",
    35  			config: map[string]interface{}{
    36  				"bind_address": "127.0.0.1",
    37  				"bind_port":    "1234",
    38  			},
    39  			want: &ConnectManagedProxyConfig{
    40  				BindAddress: "127.0.0.1",
    41  				BindPort:    1234,
    42  			},
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name: "empty addr",
    47  			config: map[string]interface{}{
    48  				"bind_address": "",
    49  				"bind_port":    "1234",
    50  			},
    51  			want: &ConnectManagedProxyConfig{
    52  				BindAddress: "",
    53  				BindPort:    1234,
    54  			},
    55  			wantErr: false,
    56  		},
    57  		{
    58  			name: "empty port",
    59  			config: map[string]interface{}{
    60  				"bind_address": "127.0.0.1",
    61  				"bind_port":    "",
    62  			},
    63  			want:    nil,
    64  			wantErr: true,
    65  		},
    66  		{
    67  			name: "junk address",
    68  			config: map[string]interface{}{
    69  				"bind_address": 42,
    70  				"bind_port":    "",
    71  			},
    72  			want:    nil,
    73  			wantErr: true,
    74  		},
    75  		{
    76  			name: "zero port, missing addr",
    77  			config: map[string]interface{}{
    78  				"bind_port": 0,
    79  			},
    80  			want: &ConnectManagedProxyConfig{
    81  				BindPort: 0,
    82  			},
    83  			wantErr: false,
    84  		},
    85  		{
    86  			name: "extra fields present",
    87  			config: map[string]interface{}{
    88  				"bind_port": 1234,
    89  				"flamingos": true,
    90  				"upstream": []map[string]interface{}{
    91  					{"foo": "bar"},
    92  				},
    93  			},
    94  			want: &ConnectManagedProxyConfig{
    95  				BindPort: 1234,
    96  			},
    97  			wantErr: false,
    98  		},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			p := &ConnectManagedProxy{
   103  				Config: tt.config,
   104  			}
   105  			got, err := p.ParseConfig()
   106  			if (err != nil) != tt.wantErr {
   107  				t.Errorf("ConnectManagedProxy.ParseConfig() error = %v, wantErr %v", err, tt.wantErr)
   108  				return
   109  			}
   110  			if !reflect.DeepEqual(got, tt.want) {
   111  				t.Errorf("ConnectManagedProxy.ParseConfig() = %v, want %v", got, tt.want)
   112  			}
   113  		})
   114  	}
   115  }