service_basic_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 kubectl
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/util/intstr"
  19. )
  20. func TestServiceBasicGenerate(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. serviceType api.ServiceType
  24. tcp []string
  25. clusterip string
  26. expected *api.Service
  27. expectErr bool
  28. }{
  29. {
  30. name: "clusterip-ok",
  31. tcp: []string{"456", "321:908"},
  32. clusterip: "",
  33. serviceType: api.ServiceTypeClusterIP,
  34. expected: &api.Service{
  35. ObjectMeta: api.ObjectMeta{
  36. Name: "clusterip-ok",
  37. Labels: map[string]string{"app": "clusterip-ok"},
  38. },
  39. Spec: api.ServiceSpec{Type: "ClusterIP",
  40. Ports: []api.ServicePort{{Name: "456", Protocol: "TCP", Port: 456, TargetPort: intstr.IntOrString{Type: 0, IntVal: 456, StrVal: ""}, NodePort: 0},
  41. {Name: "321-908", Protocol: "TCP", Port: 321, TargetPort: intstr.IntOrString{Type: 0, IntVal: 908, StrVal: ""}, NodePort: 0}},
  42. Selector: map[string]string{"app": "clusterip-ok"},
  43. ClusterIP: "", ExternalIPs: []string(nil), LoadBalancerIP: ""},
  44. },
  45. expectErr: false,
  46. },
  47. {
  48. name: "clusterip-missing",
  49. serviceType: api.ServiceTypeClusterIP,
  50. expectErr: true,
  51. },
  52. {
  53. name: "clusterip-none and port mapping",
  54. tcp: []string{"456:9898"},
  55. clusterip: "None",
  56. serviceType: api.ServiceTypeClusterIP,
  57. expectErr: true,
  58. },
  59. {
  60. name: "clusterip-none-wrong-type",
  61. tcp: []string{},
  62. clusterip: "None",
  63. serviceType: api.ServiceTypeNodePort,
  64. expectErr: true,
  65. },
  66. {
  67. name: "clusterip-none-ok",
  68. tcp: []string{},
  69. clusterip: "None",
  70. serviceType: api.ServiceTypeClusterIP,
  71. expected: &api.Service{
  72. ObjectMeta: api.ObjectMeta{
  73. Name: "clusterip-none-ok",
  74. Labels: map[string]string{"app": "clusterip-none-ok"},
  75. },
  76. Spec: api.ServiceSpec{Type: "ClusterIP",
  77. Ports: []api.ServicePort{},
  78. Selector: map[string]string{"app": "clusterip-none-ok"},
  79. ClusterIP: "None", ExternalIPs: []string(nil), LoadBalancerIP: ""},
  80. },
  81. expectErr: false,
  82. },
  83. {
  84. name: "loadbalancer-ok",
  85. tcp: []string{"456:9898"},
  86. clusterip: "",
  87. serviceType: api.ServiceTypeLoadBalancer,
  88. expected: &api.Service{
  89. ObjectMeta: api.ObjectMeta{
  90. Name: "loadbalancer-ok",
  91. Labels: map[string]string{"app": "loadbalancer-ok"},
  92. },
  93. Spec: api.ServiceSpec{Type: "LoadBalancer",
  94. Ports: []api.ServicePort{{Name: "456-9898", Protocol: "TCP", Port: 456, TargetPort: intstr.IntOrString{Type: 0, IntVal: 9898, StrVal: ""}, NodePort: 0}},
  95. Selector: map[string]string{"app": "loadbalancer-ok"},
  96. ClusterIP: "", ExternalIPs: []string(nil), LoadBalancerIP: ""},
  97. },
  98. expectErr: false,
  99. },
  100. {
  101. expectErr: true,
  102. },
  103. }
  104. for _, test := range tests {
  105. generator := ServiceCommonGeneratorV1{
  106. Name: test.name,
  107. TCP: test.tcp,
  108. Type: test.serviceType,
  109. ClusterIP: test.clusterip,
  110. }
  111. obj, err := generator.StructuredGenerate()
  112. if !test.expectErr && err != nil {
  113. t.Errorf("unexpected error: %v", err)
  114. }
  115. if test.expectErr && err != nil {
  116. continue
  117. }
  118. if !reflect.DeepEqual(obj.(*api.Service), test.expected) {
  119. t.Errorf("test: %v\nexpected:\n%#v\nsaw:\n%#v", test.name, test.expected, obj.(*api.Service))
  120. }
  121. }
  122. }