2018-02-28 19:36:54 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2018-02-28 23:54:48 +00:00
|
|
|
"fmt"
|
2018-02-28 19:36:54 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-03-06 18:35:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-05-11 05:52:57 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-28 19:36:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIntentionsList_empty(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-02-28 19:36:54 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Make sure an empty list is non-nil.
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/intentions", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionList(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
|
|
|
value := obj.(structs.Intentions)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(value)
|
|
|
|
assert.Len(value, 0)
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionsList_values(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-02-28 19:36:54 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
2018-06-08 12:10:06 +00:00
|
|
|
// Create some intentions, note we create the lowest precedence first to test
|
|
|
|
// sorting.
|
|
|
|
for _, v := range []string{"*", "foo", "bar"} {
|
2018-02-28 19:36:54 +00:00
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
2018-03-03 18:12:05 +00:00
|
|
|
Intention: structs.TestIntention(t),
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
2018-03-03 18:12:05 +00:00
|
|
|
req.Intention.SourceName = v
|
|
|
|
|
2018-02-28 19:36:54 +00:00
|
|
|
var reply string
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Apply", &req, &reply))
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/intentions", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionList(resp, req)
|
2018-06-08 12:10:06 +00:00
|
|
|
assert.NoError(err)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
|
|
|
value := obj.(structs.Intentions)
|
2018-06-08 12:10:06 +00:00
|
|
|
assert.Len(value, 3)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
2018-06-08 12:10:06 +00:00
|
|
|
expected := []string{"bar", "foo", "*"}
|
|
|
|
actual := []string{
|
|
|
|
value[0].SourceName,
|
|
|
|
value[1].SourceName,
|
|
|
|
value[2].SourceName,
|
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(expected, actual)
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
2018-02-28 22:02:00 +00:00
|
|
|
|
2018-03-02 22:17:21 +00:00
|
|
|
func TestIntentionsMatch_basic(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-02 22:17:21 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
2020-06-26 21:59:15 +00:00
|
|
|
{"default", "*", "default", "*"},
|
|
|
|
{"default", "*", "default", "bar"},
|
|
|
|
{"default", "*", "default", "baz"}, // shouldn't match
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range insert {
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
2018-03-03 18:12:05 +00:00
|
|
|
Intention: structs.TestIntention(t),
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
2018-04-05 11:53:42 +00:00
|
|
|
ixn.Intention.SourceNS = v[0]
|
|
|
|
ixn.Intention.SourceName = v[1]
|
|
|
|
ixn.Intention.DestinationNS = v[2]
|
|
|
|
ixn.Intention.DestinationName = v[3]
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2020-06-26 21:59:15 +00:00
|
|
|
require.Nil(t, a.RPC("Intention.Apply", &ixn, &reply))
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
2020-06-26 21:59:15 +00:00
|
|
|
"/v1/connect/intentions/match?by=destination&name=bar", nil)
|
2018-03-02 22:17:21 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.Nil(t, err)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
value := obj.(map[string]structs.Intentions)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.Len(t, value, 1)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
var actual [][]string
|
2018-04-05 11:53:42 +00:00
|
|
|
expected := [][]string{
|
2020-06-26 21:59:15 +00:00
|
|
|
{"default", "*", "default", "bar"},
|
|
|
|
{"default", "*", "default", "*"},
|
2018-04-05 11:53:42 +00:00
|
|
|
}
|
2020-06-26 21:59:15 +00:00
|
|
|
for _, ixn := range value["bar"] {
|
2018-04-05 11:53:42 +00:00
|
|
|
actual = append(actual, []string{
|
|
|
|
ixn.SourceNS,
|
|
|
|
ixn.SourceName,
|
|
|
|
ixn.DestinationNS,
|
|
|
|
ixn.DestinationName,
|
|
|
|
})
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:59:15 +00:00
|
|
|
require.Equal(t, expected, actual)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionsMatch_noBy(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-02 22:17:21 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/match?name=foo/bar", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "by")
|
|
|
|
assert.Nil(obj)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionsMatch_byInvalid(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-02 22:17:21 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/match?by=datacenter", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "'by' parameter")
|
|
|
|
assert.Nil(obj)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionsMatch_noName(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-02 22:17:21 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/match?by=source", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "'name' not set")
|
|
|
|
assert.Nil(obj)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 16:19:22 +00:00
|
|
|
func TestIntentionsCheck_basic(t *testing.T) {
|
2018-05-11 05:52:57 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-05-11 05:52:57 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
2020-06-26 21:59:15 +00:00
|
|
|
{"default", "*", "default", "baz"},
|
|
|
|
{"default", "*", "default", "bar"},
|
2018-05-11 05:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range insert {
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.SourceNS = v[0]
|
|
|
|
ixn.Intention.SourceName = v[1]
|
|
|
|
ixn.Intention.DestinationNS = v[2]
|
|
|
|
ixn.Intention.DestinationName = v[3]
|
|
|
|
ixn.Intention.Action = structs.IntentionActionDeny
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2020-06-26 21:59:15 +00:00
|
|
|
require.NoError(t, a.RPC("Intention.Apply", &ixn, &reply))
|
2018-05-11 05:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request matching intention
|
|
|
|
{
|
|
|
|
req, _ := http.NewRequest("GET",
|
2020-06-26 21:59:15 +00:00
|
|
|
"/v1/connect/intentions/test?source=bar&destination=baz", nil)
|
2018-05-11 05:52:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2018-05-11 16:19:22 +00:00
|
|
|
obj, err := a.srv.IntentionCheck(resp, req)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-11 16:19:22 +00:00
|
|
|
value := obj.(*structs.IntentionQueryCheckResponse)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.False(t, value.Allowed)
|
2018-05-11 05:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request non-matching intention
|
|
|
|
{
|
|
|
|
req, _ := http.NewRequest("GET",
|
2020-06-26 21:59:15 +00:00
|
|
|
"/v1/connect/intentions/test?source=bar&destination=qux", nil)
|
2018-05-11 05:52:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2018-05-11 16:19:22 +00:00
|
|
|
obj, err := a.srv.IntentionCheck(resp, req)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-11 16:19:22 +00:00
|
|
|
value := obj.(*structs.IntentionQueryCheckResponse)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.True(t, value.Allowed)
|
2018-05-11 05:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 16:19:22 +00:00
|
|
|
func TestIntentionsCheck_noSource(t *testing.T) {
|
2018-05-11 05:52:57 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-05-11 05:52:57 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/test?destination=B", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-05-11 16:19:22 +00:00
|
|
|
obj, err := a.srv.IntentionCheck(resp, req)
|
2018-05-11 05:52:57 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "'source' not set")
|
|
|
|
require.Nil(obj)
|
|
|
|
}
|
|
|
|
|
2018-05-11 16:19:22 +00:00
|
|
|
func TestIntentionsCheck_noDestination(t *testing.T) {
|
2018-05-11 05:52:57 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-05-11 05:52:57 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/test?source=B", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
2018-05-11 16:19:22 +00:00
|
|
|
obj, err := a.srv.IntentionCheck(resp, req)
|
2018-05-11 05:52:57 +00:00
|
|
|
require.NotNil(err)
|
|
|
|
require.Contains(err.Error(), "'destination' not set")
|
|
|
|
require.Nil(obj)
|
|
|
|
}
|
|
|
|
|
2018-02-28 22:02:00 +00:00
|
|
|
func TestIntentionsCreate_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-02-28 22:02:00 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Make sure an empty list is non-nil.
|
2018-03-03 18:12:05 +00:00
|
|
|
args := structs.TestIntention(t)
|
|
|
|
args.SourceName = "foo"
|
2018-02-28 22:02:00 +00:00
|
|
|
req, _ := http.NewRequest("POST", "/v1/connect/intentions", jsonReader(args))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionCreate(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-02-28 22:02:00 +00:00
|
|
|
|
|
|
|
value := obj.(intentionCreateResponse)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotEqual("", value.ID)
|
2018-02-28 22:02:00 +00:00
|
|
|
|
|
|
|
// Read the value
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: value.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-02-28 22:02:00 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal("foo", actual.SourceName)
|
2018-02-28 22:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 23:54:48 +00:00
|
|
|
|
2018-06-05 18:09:45 +00:00
|
|
|
func TestIntentionsCreate_noBody(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-06-05 18:09:45 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Create with no body
|
|
|
|
req, _ := http.NewRequest("POST", "/v1/connect/intentions", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.IntentionCreate(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2018-02-28 23:54:48 +00:00
|
|
|
func TestIntentionsSpecificGet_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-02-28 23:54:48 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// The intention
|
2018-03-03 18:12:05 +00:00
|
|
|
ixn := structs.TestIntention(t)
|
2018-02-28 23:54:48 +00:00
|
|
|
|
|
|
|
// Create an intention directly
|
|
|
|
var reply string
|
|
|
|
{
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: ixn,
|
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Apply", &req, &reply))
|
2018-02-28 23:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/connect/intentions/%s", reply), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-02-28 23:54:48 +00:00
|
|
|
|
|
|
|
value := obj.(*structs.Intention)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(reply, value.ID)
|
2018-02-28 23:54:48 +00:00
|
|
|
|
|
|
|
ixn.ID = value.ID
|
|
|
|
ixn.RaftIndex = value.RaftIndex
|
2018-03-03 16:51:40 +00:00
|
|
|
ixn.CreatedAt, ixn.UpdatedAt = value.CreatedAt, value.UpdatedAt
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(ixn, value)
|
2018-02-28 23:54:48 +00:00
|
|
|
}
|
2018-03-01 23:54:03 +00:00
|
|
|
|
2018-06-27 05:40:06 +00:00
|
|
|
func TestIntentionsSpecificGet_invalidId(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-06-27 05:40:06 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Read intention with bad ID
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/intentions/hello", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
|
|
|
require.Nil(obj)
|
|
|
|
require.Error(err)
|
|
|
|
require.IsType(BadRequestError{}, err)
|
|
|
|
require.Contains(err.Error(), "UUID")
|
|
|
|
}
|
|
|
|
|
2018-03-02 00:53:31 +00:00
|
|
|
func TestIntentionsSpecificUpdate_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-02 00:53:31 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// The intention
|
2018-03-03 18:12:05 +00:00
|
|
|
ixn := structs.TestIntention(t)
|
2018-03-02 00:53:31 +00:00
|
|
|
|
|
|
|
// Create an intention directly
|
|
|
|
var reply string
|
|
|
|
{
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: ixn,
|
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Apply", &req, &reply))
|
2018-03-02 00:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the intention
|
|
|
|
ixn.ID = "bogus"
|
|
|
|
ixn.SourceName = "bar"
|
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/connect/intentions/%s", reply), jsonReader(ixn))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-06-13 10:54:27 +00:00
|
|
|
|
|
|
|
value := obj.(intentionCreateResponse)
|
|
|
|
assert.Equal(reply, value.ID)
|
2018-03-02 00:53:31 +00:00
|
|
|
|
|
|
|
// Read the value
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: reply,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-02 00:53:31 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal("bar", actual.SourceName)
|
2018-03-02 00:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 23:54:03 +00:00
|
|
|
func TestIntentionsSpecificDelete_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-01 23:54:03 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// The intention
|
2018-03-03 18:12:05 +00:00
|
|
|
ixn := structs.TestIntention(t)
|
|
|
|
ixn.SourceName = "foo"
|
2018-03-01 23:54:03 +00:00
|
|
|
|
|
|
|
// Create an intention directly
|
|
|
|
var reply string
|
|
|
|
{
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: ixn,
|
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Apply", &req, &reply))
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check that the intention exists
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: reply,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-01 23:54:03 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal("foo", actual.SourceName)
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the intention
|
|
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/v1/connect/intentions/%s", reply), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-06-13 10:54:27 +00:00
|
|
|
assert.Equal(true, obj)
|
2018-03-01 23:54:03 +00:00
|
|
|
|
|
|
|
// Verify the intention is gone
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: reply,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
err := a.RPC("Intention.Get", req, &resp)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "not found")
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseIntentionMatchEntry(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Expected structs.IntentionMatchEntry
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"foo",
|
|
|
|
structs.IntentionMatchEntry{
|
2020-06-26 21:59:15 +00:00
|
|
|
Name: "foo",
|
2018-03-02 22:17:21 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"foo/bar",
|
|
|
|
structs.IntentionMatchEntry{
|
|
|
|
Namespace: "foo",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"foo/bar/baz",
|
|
|
|
structs.IntentionMatchEntry{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
2018-03-01 23:54:03 +00:00
|
|
|
|
2018-03-02 22:17:21 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Input, func(t *testing.T) {
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2020-06-26 21:59:15 +00:00
|
|
|
var entMeta structs.EnterpriseMeta
|
|
|
|
actual, err := parseIntentionMatchEntry(tc.Input, &entMeta)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(err != nil, tc.Err, err)
|
2018-03-02 22:17:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(tc.Expected, actual)
|
2018-03-02 22:17:21 +00:00
|
|
|
})
|
|
|
|
}
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|