...

Source file src/k8s.io/kubectl/pkg/generate/versioned/service_test.go

Documentation: k8s.io/kubectl/pkg/generate/versioned

     1  /*
     2  Copyright 2014 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package versioned
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"k8s.io/api/core/v1"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/util/intstr"
    26  	"k8s.io/kubectl/pkg/generate"
    27  )
    28  
    29  func TestGenerateService(t *testing.T) {
    30  	tests := []struct {
    31  		name      string
    32  		generator generate.Generator
    33  		params    map[string]interface{}
    34  		expected  v1.Service
    35  	}{
    36  		{
    37  			name:      "test1",
    38  			generator: ServiceGeneratorV2{},
    39  			params: map[string]interface{}{
    40  				"selector":       "foo=bar,baz=blah",
    41  				"name":           "test",
    42  				"port":           "80",
    43  				"protocol":       "TCP",
    44  				"container-port": "1234",
    45  			},
    46  			expected: v1.Service{
    47  				ObjectMeta: metav1.ObjectMeta{
    48  					Name: "test",
    49  				},
    50  				Spec: v1.ServiceSpec{
    51  					Selector: map[string]string{
    52  						"foo": "bar",
    53  						"baz": "blah",
    54  					},
    55  					Ports: []v1.ServicePort{
    56  						{
    57  							Port:       80,
    58  							Protocol:   "TCP",
    59  							TargetPort: intstr.FromInt32(1234),
    60  						},
    61  					},
    62  				},
    63  			},
    64  		},
    65  		{
    66  			name:      "test2",
    67  			generator: ServiceGeneratorV2{},
    68  			params: map[string]interface{}{
    69  				"selector":       "foo=bar,baz=blah",
    70  				"name":           "test",
    71  				"port":           "80",
    72  				"protocol":       "UDP",
    73  				"container-port": "foobar",
    74  			},
    75  			expected: v1.Service{
    76  				ObjectMeta: metav1.ObjectMeta{
    77  					Name: "test",
    78  				},
    79  				Spec: v1.ServiceSpec{
    80  					Selector: map[string]string{
    81  						"foo": "bar",
    82  						"baz": "blah",
    83  					},
    84  					Ports: []v1.ServicePort{
    85  						{
    86  							Port:       80,
    87  							Protocol:   "UDP",
    88  							TargetPort: intstr.FromString("foobar"),
    89  						},
    90  					},
    91  				},
    92  			},
    93  		},
    94  		{
    95  			name:      "test3",
    96  			generator: ServiceGeneratorV2{},
    97  			params: map[string]interface{}{
    98  				"selector":       "foo=bar,baz=blah",
    99  				"labels":         "key1=value1,key2=value2",
   100  				"name":           "test",
   101  				"port":           "80",
   102  				"protocol":       "TCP",
   103  				"container-port": "1234",
   104  			},
   105  			expected: v1.Service{
   106  				ObjectMeta: metav1.ObjectMeta{
   107  					Name: "test",
   108  					Labels: map[string]string{
   109  						"key1": "value1",
   110  						"key2": "value2",
   111  					},
   112  				},
   113  				Spec: v1.ServiceSpec{
   114  					Selector: map[string]string{
   115  						"foo": "bar",
   116  						"baz": "blah",
   117  					},
   118  					Ports: []v1.ServicePort{
   119  						{
   120  							Port:       80,
   121  							Protocol:   "TCP",
   122  							TargetPort: intstr.FromInt32(1234),
   123  						},
   124  					},
   125  				},
   126  			},
   127  		},
   128  		{
   129  			name:      "test4",
   130  			generator: ServiceGeneratorV2{},
   131  			params: map[string]interface{}{
   132  				"selector":       "foo=bar,baz=blah",
   133  				"name":           "test",
   134  				"port":           "80",
   135  				"protocol":       "UDP",
   136  				"container-port": "foobar",
   137  				"external-ip":    "1.2.3.4",
   138  			},
   139  			expected: v1.Service{
   140  				ObjectMeta: metav1.ObjectMeta{
   141  					Name: "test",
   142  				},
   143  				Spec: v1.ServiceSpec{
   144  					Selector: map[string]string{
   145  						"foo": "bar",
   146  						"baz": "blah",
   147  					},
   148  					Ports: []v1.ServicePort{
   149  						{
   150  							Port:       80,
   151  							Protocol:   "UDP",
   152  							TargetPort: intstr.FromString("foobar"),
   153  						},
   154  					},
   155  					ExternalIPs: []string{"1.2.3.4"},
   156  				},
   157  			},
   158  		},
   159  		{
   160  			name:      "test5",
   161  			generator: ServiceGeneratorV2{},
   162  			params: map[string]interface{}{
   163  				"selector":       "foo=bar,baz=blah",
   164  				"name":           "test",
   165  				"port":           "80",
   166  				"protocol":       "UDP",
   167  				"container-port": "foobar",
   168  				"external-ip":    "1.2.3.4",
   169  				"type":           "LoadBalancer",
   170  			},
   171  			expected: v1.Service{
   172  				ObjectMeta: metav1.ObjectMeta{
   173  					Name: "test",
   174  				},
   175  				Spec: v1.ServiceSpec{
   176  					Selector: map[string]string{
   177  						"foo": "bar",
   178  						"baz": "blah",
   179  					},
   180  					Ports: []v1.ServicePort{
   181  						{
   182  							Port:       80,
   183  							Protocol:   "UDP",
   184  							TargetPort: intstr.FromString("foobar"),
   185  						},
   186  					},
   187  					Type:        v1.ServiceTypeLoadBalancer,
   188  					ExternalIPs: []string{"1.2.3.4"},
   189  				},
   190  			},
   191  		},
   192  		{
   193  			name:      "test6",
   194  			generator: ServiceGeneratorV2{},
   195  			params: map[string]interface{}{
   196  				"selector":       "foo=bar,baz=blah",
   197  				"name":           "test",
   198  				"port":           "80",
   199  				"protocol":       "UDP",
   200  				"container-port": "foobar",
   201  				"type":           string(v1.ServiceTypeNodePort),
   202  			},
   203  			expected: v1.Service{
   204  				ObjectMeta: metav1.ObjectMeta{
   205  					Name: "test",
   206  				},
   207  				Spec: v1.ServiceSpec{
   208  					Selector: map[string]string{
   209  						"foo": "bar",
   210  						"baz": "blah",
   211  					},
   212  					Ports: []v1.ServicePort{
   213  						{
   214  							Port:       80,
   215  							Protocol:   "UDP",
   216  							TargetPort: intstr.FromString("foobar"),
   217  						},
   218  					},
   219  					Type: v1.ServiceTypeNodePort,
   220  				},
   221  			},
   222  		},
   223  		{
   224  			name:      "test7",
   225  			generator: ServiceGeneratorV2{},
   226  			params: map[string]interface{}{
   227  				"selector":                      "foo=bar,baz=blah",
   228  				"name":                          "test",
   229  				"port":                          "80",
   230  				"protocol":                      "UDP",
   231  				"container-port":                "foobar",
   232  				"create-external-load-balancer": "true", // ignored when type is present
   233  				"type":                          string(v1.ServiceTypeNodePort),
   234  			},
   235  			expected: v1.Service{
   236  				ObjectMeta: metav1.ObjectMeta{
   237  					Name: "test",
   238  				},
   239  				Spec: v1.ServiceSpec{
   240  					Selector: map[string]string{
   241  						"foo": "bar",
   242  						"baz": "blah",
   243  					},
   244  					Ports: []v1.ServicePort{
   245  						{
   246  							Port:       80,
   247  							Protocol:   "UDP",
   248  							TargetPort: intstr.FromString("foobar"),
   249  						},
   250  					},
   251  					Type: v1.ServiceTypeNodePort,
   252  				},
   253  			},
   254  		},
   255  		{
   256  			name:      "test8",
   257  			generator: ServiceGeneratorV1{},
   258  			params: map[string]interface{}{
   259  				"selector":       "foo=bar,baz=blah",
   260  				"name":           "test",
   261  				"port":           "80",
   262  				"protocol":       "TCP",
   263  				"container-port": "1234",
   264  			},
   265  			expected: v1.Service{
   266  				ObjectMeta: metav1.ObjectMeta{
   267  					Name: "test",
   268  				},
   269  				Spec: v1.ServiceSpec{
   270  					Selector: map[string]string{
   271  						"foo": "bar",
   272  						"baz": "blah",
   273  					},
   274  					Ports: []v1.ServicePort{
   275  						{
   276  							Name:       "default",
   277  							Port:       80,
   278  							Protocol:   "TCP",
   279  							TargetPort: intstr.FromInt32(1234),
   280  						},
   281  					},
   282  				},
   283  			},
   284  		},
   285  		{
   286  			name:      "test9",
   287  			generator: ServiceGeneratorV1{},
   288  			params: map[string]interface{}{
   289  				"selector":         "foo=bar,baz=blah",
   290  				"name":             "test",
   291  				"port":             "80",
   292  				"protocol":         "TCP",
   293  				"container-port":   "1234",
   294  				"session-affinity": "ClientIP",
   295  			},
   296  			expected: v1.Service{
   297  				ObjectMeta: metav1.ObjectMeta{
   298  					Name: "test",
   299  				},
   300  				Spec: v1.ServiceSpec{
   301  					Selector: map[string]string{
   302  						"foo": "bar",
   303  						"baz": "blah",
   304  					},
   305  					Ports: []v1.ServicePort{
   306  						{
   307  							Name:       "default",
   308  							Port:       80,
   309  							Protocol:   "TCP",
   310  							TargetPort: intstr.FromInt32(1234),
   311  						},
   312  					},
   313  					SessionAffinity: v1.ServiceAffinityClientIP,
   314  				},
   315  			},
   316  		},
   317  		{
   318  			name:      "test10",
   319  			generator: ServiceGeneratorV2{},
   320  			params: map[string]interface{}{
   321  				"selector":       "foo=bar,baz=blah",
   322  				"name":           "test",
   323  				"port":           "80",
   324  				"protocol":       "TCP",
   325  				"container-port": "1234",
   326  				"cluster-ip":     "10.10.10.10",
   327  			},
   328  			expected: v1.Service{
   329  				ObjectMeta: metav1.ObjectMeta{
   330  					Name: "test",
   331  				},
   332  				Spec: v1.ServiceSpec{
   333  					Selector: map[string]string{
   334  						"foo": "bar",
   335  						"baz": "blah",
   336  					},
   337  					Ports: []v1.ServicePort{
   338  						{
   339  							Port:       80,
   340  							Protocol:   "TCP",
   341  							TargetPort: intstr.FromInt32(1234),
   342  						},
   343  					},
   344  					ClusterIP: "10.10.10.10",
   345  				},
   346  			},
   347  		},
   348  		{
   349  			name:      "test11",
   350  			generator: ServiceGeneratorV2{},
   351  			params: map[string]interface{}{
   352  				"selector":       "foo=bar,baz=blah",
   353  				"name":           "test",
   354  				"port":           "80",
   355  				"protocol":       "TCP",
   356  				"container-port": "1234",
   357  				"cluster-ip":     "None",
   358  			},
   359  			expected: v1.Service{
   360  				ObjectMeta: metav1.ObjectMeta{
   361  					Name: "test",
   362  				},
   363  				Spec: v1.ServiceSpec{
   364  					Selector: map[string]string{
   365  						"foo": "bar",
   366  						"baz": "blah",
   367  					},
   368  					Ports: []v1.ServicePort{
   369  						{
   370  							Port:       80,
   371  							Protocol:   "TCP",
   372  							TargetPort: intstr.FromInt32(1234),
   373  						},
   374  					},
   375  					ClusterIP: v1.ClusterIPNone,
   376  				},
   377  			},
   378  		},
   379  		{
   380  			name:      "test12",
   381  			generator: ServiceGeneratorV1{},
   382  			params: map[string]interface{}{
   383  				"selector":       "foo=bar",
   384  				"name":           "test",
   385  				"ports":          "80,443",
   386  				"protocol":       "TCP",
   387  				"container-port": "foobar",
   388  			},
   389  			expected: v1.Service{
   390  				ObjectMeta: metav1.ObjectMeta{
   391  					Name: "test",
   392  				},
   393  				Spec: v1.ServiceSpec{
   394  					Selector: map[string]string{
   395  						"foo": "bar",
   396  					},
   397  					Ports: []v1.ServicePort{
   398  						{
   399  							Name:       "port-1",
   400  							Port:       80,
   401  							Protocol:   v1.ProtocolTCP,
   402  							TargetPort: intstr.FromString("foobar"),
   403  						},
   404  						{
   405  							Name:       "port-2",
   406  							Port:       443,
   407  							Protocol:   v1.ProtocolTCP,
   408  							TargetPort: intstr.FromString("foobar"),
   409  						},
   410  					},
   411  				},
   412  			},
   413  		},
   414  		{
   415  			name:      "test13",
   416  			generator: ServiceGeneratorV2{},
   417  			params: map[string]interface{}{
   418  				"selector":    "foo=bar",
   419  				"name":        "test",
   420  				"ports":       "80,443",
   421  				"protocol":    "UDP",
   422  				"target-port": "1234",
   423  			},
   424  			expected: v1.Service{
   425  				ObjectMeta: metav1.ObjectMeta{
   426  					Name: "test",
   427  				},
   428  				Spec: v1.ServiceSpec{
   429  					Selector: map[string]string{
   430  						"foo": "bar",
   431  					},
   432  					Ports: []v1.ServicePort{
   433  						{
   434  							Name:       "port-1",
   435  							Port:       80,
   436  							Protocol:   v1.ProtocolUDP,
   437  							TargetPort: intstr.FromInt32(1234),
   438  						},
   439  						{
   440  							Name:       "port-2",
   441  							Port:       443,
   442  							Protocol:   v1.ProtocolUDP,
   443  							TargetPort: intstr.FromInt32(1234),
   444  						},
   445  					},
   446  				},
   447  			},
   448  		},
   449  		{
   450  			name:      "test14",
   451  			generator: ServiceGeneratorV2{},
   452  			params: map[string]interface{}{
   453  				"selector": "foo=bar",
   454  				"name":     "test",
   455  				"ports":    "80,443",
   456  				"protocol": "TCP",
   457  			},
   458  			expected: v1.Service{
   459  				ObjectMeta: metav1.ObjectMeta{
   460  					Name: "test",
   461  				},
   462  				Spec: v1.ServiceSpec{
   463  					Selector: map[string]string{
   464  						"foo": "bar",
   465  					},
   466  					Ports: []v1.ServicePort{
   467  						{
   468  							Name:       "port-1",
   469  							Port:       80,
   470  							Protocol:   v1.ProtocolTCP,
   471  							TargetPort: intstr.FromInt32(80),
   472  						},
   473  						{
   474  							Name:       "port-2",
   475  							Port:       443,
   476  							Protocol:   v1.ProtocolTCP,
   477  							TargetPort: intstr.FromInt32(443),
   478  						},
   479  					},
   480  				},
   481  			},
   482  		},
   483  		{
   484  			name:      "test15",
   485  			generator: ServiceGeneratorV2{},
   486  			params: map[string]interface{}{
   487  				"selector":  "foo=bar",
   488  				"name":      "test",
   489  				"ports":     "80,8080",
   490  				"protocols": "8080/UDP",
   491  			},
   492  			expected: v1.Service{
   493  				ObjectMeta: metav1.ObjectMeta{
   494  					Name: "test",
   495  				},
   496  				Spec: v1.ServiceSpec{
   497  					Selector: map[string]string{
   498  						"foo": "bar",
   499  					},
   500  					Ports: []v1.ServicePort{
   501  						{
   502  							Name:       "port-1",
   503  							Port:       80,
   504  							Protocol:   v1.ProtocolTCP,
   505  							TargetPort: intstr.FromInt32(80),
   506  						},
   507  						{
   508  							Name:       "port-2",
   509  							Port:       8080,
   510  							Protocol:   v1.ProtocolUDP,
   511  							TargetPort: intstr.FromInt32(8080),
   512  						},
   513  					},
   514  				},
   515  			},
   516  		},
   517  		{
   518  			name:      "test16",
   519  			generator: ServiceGeneratorV2{},
   520  			params: map[string]interface{}{
   521  				"selector":  "foo=bar",
   522  				"name":      "test",
   523  				"ports":     "80,8080,8081",
   524  				"protocols": "8080/UDP,8081/TCP",
   525  			},
   526  			expected: v1.Service{
   527  				ObjectMeta: metav1.ObjectMeta{
   528  					Name: "test",
   529  				},
   530  				Spec: v1.ServiceSpec{
   531  					Selector: map[string]string{
   532  						"foo": "bar",
   533  					},
   534  					Ports: []v1.ServicePort{
   535  						{
   536  							Name:       "port-1",
   537  							Port:       80,
   538  							Protocol:   v1.ProtocolTCP,
   539  							TargetPort: intstr.FromInt32(80),
   540  						},
   541  						{
   542  							Name:       "port-2",
   543  							Port:       8080,
   544  							Protocol:   v1.ProtocolUDP,
   545  							TargetPort: intstr.FromInt32(8080),
   546  						},
   547  						{
   548  							Name:       "port-3",
   549  							Port:       8081,
   550  							Protocol:   v1.ProtocolTCP,
   551  							TargetPort: intstr.FromInt32(8081),
   552  						},
   553  					},
   554  				},
   555  			},
   556  		},
   557  		{
   558  			name:      "test17",
   559  			generator: ServiceGeneratorV2{},
   560  			params: map[string]interface{}{
   561  				"selector":       "foo=bar,baz=blah",
   562  				"name":           "test",
   563  				"protocol":       "TCP",
   564  				"container-port": "1234",
   565  				"cluster-ip":     "None",
   566  			},
   567  			expected: v1.Service{
   568  				ObjectMeta: metav1.ObjectMeta{
   569  					Name: "test",
   570  				},
   571  				Spec: v1.ServiceSpec{
   572  					Selector: map[string]string{
   573  						"foo": "bar",
   574  						"baz": "blah",
   575  					},
   576  					Ports:     []v1.ServicePort{},
   577  					ClusterIP: v1.ClusterIPNone,
   578  				},
   579  			},
   580  		},
   581  		{
   582  			name:      "test18",
   583  			generator: ServiceGeneratorV2{},
   584  			params: map[string]interface{}{
   585  				"selector":   "foo=bar",
   586  				"name":       "test",
   587  				"cluster-ip": "None",
   588  			},
   589  			expected: v1.Service{
   590  				ObjectMeta: metav1.ObjectMeta{
   591  					Name: "test",
   592  				},
   593  				Spec: v1.ServiceSpec{
   594  					Selector: map[string]string{
   595  						"foo": "bar",
   596  					},
   597  					Ports:     []v1.ServicePort{},
   598  					ClusterIP: v1.ClusterIPNone,
   599  				},
   600  			},
   601  		},
   602  		{
   603  			generator: ServiceGeneratorV2{},
   604  			params: map[string]interface{}{
   605  				"selector":       "foo=bar,baz=blah",
   606  				"name":           "test",
   607  				"port":           "80",
   608  				"protocol":       "SCTP",
   609  				"container-port": "1234",
   610  			},
   611  			expected: v1.Service{
   612  				ObjectMeta: metav1.ObjectMeta{
   613  					Name: "test",
   614  				},
   615  				Spec: v1.ServiceSpec{
   616  					Selector: map[string]string{
   617  						"foo": "bar",
   618  						"baz": "blah",
   619  					},
   620  					Ports: []v1.ServicePort{
   621  						{
   622  							Port:       80,
   623  							Protocol:   "SCTP",
   624  							TargetPort: intstr.FromInt32(1234),
   625  						},
   626  					},
   627  				},
   628  			},
   629  		},
   630  		{
   631  			generator: ServiceGeneratorV2{},
   632  			params: map[string]interface{}{
   633  				"selector":       "foo=bar,baz=blah",
   634  				"labels":         "key1=value1,key2=value2",
   635  				"name":           "test",
   636  				"port":           "80",
   637  				"protocol":       "SCTP",
   638  				"container-port": "1234",
   639  			},
   640  			expected: v1.Service{
   641  				ObjectMeta: metav1.ObjectMeta{
   642  					Name: "test",
   643  					Labels: map[string]string{
   644  						"key1": "value1",
   645  						"key2": "value2",
   646  					},
   647  				},
   648  				Spec: v1.ServiceSpec{
   649  					Selector: map[string]string{
   650  						"foo": "bar",
   651  						"baz": "blah",
   652  					},
   653  					Ports: []v1.ServicePort{
   654  						{
   655  							Port:       80,
   656  							Protocol:   "SCTP",
   657  							TargetPort: intstr.FromInt32(1234),
   658  						},
   659  					},
   660  				},
   661  			},
   662  		},
   663  		{
   664  			generator: ServiceGeneratorV1{},
   665  			params: map[string]interface{}{
   666  				"selector":       "foo=bar,baz=blah",
   667  				"name":           "test",
   668  				"port":           "80",
   669  				"protocol":       "SCTP",
   670  				"container-port": "1234",
   671  			},
   672  			expected: v1.Service{
   673  				ObjectMeta: metav1.ObjectMeta{
   674  					Name: "test",
   675  				},
   676  				Spec: v1.ServiceSpec{
   677  					Selector: map[string]string{
   678  						"foo": "bar",
   679  						"baz": "blah",
   680  					},
   681  					Ports: []v1.ServicePort{
   682  						{
   683  							Name:       "default",
   684  							Port:       80,
   685  							Protocol:   "SCTP",
   686  							TargetPort: intstr.FromInt32(1234),
   687  						},
   688  					},
   689  				},
   690  			},
   691  		},
   692  		{
   693  			generator: ServiceGeneratorV1{},
   694  			params: map[string]interface{}{
   695  				"selector":         "foo=bar,baz=blah",
   696  				"name":             "test",
   697  				"port":             "80",
   698  				"protocol":         "SCTP",
   699  				"container-port":   "1234",
   700  				"session-affinity": "ClientIP",
   701  			},
   702  			expected: v1.Service{
   703  				ObjectMeta: metav1.ObjectMeta{
   704  					Name: "test",
   705  				},
   706  				Spec: v1.ServiceSpec{
   707  					Selector: map[string]string{
   708  						"foo": "bar",
   709  						"baz": "blah",
   710  					},
   711  					Ports: []v1.ServicePort{
   712  						{
   713  							Name:       "default",
   714  							Port:       80,
   715  							Protocol:   "SCTP",
   716  							TargetPort: intstr.FromInt32(1234),
   717  						},
   718  					},
   719  					SessionAffinity: v1.ServiceAffinityClientIP,
   720  				},
   721  			},
   722  		},
   723  		{
   724  			generator: ServiceGeneratorV2{},
   725  			params: map[string]interface{}{
   726  				"selector":       "foo=bar,baz=blah",
   727  				"name":           "test",
   728  				"port":           "80",
   729  				"protocol":       "SCTP",
   730  				"container-port": "1234",
   731  				"cluster-ip":     "10.10.10.10",
   732  			},
   733  			expected: v1.Service{
   734  				ObjectMeta: metav1.ObjectMeta{
   735  					Name: "test",
   736  				},
   737  				Spec: v1.ServiceSpec{
   738  					Selector: map[string]string{
   739  						"foo": "bar",
   740  						"baz": "blah",
   741  					},
   742  					Ports: []v1.ServicePort{
   743  						{
   744  							Port:       80,
   745  							Protocol:   "SCTP",
   746  							TargetPort: intstr.FromInt32(1234),
   747  						},
   748  					},
   749  					ClusterIP: "10.10.10.10",
   750  				},
   751  			},
   752  		},
   753  		{
   754  			generator: ServiceGeneratorV2{},
   755  			params: map[string]interface{}{
   756  				"selector":       "foo=bar,baz=blah",
   757  				"name":           "test",
   758  				"port":           "80",
   759  				"protocol":       "SCTP",
   760  				"container-port": "1234",
   761  				"cluster-ip":     "None",
   762  			},
   763  			expected: v1.Service{
   764  				ObjectMeta: metav1.ObjectMeta{
   765  					Name: "test",
   766  				},
   767  				Spec: v1.ServiceSpec{
   768  					Selector: map[string]string{
   769  						"foo": "bar",
   770  						"baz": "blah",
   771  					},
   772  					Ports: []v1.ServicePort{
   773  						{
   774  							Port:       80,
   775  							Protocol:   "SCTP",
   776  							TargetPort: intstr.FromInt32(1234),
   777  						},
   778  					},
   779  					ClusterIP: v1.ClusterIPNone,
   780  				},
   781  			},
   782  		},
   783  		{
   784  			generator: ServiceGeneratorV1{},
   785  			params: map[string]interface{}{
   786  				"selector":       "foo=bar",
   787  				"name":           "test",
   788  				"ports":          "80,443",
   789  				"protocol":       "SCTP",
   790  				"container-port": "foobar",
   791  			},
   792  			expected: v1.Service{
   793  				ObjectMeta: metav1.ObjectMeta{
   794  					Name: "test",
   795  				},
   796  				Spec: v1.ServiceSpec{
   797  					Selector: map[string]string{
   798  						"foo": "bar",
   799  					},
   800  					Ports: []v1.ServicePort{
   801  						{
   802  							Name:       "port-1",
   803  							Port:       80,
   804  							Protocol:   v1.ProtocolSCTP,
   805  							TargetPort: intstr.FromString("foobar"),
   806  						},
   807  						{
   808  							Name:       "port-2",
   809  							Port:       443,
   810  							Protocol:   v1.ProtocolSCTP,
   811  							TargetPort: intstr.FromString("foobar"),
   812  						},
   813  					},
   814  				},
   815  			},
   816  		},
   817  		{
   818  			generator: ServiceGeneratorV2{},
   819  			params: map[string]interface{}{
   820  				"selector": "foo=bar",
   821  				"name":     "test",
   822  				"ports":    "80,443",
   823  				"protocol": "SCTP",
   824  			},
   825  			expected: v1.Service{
   826  				ObjectMeta: metav1.ObjectMeta{
   827  					Name: "test",
   828  				},
   829  				Spec: v1.ServiceSpec{
   830  					Selector: map[string]string{
   831  						"foo": "bar",
   832  					},
   833  					Ports: []v1.ServicePort{
   834  						{
   835  							Name:       "port-1",
   836  							Port:       80,
   837  							Protocol:   v1.ProtocolSCTP,
   838  							TargetPort: intstr.FromInt32(80),
   839  						},
   840  						{
   841  							Name:       "port-2",
   842  							Port:       443,
   843  							Protocol:   v1.ProtocolSCTP,
   844  							TargetPort: intstr.FromInt32(443),
   845  						},
   846  					},
   847  				},
   848  			},
   849  		},
   850  		{
   851  			generator: ServiceGeneratorV2{},
   852  			params: map[string]interface{}{
   853  				"selector":  "foo=bar",
   854  				"name":      "test",
   855  				"ports":     "80,8080",
   856  				"protocols": "8080/SCTP",
   857  			},
   858  			expected: v1.Service{
   859  				ObjectMeta: metav1.ObjectMeta{
   860  					Name: "test",
   861  				},
   862  				Spec: v1.ServiceSpec{
   863  					Selector: map[string]string{
   864  						"foo": "bar",
   865  					},
   866  					Ports: []v1.ServicePort{
   867  						{
   868  							Name:       "port-1",
   869  							Port:       80,
   870  							Protocol:   v1.ProtocolTCP,
   871  							TargetPort: intstr.FromInt32(80),
   872  						},
   873  						{
   874  							Name:       "port-2",
   875  							Port:       8080,
   876  							Protocol:   v1.ProtocolSCTP,
   877  							TargetPort: intstr.FromInt32(8080),
   878  						},
   879  					},
   880  				},
   881  			},
   882  		},
   883  		{
   884  			generator: ServiceGeneratorV2{},
   885  			params: map[string]interface{}{
   886  				"selector":  "foo=bar",
   887  				"name":      "test",
   888  				"ports":     "80,8080,8081,8082",
   889  				"protocols": "8080/UDP,8081/TCP,8082/SCTP",
   890  			},
   891  			expected: v1.Service{
   892  				ObjectMeta: metav1.ObjectMeta{
   893  					Name: "test",
   894  				},
   895  				Spec: v1.ServiceSpec{
   896  					Selector: map[string]string{
   897  						"foo": "bar",
   898  					},
   899  					Ports: []v1.ServicePort{
   900  						{
   901  							Name:       "port-1",
   902  							Port:       80,
   903  							Protocol:   v1.ProtocolTCP,
   904  							TargetPort: intstr.FromInt32(80),
   905  						},
   906  						{
   907  							Name:       "port-2",
   908  							Port:       8080,
   909  							Protocol:   v1.ProtocolUDP,
   910  							TargetPort: intstr.FromInt32(8080),
   911  						},
   912  						{
   913  							Name:       "port-3",
   914  							Port:       8081,
   915  							Protocol:   v1.ProtocolTCP,
   916  							TargetPort: intstr.FromInt32(8081),
   917  						},
   918  						{
   919  							Name:       "port-4",
   920  							Port:       8082,
   921  							Protocol:   v1.ProtocolSCTP,
   922  							TargetPort: intstr.FromInt32(8082),
   923  						},
   924  					},
   925  				},
   926  			},
   927  		},
   928  		{
   929  			generator: ServiceGeneratorV2{},
   930  			params: map[string]interface{}{
   931  				"selector":       "foo=bar,baz=blah",
   932  				"name":           "test",
   933  				"protocol":       "SCTP",
   934  				"container-port": "1234",
   935  				"cluster-ip":     "None",
   936  			},
   937  			expected: v1.Service{
   938  				ObjectMeta: metav1.ObjectMeta{
   939  					Name: "test",
   940  				},
   941  				Spec: v1.ServiceSpec{
   942  					Selector: map[string]string{
   943  						"foo": "bar",
   944  						"baz": "blah",
   945  					},
   946  					Ports:     []v1.ServicePort{},
   947  					ClusterIP: v1.ClusterIPNone,
   948  				},
   949  			},
   950  		},
   951  	}
   952  	for _, tt := range tests {
   953  		t.Run(tt.name, func(t *testing.T) {
   954  			obj, err := tt.generator.Generate(tt.params)
   955  			if !reflect.DeepEqual(obj, &tt.expected) {
   956  				t.Errorf("expected:\n%#v\ngot\n%#v\n", &tt.expected, obj)
   957  			}
   958  			if err != nil {
   959  				t.Errorf("unexpected error: %v", err)
   960  			}
   961  		})
   962  	}
   963  }
   964  

View as plain text