pod_port_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 util
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. )
  18. func TestLookupContainerPortNumberByName(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. pod v1.Pod
  22. portname string
  23. portnum int32
  24. err bool
  25. }{
  26. {
  27. name: "test success 1",
  28. pod: v1.Pod{
  29. Spec: v1.PodSpec{
  30. Containers: []v1.Container{
  31. {
  32. Ports: []v1.ContainerPort{
  33. {
  34. Name: "https",
  35. ContainerPort: int32(443)},
  36. {
  37. Name: "http",
  38. ContainerPort: int32(80)},
  39. },
  40. },
  41. },
  42. },
  43. },
  44. portname: "http",
  45. portnum: int32(80),
  46. err: false,
  47. },
  48. {
  49. name: "test faulure 1",
  50. pod: v1.Pod{
  51. Spec: v1.PodSpec{
  52. Containers: []v1.Container{
  53. {
  54. Ports: []v1.ContainerPort{
  55. {
  56. Name: "https",
  57. ContainerPort: int32(443)},
  58. },
  59. },
  60. },
  61. },
  62. },
  63. portname: "www",
  64. portnum: int32(0),
  65. err: true,
  66. },
  67. }
  68. for _, tt := range tests {
  69. t.Run(tt.name, func(t *testing.T) {
  70. portnum, err := LookupContainerPortNumberByName(tt.pod, tt.portname)
  71. if err != nil {
  72. if tt.err {
  73. return
  74. }
  75. t.Errorf("%v: unexpected error: %v", tt.name, err)
  76. return
  77. }
  78. if tt.err {
  79. t.Errorf("%v: unexpected success", tt.name)
  80. return
  81. }
  82. if portnum != tt.portnum {
  83. t.Errorf("%v: expected port number %v; got %v", tt.name, tt.portnum, portnum)
  84. }
  85. })
  86. }
  87. }