github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/stream/subscription_test.go (about)

     1  package stream
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/ci"
     7  	"github.com/hashicorp/nomad/nomad/structs"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestFilter_AllTopics(t *testing.T) {
    12  	ci.Parallel(t)
    13  
    14  	events := make([]structs.Event, 0, 5)
    15  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
    16  
    17  	req := &SubscribeRequest{
    18  		Topics: map[structs.Topic][]string{
    19  			"*": {"*"},
    20  		},
    21  	}
    22  	actual := filter(req, events)
    23  	require.Equal(t, events, actual)
    24  }
    25  
    26  func TestFilter_AllKeys(t *testing.T) {
    27  	ci.Parallel(t)
    28  
    29  	events := make([]structs.Event, 0, 5)
    30  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
    31  
    32  	req := &SubscribeRequest{
    33  		Topics: map[structs.Topic][]string{
    34  			"Test": {"*"},
    35  		},
    36  	}
    37  	actual := filter(req, events)
    38  	require.Equal(t, events, actual)
    39  }
    40  
    41  func TestFilter_PartialMatch_Topic(t *testing.T) {
    42  	ci.Parallel(t)
    43  
    44  	events := make([]structs.Event, 0, 5)
    45  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"}, structs.Event{Topic: "Exclude", Key: "Two"})
    46  
    47  	req := &SubscribeRequest{
    48  		Topics: map[structs.Topic][]string{
    49  			"Test": {"*"},
    50  		},
    51  	}
    52  	actual := filter(req, events)
    53  	expected := []structs.Event{{Topic: "Test", Key: "One"}, {Topic: "Test", Key: "Two"}}
    54  	require.Equal(t, expected, actual)
    55  
    56  	require.Equal(t, 2, cap(actual))
    57  }
    58  
    59  func TestFilter_Match_TopicAll_SpecificKey(t *testing.T) {
    60  	ci.Parallel(t)
    61  
    62  	events := []structs.Event{
    63  		{Topic: "Match", Key: "Two"},
    64  		{Topic: "NoMatch", Key: "One"},
    65  		{Topic: "OtherMatch", Key: "Two"},
    66  	}
    67  
    68  	req := &SubscribeRequest{
    69  		Topics: map[structs.Topic][]string{
    70  			"*": {"Two"},
    71  		},
    72  	}
    73  
    74  	actual := filter(req, events)
    75  	expected := []structs.Event{
    76  		{Topic: "Match", Key: "Two"},
    77  		{Topic: "OtherMatch", Key: "Two"},
    78  	}
    79  	require.Equal(t, expected, actual)
    80  }
    81  
    82  func TestFilter_Match_TopicAll_SpecificKey_Plus(t *testing.T) {
    83  	ci.Parallel(t)
    84  
    85  	events := []structs.Event{
    86  		{Topic: "FirstTwo", Key: "Two"},
    87  		{Topic: "Test", Key: "One"},
    88  		{Topic: "SecondTwo", Key: "Two"},
    89  	}
    90  
    91  	req := &SubscribeRequest{
    92  		Topics: map[structs.Topic][]string{
    93  			"*":    {"Two"},
    94  			"Test": {"One"},
    95  		},
    96  	}
    97  
    98  	actual := filter(req, events)
    99  	expected := []structs.Event{
   100  		{Topic: "FirstTwo", Key: "Two"},
   101  		{Topic: "Test", Key: "One"},
   102  		{Topic: "SecondTwo", Key: "Two"},
   103  	}
   104  	require.Equal(t, expected, actual)
   105  }
   106  
   107  func TestFilter_PartialMatch_Key(t *testing.T) {
   108  	ci.Parallel(t)
   109  
   110  	events := make([]structs.Event, 0, 5)
   111  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
   112  
   113  	req := &SubscribeRequest{
   114  		Topics: map[structs.Topic][]string{
   115  			"Test": {"One"},
   116  		},
   117  	}
   118  	actual := filter(req, events)
   119  	expected := []structs.Event{{Topic: "Test", Key: "One"}}
   120  	require.Equal(t, expected, actual)
   121  
   122  	require.Equal(t, 1, cap(actual))
   123  }
   124  
   125  func TestFilter_NoMatch(t *testing.T) {
   126  	ci.Parallel(t)
   127  
   128  	events := make([]structs.Event, 0, 5)
   129  	events = append(events, structs.Event{Topic: "Test", Key: "One"}, structs.Event{Topic: "Test", Key: "Two"})
   130  
   131  	req := &SubscribeRequest{
   132  		Topics: map[structs.Topic][]string{
   133  			"NodeEvents": {"*"},
   134  			"Test":       {"Highly-Specific-Key"},
   135  		},
   136  	}
   137  	actual := filter(req, events)
   138  	var expected []structs.Event
   139  	require.Equal(t, expected, actual)
   140  
   141  	require.Equal(t, 0, cap(actual))
   142  }
   143  
   144  func TestFilter_Namespace(t *testing.T) {
   145  	ci.Parallel(t)
   146  
   147  	events := make([]structs.Event, 0, 5)
   148  	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"})
   149  
   150  	req := &SubscribeRequest{
   151  		Topics: map[structs.Topic][]string{
   152  			"*": {"*"},
   153  		},
   154  		Namespace: "foo",
   155  	}
   156  	actual := filter(req, events)
   157  	expected := []structs.Event{
   158  		{Topic: "Test", Key: "One", Namespace: "foo"},
   159  		{Topic: "Test", Key: "Two"},
   160  	}
   161  	require.Equal(t, expected, actual)
   162  
   163  	require.Equal(t, 2, cap(actual))
   164  }
   165  
   166  func TestFilter_NamespaceAll(t *testing.T) {
   167  	ci.Parallel(t)
   168  
   169  	events := make([]structs.Event, 0, 5)
   170  	events = append(events,
   171  		structs.Event{Topic: "Test", Key: "One", Namespace: "foo"},
   172  		structs.Event{Topic: "Test", Key: "Two", Namespace: "bar"},
   173  		structs.Event{Topic: "Test", Key: "Three", Namespace: "default"},
   174  	)
   175  
   176  	req := &SubscribeRequest{
   177  		Topics: map[structs.Topic][]string{
   178  			"*": {"*"},
   179  		},
   180  		Namespace: "*",
   181  	}
   182  	actual := filter(req, events)
   183  	expected := []structs.Event{
   184  		{Topic: "Test", Key: "One", Namespace: "foo"},
   185  		{Topic: "Test", Key: "Two", Namespace: "bar"},
   186  		{Topic: "Test", Key: "Three", Namespace: "default"},
   187  	}
   188  	require.Equal(t, expected, actual)
   189  }
   190  
   191  func TestFilter_FilterKeys(t *testing.T) {
   192  	ci.Parallel(t)
   193  
   194  	events := make([]structs.Event, 0, 5)
   195  	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"})
   196  
   197  	req := &SubscribeRequest{
   198  		Topics: map[structs.Topic][]string{
   199  			"Test": {"extra-key"},
   200  		},
   201  		Namespace: "foo",
   202  	}
   203  	actual := filter(req, events)
   204  	expected := []structs.Event{
   205  		{Topic: "Test", Key: "One", FilterKeys: []string{"extra-key"}},
   206  	}
   207  	require.Equal(t, expected, actual)
   208  
   209  	require.Equal(t, 1, cap(actual))
   210  }