consul/command/connect/envoy/flags_test.go

110 lines
2.6 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
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
require.Equal(t, addr.Value(), api.ServiceAddress{Port: defaultGatewayPort})
})
t.Run("default value", func(t *testing.T) {
addr := &ServiceAddressValue{}
require.Equal(t, addr.Value(), api.ServiceAddress{Port: defaultGatewayPort})
})
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,
})
})
}
func TestServiceAddressValue_String(t *testing.T) {
t.Run("nil receiver", func(t *testing.T) {
var addr *ServiceAddressValue
require.Equal(t, addr.String(), ":8443")
})
t.Run("default value", func(t *testing.T) {
addr := &ServiceAddressValue{}
require.Equal(t, addr.String(), ":8443")
})
t.Run("set value", func(t *testing.T) {
addr := &ServiceAddressValue{}
require.NoError(t, addr.Set("localhost:3333"))
require.Equal(t, addr.String(), "localhost:3333")
})
}
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",
Port: defaultGatewayPort,
},
},
{
name: "valid address",
input: "8.8.8.8:1234",
expectedValue: api.ServiceAddress{Address: "8.8.8.8", Port: 1234},
},
{
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",
},
{
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)
})
}
}