github.com/cilium/cilium@v1.16.2/pkg/hubble/exporter/api_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package exporter
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  
    11  	"github.com/cilium/cilium/api/v1/flow"
    12  	"github.com/cilium/cilium/pkg/time"
    13  )
    14  
    15  func TestCompareFlowLogConfigs(t *testing.T) {
    16  	now := time.Now()
    17  	future := now.Add(time.Minute)
    18  
    19  	cases := []struct {
    20  		name          string
    21  		currentConfig *FlowLogConfig
    22  		newConfig     *FlowLogConfig
    23  		expectEqual   bool
    24  	}{
    25  		{
    26  			name:          "should equal for same path",
    27  			currentConfig: &FlowLogConfig{FilePath: "path"},
    28  			newConfig:     &FlowLogConfig{FilePath: "path"},
    29  			expectEqual:   true,
    30  		},
    31  		{
    32  			name:          "should not equal for different path",
    33  			currentConfig: &FlowLogConfig{FilePath: "path"},
    34  			newConfig:     &FlowLogConfig{FilePath: "other"},
    35  			expectEqual:   false,
    36  		},
    37  		{
    38  			name:          "should equal for same end date",
    39  			currentConfig: &FlowLogConfig{End: &now},
    40  			newConfig:     &FlowLogConfig{End: &now},
    41  			expectEqual:   true,
    42  		},
    43  		{
    44  			name:          "should not equal for different end date",
    45  			currentConfig: &FlowLogConfig{End: &now},
    46  			newConfig:     &FlowLogConfig{End: &future},
    47  			expectEqual:   false,
    48  		},
    49  		{
    50  			name:          "should equal for same fieldmask",
    51  			currentConfig: &FlowLogConfig{FieldMask: []string{"a", "b"}},
    52  			newConfig:     &FlowLogConfig{FieldMask: []string{"a", "b"}},
    53  			expectEqual:   true,
    54  		},
    55  		{
    56  			name:          "should equal for same fieldmask in different order",
    57  			currentConfig: &FlowLogConfig{FieldMask: []string{"a", "b"}},
    58  			newConfig:     &FlowLogConfig{FieldMask: []string{"b", "a"}},
    59  			expectEqual:   true,
    60  		},
    61  		{
    62  			name:          "should not equal for different fieldmask",
    63  			currentConfig: &FlowLogConfig{FieldMask: []string{"a", "b"}},
    64  			newConfig:     &FlowLogConfig{FieldMask: []string{"c", "b"}},
    65  			expectEqual:   false,
    66  		},
    67  		{
    68  			name: "should equal for same include filters in different order",
    69  			currentConfig: &FlowLogConfig{IncludeFilters: FlowFilters{
    70  				{
    71  					SourcePod: []string{"default/"},
    72  					EventType: []*flow.EventTypeFilter{
    73  						{Type: 1},
    74  					},
    75  				},
    76  				{
    77  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
    78  				},
    79  			}},
    80  			newConfig: &FlowLogConfig{IncludeFilters: FlowFilters{
    81  				{
    82  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
    83  				},
    84  				{
    85  					EventType: []*flow.EventTypeFilter{
    86  						{Type: 1},
    87  					},
    88  					SourcePod: []string{"default/"},
    89  				},
    90  			}},
    91  			expectEqual: true,
    92  		},
    93  		{
    94  			name: "should not equal for different include filters",
    95  			currentConfig: &FlowLogConfig{IncludeFilters: FlowFilters{
    96  				{
    97  					SourcePod: []string{"kube-system/"},
    98  					EventType: []*flow.EventTypeFilter{
    99  						{Type: 1},
   100  					},
   101  				},
   102  				{
   103  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   104  				},
   105  			}},
   106  			newConfig: &FlowLogConfig{IncludeFilters: FlowFilters{
   107  				{
   108  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   109  				},
   110  				{
   111  					EventType: []*flow.EventTypeFilter{
   112  						{Type: 1},
   113  					},
   114  					SourcePod: []string{"default/"},
   115  				},
   116  			}},
   117  			expectEqual: false,
   118  		},
   119  		{
   120  			name: "should equal for same exclude filters in different order",
   121  			currentConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   122  				{
   123  					SourcePod: []string{"default/"},
   124  					EventType: []*flow.EventTypeFilter{
   125  						{Type: 1},
   126  					},
   127  				},
   128  				{
   129  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   130  				},
   131  			}},
   132  			newConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   133  				{
   134  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   135  				},
   136  				{
   137  					EventType: []*flow.EventTypeFilter{
   138  						{Type: 1},
   139  					},
   140  					SourcePod: []string{"default/"},
   141  				},
   142  			}},
   143  			expectEqual: true,
   144  		},
   145  		{
   146  			name: "should not equal for different exclude filters",
   147  			currentConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   148  				{
   149  					SourcePod: []string{"kube-system/"},
   150  					EventType: []*flow.EventTypeFilter{
   151  						{Type: 1},
   152  					},
   153  				},
   154  				{
   155  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   156  				},
   157  			}},
   158  			newConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   159  				{
   160  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   161  				},
   162  				{
   163  					EventType: []*flow.EventTypeFilter{
   164  						{Type: 1},
   165  					},
   166  					SourcePod: []string{"default/"},
   167  				},
   168  			}},
   169  			expectEqual: false,
   170  		},
   171  		{
   172  			name:        "should equal for null current config and null new config",
   173  			expectEqual: true,
   174  		},
   175  		{
   176  			name:        "should not equal for null current config and not null new config",
   177  			newConfig:   &FlowLogConfig{},
   178  			expectEqual: false,
   179  		},
   180  		{
   181  			name:          "should not equal for not null current config and null new config",
   182  			currentConfig: &FlowLogConfig{},
   183  			expectEqual:   false,
   184  		},
   185  		{
   186  			name: "should not equal when current filters are nil",
   187  			currentConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   188  				nil,
   189  				nil,
   190  			}},
   191  			newConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   192  				{
   193  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   194  				},
   195  				{
   196  					EventType: []*flow.EventTypeFilter{
   197  						{Type: 1},
   198  					},
   199  					SourcePod: []string{"default/"},
   200  				},
   201  			}},
   202  			expectEqual: false,
   203  		},
   204  		{
   205  			name: "should not equal when new filters are nil",
   206  			currentConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   207  				{
   208  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   209  				},
   210  				{
   211  					EventType: []*flow.EventTypeFilter{
   212  						{Type: 1},
   213  					},
   214  					SourcePod: []string{"default/"},
   215  				},
   216  			}},
   217  			newConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   218  				nil,
   219  			}},
   220  			expectEqual: false,
   221  		},
   222  		{
   223  			name: "should equal when current and new filters are nil",
   224  			currentConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   225  				nil,
   226  			}},
   227  			newConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   228  				nil,
   229  			}},
   230  			expectEqual: true,
   231  		},
   232  		{
   233  			name: "should equal when current and new filters have nils",
   234  			currentConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   235  				nil,
   236  				{
   237  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   238  				},
   239  			}},
   240  			newConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   241  				{
   242  					DestinationPod: []string{"frontend/nginx-975996d4c-7hhgt"},
   243  				},
   244  				nil,
   245  			}},
   246  			expectEqual: true,
   247  		},
   248  		{
   249  			name: "should equal when nil is substituted by empty instance",
   250  			currentConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   251  				{
   252  					TcpFlags: []*flow.TCPFlags{nil},
   253  				},
   254  			}},
   255  			newConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   256  				{
   257  					TcpFlags: []*flow.TCPFlags{{}},
   258  				},
   259  			}},
   260  			expectEqual: true,
   261  		},
   262  		{
   263  			name: "should equal when empty instance is substituted by nil",
   264  			currentConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   265  				{
   266  					TcpFlags: []*flow.TCPFlags{{}},
   267  				},
   268  			}},
   269  			newConfig: &FlowLogConfig{ExcludeFilters: FlowFilters{
   270  				{
   271  					TcpFlags: []*flow.TCPFlags{nil},
   272  				},
   273  			}},
   274  			expectEqual: true,
   275  		},
   276  	}
   277  
   278  	for _, tc := range cases {
   279  		t.Run(tc.name, func(t *testing.T) {
   280  			result := tc.currentConfig.equals(tc.newConfig)
   281  			assert.Equal(t, tc.expectEqual, result)
   282  		})
   283  	}
   284  }