service_basic_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package versioned
  14. import (
  15. "reflect"
  16. "strings"
  17. "testing"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/util/intstr"
  21. )
  22. func TestServiceBasicGenerate(t *testing.T) {
  23. tests := []struct {
  24. name string
  25. serviceType v1.ServiceType
  26. tcp []string
  27. clusterip string
  28. expected *v1.Service
  29. expectErr bool
  30. }{
  31. {
  32. name: "clusterip-ok",
  33. tcp: []string{"456", "321:908"},
  34. clusterip: "",
  35. serviceType: v1.ServiceTypeClusterIP,
  36. expected: &v1.Service{
  37. ObjectMeta: metav1.ObjectMeta{
  38. Name: "clusterip-ok",
  39. Labels: map[string]string{"app": "clusterip-ok"},
  40. },
  41. Spec: v1.ServiceSpec{Type: "ClusterIP",
  42. Ports: []v1.ServicePort{{Name: "456", Protocol: "TCP", Port: 456, TargetPort: intstr.IntOrString{Type: 0, IntVal: 456, StrVal: ""}, NodePort: 0},
  43. {Name: "321-908", Protocol: "TCP", Port: 321, TargetPort: intstr.IntOrString{Type: 0, IntVal: 908, StrVal: ""}, NodePort: 0}},
  44. Selector: map[string]string{"app": "clusterip-ok"},
  45. ClusterIP: "", ExternalIPs: []string(nil), LoadBalancerIP: ""},
  46. },
  47. expectErr: false,
  48. },
  49. {
  50. name: "clusterip-missing",
  51. serviceType: v1.ServiceTypeClusterIP,
  52. expectErr: true,
  53. },
  54. {
  55. name: "clusterip-none-wrong-type",
  56. tcp: []string{},
  57. clusterip: "None",
  58. serviceType: v1.ServiceTypeNodePort,
  59. expectErr: true,
  60. },
  61. {
  62. name: "clusterip-none-ok",
  63. tcp: []string{},
  64. clusterip: "None",
  65. serviceType: v1.ServiceTypeClusterIP,
  66. expected: &v1.Service{
  67. ObjectMeta: metav1.ObjectMeta{
  68. Name: "clusterip-none-ok",
  69. Labels: map[string]string{"app": "clusterip-none-ok"},
  70. },
  71. Spec: v1.ServiceSpec{Type: "ClusterIP",
  72. Ports: []v1.ServicePort{},
  73. Selector: map[string]string{"app": "clusterip-none-ok"},
  74. ClusterIP: "None", ExternalIPs: []string(nil), LoadBalancerIP: ""},
  75. },
  76. expectErr: false,
  77. },
  78. {
  79. name: "clusterip-none-and-port-mapping",
  80. tcp: []string{"456:9898"},
  81. clusterip: "None",
  82. serviceType: v1.ServiceTypeClusterIP,
  83. expected: &v1.Service{
  84. ObjectMeta: metav1.ObjectMeta{
  85. Name: "clusterip-none-and-port-mapping",
  86. Labels: map[string]string{"app": "clusterip-none-and-port-mapping"},
  87. },
  88. Spec: v1.ServiceSpec{Type: "ClusterIP",
  89. Ports: []v1.ServicePort{{Name: "456-9898", Protocol: "TCP", Port: 456, TargetPort: intstr.IntOrString{Type: 0, IntVal: 9898, StrVal: ""}, NodePort: 0}},
  90. Selector: map[string]string{"app": "clusterip-none-and-port-mapping"},
  91. ClusterIP: "None", ExternalIPs: []string(nil), LoadBalancerIP: ""},
  92. },
  93. expectErr: false,
  94. },
  95. {
  96. name: "loadbalancer-ok",
  97. tcp: []string{"456:9898"},
  98. clusterip: "",
  99. serviceType: v1.ServiceTypeLoadBalancer,
  100. expected: &v1.Service{
  101. ObjectMeta: metav1.ObjectMeta{
  102. Name: "loadbalancer-ok",
  103. Labels: map[string]string{"app": "loadbalancer-ok"},
  104. },
  105. Spec: v1.ServiceSpec{Type: "LoadBalancer",
  106. Ports: []v1.ServicePort{{Name: "456-9898", Protocol: "TCP", Port: 456, TargetPort: intstr.IntOrString{Type: 0, IntVal: 9898, StrVal: ""}, NodePort: 0}},
  107. Selector: map[string]string{"app": "loadbalancer-ok"},
  108. ClusterIP: "", ExternalIPs: []string(nil), LoadBalancerIP: ""},
  109. },
  110. expectErr: false,
  111. },
  112. {
  113. name: "invalid-port",
  114. tcp: []string{"65536"},
  115. clusterip: "None",
  116. serviceType: v1.ServiceTypeClusterIP,
  117. expectErr: true,
  118. },
  119. {
  120. name: "invalid-port-mapping",
  121. tcp: []string{"8080:-abc"},
  122. clusterip: "None",
  123. serviceType: v1.ServiceTypeClusterIP,
  124. expectErr: true,
  125. },
  126. {
  127. expectErr: true,
  128. },
  129. }
  130. for _, test := range tests {
  131. generator := ServiceCommonGeneratorV1{
  132. Name: test.name,
  133. TCP: test.tcp,
  134. Type: test.serviceType,
  135. ClusterIP: test.clusterip,
  136. }
  137. obj, err := generator.StructuredGenerate()
  138. if !test.expectErr && err != nil {
  139. t.Errorf("unexpected error: %v", err)
  140. }
  141. if test.expectErr && err != nil {
  142. continue
  143. }
  144. if !reflect.DeepEqual(obj.(*v1.Service), test.expected) {
  145. t.Errorf("test: %v\nexpected:\n%#v\nsaw:\n%#v", test.name, test.expected, obj.(*v1.Service))
  146. }
  147. }
  148. }
  149. func TestParsePorts(t *testing.T) {
  150. tests := []struct {
  151. portString string
  152. expectPort int32
  153. expectTargetPort intstr.IntOrString
  154. expectErr string
  155. }{
  156. {
  157. portString: "3232",
  158. expectPort: 3232,
  159. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 3232},
  160. },
  161. {
  162. portString: "1:65535",
  163. expectPort: 1,
  164. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 65535},
  165. },
  166. {
  167. portString: "-5:1234",
  168. expectPort: 0,
  169. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
  170. expectErr: "must be between 1 and 65535, inclusive",
  171. },
  172. {
  173. portString: "5:65536",
  174. expectPort: 0,
  175. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
  176. expectErr: "must be between 1 and 65535, inclusive",
  177. },
  178. {
  179. portString: "test-5:443",
  180. expectPort: 0,
  181. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
  182. expectErr: "invalid syntax",
  183. },
  184. {
  185. portString: "5:test-443",
  186. expectPort: 5,
  187. expectTargetPort: intstr.IntOrString{Type: intstr.String, StrVal: "test-443"},
  188. },
  189. {
  190. portString: "5:test*443",
  191. expectPort: 0,
  192. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
  193. expectErr: "must contain only alpha-numeric characters (a-z, 0-9), and hyphens (-)",
  194. },
  195. {
  196. portString: "5:",
  197. expectPort: 0,
  198. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
  199. expectErr: "must contain at least one letter or number (a-z, 0-9)",
  200. },
  201. {
  202. portString: "5:test--443",
  203. expectPort: 0,
  204. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
  205. expectErr: "must not contain consecutive hyphens",
  206. },
  207. {
  208. portString: "5:test443-",
  209. expectPort: 0,
  210. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 0},
  211. expectErr: "must not begin or end with a hyphen",
  212. },
  213. {
  214. portString: "3232:1234:4567",
  215. expectPort: 3232,
  216. expectTargetPort: intstr.IntOrString{Type: intstr.Int, IntVal: 1234},
  217. },
  218. }
  219. for _, test := range tests {
  220. t.Run(test.portString, func(t *testing.T) {
  221. port, targetPort, err := parsePorts(test.portString)
  222. if len(test.expectErr) != 0 {
  223. if !strings.Contains(err.Error(), test.expectErr) {
  224. t.Errorf("parse ports string: %s. Expected err: %s, Got err: %v.", test.portString, test.expectErr, err)
  225. }
  226. }
  227. if !reflect.DeepEqual(targetPort, test.expectTargetPort) || port != test.expectPort {
  228. t.Errorf("parse ports string: %s. Expected port:%d, targetPort:%v, Got port:%d, targetPort:%v.", test.portString, test.expectPort, test.expectTargetPort, port, targetPort)
  229. }
  230. })
  231. }
  232. }
  233. func TestValidateServiceCommonGeneratorV1(t *testing.T) {
  234. tests := []struct {
  235. name string
  236. s ServiceCommonGeneratorV1
  237. expectErr string
  238. }{
  239. {
  240. name: "validate-ok",
  241. s: ServiceCommonGeneratorV1{
  242. Name: "validate-ok",
  243. Type: v1.ServiceTypeClusterIP,
  244. TCP: []string{"123", "234:1234"},
  245. ClusterIP: "",
  246. },
  247. },
  248. {
  249. name: "Name-none",
  250. s: ServiceCommonGeneratorV1{
  251. Type: v1.ServiceTypeClusterIP,
  252. TCP: []string{"123", "234:1234"},
  253. ClusterIP: "",
  254. },
  255. expectErr: "name must be specified",
  256. },
  257. {
  258. name: "Type-none",
  259. s: ServiceCommonGeneratorV1{
  260. Name: "validate-ok",
  261. TCP: []string{"123", "234:1234"},
  262. ClusterIP: "",
  263. },
  264. expectErr: "type must be specified",
  265. },
  266. {
  267. name: "invalid-ClusterIPNone",
  268. s: ServiceCommonGeneratorV1{
  269. Name: "validate-ok",
  270. Type: v1.ServiceTypeNodePort,
  271. TCP: []string{"123", "234:1234"},
  272. ClusterIP: v1.ClusterIPNone,
  273. },
  274. expectErr: "ClusterIP=None can only be used with ClusterIP service type",
  275. },
  276. {
  277. name: "TCP-none",
  278. s: ServiceCommonGeneratorV1{
  279. Name: "validate-ok",
  280. Type: v1.ServiceTypeClusterIP,
  281. ClusterIP: "",
  282. },
  283. expectErr: "at least one tcp port specifier must be provided",
  284. },
  285. {
  286. name: "invalid-ExternalName",
  287. s: ServiceCommonGeneratorV1{
  288. Name: "validate-ok",
  289. Type: v1.ServiceTypeExternalName,
  290. TCP: []string{"123", "234:1234"},
  291. ClusterIP: "",
  292. ExternalName: "@oi:test",
  293. },
  294. expectErr: "invalid service external name",
  295. },
  296. }
  297. for _, test := range tests {
  298. t.Run(test.name, func(t *testing.T) {
  299. err := test.s.validate()
  300. if err != nil {
  301. if !strings.Contains(err.Error(), test.expectErr) {
  302. t.Errorf("validate:%s Expected err: %s, Got err: %v", test.name, test.expectErr, err)
  303. }
  304. }
  305. if err == nil && len(test.expectErr) != 0 {
  306. t.Errorf("validate:%s Expected success, Got err: %v", test.name, err)
  307. }
  308. })
  309. }
  310. }