github.com/nginxinc/kubernetes-ingress@v1.12.5/internal/configs/transportserver_test.go (about)

     1  package configs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/nginxinc/kubernetes-ingress/internal/configs/version2"
     8  	conf_v1alpha1 "github.com/nginxinc/kubernetes-ingress/pkg/apis/configuration/v1alpha1"
     9  	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  )
    11  
    12  func TestUpstreamNamerForTransportServer(t *testing.T) {
    13  	transportServer := conf_v1alpha1.TransportServer{
    14  		ObjectMeta: meta_v1.ObjectMeta{
    15  			Name:      "tcp-app",
    16  			Namespace: "default",
    17  		},
    18  	}
    19  	upstreamNamer := newUpstreamNamerForTransportServer(&transportServer)
    20  	upstream := "test"
    21  
    22  	expected := "ts_default_tcp-app_test"
    23  
    24  	result := upstreamNamer.GetNameForUpstream(upstream)
    25  	if result != expected {
    26  		t.Errorf("GetNameForUpstream() returned %s but expected %v", result, expected)
    27  	}
    28  }
    29  
    30  func TestTransportServerExString(t *testing.T) {
    31  	tests := []struct {
    32  		input    *TransportServerEx
    33  		expected string
    34  	}{
    35  		{
    36  			input: &TransportServerEx{
    37  				TransportServer: &conf_v1alpha1.TransportServer{
    38  					ObjectMeta: meta_v1.ObjectMeta{
    39  						Name:      "test-server",
    40  						Namespace: "default",
    41  					},
    42  				},
    43  			},
    44  			expected: "default/test-server",
    45  		},
    46  		{
    47  			input:    &TransportServerEx{},
    48  			expected: "TransportServerEx has no TransportServer",
    49  		},
    50  		{
    51  			input:    nil,
    52  			expected: "<nil>",
    53  		},
    54  	}
    55  
    56  	for _, test := range tests {
    57  		result := test.input.String()
    58  		if result != test.expected {
    59  			t.Errorf("TransportServerEx.String() returned %v but expected %v", result, test.expected)
    60  		}
    61  	}
    62  }
    63  
    64  func TestGenerateTransportServerConfigForTCPSnippets(t *testing.T) {
    65  	transportServerEx := TransportServerEx{
    66  		TransportServer: &conf_v1alpha1.TransportServer{
    67  			ObjectMeta: meta_v1.ObjectMeta{
    68  				Name:      "tcp-server",
    69  				Namespace: "default",
    70  			},
    71  			Spec: conf_v1alpha1.TransportServerSpec{
    72  				Listener: conf_v1alpha1.TransportServerListener{
    73  					Name:     "tcp-listener",
    74  					Protocol: "TCP",
    75  				},
    76  				Upstreams: []conf_v1alpha1.Upstream{
    77  					{
    78  						Name:    "tcp-app",
    79  						Service: "tcp-app-svc",
    80  						Port:    5001,
    81  					},
    82  				},
    83  				Action: &conf_v1alpha1.Action{
    84  					Pass: "tcp-app",
    85  				},
    86  				ServerSnippets: "deny  192.168.1.1;\nallow 192.168.1.0/24;",
    87  				StreamSnippets: "limit_conn_zone $binary_remote_addr zone=addr:10m;",
    88  			},
    89  		},
    90  		Endpoints: map[string][]string{
    91  			"default/tcp-app-svc:5001": {
    92  				"10.0.0.20:5001",
    93  			},
    94  		},
    95  	}
    96  
    97  	listenerPort := 2020
    98  
    99  	expected := &version2.TransportServerConfig{
   100  		Upstreams: []version2.StreamUpstream{
   101  			{
   102  				Name: "ts_default_tcp-server_tcp-app",
   103  				Servers: []version2.StreamUpstreamServer{
   104  					{
   105  						Address:     "10.0.0.20:5001",
   106  						MaxFails:    1,
   107  						FailTimeout: "10s",
   108  					},
   109  				},
   110  				UpstreamLabels: version2.UpstreamLabels{
   111  					ResourceName:      "tcp-server",
   112  					ResourceType:      "transportserver",
   113  					ResourceNamespace: "default",
   114  					Service:           "tcp-app-svc",
   115  				},
   116  				LoadBalancingMethod: "random two least_conn",
   117  			},
   118  		},
   119  		Server: version2.StreamServer{
   120  			Port:                     listenerPort,
   121  			UDP:                      false,
   122  			StatusZone:               "tcp-listener",
   123  			ProxyPass:                "ts_default_tcp-server_tcp-app",
   124  			Name:                     "tcp-server",
   125  			Namespace:                "default",
   126  			ProxyConnectTimeout:      "60s",
   127  			ProxyNextUpstream:        false,
   128  			ProxyNextUpstreamTries:   0,
   129  			ProxyNextUpstreamTimeout: "0s",
   130  			ProxyTimeout:             "10m",
   131  			HealthCheck:              nil,
   132  			ServerSnippets:           []string{"deny  192.168.1.1;", "allow 192.168.1.0/24;"},
   133  		},
   134  		StreamSnippets: []string{"limit_conn_zone $binary_remote_addr zone=addr:10m;"},
   135  	}
   136  
   137  	result := generateTransportServerConfig(&transportServerEx, listenerPort, true)
   138  	if diff := cmp.Diff(expected, result); diff != "" {
   139  		t.Errorf("generateTransportServerConfig() mismatch (-want +got):\n%s", diff)
   140  	}
   141  }
   142  
   143  func TestGenerateTransportServerConfigForTCP(t *testing.T) {
   144  	transportServerEx := TransportServerEx{
   145  		TransportServer: &conf_v1alpha1.TransportServer{
   146  			ObjectMeta: meta_v1.ObjectMeta{
   147  				Name:      "tcp-server",
   148  				Namespace: "default",
   149  			},
   150  			Spec: conf_v1alpha1.TransportServerSpec{
   151  				Listener: conf_v1alpha1.TransportServerListener{
   152  					Name:     "tcp-listener",
   153  					Protocol: "TCP",
   154  				},
   155  				Upstreams: []conf_v1alpha1.Upstream{
   156  					{
   157  						Name:        "tcp-app",
   158  						Service:     "tcp-app-svc",
   159  						Port:        5001,
   160  						MaxFails:    intPointer(3),
   161  						FailTimeout: "40s",
   162  					},
   163  				},
   164  				UpstreamParameters: &conf_v1alpha1.UpstreamParameters{
   165  					ConnectTimeout: "30s",
   166  					NextUpstream:   false,
   167  				},
   168  				SessionParameters: &conf_v1alpha1.SessionParameters{
   169  					Timeout: "50s",
   170  				},
   171  				Action: &conf_v1alpha1.Action{
   172  					Pass: "tcp-app",
   173  				},
   174  			},
   175  		},
   176  		Endpoints: map[string][]string{
   177  			"default/tcp-app-svc:5001": {
   178  				"10.0.0.20:5001",
   179  			},
   180  		},
   181  	}
   182  
   183  	listenerPort := 2020
   184  
   185  	expected := &version2.TransportServerConfig{
   186  		Upstreams: []version2.StreamUpstream{
   187  			{
   188  				Name: "ts_default_tcp-server_tcp-app",
   189  				Servers: []version2.StreamUpstreamServer{
   190  					{
   191  						Address:     "10.0.0.20:5001",
   192  						MaxFails:    3,
   193  						FailTimeout: "40s",
   194  					},
   195  				},
   196  				UpstreamLabels: version2.UpstreamLabels{
   197  					ResourceName:      "tcp-server",
   198  					ResourceType:      "transportserver",
   199  					ResourceNamespace: "default",
   200  					Service:           "tcp-app-svc",
   201  				},
   202  				LoadBalancingMethod: "random two least_conn",
   203  			},
   204  		},
   205  		Server: version2.StreamServer{
   206  			Port:                     2020,
   207  			UDP:                      false,
   208  			StatusZone:               "tcp-listener",
   209  			ProxyPass:                "ts_default_tcp-server_tcp-app",
   210  			Name:                     "tcp-server",
   211  			Namespace:                "default",
   212  			ProxyConnectTimeout:      "30s",
   213  			ProxyNextUpstream:        false,
   214  			ProxyNextUpstreamTries:   0,
   215  			ProxyNextUpstreamTimeout: "0s",
   216  			ProxyTimeout:             "50s",
   217  			HealthCheck:              nil,
   218  			ServerSnippets:           []string{},
   219  		},
   220  		StreamSnippets: []string{},
   221  	}
   222  
   223  	result := generateTransportServerConfig(&transportServerEx, listenerPort, true)
   224  	if diff := cmp.Diff(expected, result); diff != "" {
   225  		t.Errorf("generateTransportServerConfig() mismatch (-want +got):\n%s", diff)
   226  	}
   227  
   228  }
   229  
   230  func TestGenerateTransportServerConfigForTCPMaxConnections(t *testing.T) {
   231  	transportServerEx := TransportServerEx{
   232  		TransportServer: &conf_v1alpha1.TransportServer{
   233  			ObjectMeta: meta_v1.ObjectMeta{
   234  				Name:      "tcp-server",
   235  				Namespace: "default",
   236  			},
   237  			Spec: conf_v1alpha1.TransportServerSpec{
   238  				Listener: conf_v1alpha1.TransportServerListener{
   239  					Name:     "tcp-listener",
   240  					Protocol: "TCP",
   241  				},
   242  				Upstreams: []conf_v1alpha1.Upstream{
   243  					{
   244  						Name:        "tcp-app",
   245  						Service:     "tcp-app-svc",
   246  						Port:        5001,
   247  						MaxFails:    intPointer(3),
   248  						MaxConns:    intPointer(3),
   249  						FailTimeout: "40s",
   250  					},
   251  				},
   252  				UpstreamParameters: &conf_v1alpha1.UpstreamParameters{
   253  					ConnectTimeout: "30s",
   254  					NextUpstream:   false,
   255  				},
   256  				SessionParameters: &conf_v1alpha1.SessionParameters{
   257  					Timeout: "50s",
   258  				},
   259  				Action: &conf_v1alpha1.Action{
   260  					Pass: "tcp-app",
   261  				},
   262  			},
   263  		},
   264  		Endpoints: map[string][]string{
   265  			"default/tcp-app-svc:5001": {
   266  				"10.0.0.20:5001",
   267  			},
   268  		},
   269  	}
   270  
   271  	listenerPort := 2020
   272  
   273  	expected := &version2.TransportServerConfig{
   274  		Upstreams: []version2.StreamUpstream{
   275  			{
   276  				Name: "ts_default_tcp-server_tcp-app",
   277  				Servers: []version2.StreamUpstreamServer{
   278  					{
   279  						Address:        "10.0.0.20:5001",
   280  						MaxFails:       3,
   281  						FailTimeout:    "40s",
   282  						MaxConnections: 3,
   283  					},
   284  				},
   285  				UpstreamLabels: version2.UpstreamLabels{
   286  					ResourceName:      "tcp-server",
   287  					ResourceType:      "transportserver",
   288  					ResourceNamespace: "default",
   289  					Service:           "tcp-app-svc",
   290  				},
   291  				LoadBalancingMethod: "random two least_conn",
   292  			},
   293  		},
   294  		Server: version2.StreamServer{
   295  			Port:                     2020,
   296  			UDP:                      false,
   297  			StatusZone:               "tcp-listener",
   298  			ProxyPass:                "ts_default_tcp-server_tcp-app",
   299  			Name:                     "tcp-server",
   300  			Namespace:                "default",
   301  			ProxyConnectTimeout:      "30s",
   302  			ProxyNextUpstream:        false,
   303  			ProxyNextUpstreamTries:   0,
   304  			ProxyNextUpstreamTimeout: "0s",
   305  			ProxyTimeout:             "50s",
   306  			HealthCheck:              nil,
   307  			ServerSnippets:           []string{},
   308  		},
   309  		StreamSnippets: []string{},
   310  	}
   311  
   312  	result := generateTransportServerConfig(&transportServerEx, listenerPort, true)
   313  	if diff := cmp.Diff(expected, result); diff != "" {
   314  		t.Errorf("generateTransportServerConfig() mismatch (-want +got):\n%s", diff)
   315  	}
   316  
   317  }
   318  
   319  func TestGenerateTransportServerConfigForTLSPasstrhough(t *testing.T) {
   320  	transportServerEx := TransportServerEx{
   321  		TransportServer: &conf_v1alpha1.TransportServer{
   322  			ObjectMeta: meta_v1.ObjectMeta{
   323  				Name:      "tcp-server",
   324  				Namespace: "default",
   325  			},
   326  			Spec: conf_v1alpha1.TransportServerSpec{
   327  				Listener: conf_v1alpha1.TransportServerListener{
   328  					Name:     "tls-passthrough",
   329  					Protocol: "TLS_PASSTHROUGH",
   330  				},
   331  				Host: "example.com",
   332  				Upstreams: []conf_v1alpha1.Upstream{
   333  					{
   334  						Name:    "tcp-app",
   335  						Service: "tcp-app-svc",
   336  						Port:    5001,
   337  					},
   338  				},
   339  				UpstreamParameters: &conf_v1alpha1.UpstreamParameters{
   340  					ConnectTimeout:      "30s",
   341  					NextUpstream:        false,
   342  					NextUpstreamTries:   0,
   343  					NextUpstreamTimeout: "",
   344  				},
   345  				Action: &conf_v1alpha1.Action{
   346  					Pass: "tcp-app",
   347  				},
   348  			},
   349  		},
   350  		Endpoints: map[string][]string{
   351  			"default/tcp-app-svc:5001": {
   352  				"10.0.0.20:5001",
   353  			},
   354  		},
   355  	}
   356  
   357  	listenerPort := 2020
   358  
   359  	expected := &version2.TransportServerConfig{
   360  		Upstreams: []version2.StreamUpstream{
   361  			{
   362  				Name: "ts_default_tcp-server_tcp-app",
   363  				Servers: []version2.StreamUpstreamServer{
   364  					{
   365  						Address:     "10.0.0.20:5001",
   366  						MaxFails:    1,
   367  						FailTimeout: "10s",
   368  					},
   369  				},
   370  				UpstreamLabels: version2.UpstreamLabels{
   371  					ResourceName:      "tcp-server",
   372  					ResourceType:      "transportserver",
   373  					ResourceNamespace: "default",
   374  					Service:           "tcp-app-svc",
   375  				},
   376  				LoadBalancingMethod: "random two least_conn",
   377  			},
   378  		},
   379  		Server: version2.StreamServer{
   380  			TLSPassthrough:           true,
   381  			UnixSocket:               "unix:/var/lib/nginx/passthrough-default_tcp-server.sock",
   382  			Port:                     2020,
   383  			UDP:                      false,
   384  			StatusZone:               "example.com",
   385  			ProxyPass:                "ts_default_tcp-server_tcp-app",
   386  			Name:                     "tcp-server",
   387  			Namespace:                "default",
   388  			ProxyConnectTimeout:      "30s",
   389  			ProxyNextUpstream:        false,
   390  			ProxyNextUpstreamTimeout: "0s",
   391  			ProxyNextUpstreamTries:   0,
   392  			ProxyTimeout:             "10m",
   393  			HealthCheck:              nil,
   394  			ServerSnippets:           []string{},
   395  		},
   396  		StreamSnippets: []string{},
   397  	}
   398  
   399  	result := generateTransportServerConfig(&transportServerEx, listenerPort, true)
   400  	if diff := cmp.Diff(expected, result); diff != "" {
   401  		t.Errorf("generateTransportServerConfig() mismatch (-want +got):\n%s", diff)
   402  	}
   403  }
   404  
   405  func TestGenerateTransportServerConfigForUDP(t *testing.T) {
   406  	udpRequests := 1
   407  	udpResponses := 5
   408  
   409  	transportServerEx := TransportServerEx{
   410  		TransportServer: &conf_v1alpha1.TransportServer{
   411  			ObjectMeta: meta_v1.ObjectMeta{
   412  				Name:      "udp-server",
   413  				Namespace: "default",
   414  			},
   415  			Spec: conf_v1alpha1.TransportServerSpec{
   416  				Listener: conf_v1alpha1.TransportServerListener{
   417  					Name:     "udp-listener",
   418  					Protocol: "UDP",
   419  				},
   420  				Upstreams: []conf_v1alpha1.Upstream{
   421  					{
   422  						Name:        "udp-app",
   423  						Service:     "udp-app-svc",
   424  						Port:        5001,
   425  						HealthCheck: &conf_v1alpha1.HealthCheck{},
   426  					},
   427  				},
   428  				UpstreamParameters: &conf_v1alpha1.UpstreamParameters{
   429  					UDPRequests:         &udpRequests,
   430  					UDPResponses:        &udpResponses,
   431  					ConnectTimeout:      "30s",
   432  					NextUpstream:        true,
   433  					NextUpstreamTimeout: "",
   434  					NextUpstreamTries:   0,
   435  				},
   436  				Action: &conf_v1alpha1.Action{
   437  					Pass: "udp-app",
   438  				},
   439  			},
   440  		},
   441  		Endpoints: map[string][]string{
   442  			"default/udp-app-svc:5001": {
   443  				"10.0.0.20:5001",
   444  			},
   445  		},
   446  	}
   447  
   448  	listenerPort := 2020
   449  
   450  	expected := &version2.TransportServerConfig{
   451  		Upstreams: []version2.StreamUpstream{
   452  			{
   453  				Name: "ts_default_udp-server_udp-app",
   454  				Servers: []version2.StreamUpstreamServer{
   455  					{
   456  						Address:     "10.0.0.20:5001",
   457  						MaxFails:    1,
   458  						FailTimeout: "10s",
   459  					},
   460  				},
   461  				UpstreamLabels: version2.UpstreamLabels{
   462  					ResourceName:      "udp-server",
   463  					ResourceType:      "transportserver",
   464  					ResourceNamespace: "default",
   465  					Service:           "udp-app-svc",
   466  				},
   467  				LoadBalancingMethod: "random two least_conn",
   468  			},
   469  		},
   470  		Server: version2.StreamServer{
   471  			Port:                     2020,
   472  			UDP:                      true,
   473  			StatusZone:               "udp-listener",
   474  			ProxyRequests:            &udpRequests,
   475  			ProxyResponses:           &udpResponses,
   476  			ProxyPass:                "ts_default_udp-server_udp-app",
   477  			Name:                     "udp-server",
   478  			Namespace:                "default",
   479  			ProxyConnectTimeout:      "30s",
   480  			ProxyNextUpstream:        true,
   481  			ProxyNextUpstreamTimeout: "0s",
   482  			ProxyNextUpstreamTries:   0,
   483  			ProxyTimeout:             "10m",
   484  			HealthCheck:              nil,
   485  			ServerSnippets:           []string{},
   486  		},
   487  		StreamSnippets: []string{},
   488  	}
   489  
   490  	result := generateTransportServerConfig(&transportServerEx, listenerPort, true)
   491  	if diff := cmp.Diff(expected, result); diff != "" {
   492  		t.Errorf("generateTransportServerConfig() mismatch (-want +got):\n%s", diff)
   493  	}
   494  }
   495  
   496  func TestGenerateUnixSocket(t *testing.T) {
   497  	transportServerEx := &TransportServerEx{
   498  		TransportServer: &conf_v1alpha1.TransportServer{
   499  			ObjectMeta: meta_v1.ObjectMeta{
   500  				Name:      "tcp-server",
   501  				Namespace: "default",
   502  			},
   503  			Spec: conf_v1alpha1.TransportServerSpec{
   504  				Listener: conf_v1alpha1.TransportServerListener{
   505  					Name: "tls-passthrough",
   506  				},
   507  			},
   508  		},
   509  	}
   510  
   511  	expected := "unix:/var/lib/nginx/passthrough-default_tcp-server.sock"
   512  
   513  	result := generateUnixSocket(transportServerEx)
   514  	if result != expected {
   515  		t.Errorf("generateUnixSocket() returned %q but expected %q", result, expected)
   516  	}
   517  
   518  	transportServerEx.TransportServer.Spec.Listener.Name = "some-listener"
   519  	expected = ""
   520  
   521  	result = generateUnixSocket(transportServerEx)
   522  	if result != expected {
   523  		t.Errorf("generateUnixSocket() returned %q but expected %q", result, expected)
   524  	}
   525  }
   526  
   527  func TestGenerateTransportServerHealthChecks(t *testing.T) {
   528  	upstreamName := "dns-tcp"
   529  	generatedUpsteamName := "ts_namespace_name_dns-tcp"
   530  
   531  	tests := []struct {
   532  		upstreams     []conf_v1alpha1.Upstream
   533  		expectedHC    *version2.StreamHealthCheck
   534  		expectedMatch *version2.Match
   535  		msg           string
   536  	}{
   537  		{
   538  			upstreams: []conf_v1alpha1.Upstream{
   539  				{
   540  					Name: "dns-tcp",
   541  					HealthCheck: &conf_v1alpha1.HealthCheck{
   542  						Enabled:  false,
   543  						Timeout:  "30s",
   544  						Jitter:   "30s",
   545  						Port:     80,
   546  						Interval: "20s",
   547  						Passes:   4,
   548  						Fails:    5,
   549  					},
   550  				},
   551  			},
   552  			expectedHC:    nil,
   553  			expectedMatch: nil,
   554  			msg:           "health checks disabled",
   555  		},
   556  		{
   557  			upstreams: []conf_v1alpha1.Upstream{
   558  				{
   559  					Name:        "dns-tcp",
   560  					HealthCheck: &conf_v1alpha1.HealthCheck{},
   561  				},
   562  			},
   563  			expectedHC:    nil,
   564  			expectedMatch: nil,
   565  			msg:           "empty health check",
   566  		},
   567  		{
   568  			upstreams: []conf_v1alpha1.Upstream{
   569  				{
   570  					Name: "dns-tcp",
   571  					HealthCheck: &conf_v1alpha1.HealthCheck{
   572  						Enabled:  true,
   573  						Timeout:  "40s",
   574  						Jitter:   "30s",
   575  						Port:     88,
   576  						Interval: "20s",
   577  						Passes:   4,
   578  						Fails:    5,
   579  					},
   580  				},
   581  			},
   582  			expectedHC: &version2.StreamHealthCheck{
   583  				Enabled:  true,
   584  				Timeout:  "40s",
   585  				Jitter:   "30s",
   586  				Port:     88,
   587  				Interval: "20s",
   588  				Passes:   4,
   589  				Fails:    5,
   590  			},
   591  			expectedMatch: nil,
   592  			msg:           "valid health checks",
   593  		},
   594  		{
   595  			upstreams: []conf_v1alpha1.Upstream{
   596  				{
   597  					Name: "dns-tcp",
   598  					HealthCheck: &conf_v1alpha1.HealthCheck{
   599  						Enabled:  true,
   600  						Timeout:  "40s",
   601  						Jitter:   "30s",
   602  						Port:     88,
   603  						Interval: "20s",
   604  						Passes:   4,
   605  						Fails:    5,
   606  					},
   607  				},
   608  				{
   609  					Name: "dns-tcp-2",
   610  					HealthCheck: &conf_v1alpha1.HealthCheck{
   611  						Enabled:  false,
   612  						Timeout:  "50s",
   613  						Jitter:   "60s",
   614  						Port:     89,
   615  						Interval: "10s",
   616  						Passes:   9,
   617  						Fails:    7,
   618  					},
   619  				},
   620  			},
   621  			expectedHC: &version2.StreamHealthCheck{
   622  				Enabled:  true,
   623  				Timeout:  "40s",
   624  				Jitter:   "30s",
   625  				Port:     88,
   626  				Interval: "20s",
   627  				Passes:   4,
   628  				Fails:    5,
   629  			},
   630  			expectedMatch: nil,
   631  			msg:           "valid 2 health checks",
   632  		},
   633  		{
   634  			upstreams: []conf_v1alpha1.Upstream{
   635  				{
   636  					Name: "dns-tcp",
   637  					Port: 90,
   638  					HealthCheck: &conf_v1alpha1.HealthCheck{
   639  						Enabled: true,
   640  					},
   641  				},
   642  			},
   643  			expectedHC: &version2.StreamHealthCheck{
   644  				Enabled:  true,
   645  				Timeout:  "5s",
   646  				Jitter:   "0s",
   647  				Port:     90,
   648  				Interval: "5s",
   649  				Passes:   1,
   650  				Fails:    1,
   651  			},
   652  			expectedMatch: nil,
   653  			msg:           "return default values for health check",
   654  		},
   655  		{
   656  			upstreams: []conf_v1alpha1.Upstream{
   657  				{
   658  					Name: "dns-tcp",
   659  					Port: 90,
   660  					HealthCheck: &conf_v1alpha1.HealthCheck{
   661  						Enabled: true,
   662  						Match: &conf_v1alpha1.Match{
   663  							Send:   `GET / HTTP/1.0\r\nHost: localhost\r\n\r\n`,
   664  							Expect: "~*200 OK",
   665  						},
   666  					},
   667  				},
   668  			},
   669  			expectedHC: &version2.StreamHealthCheck{
   670  				Enabled:  true,
   671  				Timeout:  "5s",
   672  				Jitter:   "0s",
   673  				Port:     90,
   674  				Interval: "5s",
   675  				Passes:   1,
   676  				Fails:    1,
   677  				Match:    "match_ts_namespace_name_dns-tcp",
   678  			},
   679  			expectedMatch: &version2.Match{
   680  				Name:                "match_ts_namespace_name_dns-tcp",
   681  				Send:                `GET / HTTP/1.0\r\nHost: localhost\r\n\r\n`,
   682  				ExpectRegexModifier: "~*",
   683  				Expect:              "200 OK",
   684  			},
   685  			msg: "health check with match",
   686  		},
   687  	}
   688  
   689  	for _, test := range tests {
   690  		hc, match := generateTransportServerHealthCheck(upstreamName, generatedUpsteamName, test.upstreams)
   691  		if diff := cmp.Diff(test.expectedHC, hc); diff != "" {
   692  			t.Errorf("generateTransportServerHealthCheck() '%v' mismatch (-want +got):\n%s", test.msg, diff)
   693  		}
   694  		if diff := cmp.Diff(test.expectedMatch, match); diff != "" {
   695  			t.Errorf("generateTransportServerHealthCheck() '%v' mismatch (-want +got):\n%s", test.msg, diff)
   696  		}
   697  	}
   698  }
   699  
   700  func TestGenerateHealthCheckMatch(t *testing.T) {
   701  	tests := []struct {
   702  		match    *conf_v1alpha1.Match
   703  		expected *version2.Match
   704  		msg      string
   705  	}{
   706  		{
   707  			match: &conf_v1alpha1.Match{
   708  				Send:   "",
   709  				Expect: "",
   710  			},
   711  			expected: &version2.Match{
   712  				Name:                "match",
   713  				Send:                "",
   714  				ExpectRegexModifier: "",
   715  				Expect:              "",
   716  			},
   717  			msg: "match with empty fields",
   718  		},
   719  		{
   720  			match: &conf_v1alpha1.Match{
   721  				Send:   "xxx",
   722  				Expect: "yyy",
   723  			},
   724  			expected: &version2.Match{
   725  				Name:                "match",
   726  				Send:                "xxx",
   727  				ExpectRegexModifier: "",
   728  				Expect:              "yyy",
   729  			},
   730  			msg: "match with all fields and no regexp",
   731  		},
   732  		{
   733  			match: &conf_v1alpha1.Match{
   734  				Send:   "xxx",
   735  				Expect: "~yyy",
   736  			},
   737  			expected: &version2.Match{
   738  				Name:                "match",
   739  				Send:                "xxx",
   740  				ExpectRegexModifier: "~",
   741  				Expect:              "yyy",
   742  			},
   743  			msg: "match with all fields and case sensitive regexp",
   744  		},
   745  		{
   746  			match: &conf_v1alpha1.Match{
   747  				Send:   "xxx",
   748  				Expect: "~*yyy",
   749  			},
   750  			expected: &version2.Match{
   751  				Name:                "match",
   752  				Send:                "xxx",
   753  				ExpectRegexModifier: "~*",
   754  				Expect:              "yyy",
   755  			},
   756  			msg: "match with all fields and case insensitive regexp",
   757  		},
   758  	}
   759  	name := "match"
   760  
   761  	for _, test := range tests {
   762  		result := generateHealthCheckMatch(test.match, name)
   763  		if diff := cmp.Diff(test.expected, result); diff != "" {
   764  			t.Errorf("generateHealthCheckMatch() '%v' mismatch (-want +got):\n%s", test.msg, diff)
   765  		}
   766  	}
   767  }
   768  
   769  func intPointer(value int) *int {
   770  	return &value
   771  }