2020-08-31 17:19:28 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-10-08 15:57:21 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
|
2020-08-31 17:19:28 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-09-28 14:13:10 +00:00
|
|
|
func TestFilter_AllTopics(t *testing.T) {
|
2020-10-04 19:12:35 +00:00
|
|
|
events := make([]structs.Event, 0, 5)
|
|
|
|
events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
|
2020-09-28 14:13:10 +00:00
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
2020-10-04 19:12:35 +00:00
|
|
|
Topics: map[structs.Topic][]string{
|
2020-10-08 15:57:21 +00:00
|
|
|
"*": {"*"},
|
2020-09-28 14:13:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := filter(req, events)
|
|
|
|
require.Equal(t, events, actual)
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:19:28 +00:00
|
|
|
func TestFilter_AllKeys(t *testing.T) {
|
2020-10-04 19:12:35 +00:00
|
|
|
events := make([]structs.Event, 0, 5)
|
|
|
|
events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
|
2020-08-31 17:19:28 +00:00
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
2020-10-04 19:12:35 +00:00
|
|
|
Topics: map[structs.Topic][]string{
|
2020-10-08 15:57:21 +00:00
|
|
|
"Test": {"*"},
|
2020-08-31 17:19:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := filter(req, events)
|
|
|
|
require.Equal(t, events, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_PartialMatch_Topic(t *testing.T) {
|
2020-10-04 19:12:35 +00:00
|
|
|
events := make([]structs.Event, 0, 5)
|
|
|
|
events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"}, structs.Event{Topic: "Exclude", Key: "Two"})
|
2020-08-31 17:19:28 +00:00
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
2020-10-04 19:12:35 +00:00
|
|
|
Topics: map[structs.Topic][]string{
|
2020-10-08 15:57:21 +00:00
|
|
|
"Test": {"*"},
|
2020-08-31 17:19:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := filter(req, events)
|
2020-10-04 19:12:35 +00:00
|
|
|
expected := []structs.Event{{Topic: "Test", Key: "One"}, {Topic: "Test", Key: "Two"}}
|
2020-08-31 17:19:28 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
require.Equal(t, 2, cap(actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_Match_TopicAll_SpecificKey(t *testing.T) {
|
|
|
|
events := []structs.Event{
|
|
|
|
{Topic: "Match", Key: "Two"},
|
|
|
|
{Topic: "NoMatch", Key: "One"},
|
|
|
|
{Topic: "OtherMatch", Key: "Two"},
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topics: map[structs.Topic][]string{
|
|
|
|
"*": {"Two"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := filter(req, events)
|
|
|
|
expected := []structs.Event{
|
|
|
|
{Topic: "Match", Key: "Two"},
|
|
|
|
{Topic: "OtherMatch", Key: "Two"},
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_Match_TopicAll_SpecificKey_Plus(t *testing.T) {
|
|
|
|
events := []structs.Event{
|
|
|
|
{Topic: "FirstTwo", Key: "Two"},
|
|
|
|
{Topic: "Test", Key: "One"},
|
|
|
|
{Topic: "SecondTwo", Key: "Two"},
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topics: map[structs.Topic][]string{
|
|
|
|
"*": {"Two"},
|
|
|
|
"Test": {"One"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := filter(req, events)
|
|
|
|
expected := []structs.Event{
|
|
|
|
{Topic: "FirstTwo", Key: "Two"},
|
|
|
|
{Topic: "Test", Key: "One"},
|
|
|
|
{Topic: "SecondTwo", Key: "Two"},
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, actual)
|
2020-08-31 17:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_PartialMatch_Key(t *testing.T) {
|
2020-10-04 19:12:35 +00:00
|
|
|
events := make([]structs.Event, 0, 5)
|
|
|
|
events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
|
2020-08-31 17:19:28 +00:00
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
2020-10-04 19:12:35 +00:00
|
|
|
Topics: map[structs.Topic][]string{
|
2020-10-08 15:57:21 +00:00
|
|
|
"Test": {"One"},
|
2020-08-31 17:19:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := filter(req, events)
|
2020-10-04 19:12:35 +00:00
|
|
|
expected := []structs.Event{{Topic: "Test", Key: "One"}}
|
2020-08-31 17:19:28 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
require.Equal(t, 1, cap(actual))
|
2020-08-31 17:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilter_NoMatch(t *testing.T) {
|
2020-10-04 19:12:35 +00:00
|
|
|
events := make([]structs.Event, 0, 5)
|
|
|
|
events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
|
2020-08-31 17:19:28 +00:00
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
2020-10-04 19:12:35 +00:00
|
|
|
Topics: map[structs.Topic][]string{
|
2020-10-08 15:57:21 +00:00
|
|
|
"NodeEvents": {"*"},
|
|
|
|
"Test": {"Highly-Specific-Key"},
|
2020-08-31 17:19:28 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := filter(req, events)
|
2020-10-04 19:12:35 +00:00
|
|
|
var expected []structs.Event
|
2020-08-31 17:19:28 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
require.Equal(t, 0, cap(actual))
|
2020-08-31 17:19:28 +00:00
|
|
|
}
|
2020-10-08 15:57:21 +00:00
|
|
|
|
|
|
|
func TestFilter_Namespace(t *testing.T) {
|
|
|
|
events := make([]structs.Event, 0, 5)
|
|
|
|
events = append(events, structs.Event{Topic: "Test", Key: "One", Namespace: "foo"}, structs.Event{Topic: "Test", Key: "Two"}, structs.Event{Topic: "Test", Key: "Two", Namespace: "bar"})
|
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topics: map[structs.Topic][]string{
|
|
|
|
"*": {"*"},
|
|
|
|
},
|
|
|
|
Namespace: "foo",
|
|
|
|
}
|
|
|
|
actual := filter(req, events)
|
|
|
|
expected := []structs.Event{
|
|
|
|
{Topic: "Test", Key: "One", Namespace: "foo"},
|
|
|
|
{Topic: "Test", Key: "Two"},
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
require.Equal(t, 2, cap(actual))
|
2020-10-08 15:57:21 +00:00
|
|
|
}
|
2020-10-08 18:27:52 +00:00
|
|
|
|
|
|
|
func TestFilter_FilterKeys(t *testing.T) {
|
|
|
|
events := make([]structs.Event, 0, 5)
|
|
|
|
events = append(events, structs.Event{Topic: "Test", Key: "One", FilterKeys: []string{"extra-key"}}, structs.Event{Topic: "Test", Key: "Two"}, structs.Event{Topic: "Test", Key: "Two"})
|
|
|
|
|
|
|
|
req := &SubscribeRequest{
|
|
|
|
Topics: map[structs.Topic][]string{
|
|
|
|
"Test": {"extra-key"},
|
|
|
|
},
|
|
|
|
Namespace: "foo",
|
|
|
|
}
|
|
|
|
actual := filter(req, events)
|
|
|
|
expected := []structs.Event{
|
|
|
|
{Topic: "Test", Key: "One", FilterKeys: []string{"extra-key"}},
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
2020-10-30 17:07:38 +00:00
|
|
|
require.Equal(t, 1, cap(actual))
|
2020-10-08 18:27:52 +00:00
|
|
|
}
|