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-29 22:08:09 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-12-14 15:24:22 +00:00
|
|
|
"context"
|
2019-04-29 22:08:09 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-04-29 21:44:32 +00:00
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2021-04-29 21:44:32 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2024-01-23 04:36:59 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2021-04-29 21:44:32 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2019-04-29 22:08:09 +00:00
|
|
|
)
|
|
|
|
|
2023-10-11 15:44:03 +00:00
|
|
|
func TestConfigEndpointsFailInV2(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
a := NewTestAgent(t, `experiments = ["resource-apis"]`)
|
|
|
|
|
|
|
|
checkRequest := func(method, url string) {
|
|
|
|
t.Run(method+" "+url, func(t *testing.T) {
|
|
|
|
assertV1CatalogEndpointDoesNotWorkWithV2(t, a, method, url, `{"kind":"service-defaults", "name":"web"}`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
checkRequest("GET", "/v1/config/service-defaults")
|
|
|
|
checkRequest("GET", "/v1/config/service-defaults/web")
|
|
|
|
checkRequest("DELETE", "/v1/config/service-defaults/web")
|
|
|
|
checkRequest("PUT", "/v1/config")
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
func TestConfig_Get(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-04-29 22:08:09 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
reqs := []structs.ConfigEntryRequest{
|
|
|
|
{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: &structs.ServiceConfigEntry{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: &structs.ServiceConfigEntry{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: &structs.ProxyConfigEntry{
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
"bar": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-04-29 21:44:32 +00:00
|
|
|
{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: &structs.MeshConfigEntry{
|
|
|
|
TransparentProxy: structs.TransparentProxyMeshConfig{
|
2021-06-14 20:15:09 +00:00
|
|
|
MeshDestinationsOnly: true,
|
2021-04-29 21:44:32 +00:00
|
|
|
},
|
|
|
|
Meta: map[string]string{
|
|
|
|
"key1": "value1",
|
|
|
|
"key2": "value2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
for _, req := range reqs {
|
2019-04-30 23:27:16 +00:00
|
|
|
out := false
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Apply", &req, &out))
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("get a single service entry", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/config/service-defaults/foo", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.Config(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := obj.(structs.ConfigEntry)
|
|
|
|
require.Equal(t, structs.ServiceDefaults, value.GetKind())
|
|
|
|
entry := value.(*structs.ServiceConfigEntry)
|
|
|
|
require.Equal(t, entry.Name, "foo")
|
|
|
|
})
|
|
|
|
t.Run("list both service entries", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/config/service-defaults", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.Config(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := obj.([]structs.ConfigEntry)
|
|
|
|
require.Len(t, value, 2)
|
|
|
|
require.Equal(t, value[0].(*structs.ServiceConfigEntry).Name, "bar")
|
|
|
|
require.Equal(t, value[1].(*structs.ServiceConfigEntry).Name, "foo")
|
|
|
|
})
|
|
|
|
t.Run("get global proxy config", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/config/proxy-defaults/global", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.Config(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := obj.(structs.ConfigEntry)
|
|
|
|
require.Equal(t, value.GetKind(), structs.ProxyDefaults)
|
|
|
|
entry := value.(*structs.ProxyConfigEntry)
|
|
|
|
require.Equal(t, structs.ProxyConfigGlobal, entry.Name)
|
|
|
|
require.Contains(t, entry.Config, "foo")
|
|
|
|
require.Equal(t, "bar", entry.Config["foo"])
|
|
|
|
})
|
|
|
|
t.Run("error on no arguments", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/config/", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.Config(resp, req)
|
|
|
|
require.Error(t, errors.New("Must provide either a kind or both kind and name"), err)
|
|
|
|
})
|
2021-04-29 21:44:32 +00:00
|
|
|
t.Run("get the single mesh config", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/config/mesh/mesh", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.Config(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ce, ok := obj.(*structs.MeshConfigEntry)
|
|
|
|
require.True(t, ok, "wrong type %T", obj)
|
2021-05-05 16:37:47 +00:00
|
|
|
// Set indexes and EnterpriseMeta to expected values for assertions
|
2021-04-29 21:44:32 +00:00
|
|
|
ce.CreateIndex = 12
|
|
|
|
ce.ModifyIndex = 13
|
2024-01-09 19:45:29 +00:00
|
|
|
ce.Hash = 0
|
2022-04-05 21:10:06 +00:00
|
|
|
ce.EnterpriseMeta = acl.EnterpriseMeta{}
|
2021-04-29 21:44:32 +00:00
|
|
|
|
|
|
|
out, err := a.srv.marshalJSON(req, obj)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := `
|
|
|
|
{
|
|
|
|
"Kind": "mesh",
|
|
|
|
"TransparentProxy": {
|
2021-06-14 20:15:09 +00:00
|
|
|
"MeshDestinationsOnly": true
|
2021-04-29 21:44:32 +00:00
|
|
|
},
|
|
|
|
"Meta":{
|
|
|
|
"key1": "value1",
|
|
|
|
"key2": "value2"
|
|
|
|
},
|
|
|
|
"CreateIndex": 12,
|
|
|
|
"ModifyIndex": 13
|
|
|
|
}
|
|
|
|
`
|
|
|
|
require.JSONEq(t, expected, string(out))
|
|
|
|
})
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_Delete(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-04-29 22:08:09 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
reqs := []structs.ConfigEntryRequest{
|
|
|
|
{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: &structs.ServiceConfigEntry{
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: &structs.ServiceConfigEntry{
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, req := range reqs {
|
2019-04-30 23:27:16 +00:00
|
|
|
out := false
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Apply", &req, &out))
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete an entry.
|
|
|
|
{
|
|
|
|
req, _ := http.NewRequest("DELETE", "/v1/config/service-defaults/bar", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.Config(resp, req)
|
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-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
// Get the remaining entry.
|
|
|
|
{
|
|
|
|
args := structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.IndexedConfigEntries
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.List", &args, &out))
|
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.Equal(t, structs.ServiceDefaults, out.Kind)
|
|
|
|
require.Len(t, out.Entries, 1)
|
2019-04-29 22:08:09 +00:00
|
|
|
entry := out.Entries[0].(*structs.ServiceConfigEntry)
|
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.Equal(t, entry.Name, "foo")
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 16:42:01 +00:00
|
|
|
func TestConfig_Delete_CAS(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
a := NewTestAgent(t, "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create a config entry.
|
|
|
|
entry := &structs.ServiceConfigEntry{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "foo",
|
|
|
|
}
|
|
|
|
var created bool
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Apply", &structs.ConfigEntryRequest{
|
2021-11-01 16:42:01 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: entry,
|
|
|
|
}, &created))
|
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.True(t, created)
|
2021-11-01 16:42:01 +00:00
|
|
|
|
|
|
|
// Read it back to get its ModifyIndex.
|
|
|
|
var out structs.ConfigEntryResponse
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &structs.ConfigEntryQuery{
|
2021-11-01 16:42:01 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Kind: entry.Kind,
|
|
|
|
Name: entry.Name,
|
|
|
|
}, &out))
|
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, out.Entry)
|
2021-11-01 16:42:01 +00:00
|
|
|
|
|
|
|
modifyIndex := out.Entry.GetRaftIndex().ModifyIndex
|
|
|
|
|
|
|
|
t.Run("attempt to delete with an invalid index", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(
|
|
|
|
"DELETE",
|
|
|
|
fmt.Sprintf("/v1/config/%s/%s?cas=%d", entry.Kind, entry.Name, modifyIndex-1),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
rawRsp, err := a.srv.Config(httptest.NewRecorder(), req)
|
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)
|
2021-11-01 16:42:01 +00:00
|
|
|
|
|
|
|
deleted, isBool := rawRsp.(bool)
|
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.True(t, isBool, "response should be a boolean")
|
|
|
|
require.False(t, deleted, "entry should not have been deleted")
|
2021-11-01 16:42:01 +00:00
|
|
|
|
|
|
|
// Verify it was not deleted.
|
|
|
|
var out structs.ConfigEntryResponse
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &structs.ConfigEntryQuery{
|
2021-11-01 16:42:01 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Kind: entry.Kind,
|
|
|
|
Name: entry.Name,
|
|
|
|
}, &out))
|
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, out.Entry)
|
2021-11-01 16:42:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("attempt to delete with a valid index", func(t *testing.T) {
|
|
|
|
req := httptest.NewRequest(
|
|
|
|
"DELETE",
|
|
|
|
fmt.Sprintf("/v1/config/%s/%s?cas=%d", entry.Kind, entry.Name, modifyIndex),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
rawRsp, err := a.srv.Config(httptest.NewRecorder(), req)
|
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)
|
2021-11-01 16:42:01 +00:00
|
|
|
|
|
|
|
deleted, isBool := rawRsp.(bool)
|
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.True(t, isBool, "response should be a boolean")
|
|
|
|
require.True(t, deleted, "entry should have been deleted")
|
2021-11-01 16:42:01 +00:00
|
|
|
|
|
|
|
// Verify it was deleted.
|
|
|
|
var out structs.ConfigEntryResponse
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &structs.ConfigEntryQuery{
|
2021-11-01 16:42:01 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
Kind: entry.Kind,
|
|
|
|
Name: entry.Name,
|
|
|
|
}, &out))
|
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.Nil(t, out.Entry)
|
2021-11-01 16:42:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
func TestConfig_Apply(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-04-29 22:08:09 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
body := bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"Kind": "service-defaults",
|
|
|
|
"Name": "foo",
|
|
|
|
"Protocol": "tcp"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
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-04-29 22:08:09 +00:00
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf(resp.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the remaining entry.
|
|
|
|
{
|
|
|
|
args := structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.ConfigEntryResponse
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &args, &out))
|
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, out.Entry)
|
2019-04-29 22:08:09 +00:00
|
|
|
entry := out.Entry.(*structs.ServiceConfigEntry)
|
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.Equal(t, entry.Name, "foo")
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 19:27:32 +00:00
|
|
|
func TestConfig_Apply_TerminatingGateway(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:27:32 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-04-01 16:29:40 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2020-03-31 19:27:32 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
body := bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"Kind": "terminating-gateway",
|
|
|
|
"Name": "west-gw-01",
|
|
|
|
"Services": [
|
|
|
|
{
|
|
|
|
"Name": "web",
|
|
|
|
"CAFile": "/etc/web/ca.crt",
|
|
|
|
"CertFile": "/etc/web/client.crt",
|
|
|
|
"KeyFile": "/etc/web/tls.key"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Name": "api"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code, "!200 Response Code: %s", resp.Body.String())
|
|
|
|
|
2020-04-08 18:37:24 +00:00
|
|
|
// List all entries, there should only be one
|
2020-03-31 19:27:32 +00:00
|
|
|
{
|
|
|
|
args := structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.TerminatingGateway,
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
2020-04-08 18:37:24 +00:00
|
|
|
var out structs.IndexedConfigEntries
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.List", &args, &out))
|
2020-04-08 18:37:24 +00:00
|
|
|
require.NotNil(t, out)
|
|
|
|
require.Len(t, out.Entries, 1)
|
|
|
|
|
|
|
|
got := out.Entries[0].(*structs.TerminatingGatewayConfigEntry)
|
2020-03-31 19:27:32 +00:00
|
|
|
expect := []structs.LinkedService{
|
|
|
|
{
|
2020-04-20 14:02:35 +00:00
|
|
|
Name: "web",
|
|
|
|
CAFile: "/etc/web/ca.crt",
|
|
|
|
CertFile: "/etc/web/client.crt",
|
|
|
|
KeyFile: "/etc/web/tls.key",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-03-31 19:27:32 +00:00
|
|
|
},
|
|
|
|
{
|
2020-04-20 14:02:35 +00:00
|
|
|
Name: "api",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-03-31 19:27:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, got.Services)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
func TestConfig_Apply_IngressGateway(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-04-16 21:00:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
a := NewTestAgent(t, "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
body := bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"Kind": "ingress-gateway",
|
|
|
|
"Name": "ingress",
|
|
|
|
"Listeners": [
|
|
|
|
{
|
|
|
|
"Port": 8080,
|
|
|
|
"Services": [
|
|
|
|
{ "Name": "web" }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code, "!200 Response Code: %s", resp.Body.String())
|
|
|
|
|
|
|
|
// List all entries, there should only be one
|
|
|
|
{
|
|
|
|
args := structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.IngressGateway,
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.IndexedConfigEntries
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.List", &args, &out))
|
2020-04-16 21:00:48 +00:00
|
|
|
require.NotNil(t, out)
|
|
|
|
require.Len(t, out.Entries, 1)
|
|
|
|
|
|
|
|
got := out.Entries[0].(*structs.IngressGatewayConfigEntry)
|
|
|
|
// Ignore create and modify indices
|
|
|
|
got.CreateIndex = 0
|
|
|
|
got.ModifyIndex = 0
|
2024-01-09 19:45:29 +00:00
|
|
|
got.Hash = 0
|
2020-04-16 21:00:48 +00:00
|
|
|
|
|
|
|
expect := &structs.IngressGatewayConfigEntry{
|
|
|
|
Name: "ingress",
|
|
|
|
Kind: structs.IngressGateway,
|
|
|
|
Listeners: []structs.IngressListener{
|
|
|
|
{
|
|
|
|
Port: 8080,
|
|
|
|
Protocol: "tcp",
|
|
|
|
Services: []structs.IngressService{
|
2020-04-20 14:02:35 +00:00
|
|
|
{
|
|
|
|
Name: "web",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-04-20 14:02:35 +00:00
|
|
|
},
|
2020-04-16 21:00:48 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2020-04-16 21:00:48 +00:00
|
|
|
}
|
|
|
|
require.Equal(t, expect, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
func TestConfig_Apply_ProxyDefaultsMeshGateway(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-06-18 00:52:01 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-06-18 00:52:01 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
body := bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"Kind": "proxy-defaults",
|
|
|
|
"Name": "global",
|
|
|
|
"MeshGateway": {
|
|
|
|
"Mode": "local"
|
|
|
|
}
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code, "!200 Response Code: %s", resp.Body.String())
|
|
|
|
|
|
|
|
// Get the remaining entry.
|
|
|
|
{
|
|
|
|
args := structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.ConfigEntryResponse
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &args, &out))
|
2019-06-18 00:52:01 +00:00
|
|
|
require.NotNil(t, out.Entry)
|
|
|
|
entry := out.Entry.(*structs.ProxyConfigEntry)
|
|
|
|
require.Equal(t, structs.MeshGatewayModeLocal, entry.MeshGateway.Mode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
func TestConfig_Apply_CAS(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-04-29 22:08:09 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
body := bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"Kind": "service-defaults",
|
|
|
|
"Name": "foo",
|
|
|
|
"Protocol": "tcp"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
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-04-29 22:08:09 +00:00
|
|
|
if resp.Code != 200 {
|
|
|
|
t.Fatalf(resp.Body.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the entry remaining entry.
|
|
|
|
args := structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
out := &structs.ConfigEntryResponse{}
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &args, out))
|
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, out.Entry)
|
2019-04-29 22:08:09 +00:00
|
|
|
entry := out.Entry.(*structs.ServiceConfigEntry)
|
|
|
|
|
2019-04-30 23:27:16 +00:00
|
|
|
body = bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"Kind": "service-defaults",
|
|
|
|
"Name": "foo",
|
|
|
|
"Protocol": "udp"
|
|
|
|
}
|
|
|
|
`))
|
2019-04-29 22:08:09 +00:00
|
|
|
req, _ = http.NewRequest("PUT", "/v1/config?cas=0", body)
|
|
|
|
resp = httptest.NewRecorder()
|
2019-04-30 23:27:16 +00:00
|
|
|
writtenRaw, err := a.srv.ConfigApply(resp, req)
|
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-04-30 23:27:16 +00:00
|
|
|
written, ok := writtenRaw.(bool)
|
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.True(t, ok)
|
|
|
|
require.False(t, written)
|
|
|
|
require.EqualValues(t, 200, resp.Code, resp.Body.String())
|
2019-04-29 22:08:09 +00:00
|
|
|
|
|
|
|
body = bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"Kind": "service-defaults",
|
|
|
|
"Name": "foo",
|
|
|
|
"Protocol": "udp"
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
req, _ = http.NewRequest("PUT", fmt.Sprintf("/v1/config?cas=%d", entry.GetRaftIndex().ModifyIndex), body)
|
|
|
|
resp = httptest.NewRecorder()
|
2019-04-30 23:27:16 +00:00
|
|
|
writtenRaw, err = a.srv.ConfigApply(resp, req)
|
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-04-30 23:27:16 +00:00
|
|
|
written, ok = writtenRaw.(bool)
|
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.True(t, ok)
|
|
|
|
require.True(t, written)
|
|
|
|
require.EqualValues(t, 200, resp.Code, resp.Body.String())
|
2019-04-29 22:08:09 +00:00
|
|
|
|
|
|
|
// Get the entry remaining entry.
|
|
|
|
args = structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
out = &structs.ConfigEntryResponse{}
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &args, out))
|
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, out.Entry)
|
2019-04-29 22:08:09 +00:00
|
|
|
newEntry := out.Entry.(*structs.ServiceConfigEntry)
|
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.NotEqual(t, entry.GetRaftIndex(), newEntry.GetRaftIndex())
|
2019-04-29 22:08:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfig_Apply_Decoding(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-04-29 22:08:09 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-04-29 22:08:09 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
t.Run("No Kind", func(t *testing.T) {
|
|
|
|
body := bytes.NewBuffer([]byte(
|
|
|
|
`{
|
|
|
|
"Name": "foo",
|
|
|
|
"Protocol": "tcp"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
|
|
|
require.Error(t, err)
|
2022-04-29 17:42:49 +00:00
|
|
|
require.True(t, isHTTPBadRequest(err))
|
|
|
|
require.Equal(t, "Request decoding failed: Payload does not contain a kind/Kind key at the top level", err.Error())
|
2019-04-29 22:08:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Kind Not String", func(t *testing.T) {
|
|
|
|
body := bytes.NewBuffer([]byte(
|
|
|
|
`{
|
|
|
|
"Kind": 123,
|
|
|
|
"Name": "foo",
|
|
|
|
"Protocol": "tcp"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
|
|
|
require.Error(t, err)
|
2022-04-29 17:42:49 +00:00
|
|
|
require.True(t, isHTTPBadRequest(err))
|
|
|
|
require.Equal(t, "Request decoding failed: Kind value in payload is not a string", err.Error())
|
2019-04-29 22:08:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Lowercase kind", func(t *testing.T) {
|
|
|
|
body := bytes.NewBuffer([]byte(
|
|
|
|
`{
|
|
|
|
"kind": "service-defaults",
|
|
|
|
"Name": "foo",
|
|
|
|
"Protocol": "tcp"
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 200, resp.Code, resp.Body.String())
|
|
|
|
|
|
|
|
// Get the remaining entry.
|
|
|
|
{
|
|
|
|
args := structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.ServiceDefaults,
|
|
|
|
Name: "foo",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.ConfigEntryResponse
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &args, &out))
|
2019-04-29 22:08:09 +00:00
|
|
|
require.NotNil(t, out.Entry)
|
|
|
|
entry := out.Entry.(*structs.ServiceConfigEntry)
|
|
|
|
require.Equal(t, entry.Name, "foo")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-09-26 02:55:52 +00:00
|
|
|
|
|
|
|
func TestConfig_Apply_ProxyDefaultsExpose(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-09-26 02:55:52 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2019-09-26 02:55:52 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some config entries.
|
|
|
|
body := bytes.NewBuffer([]byte(`
|
|
|
|
{
|
|
|
|
"Kind": "proxy-defaults",
|
|
|
|
"Name": "global",
|
|
|
|
"Expose": {
|
|
|
|
"Checks": true,
|
|
|
|
"Paths": [
|
|
|
|
{
|
|
|
|
"LocalPathPort": 8080,
|
|
|
|
"ListenerPort": 21500,
|
|
|
|
"Path": "/healthz",
|
|
|
|
"Protocol": "http2"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`))
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("PUT", "/v1/config", body)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.ConfigApply(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, resp.Code, "!200 Response Code: %s", resp.Body.String())
|
|
|
|
|
|
|
|
// Get the remaining entry.
|
|
|
|
{
|
|
|
|
args := structs.ConfigEntryQuery{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: "global",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var out structs.ConfigEntryResponse
|
2022-12-14 15:24:22 +00:00
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Get", &args, &out))
|
2019-09-26 02:55:52 +00:00
|
|
|
require.NotNil(t, out.Entry)
|
|
|
|
entry := out.Entry.(*structs.ProxyConfigEntry)
|
|
|
|
|
|
|
|
expose := structs.ExposeConfig{
|
|
|
|
Checks: true,
|
|
|
|
Paths: []structs.ExposePath{
|
|
|
|
{
|
|
|
|
LocalPathPort: 8080,
|
|
|
|
ListenerPort: 21500,
|
|
|
|
Path: "/healthz",
|
|
|
|
Protocol: "http2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expose, entry.Expose)
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 04:36:59 +00:00
|
|
|
|
|
|
|
func TestConfig_Exported_Services(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
a := NewTestAgent(t, "")
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
{
|
|
|
|
// Register exported services
|
|
|
|
args := &structs.ExportedServicesConfigEntry{
|
|
|
|
Name: "default",
|
|
|
|
Services: []structs.ExportedService{
|
|
|
|
{
|
|
|
|
Name: "api",
|
|
|
|
Consumers: []structs.ServiceConsumer{
|
|
|
|
{
|
|
|
|
Peer: "east",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Peer: "west",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "db",
|
|
|
|
Consumers: []structs.ServiceConsumer{
|
|
|
|
{
|
|
|
|
Peer: "east",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
req := structs.ConfigEntryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Entry: args,
|
|
|
|
}
|
|
|
|
var configOutput bool
|
|
|
|
require.NoError(t, a.RPC(context.Background(), "ConfigEntry.Apply", &req, &configOutput))
|
|
|
|
require.True(t, configOutput)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("exported services", func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/exported-services", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
raw, err := a.srv.ExportedServices(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
|
|
|
|
services, ok := raw.([]api.ResolvedExportedService)
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Len(t, services, 2)
|
|
|
|
assertIndex(t, resp)
|
|
|
|
|
|
|
|
entMeta := acl.DefaultEnterpriseMeta()
|
|
|
|
|
|
|
|
expected := []api.ResolvedExportedService{
|
|
|
|
{
|
|
|
|
Service: "api",
|
|
|
|
Partition: entMeta.PartitionOrEmpty(),
|
|
|
|
Namespace: entMeta.NamespaceOrEmpty(),
|
|
|
|
Consumers: api.ResolvedConsumers{
|
|
|
|
Peers: []string{"east", "west"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Service: "db",
|
|
|
|
Partition: entMeta.PartitionOrEmpty(),
|
|
|
|
Namespace: entMeta.NamespaceOrEmpty(),
|
|
|
|
Consumers: api.ResolvedConsumers{
|
|
|
|
Peers: []string{"east"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, services)
|
|
|
|
})
|
|
|
|
}
|