github.com/google/cloudprober@v0.11.3/rds/client/srvlist_test.go (about)

     1  // Copyright 2020 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client
    16  
    17  import (
    18  	"fmt"
    19  	"reflect"
    20  	"sort"
    21  	"testing"
    22  )
    23  
    24  var testDefaultPort = "9999"
    25  
    26  func TestParseAddr(t *testing.T) {
    27  	var tests = []struct {
    28  		addr, host, port string
    29  		err              error
    30  	}{
    31  		{
    32  			addr: "rds-service:443",
    33  			host: "rds-service",
    34  			port: "443",
    35  			err:  nil,
    36  		},
    37  		{
    38  			addr: "192.168.1.2:4430",
    39  			host: "192.168.1.2",
    40  			port: "4430",
    41  			err:  nil,
    42  		},
    43  		{
    44  			addr: "192.168.1.4",
    45  			host: "192.168.1.4",
    46  			port: testDefaultPort,
    47  			err:  nil,
    48  		},
    49  		{
    50  			addr: "1620:15c:2c4:201::ff",
    51  			host: "[1620:15c:2c4:201::ff]",
    52  			port: testDefaultPort,
    53  			err:  nil,
    54  		},
    55  		{
    56  			addr: "rds-service:",
    57  			host: "rds-service",
    58  			port: testDefaultPort,
    59  			err:  nil,
    60  		},
    61  		{
    62  			addr: ":9314",
    63  			host: "localhost",
    64  			port: "9314",
    65  			err:  nil,
    66  		},
    67  	}
    68  
    69  	for _, test := range tests {
    70  		t.Run(fmt.Sprintf("parsing %s", test.addr), func(t *testing.T) {
    71  			host, port, err := parseAddr(test.addr, testDefaultPort)
    72  
    73  			if host != test.host || port != test.port || err != test.err {
    74  				t.Errorf("parseAddr(%s)=%s, %s, %v, want=%s, %s, %v", test.addr, host, port, err, test.host, test.port, test.err)
    75  			}
    76  		})
    77  	}
    78  }
    79  
    80  func TestNewResolver(t *testing.T) {
    81  	var tests = []struct {
    82  		target string
    83  		hosts  []string
    84  		ports  []string
    85  	}{
    86  		{
    87  			target: "rds-service-a:443,rds-service-b:9314",
    88  			hosts:  []string{"rds-service-a", "rds-service-b"},
    89  			ports:  []string{"443", "9314"},
    90  		},
    91  		{
    92  			target: "35.14.14.1:443,rds-service-b:9314",
    93  			hosts:  []string{"35.14.14.1", "rds-service-b"},
    94  			ports:  []string{"443", "9314"},
    95  		},
    96  		{
    97  			target: "35.14.14.1,35.14.14.2",
    98  			hosts:  []string{"35.14.14.1", "35.14.14.2"},
    99  			ports:  []string{testDefaultPort, testDefaultPort},
   100  		},
   101  	}
   102  
   103  	for _, test := range tests {
   104  		t.Run(fmt.Sprintf("parsing %s", test.target), func(t *testing.T) {
   105  			res, err := newSrvListResolver(test.target, testDefaultPort)
   106  			if err != nil {
   107  				t.Errorf("Unexpected error: %v", err)
   108  			}
   109  
   110  			sort.Strings(res.hostList)
   111  			sort.Strings(test.hosts)
   112  			if !reflect.DeepEqual(res.hostList, test.hosts) {
   113  				t.Errorf("res.hostList, got=%v, want=%v", res.hostList, test.hosts)
   114  			}
   115  
   116  			sort.Strings(res.portList)
   117  			sort.Strings(test.ports)
   118  			if !reflect.DeepEqual(res.portList, test.ports) {
   119  				t.Errorf("res.portList, got=%v, want=%v", res.portList, test.ports)
   120  			}
   121  		})
   122  	}
   123  
   124  }