...

Source file src/github.com/linkerd/linkerd2/pkg/util/portrange_test.go

Documentation: github.com/linkerd/linkerd2/pkg/util

     1  package util
     2  
     3  import (
     4  	"reflect"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestParsePort(t *testing.T) {
    10  	tests := []struct {
    11  		input  string
    12  		expect int
    13  	}{
    14  		{"0", 0},
    15  		{"8080", 8080},
    16  		{"65535", 65535},
    17  	}
    18  	for _, tt := range tests {
    19  		t.Run(tt.input, func(t *testing.T) {
    20  			if check, _ := ParsePort(tt.input); check != tt.expect {
    21  				t.Fatalf("expected %d but received %d", tt.expect, check)
    22  			}
    23  		})
    24  	}
    25  }
    26  
    27  func TestParsePort_Errors(t *testing.T) {
    28  	tests := []string{"-1", "65536"}
    29  	for _, tt := range tests {
    30  		t.Run(tt, func(t *testing.T) {
    31  			if r, err := ParsePort(tt); err == nil {
    32  				t.Fatalf("expected error but received %d", r)
    33  			}
    34  		})
    35  	}
    36  }
    37  
    38  func TestParsePortRange(t *testing.T) {
    39  	tests := []struct {
    40  		input    string
    41  		expected PortRange
    42  	}{
    43  		{"23-23", PortRange{LowerBound: 23, UpperBound: 23}},
    44  		{"25-27", PortRange{LowerBound: 25, UpperBound: 27}},
    45  		{"0-65535", PortRange{LowerBound: 0, UpperBound: 65535}},
    46  		{"33", PortRange{LowerBound: 33, UpperBound: 33}},
    47  	}
    48  	for _, tt := range tests {
    49  		t.Run(tt.input, func(t *testing.T) {
    50  			check, _ := ParsePortRange(tt.input)
    51  			reflect.DeepEqual(tt.expected, check)
    52  		})
    53  	}
    54  }
    55  
    56  func TestParsePortRange_Errors(t *testing.T) {
    57  	tests := []struct {
    58  		input string
    59  		check string
    60  	}{
    61  		{"", "not a valid lower-bound"},
    62  		{"notanumber", "not a valid lower-bound"},
    63  		{"not-number", "not a valid lower-bound"},
    64  		{"-23-25", "ranges expected as"},
    65  		{"-23", "not a valid lower-bound"},
    66  		{"25-23", "upper-bound must be greater than or equal to"},
    67  		{"65536-65539", "not a valid lower-bound"},
    68  		{"23-notanumber", "not a valid upper-bound"},
    69  	}
    70  	for _, tt := range tests {
    71  		t.Run(tt.input, func(t *testing.T) {
    72  			_, err := ParsePortRange(tt.input)
    73  			assertError(t, err, tt.check)
    74  		})
    75  	}
    76  }
    77  
    78  // assertError confirms that the provided is an error having the provided message.
    79  func assertError(t *testing.T, err error, containing string) {
    80  	if err == nil {
    81  		t.Fatal("expected error; got nothing")
    82  	}
    83  	if !strings.Contains(err.Error(), containing) {
    84  		t.Fatalf("expected error to contain '%s' but received '%s'", containing, err.Error())
    85  	}
    86  }
    87  

View as plain text