k8s.io/apiserver@v0.31.1/pkg/server/options/serving_unix_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5  Copyright 2020 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package options
    21  
    22  import (
    23  	"fmt"
    24  	"net"
    25  	"testing"
    26  )
    27  
    28  func TestCreateListenerSharePort(t *testing.T) {
    29  	c := net.ListenConfig{Control: permitPortReuse}
    30  
    31  	l, port, err := CreateListener("tcp", "127.0.0.1:0", c)
    32  	if err != nil {
    33  		t.Fatalf("failed to create listener: %v", err)
    34  	}
    35  	defer l.Close()
    36  
    37  	l2, _, err := CreateListener("tcp", fmt.Sprintf("127.0.0.1:%d", port), c)
    38  	if err != nil {
    39  		t.Fatalf("failed to create 2nd listener: %v", err)
    40  	}
    41  	defer l2.Close()
    42  }
    43  
    44  func TestCreateListenerPreventUpgrades(t *testing.T) {
    45  	l, port, err := CreateListener("tcp", "127.0.0.1:0", net.ListenConfig{})
    46  	if err != nil {
    47  		t.Fatalf("failed to create listener: %v", err)
    48  	}
    49  	defer l.Close()
    50  
    51  	l2, _, err := CreateListener("tcp", fmt.Sprintf("127.0.0.1:%d", port), net.ListenConfig{Control: permitPortReuse})
    52  	if err == nil {
    53  		l2.Close()
    54  		t.Fatalf("creating second listener without port sharing should fail")
    55  	}
    56  }