...

Source file src/edge-infra.dev/pkg/sds/lib/etcd/server/embed/embed_test.go

Documentation: edge-infra.dev/pkg/sds/lib/etcd/server/embed

     1  package embed
     2  
     3  import (
     4  	"net"
     5  	"net/url"
     6  	"os"
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestMain(m *testing.M) {
    15  	os.Exit(m.Run())
    16  }
    17  
    18  func TestAssign(t *testing.T) {
    19  	localIP, err := getLocalIP()
    20  	require.NoError(t, err)
    21  
    22  	testCases := map[string]struct {
    23  		input        *url.URL
    24  		expectedHost string
    25  		expectedPort string
    26  		expectError  bool
    27  	}{
    28  		"Success": {
    29  			input: &url.URL{
    30  				Host: net.JoinHostPort(localIP.String(), "31654"),
    31  			},
    32  			expectedHost: localIP.String(),
    33  			expectedPort: "31654",
    34  			expectError:  false,
    35  		},
    36  		"Success_NoPort": {
    37  			input: &url.URL{
    38  				Host: localIP.String(),
    39  			},
    40  			expectedHost: localIP.String(),
    41  			expectedPort: "",
    42  			expectError:  false,
    43  		},
    44  		"Success_NoHost": {
    45  			input: &url.URL{
    46  				Host: ":31655",
    47  			},
    48  			expectedHost: "127.0.0.1",
    49  			expectedPort: "31655",
    50  			expectError:  false,
    51  		},
    52  		"Failure_InvalidHost": {
    53  			input: &url.URL{
    54  				Host: "invalid-made-up-host:8085",
    55  			},
    56  			expectError: true,
    57  		},
    58  		"Failure_InvalidPort": {
    59  			input: &url.URL{
    60  				Host: "127.0.0.1:invalid",
    61  			},
    62  			expectError: true,
    63  		},
    64  		"Failure_PortTooHigh": {
    65  			input: &url.URL{
    66  				Host: "127.0.0.1:70000",
    67  			},
    68  			expectError: true,
    69  		},
    70  	}
    71  
    72  	for name, tc := range testCases {
    73  		t.Run(name, func(t *testing.T) {
    74  			a := assignedAddress{}
    75  			err := a.assign(tc.input)
    76  			if tc.expectError {
    77  				require.Error(t, err)
    78  				return
    79  			}
    80  			require.NoError(t, err)
    81  
    82  			assert.Equal(t, tc.expectedHost, a.host)
    83  			switch tc.expectedPort {
    84  			case "":
    85  				port, err := strconv.Atoi(a.port)
    86  				require.NoError(t, err)
    87  				assert.GreaterOrEqual(t, 65535, port)
    88  				assert.LessOrEqual(t, 1, port)
    89  			default:
    90  				assert.Equal(t, tc.expectedPort, a.port)
    91  			}
    92  		})
    93  	}
    94  }
    95  
    96  func getLocalIP() (net.IP, error) {
    97  	conn, err := net.Dial("udp", "8.8.8.8:80")
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  	defer conn.Close()
   102  
   103  	localAddress := conn.LocalAddr().(*net.UDPAddr)
   104  
   105  	return localAddress.IP, nil
   106  }
   107  
   108  func TestURL(t *testing.T) {
   109  	testCases := map[string]struct {
   110  		input    assignedAddress
   111  		expected url.URL
   112  	}{
   113  		"Success_IP": {
   114  			input: assignedAddress{
   115  				host: "166.55.44.33",
   116  				port: "5432",
   117  			},
   118  			expected: url.URL{
   119  				Scheme: "http",
   120  				Host:   net.JoinHostPort("166.55.44.33", "5432"),
   121  			},
   122  		},
   123  		"Success_Hostname": {
   124  			input: assignedAddress{
   125  				host: "my-hostname",
   126  				port: "2345",
   127  			},
   128  			expected: url.URL{
   129  				Scheme: "http",
   130  				Host:   net.JoinHostPort("my-hostname", "2345"),
   131  			},
   132  		},
   133  	}
   134  
   135  	for name, tc := range testCases {
   136  		t.Run(name, func(t *testing.T) {
   137  			assert.Equal(t, tc.expected, tc.input.url())
   138  		})
   139  	}
   140  }
   141  

View as plain text