server_pod_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package compute
  15. import (
  16. "reflect"
  17. "testing"
  18. computeapi "yunion.io/x/onecloud/pkg/apis/compute"
  19. )
  20. func Test_parsePodPortMapping(t *testing.T) {
  21. var port80 int = 80
  22. tests := []struct {
  23. args string
  24. want *computeapi.PodPortMapping
  25. wantErr bool
  26. }{
  27. {
  28. args: "80:8080/tcp",
  29. want: &computeapi.PodPortMapping{
  30. Protocol: computeapi.PodPortMappingProtocolTCP,
  31. ContainerPort: 8080,
  32. HostPort: &port80,
  33. },
  34. wantErr: false,
  35. },
  36. {
  37. args: "80:8080",
  38. want: &computeapi.PodPortMapping{
  39. Protocol: computeapi.PodPortMappingProtocolTCP,
  40. ContainerPort: 8080,
  41. HostPort: &port80,
  42. },
  43. wantErr: false,
  44. },
  45. {
  46. args: "80:8080:tcp",
  47. want: nil,
  48. wantErr: true,
  49. },
  50. {
  51. args: "80",
  52. want: &computeapi.PodPortMapping{
  53. Protocol: computeapi.PodPortMappingProtocolTCP,
  54. ContainerPort: 80,
  55. },
  56. },
  57. {
  58. args: "80s:ctrP",
  59. want: nil,
  60. wantErr: true,
  61. },
  62. }
  63. for _, tt := range tests {
  64. t.Run(tt.args, func(t *testing.T) {
  65. got, err := parsePodPortMapping(tt.args)
  66. if (err != nil) != tt.wantErr {
  67. t.Errorf("parsePodPortMapping() error = %v, wantErr %v", err, tt.wantErr)
  68. return
  69. }
  70. if !reflect.DeepEqual(got, tt.want) {
  71. t.Errorf("parsePodPortMapping() got = %v, want %v", got, tt.want)
  72. }
  73. })
  74. }
  75. }
  76. func Test_parsePodPortMappingDetails(t *testing.T) {
  77. port8080 := 8080
  78. tests := []struct {
  79. input string
  80. want *computeapi.PodPortMapping
  81. wantErr bool
  82. }{
  83. {
  84. input: "host_port=8080,port=80",
  85. want: &computeapi.PodPortMapping{
  86. Protocol: computeapi.PodPortMappingProtocolTCP,
  87. ContainerPort: 80,
  88. HostPort: &port8080,
  89. },
  90. },
  91. {
  92. input: "host_port=8080,port=80,protocol=udp",
  93. want: &computeapi.PodPortMapping{
  94. Protocol: computeapi.PodPortMappingProtocolUDP,
  95. ContainerPort: 80,
  96. HostPort: &port8080,
  97. },
  98. },
  99. {
  100. input: "host_port=8080,protocol=udp",
  101. wantErr: true,
  102. },
  103. {
  104. input: "container_port=80,protocol=udp,host_port_range=20000-25000",
  105. want: &computeapi.PodPortMapping{
  106. Protocol: computeapi.PodPortMappingProtocolUDP,
  107. ContainerPort: 80,
  108. HostPortRange: &computeapi.PodPortMappingPortRange{
  109. Start: 20000,
  110. End: 25000,
  111. },
  112. },
  113. },
  114. }
  115. for _, tt := range tests {
  116. t.Run(tt.input, func(t *testing.T) {
  117. got, err := parsePodPortMappingDetails(tt.input)
  118. if (err != nil) != tt.wantErr {
  119. t.Errorf("parsePodPortMappingDetails() error = %v, wantErr %v", err, tt.wantErr)
  120. return
  121. }
  122. if !reflect.DeepEqual(got, tt.want) {
  123. t.Errorf("parsePodPortMappingDetails() got = %v, want %v", got, tt.want)
  124. }
  125. })
  126. }
  127. }