...

Source file src/github.com/emissary-ingress/emissary/v3/pkg/emissaryutil/svc_test.go

Documentation: github.com/emissary-ingress/emissary/v3/pkg/emissaryutil

     1  package emissaryutil
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestParseServiceName(t *testing.T) {
    11  	t.Parallel()
    12  	testcases := map[string]struct {
    13  		Scheme   string
    14  		Hostname string
    15  		Port     uint16
    16  		Err      string
    17  	}{
    18  		"[fe80::e022:9cff:fecc:c7c4%zone]":   {"", "", 0, `service "[fe80::e022:9cff:fecc:c7c4%zone]": parse "//[fe80::e022:9cff:fecc:c7c4%zone]": invalid URL escape "%zo"`},
    19  		"[fe80::e022:9cff:fecc:c7c4%25zone]": {"", "fe80::e022:9cff:fecc:c7c4%zone", 0, ``},
    20  		"https://[::1%25lo]:443":             {"https", "::1%lo", 443, ``},
    21  	}
    22  	for input, exp := range testcases {
    23  		input := input
    24  		exp := exp
    25  		t.Run(input, func(t *testing.T) {
    26  			t.Parallel()
    27  			scheme, hostname, port, err := ParseServiceName(input)
    28  			assert.Equal(t, exp.Scheme, scheme)
    29  			assert.Equal(t, exp.Hostname, hostname)
    30  			assert.Equal(t, exp.Port, port)
    31  			if exp.Err == "" {
    32  				assert.NoError(t, err)
    33  			} else {
    34  				assert.EqualError(t, err, exp.Err)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  type testGlobalResolverConfig struct {
    41  	ambassadorNamespace                        string
    42  	useAmbassadorNamespaceForServiceResolution bool
    43  }
    44  
    45  func (ir testGlobalResolverConfig) AmbassadorNamespace() string { return ir.ambassadorNamespace }
    46  func (ir testGlobalResolverConfig) UseAmbassadorNamespaceForServiceResolution() bool {
    47  	return ir.useAmbassadorNamespaceForServiceResolution
    48  }
    49  
    50  // TestQualifyService mimics python/tests/unit/test_qualify_service.py:test_qualify_service().
    51  // Please keep them in-sync.
    52  func TestQualifyService(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	type tcInput struct {
    56  		Svc          string
    57  		Namespace    string
    58  		ResolverKind string
    59  	}
    60  	type tc struct {
    61  		Input  tcInput
    62  		Output string
    63  		Err    string
    64  	}
    65  	normalizeServiceName := func(service, namespace, resolverKind string) tcInput {
    66  		return tcInput{
    67  			Svc:          service,
    68  			Namespace:    namespace,
    69  			ResolverKind: resolverKind,
    70  		}
    71  	}
    72  	qualifyServiceName := func(service, namespace string) tcInput {
    73  		return tcInput{
    74  			Svc:          service,
    75  			Namespace:    namespace,
    76  			ResolverKind: "KubernetesTestResolver",
    77  		}
    78  	}
    79  	ir := testGlobalResolverConfig{
    80  		ambassadorNamespace:                        "default",
    81  		useAmbassadorNamespaceForServiceResolution: false,
    82  	}
    83  	testcases := []tc{
    84  		{Input: qualifyServiceName("backoffice", ""), Output: "backoffice"},
    85  		{Input: qualifyServiceName("backoffice", "default"), Output: "backoffice"},
    86  		{Input: qualifyServiceName("backoffice", "otherns"), Output: "backoffice.otherns"},
    87  		{Input: qualifyServiceName("backoffice.otherns", ""), Output: "backoffice.otherns"},
    88  		{Input: qualifyServiceName("backoffice.otherns", "default"), Output: "backoffice.otherns"},
    89  		{Input: qualifyServiceName("backoffice.otherns", "otherns"), Output: "backoffice.otherns"},
    90  
    91  		{Input: normalizeServiceName("backoffice", "", "ConsulResolver"), Output: "backoffice"},
    92  		{Input: normalizeServiceName("backoffice", "default", "ConsulResolver"), Output: "backoffice"},
    93  		{Input: normalizeServiceName("backoffice", "otherns", "ConsulResolver"), Output: "backoffice"},
    94  		{Input: normalizeServiceName("backoffice.otherns", "", "ConsulResolver"), Output: "backoffice.otherns"},
    95  		{Input: normalizeServiceName("backoffice.otherns", "default", "ConsulResolver"), Output: "backoffice.otherns"},
    96  		{Input: normalizeServiceName("backoffice.otherns", "otherns", "ConsulResolver"), Output: "backoffice.otherns"},
    97  
    98  		{Input: qualifyServiceName("backoffice:80", ""), Output: "backoffice:80"},
    99  		{Input: qualifyServiceName("backoffice:80", "default"), Output: "backoffice:80"},
   100  		{Input: qualifyServiceName("backoffice:80", "otherns"), Output: "backoffice.otherns:80"},
   101  		{Input: qualifyServiceName("backoffice.otherns:80", ""), Output: "backoffice.otherns:80"},
   102  		{Input: qualifyServiceName("backoffice.otherns:80", "default"), Output: "backoffice.otherns:80"},
   103  		{Input: qualifyServiceName("backoffice.otherns:80", "otherns"), Output: "backoffice.otherns:80"},
   104  
   105  		{Input: qualifyServiceName("[fe80::e022:9cff:fecc:c7c4]", ""), Output: "[fe80::e022:9cff:fecc:c7c4]"},
   106  		{Input: qualifyServiceName("[fe80::e022:9cff:fecc:c7c4]", "default"), Output: "[fe80::e022:9cff:fecc:c7c4]"},
   107  		{Input: qualifyServiceName("[fe80::e022:9cff:fecc:c7c4]", "other"), Output: "[fe80::e022:9cff:fecc:c7c4]"},
   108  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4]", ""), Output: "https://[fe80::e022:9cff:fecc:c7c4]"},
   109  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4]", "default"), Output: "https://[fe80::e022:9cff:fecc:c7c4]"},
   110  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4]", "other"), Output: "https://[fe80::e022:9cff:fecc:c7c4]"},
   111  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4]:443", ""), Output: "https://[fe80::e022:9cff:fecc:c7c4]:443"},
   112  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4]:443", "default"), Output: "https://[fe80::e022:9cff:fecc:c7c4]:443"},
   113  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4]:443", "other"), Output: "https://[fe80::e022:9cff:fecc:c7c4]:443"},
   114  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4%25zone]:443", "other"), Output: "https://[fe80::e022:9cff:fecc:c7c4%25zone]:443"},
   115  
   116  		{Input: normalizeServiceName("backoffice:80", "", "ConsulResolver"), Output: "backoffice:80"},
   117  		{Input: normalizeServiceName("backoffice:80", "default", "ConsulResolver"), Output: "backoffice:80"},
   118  		{Input: normalizeServiceName("backoffice:80", "otherns", "ConsulResolver"), Output: "backoffice:80"},
   119  		{Input: normalizeServiceName("backoffice.otherns:80", "", "ConsulResolver"), Output: "backoffice.otherns:80"},
   120  		{Input: normalizeServiceName("backoffice.otherns:80", "default", "ConsulResolver"), Output: "backoffice.otherns:80"},
   121  		{Input: normalizeServiceName("backoffice.otherns:80", "otherns", "ConsulResolver"), Output: "backoffice.otherns:80"},
   122  
   123  		{Input: qualifyServiceName("http://backoffice", ""), Output: "http://backoffice"},
   124  		{Input: qualifyServiceName("http://backoffice", "default"), Output: "http://backoffice"},
   125  		{Input: qualifyServiceName("http://backoffice", "otherns"), Output: "http://backoffice.otherns"},
   126  		{Input: qualifyServiceName("http://backoffice.otherns", ""), Output: "http://backoffice.otherns"},
   127  		{Input: qualifyServiceName("http://backoffice.otherns", "default"), Output: "http://backoffice.otherns"},
   128  		{Input: qualifyServiceName("http://backoffice.otherns", "otherns"), Output: "http://backoffice.otherns"},
   129  
   130  		{Input: normalizeServiceName("http://backoffice", "", "ConsulResolver"), Output: "http://backoffice"},
   131  		{Input: normalizeServiceName("http://backoffice", "default", "ConsulResolver"), Output: "http://backoffice"},
   132  		{Input: normalizeServiceName("http://backoffice", "otherns", "ConsulResolver"), Output: "http://backoffice"},
   133  		{Input: normalizeServiceName("http://backoffice.otherns", "", "ConsulResolver"), Output: "http://backoffice.otherns"},
   134  		{Input: normalizeServiceName("http://backoffice.otherns", "default", "ConsulResolver"), Output: "http://backoffice.otherns"},
   135  		{Input: normalizeServiceName("http://backoffice.otherns", "otherns", "ConsulResolver"), Output: "http://backoffice.otherns"},
   136  
   137  		{Input: qualifyServiceName("http://backoffice:80", ""), Output: "http://backoffice:80"},
   138  		{Input: qualifyServiceName("http://backoffice:80", "default"), Output: "http://backoffice:80"},
   139  		{Input: qualifyServiceName("http://backoffice:80", "otherns"), Output: "http://backoffice.otherns:80"},
   140  		{Input: qualifyServiceName("http://backoffice.otherns:80", ""), Output: "http://backoffice.otherns:80"},
   141  		{Input: qualifyServiceName("http://backoffice.otherns:80", "default"), Output: "http://backoffice.otherns:80"},
   142  		{Input: qualifyServiceName("http://backoffice.otherns:80", "otherns"), Output: "http://backoffice.otherns:80"},
   143  
   144  		{Input: normalizeServiceName("http://backoffice:80", "", "ConsulResolver"), Output: "http://backoffice:80"},
   145  		{Input: normalizeServiceName("http://backoffice:80", "default", "ConsulResolver"), Output: "http://backoffice:80"},
   146  		{Input: normalizeServiceName("http://backoffice:80", "otherns", "ConsulResolver"), Output: "http://backoffice:80"},
   147  		{Input: normalizeServiceName("http://backoffice.otherns:80", "", "ConsulResolver"), Output: "http://backoffice.otherns:80"},
   148  		{Input: normalizeServiceName("http://backoffice.otherns:80", "default", "ConsulResolver"), Output: "http://backoffice.otherns:80"},
   149  		{Input: normalizeServiceName("http://backoffice.otherns:80", "otherns", "ConsulResolver"), Output: "http://backoffice.otherns:80"},
   150  
   151  		{Input: qualifyServiceName("https://backoffice", ""), Output: "https://backoffice"},
   152  		{Input: qualifyServiceName("https://backoffice", "default"), Output: "https://backoffice"},
   153  		{Input: qualifyServiceName("https://backoffice", "otherns"), Output: "https://backoffice.otherns"},
   154  		{Input: qualifyServiceName("https://backoffice.otherns", ""), Output: "https://backoffice.otherns"},
   155  		{Input: qualifyServiceName("https://backoffice.otherns", "default"), Output: "https://backoffice.otherns"},
   156  		{Input: qualifyServiceName("https://backoffice.otherns", "otherns"), Output: "https://backoffice.otherns"},
   157  
   158  		{Input: normalizeServiceName("https://backoffice", "", "ConsulResolver"), Output: "https://backoffice"},
   159  		{Input: normalizeServiceName("https://backoffice", "default", "ConsulResolver"), Output: "https://backoffice"},
   160  		{Input: normalizeServiceName("https://backoffice", "otherns", "ConsulResolver"), Output: "https://backoffice"},
   161  		{Input: normalizeServiceName("https://backoffice.otherns", "", "ConsulResolver"), Output: "https://backoffice.otherns"},
   162  		{Input: normalizeServiceName("https://backoffice.otherns", "default", "ConsulResolver"), Output: "https://backoffice.otherns"},
   163  		{Input: normalizeServiceName("https://backoffice.otherns", "otherns", "ConsulResolver"), Output: "https://backoffice.otherns"},
   164  
   165  		{Input: qualifyServiceName("https://backoffice:443", ""), Output: "https://backoffice:443"},
   166  		{Input: qualifyServiceName("https://backoffice:443", "default"), Output: "https://backoffice:443"},
   167  		{Input: qualifyServiceName("https://backoffice:443", "otherns"), Output: "https://backoffice.otherns:443"},
   168  		{Input: qualifyServiceName("https://backoffice.otherns:443", ""), Output: "https://backoffice.otherns:443"},
   169  		{Input: qualifyServiceName("https://backoffice.otherns:443", "default"), Output: "https://backoffice.otherns:443"},
   170  		{Input: qualifyServiceName("https://backoffice.otherns:443", "otherns"), Output: "https://backoffice.otherns:443"},
   171  
   172  		{Input: normalizeServiceName("https://backoffice:443", "", "ConsulResolver"), Output: "https://backoffice:443"},
   173  		{Input: normalizeServiceName("https://backoffice:443", "default", "ConsulResolver"), Output: "https://backoffice:443"},
   174  		{Input: normalizeServiceName("https://backoffice:443", "otherns", "ConsulResolver"), Output: "https://backoffice:443"},
   175  		{Input: normalizeServiceName("https://backoffice.otherns:443", "", "ConsulResolver"), Output: "https://backoffice.otherns:443"},
   176  		{Input: normalizeServiceName("https://backoffice.otherns:443", "default", "ConsulResolver"), Output: "https://backoffice.otherns:443"},
   177  		{Input: normalizeServiceName("https://backoffice.otherns:443", "otherns", "ConsulResolver"), Output: "https://backoffice.otherns:443"},
   178  
   179  		{Input: qualifyServiceName("localhost", ""), Output: "localhost"},
   180  		{Input: qualifyServiceName("localhost", "default"), Output: "localhost"},
   181  		{Input: qualifyServiceName("localhost", "otherns"), Output: "localhost"},
   182  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   183  		{Input: qualifyServiceName("localhost.otherns", ""), Output: "localhost.otherns"},
   184  		{Input: qualifyServiceName("localhost.otherns", "default"), Output: "localhost.otherns"},
   185  		{Input: qualifyServiceName("localhost.otherns", "otherns"), Output: "localhost.otherns"},
   186  
   187  		{Input: normalizeServiceName("localhost", "", "ConsulResolver"), Output: "localhost"},
   188  		{Input: normalizeServiceName("localhost", "default", "ConsulResolver"), Output: "localhost"},
   189  		{Input: normalizeServiceName("localhost", "otherns", "ConsulResolver"), Output: "localhost"},
   190  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   191  		{Input: normalizeServiceName("localhost.otherns", "", "ConsulResolver"), Output: "localhost.otherns"},
   192  		{Input: normalizeServiceName("localhost.otherns", "default", "ConsulResolver"), Output: "localhost.otherns"},
   193  		{Input: normalizeServiceName("localhost.otherns", "otherns", "ConsulResolver"), Output: "localhost.otherns"},
   194  
   195  		{Input: qualifyServiceName("localhost:80", ""), Output: "localhost:80"},
   196  		{Input: qualifyServiceName("localhost:80", "default"), Output: "localhost:80"},
   197  		{Input: qualifyServiceName("localhost:80", "otherns"), Output: "localhost:80"},
   198  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   199  		{Input: qualifyServiceName("localhost.otherns:80", ""), Output: "localhost.otherns:80"},
   200  		{Input: qualifyServiceName("localhost.otherns:80", "default"), Output: "localhost.otherns:80"},
   201  		{Input: qualifyServiceName("localhost.otherns:80", "otherns"), Output: "localhost.otherns:80"},
   202  
   203  		{Input: normalizeServiceName("localhost:80", "", "ConsulResolver"), Output: "localhost:80"},
   204  		{Input: normalizeServiceName("localhost:80", "default", "ConsulResolver"), Output: "localhost:80"},
   205  		{Input: normalizeServiceName("localhost:80", "otherns", "ConsulResolver"), Output: "localhost:80"},
   206  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   207  		{Input: normalizeServiceName("localhost.otherns:80", "", "ConsulResolver"), Output: "localhost.otherns:80"},
   208  		{Input: normalizeServiceName("localhost.otherns:80", "default", "ConsulResolver"), Output: "localhost.otherns:80"},
   209  		{Input: normalizeServiceName("localhost.otherns:80", "otherns", "ConsulResolver"), Output: "localhost.otherns:80"},
   210  
   211  		{Input: qualifyServiceName("http://localhost", ""), Output: "http://localhost"},
   212  		{Input: qualifyServiceName("http://localhost", "default"), Output: "http://localhost"},
   213  		{Input: qualifyServiceName("http://localhost", "otherns"), Output: "http://localhost"},
   214  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   215  		{Input: qualifyServiceName("http://localhost.otherns", ""), Output: "http://localhost.otherns"},
   216  		{Input: qualifyServiceName("http://localhost.otherns", "default"), Output: "http://localhost.otherns"},
   217  		{Input: qualifyServiceName("http://localhost.otherns", "otherns"), Output: "http://localhost.otherns"},
   218  
   219  		{Input: normalizeServiceName("http://localhost", "", "ConsulResolver"), Output: "http://localhost"},
   220  		{Input: normalizeServiceName("http://localhost", "default", "ConsulResolver"), Output: "http://localhost"},
   221  		{Input: normalizeServiceName("http://localhost", "otherns", "ConsulResolver"), Output: "http://localhost"},
   222  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   223  		{Input: normalizeServiceName("http://localhost.otherns", "", "ConsulResolver"), Output: "http://localhost.otherns"},
   224  		{Input: normalizeServiceName("http://localhost.otherns", "default", "ConsulResolver"), Output: "http://localhost.otherns"},
   225  		{Input: normalizeServiceName("http://localhost.otherns", "otherns", "ConsulResolver"), Output: "http://localhost.otherns"},
   226  
   227  		{Input: qualifyServiceName("http://localhost:80", ""), Output: "http://localhost:80"},
   228  		{Input: qualifyServiceName("http://localhost:80", "default"), Output: "http://localhost:80"},
   229  		{Input: qualifyServiceName("http://localhost:80", "otherns"), Output: "http://localhost:80"},
   230  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   231  		{Input: qualifyServiceName("http://localhost.otherns:80", ""), Output: "http://localhost.otherns:80"},
   232  		{Input: qualifyServiceName("http://localhost.otherns:80", "default"), Output: "http://localhost.otherns:80"},
   233  		{Input: qualifyServiceName("http://localhost.otherns:80", "otherns"), Output: "http://localhost.otherns:80"},
   234  
   235  		{Input: normalizeServiceName("http://localhost:80", "", "ConsulResolver"), Output: "http://localhost:80"},
   236  		{Input: normalizeServiceName("http://localhost:80", "default", "ConsulResolver"), Output: "http://localhost:80"},
   237  		{Input: normalizeServiceName("http://localhost:80", "otherns", "ConsulResolver"), Output: "http://localhost:80"},
   238  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   239  		{Input: normalizeServiceName("http://localhost.otherns:80", "", "ConsulResolver"), Output: "http://localhost.otherns:80"},
   240  		{Input: normalizeServiceName("http://localhost.otherns:80", "default", "ConsulResolver"), Output: "http://localhost.otherns:80"},
   241  		{Input: normalizeServiceName("http://localhost.otherns:80", "otherns", "ConsulResolver"), Output: "http://localhost.otherns:80"},
   242  
   243  		{Input: qualifyServiceName("https://localhost", ""), Output: "https://localhost"},
   244  		{Input: qualifyServiceName("https://localhost", "default"), Output: "https://localhost"},
   245  		{Input: qualifyServiceName("https://localhost", "otherns"), Output: "https://localhost"},
   246  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   247  		{Input: qualifyServiceName("https://localhost.otherns", ""), Output: "https://localhost.otherns"},
   248  		{Input: qualifyServiceName("https://localhost.otherns", "default"), Output: "https://localhost.otherns"},
   249  		{Input: qualifyServiceName("https://localhost.otherns", "otherns"), Output: "https://localhost.otherns"},
   250  
   251  		{Input: normalizeServiceName("https://localhost", "", "ConsulResolver"), Output: "https://localhost"},
   252  		{Input: normalizeServiceName("https://localhost", "default", "ConsulResolver"), Output: "https://localhost"},
   253  		{Input: normalizeServiceName("https://localhost", "otherns", "ConsulResolver"), Output: "https://localhost"},
   254  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   255  		{Input: normalizeServiceName("https://localhost.otherns", "", "ConsulResolver"), Output: "https://localhost.otherns"},
   256  		{Input: normalizeServiceName("https://localhost.otherns", "default", "ConsulResolver"), Output: "https://localhost.otherns"},
   257  		{Input: normalizeServiceName("https://localhost.otherns", "otherns", "ConsulResolver"), Output: "https://localhost.otherns"},
   258  
   259  		{Input: qualifyServiceName("https://localhost:443", ""), Output: "https://localhost:443"},
   260  		{Input: qualifyServiceName("https://localhost:443", "default"), Output: "https://localhost:443"},
   261  		{Input: qualifyServiceName("https://localhost:443", "otherns"), Output: "https://localhost:443"},
   262  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   263  		{Input: qualifyServiceName("https://localhost.otherns:443", ""), Output: "https://localhost.otherns:443"},
   264  		{Input: qualifyServiceName("https://localhost.otherns:443", "default"), Output: "https://localhost.otherns:443"},
   265  		{Input: qualifyServiceName("https://localhost.otherns:443", "otherns"), Output: "https://localhost.otherns:443"},
   266  
   267  		{Input: normalizeServiceName("https://localhost:443", "", "ConsulResolver"), Output: "https://localhost:443"},
   268  		{Input: normalizeServiceName("https://localhost:443", "default", "ConsulResolver"), Output: "https://localhost:443"},
   269  		{Input: normalizeServiceName("https://localhost:443", "otherns", "ConsulResolver"), Output: "https://localhost:443"},
   270  		// It's not meaningful to actually say "localhost.otherns", but it should passed through unchanged.
   271  		{Input: normalizeServiceName("https://localhost.otherns:443", "", "ConsulResolver"), Output: "https://localhost.otherns:443"},
   272  		{Input: normalizeServiceName("https://localhost.otherns:443", "default", "ConsulResolver"), Output: "https://localhost.otherns:443"},
   273  		{Input: normalizeServiceName("https://localhost.otherns:443", "otherns", "ConsulResolver"), Output: "https://localhost.otherns:443"},
   274  
   275  		{Input: qualifyServiceName("ambassador://foo.ns", "otherns"), Output: "ambassador://foo.ns"}, // let's not introduce silly semantics
   276  		{Input: qualifyServiceName("//foo.ns:1234", "otherns"), Output: "foo.ns:1234"},               // we tell people "URL-ish", actually support URL-ish
   277  		{Input: qualifyServiceName("foo.ns:1234", "otherns"), Output: "foo.ns:1234"},
   278  
   279  		{Input: normalizeServiceName("ambassador://foo.ns", "otherns", "ConsulResolver"), Output: "ambassador://foo.ns"}, // let's not introduce silly semantics
   280  		{Input: normalizeServiceName("//foo.ns:1234", "otherns", "ConsulResolver"), Output: "foo.ns:1234"},               // we tell people "URL-ish", actually support URL-ish
   281  		{Input: normalizeServiceName("foo.ns:1234", "otherns", "ConsulResolver"), Output: "foo.ns:1234"},
   282  
   283  		{Input: qualifyServiceName("https://bad-service:443:443", "otherns"), Err: `service "https://bad-service:443:443": address bad-service:443:443: too many colons in address`},
   284  		{Input: qualifyServiceName("bad-service:443:443", "otherns"), Err: `service "bad-service:443:443": address bad-service:443:443: too many colons in address`},
   285  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4:443", "otherns"), Err: `service "https://[fe80::e022:9cff:fecc:c7c4:443": parse "https://[fe80::e022:9cff:fecc:c7c4:443": missing ']' in host`},
   286  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4", "otherns"), Err: `service "https://[fe80::e022:9cff:fecc:c7c4": parse "https://[fe80::e022:9cff:fecc:c7c4": missing ']' in host`},
   287  		{Input: qualifyServiceName("https://fe80::e022:9cff:fecc:c7c4", "otherns"), Err: `service "https://fe80::e022:9cff:fecc:c7c4": parse "https://fe80::e022:9cff:fecc:c7c4": invalid port ":c7c4" after host`},
   288  		{Input: qualifyServiceName("https://bad-service:-1", "otherns"), Err: `service "https://bad-service:-1": parse "https://bad-service:-1": invalid port ":-1" after host`},
   289  		{Input: qualifyServiceName("https://bad-service:70000", "otherns"), Err: `service "https://bad-service:70000": port 70000: strconv.ParseUint: parsing "70000": value out of range`},
   290  
   291  		{Input: normalizeServiceName("https://bad-service:443:443", "otherns", "ConsulResolver"), Err: `service "https://bad-service:443:443": address bad-service:443:443: too many colons in address`},
   292  		{Input: normalizeServiceName("bad-service:443:443", "otherns", "ConsulResolver"), Err: `service "bad-service:443:443": address bad-service:443:443: too many colons in address`},
   293  		{Input: normalizeServiceName("https://[fe80::e022:9cff:fecc:c7c4:443", "otherns", "ConsulResolver"), Err: `service "https://[fe80::e022:9cff:fecc:c7c4:443": parse "https://[fe80::e022:9cff:fecc:c7c4:443": missing ']' in host`},
   294  		{Input: normalizeServiceName("https://[fe80::e022:9cff:fecc:c7c4", "otherns", "ConsulResolver"), Err: `service "https://[fe80::e022:9cff:fecc:c7c4": parse "https://[fe80::e022:9cff:fecc:c7c4": missing ']' in host`},
   295  		{Input: normalizeServiceName("https://fe80::e022:9cff:fecc:c7c4", "otherns", "ConsulResolver"), Err: `service "https://fe80::e022:9cff:fecc:c7c4": parse "https://fe80::e022:9cff:fecc:c7c4": invalid port ":c7c4" after host`},
   296  		{Input: normalizeServiceName("https://bad-service:-1", "otherns", "ConsulResolver"), Err: `service "https://bad-service:-1": parse "https://bad-service:-1": invalid port ":-1" after host`},
   297  		{Input: normalizeServiceName("https://bad-service:70000", "otherns", "ConsulResolver"), Err: `service "https://bad-service:70000": port 70000: strconv.ParseUint: parsing "70000": value out of range`},
   298  		{Input: qualifyServiceName("https://[fe80::e022:9cff:fecc:c7c4%zone]:443", "other"), Err: `service "https://[fe80::e022:9cff:fecc:c7c4%zone]:443": parse "https://[fe80::e022:9cff:fecc:c7c4%zone]:443": invalid URL escape "%zo"`},
   299  	}
   300  	for i, tc := range testcases {
   301  		tc := tc // capture loop variable
   302  		t.Run(fmt.Sprintf("%v: %v", i, tc), func(t *testing.T) {
   303  			t.Parallel()
   304  			actVal, actErr := NormalizeServiceName(ir, tc.Input.Svc, tc.Input.Namespace, tc.Input.ResolverKind)
   305  			if tc.Err == "" {
   306  				assert.Equal(t, tc.Output, actVal)
   307  				assert.NoError(t, actErr)
   308  			} else {
   309  				assert.Equal(t, "", actVal)
   310  				assert.EqualError(t, actErr, tc.Err)
   311  			}
   312  		})
   313  	}
   314  }
   315  
   316  func TestIsLocalhost(t *testing.T) {
   317  	t.Parallel()
   318  	testcases := map[string]bool{
   319  		"localhost":  true,
   320  		"localhost4": false, // don't rely on non-standard things sometimes in /etc/hosts
   321  		"localhost6": false, // don't rely on non-standard things sometimes in /etc/hosts
   322  
   323  		"127.0.0.1":  true,
   324  		"127.0.0.01": false, // RFC 3986 URL parsing says to not treat it as an IPv4 if there are leading zeros (but is what we consume a URL?)
   325  		"2130706433": false, // RFC 3986 URL parsing says to not treat it as an IPv4 unless it's dotted-quad (but is what we consume a URL?)
   326  		"127.1":      false, // RFC 3986 URL parsing says to not treat it as an IPv4 unless it's dotted-quad (but is what we consume a URL?)
   327  
   328  		"127.2.3.4": true,
   329  
   330  		"::1":        true,
   331  		"::1%lo":     true,
   332  		"::0:0:1":    true,
   333  		"::00:00:01": true,
   334  	}
   335  	for hostname, exp := range testcases {
   336  		hostname := hostname
   337  		exp := exp
   338  		t.Run(hostname, func(t *testing.T) {
   339  			t.Parallel()
   340  			assert.Equal(t, exp, IsLocalhost(hostname))
   341  		})
   342  	}
   343  }
   344  

View as plain text