k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/core/service/portallocator/operation_test.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package portallocator 18 19 import ( 20 "testing" 21 22 "k8s.io/apimachinery/pkg/util/net" 23 ) 24 25 // TestDryRunAllocate tests the Allocate function in dry run mode 26 func TestDryRunAllocate(t *testing.T) { 27 pr, err := net.ParsePortRange("10000-10200") 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 // Allocate some ports before calling 33 previouslyAllocated := []int{10000, 10010, 10020} 34 r, err := NewInMemory(*pr) 35 if err != nil { 36 t.Fatal(err) 37 } 38 for _, port := range previouslyAllocated { 39 _ = r.Allocate(port) 40 } 41 freeAtStart := r.Free() 42 43 // Do some allocations with a dry run operation 44 toAllocate := []int{ 45 10000, 46 10030, 47 10030, 48 10040, 49 } 50 expectedErrors := []error{ 51 ErrAllocated, 52 nil, 53 ErrAllocated, 54 nil, 55 } 56 op := StartOperation(r, true) 57 for i, port := range toAllocate { 58 err := op.Allocate(port) 59 if err != expectedErrors[i] { 60 t.Errorf("%v: expected error %v but got %v", i, expectedErrors[i], err) 61 } 62 } 63 64 // Make sure no port allocations were actually made by the dry run 65 freeAtEnd := r.Free() 66 if freeAtStart != freeAtEnd { 67 t.Errorf("expected %v free ports but got %v", freeAtStart, freeAtEnd) 68 } 69 } 70 71 // TestDryRunAllocateNext tests the AllocateNext function in dry run mode 72 func TestDryRunAllocateNext(t *testing.T) { 73 pr, err := net.ParsePortRange("10000-10200") 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 // Allocate some ports before calling 79 previouslyAllocated := []int{10000, 10010, 10020} 80 r, err := NewInMemory(*pr) 81 if err != nil { 82 t.Fatal(err) 83 } 84 for _, port := range previouslyAllocated { 85 _ = r.Allocate(port) 86 } 87 freeAtStart := r.Free() 88 89 // AllocateNext without a previously unused dry run operation 90 op := StartOperation(r, true) 91 port, err := op.AllocateNext() 92 if port == 0 { 93 t.Errorf("expected non zero port but got: %v", port) 94 } 95 if err != nil { 96 t.Errorf("expected no error but got: %v", err) 97 } 98 99 // Try to allocate the returned port using the same operation 100 if e, a := ErrAllocated, op.Allocate(port); e != a { 101 t.Errorf("expected %v but got: %v", e, a) 102 } 103 104 // AllocateNext with a previously used dry run operation 105 op = StartOperation(r, true) 106 _ = op.Allocate(12345) 107 port, err = op.AllocateNext() 108 if port == 0 { 109 t.Errorf("expected non zero port but got: %v", port) 110 } 111 if port == 12345 { 112 t.Errorf("expected port not to be 12345 but got %v", port) 113 } 114 if err != nil { 115 t.Errorf("expected no error but got: %v", err) 116 } 117 118 // Make sure no port allocations were actually made by the dry run 119 freeAtEnd := r.Free() 120 if freeAtStart != freeAtEnd { 121 t.Errorf("expected %v free ports but got %v", freeAtStart, freeAtEnd) 122 } 123 }