github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/controller/component/port_utils_test.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package component
    21  
    22  import (
    23  	"testing"
    24  
    25  	corev1 "k8s.io/api/core/v1"
    26  )
    27  
    28  func TestGetAvailableContainerPorts(t *testing.T) {
    29  	var containers []corev1.Container
    30  
    31  	tests := []struct {
    32  		inputPort  int32
    33  		outputPort int32
    34  	}{{
    35  		inputPort:  80, // 80 is a privileged port
    36  		outputPort: minAvailPort,
    37  	}, {
    38  		inputPort:  65536, // 65536 is an invalid port
    39  		outputPort: minAvailPort,
    40  	}, {
    41  		inputPort:  3306, // 3306 is a qualified port
    42  		outputPort: 3306,
    43  	}}
    44  
    45  	for _, test := range tests {
    46  		containerPorts := []int32{test.inputPort}
    47  		foundPorts, err := getAvailableContainerPorts(containers, containerPorts)
    48  		if err != nil {
    49  			t.Error("expect getAvailableContainerPorts success")
    50  		}
    51  		if len(foundPorts) != 1 || foundPorts[0] != test.outputPort {
    52  			t.Error("expect getAvailableContainerPorts returns", test.outputPort)
    53  		}
    54  	}
    55  }
    56  
    57  func TestGetAvailableContainerPortsPartlyOccupied(t *testing.T) {
    58  	var containers []corev1.Container
    59  
    60  	destPort := 3306
    61  	for p := minAvailPort; p < destPort; p++ {
    62  		containers = append(containers, corev1.Container{Ports: []corev1.ContainerPort{{ContainerPort: int32(p)}}})
    63  	}
    64  
    65  	containerPorts := []int32{minAvailPort + 1}
    66  	foundPorts, err := getAvailableContainerPorts(containers, containerPorts)
    67  	if err != nil {
    68  		t.Error("expect getAvailableContainerPorts success")
    69  	}
    70  	if len(foundPorts) != 1 || foundPorts[0] != int32(destPort) {
    71  		t.Error("expect getAvailableContainerPorts returns 3306")
    72  	}
    73  }
    74  
    75  func TestGetAvailableContainerPortsFullyOccupied(t *testing.T) {
    76  	var containers []corev1.Container
    77  
    78  	for p := minAvailPort; p <= maxAvailPort; p++ {
    79  		containers = append(containers, corev1.Container{Ports: []corev1.ContainerPort{{ContainerPort: int32(p)}}})
    80  	}
    81  
    82  	containerPorts := []int32{3306}
    83  	_, err := getAvailableContainerPorts(containers, containerPorts)
    84  	if err == nil {
    85  		t.Error("expect getAvailableContainerPorts return error")
    86  	}
    87  }