github.com/hernad/nomad@v1.6.112/nomad/stream/subscription_test.go (about)

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