port_allocator_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright 2015 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 userspace
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apimachinery/pkg/util/net"
  18. )
  19. func TestRangeAllocatorEmpty(t *testing.T) {
  20. r := &net.PortRange{}
  21. r.Set("0-0")
  22. defer func() {
  23. if rv := recover(); rv == nil {
  24. t.Fatalf("expected panic because of empty port range: %#v", r)
  25. }
  26. }()
  27. _ = newPortRangeAllocator(*r, true)
  28. }
  29. func TestRangeAllocatorFullyAllocated(t *testing.T) {
  30. r := &net.PortRange{}
  31. r.Set("1-1")
  32. // Don't auto-fill ports, we'll manually turn the crank
  33. pra := newPortRangeAllocator(*r, false)
  34. a := pra.(*rangeAllocator)
  35. // Fill in the one available port
  36. if !a.fillPortsOnce() {
  37. t.Fatalf("Expected to be able to fill ports")
  38. }
  39. // There should be no ports available
  40. if a.fillPortsOnce() {
  41. t.Fatalf("Expected to be unable to fill ports")
  42. }
  43. p, err := a.AllocateNext()
  44. if err != nil {
  45. t.Fatalf("unexpected error: %v", err)
  46. }
  47. if p != 1 {
  48. t.Fatalf("unexpected allocated port: %d", p)
  49. }
  50. a.lock.Lock()
  51. if bit := a.used.Bit(p - a.Base); bit != 1 {
  52. a.lock.Unlock()
  53. t.Fatalf("unexpected used bit for allocated port: %d", p)
  54. }
  55. a.lock.Unlock()
  56. _, err = a.AllocateNext()
  57. if err == nil {
  58. t.Fatalf("expected error because of fully-allocated range")
  59. }
  60. a.Release(p)
  61. a.lock.Lock()
  62. if bit := a.used.Bit(p - a.Base); bit != 0 {
  63. a.lock.Unlock()
  64. t.Fatalf("unexpected used bit for allocated port: %d", p)
  65. }
  66. a.lock.Unlock()
  67. // Fill in the one available port
  68. if !a.fillPortsOnce() {
  69. t.Fatalf("Expected to be able to fill ports")
  70. }
  71. p, err = a.AllocateNext()
  72. if err != nil {
  73. t.Fatalf("unexpected error: %v", err)
  74. }
  75. if p != 1 {
  76. t.Fatalf("unexpected allocated port: %d", p)
  77. }
  78. a.lock.Lock()
  79. if bit := a.used.Bit(p - a.Base); bit != 1 {
  80. a.lock.Unlock()
  81. t.Fatalf("unexpected used bit for allocated port: %d", p)
  82. }
  83. a.lock.Unlock()
  84. _, err = a.AllocateNext()
  85. if err == nil {
  86. t.Fatalf("expected error because of fully-allocated range")
  87. }
  88. }
  89. func TestRangeAllocator_RandomishAllocation(t *testing.T) {
  90. r := &net.PortRange{}
  91. r.Set("1-100")
  92. pra := newPortRangeAllocator(*r, false)
  93. a := pra.(*rangeAllocator)
  94. // allocate all the ports
  95. var err error
  96. ports := make([]int, 100, 100)
  97. for i := 0; i < 100; i++ {
  98. if !a.fillPortsOnce() {
  99. t.Fatalf("Expected to be able to fill ports")
  100. }
  101. ports[i], err = a.AllocateNext()
  102. if err != nil {
  103. t.Fatalf("unexpected error: %v", err)
  104. }
  105. if ports[i] < 1 || ports[i] > 100 {
  106. t.Fatalf("unexpected allocated port: %d", ports[i])
  107. }
  108. a.lock.Lock()
  109. if bit := a.used.Bit(ports[i] - a.Base); bit != 1 {
  110. a.lock.Unlock()
  111. t.Fatalf("unexpected used bit for allocated port: %d", ports[i])
  112. }
  113. a.lock.Unlock()
  114. }
  115. if a.fillPortsOnce() {
  116. t.Fatalf("Expected to be unable to fill ports")
  117. }
  118. // release them all
  119. for i := 0; i < 100; i++ {
  120. a.Release(ports[i])
  121. a.lock.Lock()
  122. if bit := a.used.Bit(ports[i] - a.Base); bit != 0 {
  123. a.lock.Unlock()
  124. t.Fatalf("unexpected used bit for allocated port: %d", ports[i])
  125. }
  126. a.lock.Unlock()
  127. }
  128. // allocate the ports again
  129. rports := make([]int, 100, 100)
  130. for i := 0; i < 100; i++ {
  131. if !a.fillPortsOnce() {
  132. t.Fatalf("Expected to be able to fill ports")
  133. }
  134. rports[i], err = a.AllocateNext()
  135. if err != nil {
  136. t.Fatalf("unexpected error: %v", err)
  137. }
  138. if rports[i] < 1 || rports[i] > 100 {
  139. t.Fatalf("unexpected allocated port: %d", rports[i])
  140. }
  141. a.lock.Lock()
  142. if bit := a.used.Bit(rports[i] - a.Base); bit != 1 {
  143. a.lock.Unlock()
  144. t.Fatalf("unexpected used bit for allocated port: %d", rports[i])
  145. }
  146. a.lock.Unlock()
  147. }
  148. if a.fillPortsOnce() {
  149. t.Fatalf("Expected to be unable to fill ports")
  150. }
  151. if reflect.DeepEqual(ports, rports) {
  152. t.Fatalf("expected re-allocated ports to be in a somewhat random order")
  153. }
  154. }