2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2019-04-23 06:39:02 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2022-12-14 15:24:22 +00:00
|
|
|
"context"
|
2019-09-24 15:04:48 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-01-18 19:43:53 +00:00
|
|
|
"sort"
|
2019-04-23 06:39:02 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-06-09 16:14:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-04-23 06:39:02 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-09-24 15:04:48 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2019-04-23 06:39:02 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceManager_RegisterService(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:19:14 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-04-23 06:39:02 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
2019-05-01 23:39:31 +00:00
|
|
|
// Register a global proxy and service config
|
2019-09-24 15:04:48 +00:00
|
|
|
testApplyConfigEntries(t, a,
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": 1,
|
2019-05-01 23:39:31 +00:00
|
|
|
},
|
2019-09-24 15:04:48 +00:00
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "redis",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
)
|
2019-04-23 06:39:02 +00:00
|
|
|
|
2019-05-01 23:39:31 +00:00
|
|
|
// Now register a service locally with no sidecar, it should be a no-op.
|
2019-04-23 06:39:02 +00:00
|
|
|
svc := &structs.NodeService{
|
2019-12-10 02:26:41 +00:00
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Port: 8000,
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-04-23 06:39:02 +00:00
|
|
|
}
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, a.addServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
|
2019-05-01 23:39:31 +00:00
|
|
|
|
|
|
|
// Verify both the service and sidecar.
|
2019-12-10 02:26:41 +00:00
|
|
|
redisService := a.State.Service(structs.NewServiceID("redis", nil))
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, redisService)
|
|
|
|
require.Equal(t, &structs.NodeService{
|
2020-01-17 14:54:17 +00:00
|
|
|
ID: "redis",
|
|
|
|
Service: "redis",
|
|
|
|
Port: 8000,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{},
|
2019-05-01 23:39:31 +00:00
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-05-01 23:39:31 +00:00
|
|
|
}, redisService)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceManager_RegisterSidecar(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:19:14 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-05-01 23:39:31 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a global proxy and service config
|
2019-09-24 15:04:48 +00:00
|
|
|
testApplyConfigEntries(t, a,
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": 1,
|
2019-05-01 23:39:31 +00:00
|
|
|
},
|
2019-09-24 15:04:48 +00:00
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "redis",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
)
|
2019-05-01 23:39:31 +00:00
|
|
|
|
|
|
|
// Now register a sidecar proxy. Note we don't use SidecarService here because
|
|
|
|
// that gets resolved earlier in config handling than the AddService call
|
|
|
|
// here.
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
ID: "web-sidecar-proxy",
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 21000,
|
2019-04-23 06:39:02 +00:00
|
|
|
Proxy: structs.ConnectProxyConfig{
|
2019-05-01 23:39:31 +00:00
|
|
|
DestinationServiceName: "web",
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8000,
|
|
|
|
Upstreams: structs.Upstreams{
|
|
|
|
{
|
2021-03-18 05:05:00 +00:00
|
|
|
DestinationName: "redis",
|
|
|
|
DestinationNamespace: "default",
|
2021-08-20 16:57:45 +00:00
|
|
|
DestinationPartition: "default",
|
2021-03-18 05:05:00 +00:00
|
|
|
LocalBindPort: 5000,
|
2019-05-01 23:39:31 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-05-01 23:39:31 +00:00
|
|
|
}
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, a.addServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
|
2019-05-01 23:39:31 +00:00
|
|
|
|
|
|
|
// Verify sidecar got global config loaded
|
2019-12-10 02:26:41 +00:00
|
|
|
sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, sidecarService)
|
|
|
|
require.Equal(t, &structs.NodeService{
|
2020-01-17 14:54:17 +00:00
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
ID: "web-sidecar-proxy",
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 21000,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{},
|
2019-05-01 23:39:31 +00:00
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8000,
|
2019-04-23 06:39:02 +00:00
|
|
|
Config: map[string]interface{}{
|
2019-05-01 23:39:31 +00:00
|
|
|
"foo": int64(1),
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
Upstreams: structs.Upstreams{
|
|
|
|
{
|
2021-03-18 05:05:00 +00:00
|
|
|
DestinationName: "redis",
|
|
|
|
DestinationNamespace: "default",
|
2021-08-20 16:57:45 +00:00
|
|
|
DestinationPartition: "default",
|
2021-03-18 05:05:00 +00:00
|
|
|
LocalBindPort: 5000,
|
2019-05-01 23:39:31 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "tcp",
|
|
|
|
},
|
|
|
|
},
|
2019-04-23 06:39:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-05-01 23:39:31 +00:00
|
|
|
}, sidecarService)
|
2019-04-23 06:39:02 +00:00
|
|
|
}
|
2019-04-24 13:11:08 +00:00
|
|
|
|
2019-08-09 20:07:01 +00:00
|
|
|
func TestServiceManager_RegisterMeshGateway(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:19:14 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-08-09 20:07:01 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a global proxy and service config
|
2019-09-24 15:04:48 +00:00
|
|
|
testApplyConfigEntries(t, a,
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": 1,
|
2019-08-09 20:07:01 +00:00
|
|
|
},
|
2019-09-24 15:04:48 +00:00
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "mesh-gateway",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
)
|
2019-08-09 20:07:01 +00:00
|
|
|
|
|
|
|
// Now register a mesh-gateway.
|
|
|
|
svc := &structs.NodeService{
|
2019-12-10 02:26:41 +00:00
|
|
|
Kind: structs.ServiceKindMeshGateway,
|
|
|
|
ID: "mesh-gateway",
|
|
|
|
Service: "mesh-gateway",
|
|
|
|
Port: 443,
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-08-09 20:07:01 +00:00
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, a.addServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
|
2019-08-09 20:07:01 +00:00
|
|
|
|
|
|
|
// Verify gateway got global config loaded
|
2019-12-10 02:26:41 +00:00
|
|
|
gateway := a.State.Service(structs.NewServiceID("mesh-gateway", nil))
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, gateway)
|
|
|
|
require.Equal(t, &structs.NodeService{
|
2020-01-17 14:54:17 +00:00
|
|
|
Kind: structs.ServiceKindMeshGateway,
|
|
|
|
ID: "mesh-gateway",
|
|
|
|
Service: "mesh-gateway",
|
|
|
|
Port: 443,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{},
|
2019-08-09 20:07:01 +00:00
|
|
|
Proxy: structs.ConnectProxyConfig{
|
2020-03-26 16:20:56 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": int64(1),
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-03-26 16:20:56 +00:00
|
|
|
}, gateway)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceManager_RegisterTerminatingGateway(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-01 14:19:14 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2020-03-26 16:20:56 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a global proxy and service config
|
|
|
|
testApplyConfigEntries(t, a,
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "terminating-gateway",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Now register a terminating-gateway.
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
Kind: structs.ServiceKindTerminatingGateway,
|
|
|
|
ID: "terminating-gateway",
|
|
|
|
Service: "terminating-gateway",
|
|
|
|
Port: 443,
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-03-26 16:20:56 +00:00
|
|
|
}
|
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, a.addServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
|
2020-03-26 16:20:56 +00:00
|
|
|
|
|
|
|
// Verify gateway got global config loaded
|
|
|
|
gateway := a.State.Service(structs.NewServiceID("terminating-gateway", nil))
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, gateway)
|
|
|
|
require.Equal(t, &structs.NodeService{
|
2020-03-26 16:20:56 +00:00
|
|
|
Kind: structs.ServiceKindTerminatingGateway,
|
|
|
|
ID: "terminating-gateway",
|
|
|
|
Service: "terminating-gateway",
|
|
|
|
Port: 443,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{},
|
|
|
|
Proxy: structs.ConnectProxyConfig{
|
2019-08-09 20:07:01 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": int64(1),
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-08-09 20:07:01 +00:00
|
|
|
}, gateway)
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:04:48 +00:00
|
|
|
func TestServiceManager_PersistService_API(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:04:48 +00:00
|
|
|
// This is the ServiceManager version of TestAgent_PersistService and
|
|
|
|
// TestAgent_PurgeService.
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Launch a server to manage the config entries.
|
2020-10-01 14:19:14 +00:00
|
|
|
serverAgent := NewTestAgent(t, "")
|
2019-09-24 15:04:48 +00:00
|
|
|
defer serverAgent.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, serverAgent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a global proxy and service config
|
|
|
|
testApplyConfigEntries(t, serverAgent,
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "redis",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Now launch a single client agent
|
|
|
|
cfg := `
|
|
|
|
server = false
|
|
|
|
bootstrap = false
|
|
|
|
`
|
2020-08-13 21:17:21 +00:00
|
|
|
a := StartTestAgent(t, TestAgent{HCL: cfg})
|
2019-04-24 13:11:08 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2019-09-24 15:04:48 +00:00
|
|
|
// Join first
|
|
|
|
_, err := a.JoinLAN([]string{
|
|
|
|
fmt.Sprintf("127.0.0.1:%d", serverAgent.Config.SerfPortLAN),
|
2021-10-26 20:08:55 +00:00
|
|
|
}, nil)
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-24 15:04:48 +00:00
|
|
|
|
2019-04-24 13:11:08 +00:00
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
2021-06-09 16:14:36 +00:00
|
|
|
newNodeService := func() *structs.NodeService {
|
|
|
|
return &structs.NodeService{
|
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
ID: "web-sidecar-proxy",
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 21000,
|
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8000,
|
|
|
|
Upstreams: structs.Upstreams{
|
|
|
|
{
|
|
|
|
DestinationName: "redis",
|
|
|
|
DestinationNamespace: "default",
|
2021-08-20 16:57:45 +00:00
|
|
|
DestinationPartition: "default",
|
2021-06-09 16:14:36 +00:00
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
2019-05-01 23:39:31 +00:00
|
|
|
},
|
2019-04-24 13:11:08 +00:00
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-06-09 16:14:36 +00:00
|
|
|
}
|
2019-09-24 15:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expectState := &structs.NodeService{
|
2020-01-17 14:54:17 +00:00
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
ID: "web-sidecar-proxy",
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 21000,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{},
|
2019-09-24 15:04:48 +00:00
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8000,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": int64(1),
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
Upstreams: structs.Upstreams{
|
|
|
|
{
|
2021-03-18 05:05:00 +00:00
|
|
|
DestinationName: "redis",
|
|
|
|
DestinationNamespace: "default",
|
2021-08-20 16:57:45 +00:00
|
|
|
DestinationPartition: "default",
|
2021-03-18 05:05:00 +00:00
|
|
|
LocalBindPort: 5000,
|
2019-09-24 15:04:48 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-05-01 23:39:31 +00:00
|
|
|
}
|
2019-09-24 15:04:48 +00:00
|
|
|
|
2021-06-09 16:14:36 +00:00
|
|
|
svc := newNodeService()
|
2020-02-05 15:06:11 +00:00
|
|
|
svcID := svc.CompoundServiceID()
|
|
|
|
|
2021-11-04 20:07:54 +00:00
|
|
|
svcFile := filepath.Join(a.Config.DataDir, servicesDir, svcID.StringHashSHA256())
|
|
|
|
configFile := filepath.Join(a.Config.DataDir, serviceConfigDir, svcID.StringHashSHA256())
|
2019-09-24 15:04:48 +00:00
|
|
|
|
|
|
|
// Service is not persisted unless requested, but we always persist service configs.
|
2020-11-30 22:50:06 +00:00
|
|
|
err = a.AddService(AddServiceRequest{Service: svc, Source: ConfigSourceRemote})
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-24 15:04:48 +00:00
|
|
|
requireFileIsAbsent(t, svcFile)
|
|
|
|
requireFileIsPresent(t, configFile)
|
|
|
|
|
|
|
|
// Persists to file if requested
|
2020-11-30 22:50:06 +00:00
|
|
|
err = a.AddService(AddServiceRequest{
|
|
|
|
Service: svc,
|
|
|
|
persist: true,
|
|
|
|
token: "mytoken",
|
|
|
|
Source: ConfigSourceRemote,
|
|
|
|
})
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-24 15:04:48 +00:00
|
|
|
requireFileIsPresent(t, svcFile)
|
|
|
|
requireFileIsPresent(t, configFile)
|
|
|
|
|
2021-07-02 16:18:46 +00:00
|
|
|
// Service definition file is reasonable.
|
2019-09-24 15:04:48 +00:00
|
|
|
expectJSONFile(t, svcFile, persistedService{
|
|
|
|
Token: "mytoken",
|
|
|
|
Service: svc,
|
|
|
|
Source: "remote",
|
|
|
|
}, nil)
|
|
|
|
|
2021-07-02 16:18:46 +00:00
|
|
|
// Service config file is reasonable.
|
2019-12-10 02:26:41 +00:00
|
|
|
pcfg := persistedServiceConfig{
|
2019-09-24 15:04:48 +00:00
|
|
|
ServiceID: "web-sidecar-proxy",
|
|
|
|
Defaults: &structs.ServiceConfigResponse{
|
|
|
|
ProxyConfig: map[string]interface{}{
|
|
|
|
"foo": 1,
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
2023-02-03 15:51:53 +00:00
|
|
|
UpstreamConfigs: structs.OpaqueUpstreamConfigs{
|
2023-01-18 19:43:53 +00:00
|
|
|
{
|
2023-02-03 15:51:53 +00:00
|
|
|
Upstream: structs.PeeredServiceName{
|
|
|
|
ServiceName: structs.NewServiceName("redis", nil),
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "tcp",
|
|
|
|
},
|
2019-09-24 15:04:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
2023-01-18 19:43:53 +00:00
|
|
|
expectJSONFile(t, configFile, pcfg, fixPersistedServiceConfigForTest)
|
2019-09-24 15:04:48 +00:00
|
|
|
|
|
|
|
// Verify in memory state.
|
2019-05-01 23:39:31 +00:00
|
|
|
{
|
2019-12-10 02:26:41 +00:00
|
|
|
sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, sidecarService)
|
|
|
|
require.Equal(t, expectState, sidecarService)
|
2019-09-24 15:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Updates service definition on disk
|
2021-06-09 16:14:36 +00:00
|
|
|
svc = newNodeService()
|
2019-09-24 15:04:48 +00:00
|
|
|
svc.Proxy.LocalServicePort = 8001
|
2021-06-09 16:18:18 +00:00
|
|
|
err = a.AddService(AddServiceRequest{
|
|
|
|
Service: svc,
|
|
|
|
persist: true,
|
|
|
|
token: "mytoken",
|
|
|
|
Source: ConfigSourceRemote,
|
|
|
|
})
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-24 15:04:48 +00:00
|
|
|
requireFileIsPresent(t, svcFile)
|
|
|
|
requireFileIsPresent(t, configFile)
|
|
|
|
|
|
|
|
// Service definition file is updated.
|
|
|
|
expectJSONFile(t, svcFile, persistedService{
|
|
|
|
Token: "mytoken",
|
|
|
|
Service: svc,
|
|
|
|
Source: "remote",
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
// Service config file is the same.
|
2019-12-10 02:26:41 +00:00
|
|
|
pcfg = persistedServiceConfig{
|
2019-09-24 15:04:48 +00:00
|
|
|
ServiceID: "web-sidecar-proxy",
|
|
|
|
Defaults: &structs.ServiceConfigResponse{
|
|
|
|
ProxyConfig: map[string]interface{}{
|
|
|
|
"foo": 1,
|
|
|
|
"protocol": "http",
|
2019-05-01 23:39:31 +00:00
|
|
|
},
|
2023-02-03 15:51:53 +00:00
|
|
|
UpstreamConfigs: structs.OpaqueUpstreamConfigs{
|
2023-01-18 19:43:53 +00:00
|
|
|
{
|
2023-02-03 15:51:53 +00:00
|
|
|
Upstream: structs.PeeredServiceName{
|
|
|
|
ServiceName: structs.NewServiceName("redis", nil),
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "tcp",
|
|
|
|
},
|
2019-09-24 15:04:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
2023-01-18 19:43:53 +00:00
|
|
|
expectJSONFile(t, configFile, pcfg, fixPersistedServiceConfigForTest)
|
2019-09-24 15:04:48 +00:00
|
|
|
|
|
|
|
// Verify in memory state.
|
|
|
|
expectState.Proxy.LocalServicePort = 8001
|
|
|
|
{
|
2019-12-10 02:26:41 +00:00
|
|
|
sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, sidecarService)
|
|
|
|
require.Equal(t, expectState, sidecarService)
|
2019-05-01 23:39:31 +00:00
|
|
|
}
|
2019-09-24 15:04:48 +00:00
|
|
|
|
|
|
|
// Kill the agent to restart it.
|
|
|
|
a.Shutdown()
|
|
|
|
|
|
|
|
// Kill the server so that it can't phone home and must rely upon the persisted defaults.
|
|
|
|
serverAgent.Shutdown()
|
|
|
|
|
|
|
|
// Should load it back during later start.
|
2020-08-13 21:17:21 +00:00
|
|
|
a2 := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: a.DataDir})
|
2019-09-24 15:04:48 +00:00
|
|
|
defer a2.Shutdown()
|
|
|
|
|
2019-05-01 23:39:31 +00:00
|
|
|
{
|
2019-12-10 02:26:41 +00:00
|
|
|
restored := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, restored)
|
|
|
|
require.Equal(t, expectState, restored)
|
2019-09-24 15:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now remove it.
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, a2.RemoveService(structs.NewServiceID("web-sidecar-proxy", nil)))
|
2019-09-24 15:04:48 +00:00
|
|
|
requireFileIsAbsent(t, svcFile)
|
|
|
|
requireFileIsAbsent(t, configFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceManager_PersistService_ConfigFiles(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:04:48 +00:00
|
|
|
// This is the ServiceManager version of TestAgent_PersistService and
|
|
|
|
// TestAgent_PurgeService but for config files.
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Launch a server to manage the config entries.
|
2020-10-01 14:19:14 +00:00
|
|
|
serverAgent := NewTestAgent(t, "")
|
2019-09-24 15:04:48 +00:00
|
|
|
defer serverAgent.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, serverAgent.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a global proxy and service config
|
|
|
|
testApplyConfigEntries(t, serverAgent,
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": 1,
|
2019-05-01 23:39:31 +00:00
|
|
|
},
|
2019-09-24 15:04:48 +00:00
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "redis",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
// Now launch a single client agent
|
|
|
|
serviceSnippet := `
|
|
|
|
service = {
|
|
|
|
kind = "connect-proxy"
|
|
|
|
id = "web-sidecar-proxy"
|
|
|
|
name = "web-sidecar-proxy"
|
|
|
|
port = 21000
|
|
|
|
token = "mytoken"
|
|
|
|
proxy {
|
|
|
|
destination_service_name = "web"
|
|
|
|
destination_service_id = "web"
|
|
|
|
local_service_address = "127.0.0.1"
|
|
|
|
local_service_port = 8000
|
|
|
|
upstreams = [{
|
|
|
|
destination_name = "redis"
|
2021-03-18 05:05:00 +00:00
|
|
|
destination_namespace = "default"
|
2021-08-20 16:57:45 +00:00
|
|
|
destination_partition = "default"
|
2019-09-24 15:04:48 +00:00
|
|
|
local_bind_port = 5000
|
|
|
|
}]
|
|
|
|
}
|
2019-05-01 23:39:31 +00:00
|
|
|
}
|
2019-09-24 15:04:48 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
cfg := `
|
|
|
|
server = false
|
|
|
|
bootstrap = false
|
|
|
|
` + serviceSnippet
|
|
|
|
|
2020-08-13 21:17:21 +00:00
|
|
|
a := StartTestAgent(t, TestAgent{HCL: cfg})
|
2019-09-24 15:04:48 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Join first
|
|
|
|
_, err := a.JoinLAN([]string{
|
|
|
|
fmt.Sprintf("127.0.0.1:%d", serverAgent.Config.SerfPortLAN),
|
2021-10-26 20:08:55 +00:00
|
|
|
}, nil)
|
2019-12-10 02:26:41 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-24 15:04:48 +00:00
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Now register a sidecar proxy via the API.
|
|
|
|
svcID := "web-sidecar-proxy"
|
|
|
|
|
|
|
|
expectState := &structs.NodeService{
|
2020-01-17 14:54:17 +00:00
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
ID: "web-sidecar-proxy",
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 21000,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{},
|
2019-09-24 15:04:48 +00:00
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8000,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": int64(1),
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
Upstreams: structs.Upstreams{
|
|
|
|
{
|
2021-03-18 05:05:00 +00:00
|
|
|
DestinationType: "service",
|
|
|
|
DestinationName: "redis",
|
|
|
|
DestinationNamespace: "default",
|
2021-08-20 16:57:45 +00:00
|
|
|
DestinationPartition: "default",
|
2021-03-18 05:05:00 +00:00
|
|
|
LocalBindPort: 5000,
|
2019-09-24 15:04:48 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "tcp",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-04-24 13:11:08 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 15:04:48 +00:00
|
|
|
// Now wait until we've re-registered using central config updated data.
|
|
|
|
retry.Run(t, func(r *retry.R) {
|
|
|
|
a.stateLock.Lock()
|
|
|
|
defer a.stateLock.Unlock()
|
2019-12-10 02:26:41 +00:00
|
|
|
current := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
|
2019-09-24 15:04:48 +00:00
|
|
|
if current == nil {
|
|
|
|
r.Fatalf("service is missing")
|
|
|
|
}
|
2019-12-10 02:26:41 +00:00
|
|
|
require.Equal(r, expectState, current)
|
2019-09-24 15:04:48 +00:00
|
|
|
})
|
|
|
|
|
2021-11-04 20:07:54 +00:00
|
|
|
svcFile := filepath.Join(a.Config.DataDir, servicesDir, stringHashSHA256(svcID))
|
|
|
|
configFile := filepath.Join(a.Config.DataDir, serviceConfigDir, stringHashSHA256(svcID))
|
2019-09-24 15:04:48 +00:00
|
|
|
|
|
|
|
// Service is never persisted, but we always persist service configs.
|
|
|
|
requireFileIsAbsent(t, svcFile)
|
|
|
|
requireFileIsPresent(t, configFile)
|
|
|
|
|
2021-07-02 16:18:46 +00:00
|
|
|
// Service config file is reasonable.
|
2019-09-24 15:04:48 +00:00
|
|
|
expectJSONFile(t, configFile, persistedServiceConfig{
|
|
|
|
ServiceID: "web-sidecar-proxy",
|
|
|
|
Defaults: &structs.ServiceConfigResponse{
|
|
|
|
ProxyConfig: map[string]interface{}{
|
|
|
|
"foo": 1,
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
2023-02-03 15:51:53 +00:00
|
|
|
UpstreamConfigs: structs.OpaqueUpstreamConfigs{
|
2023-01-18 19:43:53 +00:00
|
|
|
{
|
2023-02-03 15:51:53 +00:00
|
|
|
Upstream: structs.PeeredServiceName{
|
|
|
|
ServiceName: structs.NewServiceName("redis", nil),
|
|
|
|
},
|
2020-01-24 15:04:58 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "tcp",
|
|
|
|
},
|
2019-09-24 15:04:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2023-01-18 19:43:53 +00:00
|
|
|
}, fixPersistedServiceConfigForTest)
|
2019-09-24 15:04:48 +00:00
|
|
|
|
|
|
|
// Verify in memory state.
|
|
|
|
{
|
2019-12-10 02:26:41 +00:00
|
|
|
sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
|
|
|
|
require.NotNil(t, sidecarService)
|
|
|
|
require.Equal(t, expectState, sidecarService)
|
2019-09-24 15:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kill the agent to restart it.
|
|
|
|
a.Shutdown()
|
|
|
|
|
|
|
|
// Kill the server so that it can't phone home and must rely upon the persisted defaults.
|
|
|
|
serverAgent.Shutdown()
|
|
|
|
|
|
|
|
// Should load it back during later start.
|
2020-08-13 21:17:21 +00:00
|
|
|
a2 := StartTestAgent(t, TestAgent{HCL: cfg, DataDir: a.DataDir})
|
2019-09-24 15:04:48 +00:00
|
|
|
defer a2.Shutdown()
|
|
|
|
|
|
|
|
{
|
2019-12-10 02:26:41 +00:00
|
|
|
restored := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
|
|
|
|
require.NotNil(t, restored)
|
|
|
|
require.Equal(t, expectState, restored)
|
2019-09-24 15:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now remove it.
|
2019-12-10 02:26:41 +00:00
|
|
|
require.NoError(t, a2.RemoveService(structs.NewServiceID("web-sidecar-proxy", nil)))
|
2019-09-24 15:04:48 +00:00
|
|
|
requireFileIsAbsent(t, svcFile)
|
|
|
|
requireFileIsAbsent(t, configFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceManager_Disabled(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "enable_central_service_config = false")
|
2019-09-24 15:04:48 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Register a global proxy and service config
|
|
|
|
testApplyConfigEntries(t, a,
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "web",
|
|
|
|
Protocol: "http",
|
|
|
|
},
|
|
|
|
&structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "redis",
|
|
|
|
Protocol: "tcp",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2019-05-01 23:39:31 +00:00
|
|
|
// Now register a sidecar proxy. Note we don't use SidecarService here because
|
|
|
|
// that gets resolved earlier in config handling than the AddService call
|
|
|
|
// here.
|
2019-04-24 13:11:08 +00:00
|
|
|
svc := &structs.NodeService{
|
2019-05-01 23:39:31 +00:00
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
ID: "web-sidecar-proxy",
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 21000,
|
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8000,
|
|
|
|
Upstreams: structs.Upstreams{
|
|
|
|
{
|
|
|
|
DestinationName: "redis",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-04-24 13:11:08 +00:00
|
|
|
}
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NoError(t, a.addServiceFromSource(svc, nil, false, "", ConfigSourceLocal))
|
2019-05-01 23:39:31 +00:00
|
|
|
|
|
|
|
// Verify sidecar got global config loaded
|
2019-12-10 02:26:41 +00:00
|
|
|
sidecarService := a.State.Service(structs.NewServiceID("web-sidecar-proxy", nil))
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
require.NotNil(t, sidecarService)
|
|
|
|
require.Equal(t, &structs.NodeService{
|
2020-01-17 14:54:17 +00:00
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
ID: "web-sidecar-proxy",
|
|
|
|
Service: "web-sidecar-proxy",
|
|
|
|
Port: 21000,
|
|
|
|
TaggedAddresses: map[string]structs.ServiceAddress{},
|
2019-05-01 23:39:31 +00:00
|
|
|
Proxy: structs.ConnectProxyConfig{
|
|
|
|
DestinationServiceName: "web",
|
|
|
|
DestinationServiceID: "web",
|
|
|
|
LocalServiceAddress: "127.0.0.1",
|
|
|
|
LocalServicePort: 8000,
|
|
|
|
// No config added
|
|
|
|
Upstreams: structs.Upstreams{
|
|
|
|
{
|
|
|
|
DestinationName: "redis",
|
|
|
|
LocalBindPort: 5000,
|
|
|
|
// No config added
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-24 13:11:08 +00:00
|
|
|
Weights: &structs.Weights{
|
|
|
|
Passing: 1,
|
|
|
|
Warning: 1,
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2019-05-01 23:39:31 +00:00
|
|
|
}, sidecarService)
|
2019-04-24 13:11:08 +00:00
|
|
|
}
|
2019-09-24 15:04:48 +00:00
|
|
|
|
|
|
|
func testApplyConfigEntries(t *testing.T, a *TestAgent, entries ...structs.ConfigEntry) {
|
|
|
|
t.Helper()
|
|
|
|
for _, entry := range entries {
|
|
|
|
args := &structs.ConfigEntryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: entry,
|
|
|
|
}
|
|
|
|
var out bool
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Apply", args, &out))
|
2019-09-24 15:04:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireFileIsAbsent(t *testing.T, file string) {
|
|
|
|
t.Helper()
|
|
|
|
if _, err := os.Stat(file); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("should not persist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireFileIsPresent(t *testing.T, file string) {
|
|
|
|
t.Helper()
|
|
|
|
if _, err := os.Stat(file); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectJSONFile(t *testing.T, file string, expect interface{}, fixupContentBeforeCompareFn func([]byte) ([]byte, error)) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
expected, err := json.Marshal(expect)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
content, err := os.ReadFile(file)
|
2019-09-24 15:04:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
if fixupContentBeforeCompareFn != nil {
|
|
|
|
content, err = fixupContentBeforeCompareFn(content)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.JSONEq(t, string(expected), string(content))
|
|
|
|
}
|
|
|
|
|
2023-01-18 19:43:53 +00:00
|
|
|
func fixPersistedServiceConfigForTest(content []byte) ([]byte, error) {
|
|
|
|
var parsed persistedServiceConfig
|
|
|
|
if err := json.Unmarshal(content, &parsed); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Sort the output, since it's randomized and causes flaky tests otherwise.
|
2023-02-03 15:51:53 +00:00
|
|
|
sort.Slice(parsed.Defaults.UpstreamConfigs, func(i, j int) bool {
|
|
|
|
return parsed.Defaults.UpstreamConfigs[i].Upstream.String() < parsed.Defaults.UpstreamConfigs[j].Upstream.String()
|
|
|
|
})
|
2023-01-18 19:43:53 +00:00
|
|
|
out, err := json.Marshal(parsed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Clean the query meta
|
|
|
|
return resetDefaultsQueryMeta(out)
|
|
|
|
}
|
|
|
|
|
2019-09-24 15:04:48 +00:00
|
|
|
// resetDefaultsQueryMeta will reset the embedded fields from structs.QueryMeta
|
|
|
|
// to their zero values in the json object keyed under 'Defaults'.
|
|
|
|
func resetDefaultsQueryMeta(content []byte) ([]byte, error) {
|
|
|
|
var raw map[string]interface{}
|
|
|
|
if err := json.Unmarshal(content, &raw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
def, ok := raw["Defaults"]
|
|
|
|
if !ok {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rawDef, ok := def.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected structure found in 'Defaults' key")
|
|
|
|
}
|
|
|
|
|
|
|
|
qmZero, err := convertToMap(structs.QueryMeta{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range qmZero {
|
|
|
|
rawDef[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
raw["Defaults"] = rawDef
|
|
|
|
|
|
|
|
return json.Marshal(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertToMap(v interface{}) (map[string]interface{}, error) {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var raw map[string]interface{}
|
|
|
|
if err := json.Unmarshal(b, &raw); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return raw, nil
|
|
|
|
}
|