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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2018-03-02 22:17:21 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
2018-04-05 11:53:42 +00:00
|
|
|
{"foo", "*", "foo", "*"},
|
|
|
|
{"foo", "*", "foo", "bar"},
|
|
|
|
{"foo", "*", "foo", "baz"}, // shouldn't match
|
|
|
|
{"foo", "*", "bar", "bar"}, // shouldn't match
|
|
|
|
{"foo", "*", "bar", "*"}, // shouldn't match
|
|
|
|
{"foo", "*", "*", "*"},
|
|
|
|
{"bar", "*", "foo", "bar"}, // duplicate destination different source
|
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
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Apply", &ixn, &reply))
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/match?by=destination&name=foo/bar", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
value := obj.(map[string]structs.Intentions)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Len(value, 1)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
var actual [][]string
|
2018-04-05 11:53:42 +00:00
|
|
|
expected := [][]string{
|
|
|
|
{"bar", "*", "foo", "bar"},
|
|
|
|
{"foo", "*", "foo", "bar"},
|
|
|
|
{"foo", "*", "foo", "*"},
|
|
|
|
{"foo", "*", "*", "*"},
|
|
|
|
}
|
2018-03-02 22:17:21 +00:00
|
|
|
for _, ixn := range value["foo/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
|
|
|
}
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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()
|
|
|
|
|
|
|
|
require := require.New(t)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
2018-05-11 05:52:57 +00:00
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
|
|
|
{"foo", "*", "foo", "*"},
|
|
|
|
{"foo", "*", "foo", "bar"},
|
|
|
|
{"bar", "*", "foo", "bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
require.Nil(a.RPC("Intention.Apply", &ixn, &reply))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request matching intention
|
|
|
|
{
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/test?source=foo/bar&destination=foo/baz", 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.Nil(err)
|
2018-05-11 16:19:22 +00:00
|
|
|
value := obj.(*structs.IntentionQueryCheckResponse)
|
2018-05-11 05:52:57 +00:00
|
|
|
require.False(value.Allowed)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request non-matching intention
|
|
|
|
{
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/test?source=foo/bar&destination=bar/qux", 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.Nil(err)
|
2018-05-11 16:19:22 +00:00
|
|
|
value := obj.(*structs.IntentionQueryCheckResponse)
|
2018-05-11 05:52:57 +00:00
|
|
|
require.True(value.Allowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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()
|
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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)
|
2019-02-14 15:59:14 +00:00
|
|
|
a := NewTestAgent(t, t.Name(), "")
|
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{
|
2018-03-03 16:51:40 +00:00
|
|
|
Namespace: structs.IntentionDefaultNamespace,
|
|
|
|
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)
|
2018-03-02 22:17:21 +00:00
|
|
|
actual, err := parseIntentionMatchEntry(tc.Input)
|
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
|
|
|
}
|