github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/integration/resources/net/net.go (about)

     1  // Copyright (c) 2021  Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  // Package net contains network-related helpers for integration testing.
    22  package net
    23  
    24  import (
    25  	"net"
    26  	"strconv"
    27  )
    28  
    29  // GetAvailablePort retrieves an available port on the host machine. This
    30  // delegates the port selection to the golang net library by starting a server and
    31  // then checking the port that the server is using.
    32  func GetAvailablePort() (int, error) {
    33  	l, err := net.Listen("tcp", ":0") // nolint: gosec
    34  	if err != nil {
    35  		return 0, nil
    36  	}
    37  	defer l.Close() // nolint: errcheck
    38  
    39  	_, p, err := net.SplitHostPort(l.Addr().String())
    40  	if err != nil {
    41  		return 0, err
    42  	}
    43  	port, err := strconv.Atoi(p)
    44  	if err != nil {
    45  		return 0, err
    46  	}
    47  	return port, nil
    48  }
    49  
    50  // MaybeGeneratePort takes an address and generates a new version of
    51  // that address with the port replaced with an open port if the original
    52  // port was 0. Otherwise, the address is returned unchanged.
    53  // This method returns the potentially updated address, the potentially
    54  // updated port, a boolean indicating if the address was changed, and
    55  // an error in case the method failed
    56  func MaybeGeneratePort(address string) (string, int, bool, error) {
    57  	_, p, err := net.SplitHostPort(address)
    58  	if err != nil {
    59  		return "", 0, false, err
    60  	}
    61  
    62  	port, err := strconv.Atoi(p)
    63  	if err != nil {
    64  		return "", 0, false, err
    65  	}
    66  
    67  	if port == 0 {
    68  		if address, port, err = GeneratePort(address); err != nil {
    69  			return "", 0, false, err
    70  		}
    71  	}
    72  
    73  	return address, port, false, nil
    74  }
    75  
    76  // GeneratePort takes the address in the string provided and updates
    77  // the port to one that is open. Returns the complete address, the
    78  // chosen port, and the error, if any.
    79  func GeneratePort(address string) (string, int, error) {
    80  	newPort, err := GetAvailablePort()
    81  	if err != nil {
    82  		return "", 0, err
    83  	}
    84  
    85  	h, _, err := net.SplitHostPort(address)
    86  	if err != nil {
    87  		return "", 0, err
    88  	}
    89  
    90  	newAddr := net.JoinHostPort(h, strconv.Itoa(newPort))
    91  
    92  	return newAddr, newPort, nil
    93  }