2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2020-03-26 14:19:21 +00:00
|
|
|
package envoy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceAddressValue_Value(t *testing.T) {
|
|
|
|
t.Run("nil receiver", func(t *testing.T) {
|
|
|
|
var addr *ServiceAddressValue
|
2020-03-26 16:20:56 +00:00
|
|
|
require.Equal(t, addr.Value(), api.ServiceAddress{Port: defaultGatewayPort})
|
2020-03-26 14:19:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("default value", func(t *testing.T) {
|
|
|
|
addr := &ServiceAddressValue{}
|
2020-03-26 16:20:56 +00:00
|
|
|
require.Equal(t, addr.Value(), api.ServiceAddress{Port: defaultGatewayPort})
|
2020-03-26 14:19:21 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("set value", func(t *testing.T) {
|
|
|
|
addr := &ServiceAddressValue{}
|
|
|
|
require.NoError(t, addr.Set("localhost:3333"))
|
|
|
|
require.Equal(t, addr.Value(), api.ServiceAddress{
|
|
|
|
Address: "localhost",
|
|
|
|
Port: 3333,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-14 14:48:02 +00:00
|
|
|
func TestServiceAddressValue_String(t *testing.T) {
|
|
|
|
t.Run("nil receiver", func(t *testing.T) {
|
|
|
|
var addr *ServiceAddressValue
|
2020-11-07 01:47:29 +00:00
|
|
|
require.Equal(t, addr.String(), ":8443")
|
2020-04-14 14:48:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("default value", func(t *testing.T) {
|
|
|
|
addr := &ServiceAddressValue{}
|
2020-11-07 01:47:29 +00:00
|
|
|
require.Equal(t, addr.String(), ":8443")
|
2020-04-14 14:48:02 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("set value", func(t *testing.T) {
|
|
|
|
addr := &ServiceAddressValue{}
|
|
|
|
require.NoError(t, addr.Set("localhost:3333"))
|
|
|
|
require.Equal(t, addr.String(), "localhost:3333")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:19:21 +00:00
|
|
|
func TestServiceAddressValue_Set(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
expectedErr string
|
|
|
|
expectedValue api.ServiceAddress
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default port",
|
|
|
|
input: "8.8.8.8:",
|
|
|
|
expectedValue: api.ServiceAddress{
|
|
|
|
Address: "8.8.8.8",
|
2020-03-26 16:20:56 +00:00
|
|
|
Port: defaultGatewayPort,
|
2020-03-26 14:19:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid address",
|
|
|
|
input: "8.8.8.8:1234",
|
|
|
|
expectedValue: api.ServiceAddress{Address: "8.8.8.8", Port: 1234},
|
|
|
|
},
|
|
|
|
{
|
2020-05-21 14:08:12 +00:00
|
|
|
name: "address with no port",
|
|
|
|
input: "8.8.8.8",
|
|
|
|
expectedValue: api.ServiceAddress{
|
|
|
|
Address: "8.8.8.8",
|
|
|
|
Port: defaultGatewayPort,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid addres",
|
|
|
|
input: "not-an-ip-address",
|
|
|
|
expectedErr: "not an IP address",
|
2020-03-26 14:19:21 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid port",
|
|
|
|
input: "localhost:notaport",
|
|
|
|
expectedErr: `Error parsing port "notaport"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid address format",
|
|
|
|
input: "too:many:colons",
|
|
|
|
expectedErr: "address too:many:colons: too many colons",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
addr := &ServiceAddressValue{}
|
|
|
|
err := addr.Set(tc.input)
|
|
|
|
if tc.expectedErr != "" {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tc.expectedErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, addr.Value(), tc.expectedValue)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|