101 lines
2 KiB
Go
101 lines
2 KiB
Go
package api
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestAPI_ConnectIntentionCreateListGet(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
|
|
require.Equal(ixn, actual)
|
|
|
|
// Get it
|
|
actual, _, err = connect.IntentionGet(id, nil)
|
|
require.Nil(err)
|
|
require.Equal(ixn, actual)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func testIntention() *Intention {
|
|
return &Intention{
|
|
SourceNS: "eng",
|
|
SourceName: "api",
|
|
DestinationNS: "eng",
|
|
DestinationName: "db",
|
|
Action: IntentionActionAllow,
|
|
SourceType: IntentionSourceConsul,
|
|
Meta: map[string]string{},
|
|
}
|
|
}
|