2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-09-12 16:07:47 +00:00
|
|
|
package structs
|
|
|
|
|
2022-03-13 03:55:53 +00:00
|
|
|
import (
|
|
|
|
"github.com/mitchellh/go-testing-interface"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
|
|
|
)
|
2018-09-12 16:07:47 +00:00
|
|
|
|
|
|
|
// TestConnectProxyConfig returns a ConnectProxyConfig representing a valid
|
|
|
|
// Connect proxy.
|
|
|
|
func TestConnectProxyConfig(t testing.T) ConnectProxyConfig {
|
|
|
|
return ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "web",
|
2023-03-22 18:56:18 +00:00
|
|
|
Upstreams: TestUpstreams(t, false),
|
2018-09-12 16:07:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestUpstreams returns a set of upstreams to be used in tests exercising most
|
|
|
|
// important configuration patterns.
|
2023-03-22 18:56:18 +00:00
|
|
|
func TestUpstreams(t testing.T, enterprise bool) Upstreams {
|
|
|
|
db := Upstream{
|
|
|
|
// We rely on this one having default type in a few tests...
|
|
|
|
DestinationName: "db",
|
|
|
|
LocalBindPort: 9191,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
// Float because this is how it is decoded by JSON decoder so this
|
|
|
|
// enables the value returned to be compared directly to a decoded JSON
|
|
|
|
// response without spurious type loss.
|
|
|
|
"connect_timeout_ms": float64(1000),
|
2021-03-26 20:00:44 +00:00
|
|
|
},
|
2018-09-12 16:07:47 +00:00
|
|
|
}
|
2023-03-22 18:56:18 +00:00
|
|
|
|
|
|
|
geoCache := Upstream{
|
|
|
|
DestinationType: UpstreamDestTypePreparedQuery,
|
|
|
|
DestinationName: "geo-cache",
|
|
|
|
LocalBindPort: 8181,
|
|
|
|
LocalBindAddress: "127.10.10.10",
|
|
|
|
}
|
|
|
|
|
|
|
|
if enterprise {
|
|
|
|
db.DestinationNamespace = "foo"
|
|
|
|
db.DestinationPartition = "bar"
|
|
|
|
|
|
|
|
geoCache.DestinationNamespace = "baz"
|
|
|
|
geoCache.DestinationPartition = "qux"
|
|
|
|
}
|
|
|
|
|
|
|
|
return Upstreams{db, geoCache, {
|
|
|
|
DestinationName: "upstream_socket",
|
|
|
|
LocalBindSocketPath: "/tmp/upstream.sock",
|
|
|
|
LocalBindSocketMode: "0700",
|
|
|
|
},
|
|
|
|
}
|
2018-09-12 16:07:47 +00:00
|
|
|
}
|
2018-09-27 13:33:12 +00:00
|
|
|
|
|
|
|
// TestAddDefaultsToUpstreams takes an array of upstreams (such as that from
|
|
|
|
// TestUpstreams) and adds default values that are populated during
|
2021-07-26 21:12:29 +00:00
|
|
|
// registration. Use this for generating the expected Upstreams value after
|
2018-09-27 13:33:12 +00:00
|
|
|
// registration.
|
2022-03-13 03:55:53 +00:00
|
|
|
func TestAddDefaultsToUpstreams(t testing.T, upstreams []Upstream, entMeta acl.EnterpriseMeta) Upstreams {
|
2018-09-27 13:33:12 +00:00
|
|
|
ups := make([]Upstream, len(upstreams))
|
|
|
|
for i := range upstreams {
|
|
|
|
ups[i] = upstreams[i]
|
|
|
|
// Fill in default fields we expect to have back explicitly in a response
|
|
|
|
if ups[i].DestinationType == "" {
|
|
|
|
ups[i].DestinationType = UpstreamDestTypeService
|
|
|
|
}
|
2021-07-26 21:12:29 +00:00
|
|
|
if ups[i].DestinationNamespace == "" {
|
|
|
|
ups[i].DestinationNamespace = entMeta.NamespaceOrEmpty()
|
|
|
|
}
|
2021-08-20 16:57:45 +00:00
|
|
|
if ups[i].DestinationPartition == "" {
|
|
|
|
ups[i].DestinationPartition = entMeta.PartitionOrEmpty()
|
|
|
|
}
|
2018-09-27 13:33:12 +00:00
|
|
|
}
|
|
|
|
return ups
|
|
|
|
}
|