go.temporal.io/server@v1.23.0/common/config/localip_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package config
    26  
    27  // ** This code is copied from tchannel, we would like to not take dependency on tchannel code **
    28  
    29  import (
    30  	"net"
    31  	"testing"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  )
    35  
    36  func TestScoreAddr(t *testing.T) {
    37  	ipv4 := net.ParseIP("10.0.1.2")
    38  	ipv6 := net.ParseIP("2001:db8:a0b:12f0::1")
    39  
    40  	tests := []struct {
    41  		msg    string
    42  		iface  net.Interface
    43  		addr   net.Addr
    44  		want   int
    45  		wantIP net.IP
    46  	}{
    47  		{
    48  			msg:    "non-local up ipv4 IPNet address",
    49  			iface:  net.Interface{Flags: net.FlagUp},
    50  			addr:   &net.IPNet{IP: ipv4},
    51  			want:   500,
    52  			wantIP: ipv4,
    53  		},
    54  		{
    55  			msg:    "non-local up ipv4 IPAddr address",
    56  			iface:  net.Interface{Flags: net.FlagUp},
    57  			addr:   &net.IPAddr{IP: ipv4},
    58  			want:   500,
    59  			wantIP: ipv4,
    60  		},
    61  		{
    62  			msg: "non-local up ipv4 IPAddr address, docker interface",
    63  			iface: net.Interface{
    64  				Flags:        net.FlagUp,
    65  				HardwareAddr: mustParseMAC("02:42:ac:11:56:af"),
    66  			},
    67  			addr:   &net.IPNet{IP: ipv4},
    68  			want:   500,
    69  			wantIP: ipv4,
    70  		},
    71  		{
    72  			msg: "non-local up ipv4 address, local MAC address",
    73  			iface: net.Interface{
    74  				Flags:        net.FlagUp,
    75  				HardwareAddr: mustParseMAC("02:42:9c:52:fc:86"),
    76  			},
    77  			addr:   &net.IPNet{IP: ipv4},
    78  			want:   500,
    79  			wantIP: ipv4,
    80  		},
    81  		{
    82  			msg:    "non-local down ipv4 address",
    83  			iface:  net.Interface{},
    84  			addr:   &net.IPNet{IP: ipv4},
    85  			want:   400,
    86  			wantIP: ipv4,
    87  		},
    88  		{
    89  			msg:    "non-local down ipv6 address",
    90  			iface:  net.Interface{},
    91  			addr:   &net.IPAddr{IP: ipv6},
    92  			want:   100,
    93  			wantIP: ipv6,
    94  		},
    95  		{
    96  			msg:   "unknown address type",
    97  			iface: net.Interface{},
    98  			addr:  &net.UnixAddr{Name: "/tmp/socket"},
    99  			want:  -1,
   100  		},
   101  	}
   102  
   103  	for _, tt := range tests {
   104  		gotScore, gotIP := scoreAddr(tt.iface, tt.addr)
   105  		assert.Equal(t, tt.want, gotScore, tt.msg)
   106  		assert.Equal(t, tt.wantIP, gotIP, tt.msg)
   107  	}
   108  }