github.com/outbrain/consul@v1.4.5/agent/structs/connect_proxy_config_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/consul/api"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestConnectProxyConfig_ToAPI(t *testing.T) {
    12  	tests := []struct {
    13  		name string
    14  		in   ConnectProxyConfig
    15  		want *api.AgentServiceConnectProxyConfig
    16  	}{
    17  		{
    18  			name: "service",
    19  			in: ConnectProxyConfig{
    20  				DestinationServiceName: "web",
    21  				DestinationServiceID:   "web1",
    22  				LocalServiceAddress:    "127.0.0.2",
    23  				LocalServicePort:       5555,
    24  				Config: map[string]interface{}{
    25  					"foo": "bar",
    26  				},
    27  				Upstreams: Upstreams{
    28  					{
    29  						DestinationType: UpstreamDestTypeService,
    30  						DestinationName: "foo",
    31  						Datacenter:      "dc1",
    32  						LocalBindPort:   1234,
    33  					},
    34  					{
    35  						DestinationType:  UpstreamDestTypePreparedQuery,
    36  						DestinationName:  "foo",
    37  						Datacenter:       "dc1",
    38  						LocalBindPort:    2345,
    39  						LocalBindAddress: "127.10.10.10",
    40  					},
    41  				},
    42  			},
    43  			want: &api.AgentServiceConnectProxyConfig{
    44  				DestinationServiceName: "web",
    45  				DestinationServiceID:   "web1",
    46  				LocalServiceAddress:    "127.0.0.2",
    47  				LocalServicePort:       5555,
    48  				Config: map[string]interface{}{
    49  					"foo": "bar",
    50  				},
    51  				Upstreams: []api.Upstream{
    52  					{
    53  						DestinationType: UpstreamDestTypeService,
    54  						DestinationName: "foo",
    55  						Datacenter:      "dc1",
    56  						LocalBindPort:   1234,
    57  					},
    58  					{
    59  						DestinationType:  UpstreamDestTypePreparedQuery,
    60  						DestinationName:  "foo",
    61  						Datacenter:       "dc1",
    62  						LocalBindPort:    2345,
    63  						LocalBindAddress: "127.10.10.10",
    64  					},
    65  				},
    66  			},
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  			require.Equal(t, tt.want, tt.in.ToAPI())
    72  		})
    73  	}
    74  }
    75  
    76  func TestUpstream_MarshalJSON(t *testing.T) {
    77  	tests := []struct {
    78  		name    string
    79  		in      Upstream
    80  		want    string
    81  		wantErr bool
    82  	}{
    83  		{
    84  			name: "service",
    85  			in: Upstream{
    86  				DestinationType: UpstreamDestTypeService,
    87  				DestinationName: "foo",
    88  				Datacenter:      "dc1",
    89  				LocalBindPort:   1234,
    90  			},
    91  			want: `{
    92  				"DestinationType": "service",
    93  				"DestinationName": "foo",
    94  				"Datacenter": "dc1",
    95  				"LocalBindPort": 1234,
    96  				"Config": null
    97  			}`,
    98  			wantErr: false,
    99  		},
   100  		{
   101  			name: "pq",
   102  			in: Upstream{
   103  				DestinationType: UpstreamDestTypePreparedQuery,
   104  				DestinationName: "foo",
   105  				Datacenter:      "dc1",
   106  				LocalBindPort:   1234,
   107  			},
   108  			want: `{
   109  				"DestinationType": "prepared_query",
   110  				"DestinationName": "foo",
   111  				"Datacenter": "dc1",
   112  				"LocalBindPort": 1234,
   113  				"Config": null
   114  			}`,
   115  			wantErr: false,
   116  		},
   117  	}
   118  	for _, tt := range tests {
   119  		t.Run(tt.name, func(t *testing.T) {
   120  			require := require.New(t)
   121  			got, err := json.Marshal(tt.in)
   122  			if tt.wantErr {
   123  				require.Error(err)
   124  				return
   125  			}
   126  			require.NoError(err)
   127  			require.JSONEq(tt.want, string(got))
   128  		})
   129  	}
   130  }
   131  
   132  func TestUpstream_UnmarshalJSON(t *testing.T) {
   133  	tests := []struct {
   134  		name    string
   135  		json    string
   136  		want    Upstream
   137  		wantErr bool
   138  	}{
   139  		{
   140  			name: "service",
   141  			json: `{
   142  				"DestinationType": "service",
   143  				"DestinationName": "foo",
   144  				"Datacenter": "dc1"
   145  			}`,
   146  			want: Upstream{
   147  				DestinationType: UpstreamDestTypeService,
   148  				DestinationName: "foo",
   149  				Datacenter:      "dc1",
   150  			},
   151  			wantErr: false,
   152  		},
   153  		{
   154  			name: "pq",
   155  			json: `{
   156  				"DestinationType": "prepared_query",
   157  				"DestinationName": "foo",
   158  				"Datacenter": "dc1"
   159  			}`,
   160  			want: Upstream{
   161  				DestinationType: UpstreamDestTypePreparedQuery,
   162  				DestinationName: "foo",
   163  				Datacenter:      "dc1",
   164  			},
   165  			wantErr: false,
   166  		},
   167  	}
   168  	for _, tt := range tests {
   169  		t.Run(tt.name, func(t *testing.T) {
   170  			require := require.New(t)
   171  			var got Upstream
   172  			err := json.Unmarshal([]byte(tt.json), &got)
   173  			if tt.wantErr {
   174  				require.Error(err)
   175  				return
   176  			}
   177  			require.NoError(err)
   178  			require.Equal(tt.want, got)
   179  		})
   180  	}
   181  }