open-consul/api/connect_intention_test.go

190 lines
3.8 KiB
Go
Raw Normal View History

package api
import (
"testing"
"github.com/stretchr/testify/require"
)
2018-05-16 15:53:33 +00:00
func TestAPI_ConnectIntentionCreateListGetUpdateDelete(t *testing.T) {
t.Parallel()
require := require.New(t)
c, s := makeClient(t)
defer s.Stop()
connect := c.Connect()
// Create
ixn := testIntention()
id, _, err := connect.IntentionCreate(ixn, nil)
require.Nil(err)
require.NotEmpty(id)
// List it
list, _, err := connect.Intentions(nil)
require.Nil(err)
require.Len(list, 1)
actual := list[0]
ixn.ID = id
ixn.CreatedAt = actual.CreatedAt
ixn.UpdatedAt = actual.UpdatedAt
ixn.CreateIndex = actual.CreateIndex
ixn.ModifyIndex = actual.ModifyIndex
ixn.Hash = actual.Hash
require.Equal(ixn, actual)
2018-03-28 17:14:32 +00:00
// Get it
actual, _, err = connect.IntentionGet(id, nil)
require.Nil(err)
require.Equal(ixn, actual)
2018-05-12 05:19:21 +00:00
2018-05-16 15:53:33 +00:00
// Update it
ixn.SourceNS = ixn.SourceNS + "-different"
_, err = connect.IntentionUpdate(ixn, nil)
require.NoError(err)
// Get it
actual, _, err = connect.IntentionGet(id, nil)
require.NoError(err)
ixn.UpdatedAt = actual.UpdatedAt
ixn.ModifyIndex = actual.ModifyIndex
ixn.Hash = actual.Hash
2018-05-16 15:53:33 +00:00
require.Equal(ixn, actual)
2018-05-12 05:19:21 +00:00
// Delete it
_, err = connect.IntentionDelete(id, nil)
require.Nil(err)
// Get it (should be gone)
actual, _, err = connect.IntentionGet(id, nil)
require.Nil(err)
require.Nil(actual)
2018-03-28 17:14:32 +00:00
}
func TestAPI_ConnectIntentionGet_invalidId(t *testing.T) {
t.Parallel()
require := require.New(t)
c, s := makeClient(t)
defer s.Stop()
connect := c.Connect()
// Get it
actual, _, err := connect.IntentionGet("hello", nil)
require.Nil(actual)
require.Error(err)
require.Contains(err.Error(), "UUID") // verify it contains the message
}
2018-03-28 17:14:32 +00:00
func TestAPI_ConnectIntentionMatch(t *testing.T) {
t.Parallel()
require := require.New(t)
c, s := makeClient(t)
defer s.Stop()
connect := c.Connect()
// Create
{
insert := [][]string{
{"foo", "*"},
{"foo", "bar"},
{"foo", "baz"}, // shouldn't match
{"bar", "bar"}, // shouldn't match
{"bar", "*"}, // shouldn't match
{"*", "*"},
}
for _, v := range insert {
ixn := testIntention()
ixn.DestinationNS = v[0]
ixn.DestinationName = v[1]
id, _, err := connect.IntentionCreate(ixn, nil)
require.Nil(err)
require.NotEmpty(id)
}
}
// Match it
result, _, err := connect.IntentionMatch(&IntentionMatch{
By: IntentionMatchDestination,
Names: []string{"foo/bar"},
}, nil)
require.Nil(err)
require.Len(result, 1)
var actual [][]string
expected := [][]string{{"foo", "bar"}, {"foo", "*"}, {"*", "*"}}
for _, ixn := range result["foo/bar"] {
actual = append(actual, []string{ixn.DestinationNS, ixn.DestinationName})
}
require.Equal(expected, actual)
}
2018-05-12 00:19:54 +00:00
func TestAPI_ConnectIntentionCheck(t *testing.T) {
t.Parallel()
require := require.New(t)
c, s := makeClient(t)
defer s.Stop()
connect := c.Connect()
// Create
{
insert := [][]string{
{"foo", "*", "foo", "bar"},
}
for _, v := range insert {
ixn := testIntention()
ixn.SourceNS = v[0]
ixn.SourceName = v[1]
ixn.DestinationNS = v[2]
ixn.DestinationName = v[3]
ixn.Action = IntentionActionDeny
id, _, err := connect.IntentionCreate(ixn, nil)
require.Nil(err)
require.NotEmpty(id)
}
}
// Match it
{
result, _, err := connect.IntentionCheck(&IntentionCheck{
Source: "foo/qux",
Destination: "foo/bar",
}, nil)
require.Nil(err)
require.False(result)
}
// Match it (non-matching)
{
result, _, err := connect.IntentionCheck(&IntentionCheck{
Source: "bar/qux",
Destination: "foo/bar",
}, nil)
require.Nil(err)
require.True(result)
}
}
func testIntention() *Intention {
return &Intention{
SourceNS: "eng",
SourceName: "api",
DestinationNS: "eng",
DestinationName: "db",
Precedence: 9,
Action: IntentionActionAllow,
SourceType: IntentionSourceConsul,
Meta: map[string]string{},
}
}