github.com/m-lab/locate@v0.17.6/cmd/heartbeat/health/port-probe_test.go (about)

     1  package health
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"reflect"
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestPortProbe_scanPorts(t *testing.T) {
    12  	tests := []struct {
    13  		name            string
    14  		servers         int
    15  		unreachablePort string
    16  		want            bool
    17  	}{
    18  		{
    19  			name:    "no-ports",
    20  			servers: 0,
    21  			want:    true,
    22  		},
    23  		{
    24  			name:    "one-open-port",
    25  			servers: 1,
    26  			want:    true,
    27  		},
    28  		{
    29  			name:    "multiple-open-ports",
    30  			servers: 3,
    31  			want:    true,
    32  		},
    33  		{
    34  			name:            "one-unreachable-port",
    35  			servers:         0,
    36  			unreachablePort: "65536",
    37  			want:            false,
    38  		},
    39  		{
    40  			name:            "open-and-unreachable-ports",
    41  			servers:         2,
    42  			unreachablePort: "65536",
    43  			want:            false,
    44  		},
    45  	}
    46  
    47  	for _, tt := range tests {
    48  		t.Run(tt.name, func(t *testing.T) {
    49  			svcs := make(map[string][]string)
    50  
    51  			for i := 0; i < tt.servers; i++ {
    52  				mux := http.NewServeMux()
    53  				srv := httptest.NewServer(mux)
    54  				defer srv.Close()
    55  
    56  				svcs[strconv.Itoa(i)] = []string{srv.URL}
    57  			}
    58  
    59  			if tt.unreachablePort != "" {
    60  				svcs["unreachable"] = []string{tt.unreachablePort}
    61  			}
    62  
    63  			pp := NewPortProbe(svcs)
    64  
    65  			got := pp.checkPorts()
    66  			if got != tt.want {
    67  				t.Errorf("PortProbe.scanPorts() = %v, want %v", got, tt.want)
    68  			}
    69  		})
    70  	}
    71  }
    72  
    73  func Test_getPorts(t *testing.T) {
    74  	tests := []struct {
    75  		name     string
    76  		services map[string][]string
    77  		want     map[string]bool
    78  	}{
    79  		{
    80  			name: "invalid-url",
    81  			services: map[string][]string{
    82  				"ndt/ndt7": {
    83  					"url%",
    84  				},
    85  			},
    86  			want: map[string]bool{},
    87  		},
    88  		{
    89  			name: "with-port",
    90  			services: map[string][]string{
    91  				"ndt/ndt5": {
    92  					"ws://:3001/ndt_protocol",
    93  					"wss://:3010/ndt_protocol",
    94  				},
    95  			},
    96  			want: map[string]bool{
    97  				"3001": true,
    98  				"3010": true,
    99  			},
   100  		},
   101  		{
   102  			name: "without-port-ws",
   103  			services: map[string][]string{
   104  				"ndt/ndt7": {
   105  					"ws:///ndt/v7/download",
   106  					"ws:///ndt/v7/upload",
   107  				},
   108  			},
   109  			want: map[string]bool{
   110  				"80": true,
   111  			},
   112  		},
   113  		{
   114  			name: "without-port-wss",
   115  			services: map[string][]string{
   116  				"ndt/ndt7": {
   117  					"wss:///ndt/v7/download",
   118  					"wss:///ndt/v7/upload",
   119  				},
   120  			},
   121  			want: map[string]bool{
   122  				"443": true,
   123  			},
   124  		},
   125  		{
   126  			name: "all-types",
   127  			services: map[string][]string{
   128  				"ndt/ndt7": {
   129  					"ws:///ndt/v7/download",
   130  					"ws:///ndt/v7/upload",
   131  					"wss:///ndt/v7/download",
   132  					"wss:///ndt/v7/upload",
   133  				},
   134  				"ndt/ndt5": {
   135  					"ws://:3001/ndt_protocol",
   136  					"wss://:3010/ndt_protocol",
   137  				},
   138  			},
   139  			want: map[string]bool{
   140  				"80":   true,
   141  				"443":  true,
   142  				"3001": true,
   143  				"3010": true,
   144  			},
   145  		},
   146  	}
   147  
   148  	for _, tt := range tests {
   149  		t.Run(tt.name, func(t *testing.T) {
   150  			got := getPorts(tt.services)
   151  			if !reflect.DeepEqual(got, tt.want) {
   152  				t.Errorf("getPorts() = %v, want %v", got, tt.want)
   153  			}
   154  		})
   155  	}
   156  }