protocolsforobject_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Copyright 2018 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 polymorphichelpers
  14. import (
  15. "testing"
  16. "reflect"
  17. corev1 "k8s.io/api/core/v1"
  18. extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
  19. "k8s.io/apimachinery/pkg/runtime"
  20. )
  21. func TestProtocolsForObject(t *testing.T) {
  22. tests := []struct {
  23. object runtime.Object
  24. expectErr bool
  25. }{
  26. {
  27. object: &corev1.Pod{
  28. Spec: corev1.PodSpec{
  29. Containers: []corev1.Container{
  30. {
  31. Ports: []corev1.ContainerPort{
  32. {
  33. ContainerPort: 101,
  34. Protocol: "TCP",
  35. },
  36. },
  37. },
  38. },
  39. },
  40. },
  41. },
  42. // No protocol--should default to TCP.
  43. {
  44. object: &corev1.Pod{
  45. Spec: corev1.PodSpec{
  46. Containers: []corev1.Container{
  47. {
  48. Ports: []corev1.ContainerPort{
  49. {
  50. ContainerPort: 101,
  51. },
  52. },
  53. },
  54. },
  55. },
  56. },
  57. },
  58. {
  59. object: &corev1.Service{
  60. Spec: corev1.ServiceSpec{
  61. Ports: []corev1.ServicePort{
  62. {
  63. Port: 101,
  64. Protocol: "TCP",
  65. },
  66. },
  67. },
  68. },
  69. },
  70. // No protocol for service port--default to TCP
  71. {
  72. object: &corev1.Service{
  73. Spec: corev1.ServiceSpec{
  74. Ports: []corev1.ServicePort{
  75. {
  76. Port: 101,
  77. },
  78. },
  79. },
  80. },
  81. },
  82. {
  83. object: &corev1.ReplicationController{
  84. Spec: corev1.ReplicationControllerSpec{
  85. Template: &corev1.PodTemplateSpec{
  86. Spec: corev1.PodSpec{
  87. Containers: []corev1.Container{
  88. {
  89. Ports: []corev1.ContainerPort{
  90. {
  91. ContainerPort: 101,
  92. Protocol: "TCP",
  93. },
  94. },
  95. },
  96. },
  97. },
  98. },
  99. },
  100. },
  101. },
  102. {
  103. object: &extensionsv1beta1.Deployment{
  104. Spec: extensionsv1beta1.DeploymentSpec{
  105. Template: corev1.PodTemplateSpec{
  106. Spec: corev1.PodSpec{
  107. Containers: []corev1.Container{
  108. {
  109. Ports: []corev1.ContainerPort{
  110. {
  111. ContainerPort: 101,
  112. Protocol: "TCP",
  113. },
  114. },
  115. },
  116. },
  117. },
  118. },
  119. },
  120. },
  121. },
  122. {
  123. object: &extensionsv1beta1.ReplicaSet{
  124. Spec: extensionsv1beta1.ReplicaSetSpec{
  125. Template: corev1.PodTemplateSpec{
  126. Spec: corev1.PodSpec{
  127. Containers: []corev1.Container{
  128. {
  129. Ports: []corev1.ContainerPort{
  130. {
  131. ContainerPort: 101,
  132. Protocol: "TCP",
  133. },
  134. },
  135. },
  136. },
  137. },
  138. },
  139. },
  140. },
  141. },
  142. {
  143. object: &corev1.Node{},
  144. expectErr: true,
  145. },
  146. }
  147. expectedPorts := map[string]string{"101": "TCP"}
  148. for _, test := range tests {
  149. actual, err := protocolsForObject(test.object)
  150. if test.expectErr {
  151. if err == nil {
  152. t.Error("unexpected non-error")
  153. }
  154. continue
  155. }
  156. if !test.expectErr && err != nil {
  157. t.Errorf("unexpected error: %v", err)
  158. continue
  159. }
  160. if !reflect.DeepEqual(actual, expectedPorts) {
  161. t.Errorf("expected ports %v, but got %v", expectedPorts, actual)
  162. }
  163. }
  164. }