github.com/observiq/bindplane-agent@v1.51.0/internal/report/snapshot/filter_test.go (about)

     1  // Copyright observIQ, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package snapshot
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  	"go.opentelemetry.io/collector/pdata/pcommon"
    22  )
    23  
    24  func TestQueryMatchesValue(t *testing.T) {
    25  	testCases := []struct {
    26  		name           string
    27  		valFunc        func(t *testing.T) pcommon.Value
    28  		query          string
    29  		expectedOutput bool
    30  	}{
    31  		{
    32  			name:           "Matches string value",
    33  			valFunc:        func(_ *testing.T) pcommon.Value { return pcommon.NewValueStr("String") },
    34  			query:          "String",
    35  			expectedOutput: true,
    36  		},
    37  		{
    38  			name:           "Does not match string value (Case sensitive)",
    39  			valFunc:        func(_ *testing.T) pcommon.Value { return pcommon.NewValueStr("String") },
    40  			query:          "string",
    41  			expectedOutput: false,
    42  		},
    43  		{
    44  			name:           "Match string value (substring)",
    45  			valFunc:        func(_ *testing.T) pcommon.Value { return pcommon.NewValueStr("String") },
    46  			query:          "rin",
    47  			expectedOutput: true,
    48  		},
    49  		{
    50  			name: "Value is map (matches)",
    51  			valFunc: func(t *testing.T) pcommon.Value {
    52  				m := pcommon.NewValueMap()
    53  				err := m.Map().FromRaw(map[string]any{
    54  					"Key":        "Value",
    55  					"AnotherKey": "AnotherValue",
    56  				})
    57  				require.NoError(t, err)
    58  
    59  				return m
    60  			},
    61  			query:          "AnotherVal",
    62  			expectedOutput: true,
    63  		},
    64  		{
    65  			name: "Value is map (does not match)",
    66  			valFunc: func(t *testing.T) pcommon.Value {
    67  				m := pcommon.NewValueMap()
    68  				err := m.Map().FromRaw(map[string]any{
    69  					"Key":        "Value",
    70  					"AnotherKey": "AnotherValue",
    71  				})
    72  				require.NoError(t, err)
    73  
    74  				return m
    75  			},
    76  			query:          "aaaaa",
    77  			expectedOutput: false,
    78  		},
    79  		{
    80  			name: "Value is slice (matches)",
    81  			valFunc: func(t *testing.T) pcommon.Value {
    82  				s := pcommon.NewValueSlice()
    83  				err := s.Slice().FromRaw([]any{"Thing1", "Thing2", 34})
    84  				require.NoError(t, err)
    85  				return s
    86  			},
    87  			query:          "Th",
    88  			expectedOutput: true,
    89  		},
    90  		{
    91  			name: "Value is slice (does not match)",
    92  			valFunc: func(t *testing.T) pcommon.Value {
    93  				s := pcommon.NewValueSlice()
    94  				err := s.Slice().FromRaw([]any{"Thing1", "Thing2", 34})
    95  				require.NoError(t, err)
    96  				return s
    97  			},
    98  			query:          "DNE",
    99  			expectedOutput: false,
   100  		},
   101  		{
   102  			name: "Value is empty",
   103  			valFunc: func(_ *testing.T) pcommon.Value {
   104  				return pcommon.NewValueEmpty()
   105  			},
   106  			query:          "",
   107  			expectedOutput: false,
   108  		},
   109  		{
   110  			name: "Value is int",
   111  			valFunc: func(_ *testing.T) pcommon.Value {
   112  				return pcommon.NewValueInt(1345)
   113  			},
   114  			query:          "134",
   115  			expectedOutput: true,
   116  		},
   117  		{
   118  			name: "Value is double",
   119  			valFunc: func(_ *testing.T) pcommon.Value {
   120  				return pcommon.NewValueDouble(1452.25)
   121  			},
   122  			query:          "452.25",
   123  			expectedOutput: true,
   124  		},
   125  	}
   126  
   127  	for _, tc := range testCases {
   128  		t.Run(tc.name, func(t *testing.T) {
   129  			o := queryMatchesValue(tc.valFunc(t), tc.query)
   130  			require.Equal(t, tc.expectedOutput, o)
   131  		})
   132  	}
   133  }
   134  
   135  func TestQueryMatchesMap(t *testing.T) {
   136  	testCases := []struct {
   137  		name           string
   138  		mapFunc        func(t *testing.T) pcommon.Map
   139  		query          string
   140  		expectedOutput bool
   141  	}{
   142  		{
   143  			name: "Matches key",
   144  			mapFunc: func(t *testing.T) pcommon.Map {
   145  				m := pcommon.NewMap()
   146  				err := m.FromRaw(map[string]any{
   147  					"Key":        "Value",
   148  					"AnotherKey": "AnotherValue",
   149  				})
   150  				require.NoError(t, err)
   151  
   152  				return m
   153  			},
   154  			query:          "AnotherKey",
   155  			expectedOutput: true,
   156  		},
   157  		{
   158  			name: "Matches subset of key",
   159  			mapFunc: func(t *testing.T) pcommon.Map {
   160  				m := pcommon.NewMap()
   161  				err := m.FromRaw(map[string]any{
   162  					"Key":        "Value",
   163  					"AnotherKey": "AnotherValue",
   164  				})
   165  				require.NoError(t, err)
   166  
   167  				return m
   168  			},
   169  			query:          "herK",
   170  			expectedOutput: true,
   171  		},
   172  		{
   173  			name: "Key is substring of query",
   174  			mapFunc: func(t *testing.T) pcommon.Map {
   175  				m := pcommon.NewMap()
   176  				err := m.FromRaw(map[string]any{
   177  					"Key":        "Value",
   178  					"AnotherKey": "AnotherValue",
   179  				})
   180  				require.NoError(t, err)
   181  
   182  				return m
   183  			},
   184  			query:          "ThisIsAnotherKeyVeryLong",
   185  			expectedOutput: false,
   186  		},
   187  		{
   188  			name: "Matches string value",
   189  			mapFunc: func(t *testing.T) pcommon.Map {
   190  				m := pcommon.NewMap()
   191  				err := m.FromRaw(map[string]any{
   192  					"Key":        "Value",
   193  					"AnotherKey": "AnotherValue",
   194  				})
   195  				require.NoError(t, err)
   196  
   197  				return m
   198  			},
   199  			query:          "Val",
   200  			expectedOutput: true,
   201  		},
   202  		{
   203  			name: "Matches int value",
   204  			mapFunc: func(t *testing.T) pcommon.Map {
   205  				m := pcommon.NewMap()
   206  				err := m.FromRaw(map[string]any{
   207  					"Key":        123,
   208  					"AnotherKey": "AnotherValue",
   209  				})
   210  				require.NoError(t, err)
   211  
   212  				return m
   213  			},
   214  			query:          "123",
   215  			expectedOutput: true,
   216  		},
   217  		{
   218  			name: "Matches value in nested map",
   219  			mapFunc: func(t *testing.T) pcommon.Map {
   220  				m := pcommon.NewMap()
   221  				err := m.FromRaw(map[string]any{
   222  					"Key": map[string]any{
   223  						"Nested": "FindMeIfYouCan",
   224  					},
   225  					"AnotherKey": "AnotherValue",
   226  				})
   227  				require.NoError(t, err)
   228  
   229  				return m
   230  			},
   231  			query:          "FindMeIfYouCan",
   232  			expectedOutput: true,
   233  		},
   234  		{
   235  			name: "Matches value in nested slice",
   236  			mapFunc: func(t *testing.T) pcommon.Map {
   237  				m := pcommon.NewMap()
   238  				err := m.FromRaw(map[string]any{
   239  					"Key":        []any{"FindMeIfYouCan"},
   240  					"AnotherKey": "AnotherValue",
   241  				})
   242  				require.NoError(t, err)
   243  
   244  				return m
   245  			},
   246  			query:          "FindMeIfYouCan",
   247  			expectedOutput: true,
   248  		},
   249  	}
   250  
   251  	for _, tc := range testCases {
   252  		t.Run(tc.name, func(t *testing.T) {
   253  			o := queryMatchesMap(tc.mapFunc(t), tc.query)
   254  			require.Equal(t, tc.expectedOutput, o)
   255  		})
   256  	}
   257  }
   258  
   259  func TestQueryMatchesSlice(t *testing.T) {
   260  	testCases := []struct {
   261  		name           string
   262  		sliceFunc      func(t *testing.T) pcommon.Slice
   263  		query          string
   264  		expectedOutput bool
   265  	}{
   266  		{
   267  			name: "Empty Slice",
   268  			sliceFunc: func(t *testing.T) pcommon.Slice {
   269  				s := pcommon.NewSlice()
   270  				err := s.FromRaw([]any{})
   271  				require.NoError(t, err)
   272  				return s
   273  			},
   274  			query:          "",
   275  			expectedOutput: false,
   276  		},
   277  		{
   278  			name: "Matches element in slice",
   279  			sliceFunc: func(t *testing.T) pcommon.Slice {
   280  				s := pcommon.NewSlice()
   281  				err := s.FromRaw([]any{"Elem1", "Elem2", "Elem3"})
   282  				require.NoError(t, err)
   283  				return s
   284  			},
   285  			query:          "Elem2",
   286  			expectedOutput: true,
   287  		},
   288  		{
   289  			name: "Matches element in nested slice",
   290  			sliceFunc: func(t *testing.T) pcommon.Slice {
   291  				s := pcommon.NewSlice()
   292  				err := s.FromRaw([]any{"Elem1", []any{"Elem2"}, "Elem3"})
   293  				require.NoError(t, err)
   294  				return s
   295  			},
   296  			query:          "Elem2",
   297  			expectedOutput: true,
   298  		},
   299  		{
   300  			name: "Matches element in nested map",
   301  			sliceFunc: func(t *testing.T) pcommon.Slice {
   302  				s := pcommon.NewSlice()
   303  				err := s.FromRaw([]any{"Elem1", map[string]any{
   304  					"SomeKey": "Elem2",
   305  				}, "Elem3"})
   306  				require.NoError(t, err)
   307  				return s
   308  			},
   309  			query:          "SomeKey",
   310  			expectedOutput: true,
   311  		},
   312  		{
   313  			name: "Does not match any element in slice",
   314  			sliceFunc: func(t *testing.T) pcommon.Slice {
   315  				s := pcommon.NewSlice()
   316  				err := s.FromRaw([]any{"Elem1", "Elem2", "Elem3"})
   317  				require.NoError(t, err)
   318  				return s
   319  			},
   320  			query:          "Does Not Match",
   321  			expectedOutput: false,
   322  		},
   323  	}
   324  
   325  	for _, tc := range testCases {
   326  		t.Run(tc.name, func(t *testing.T) {
   327  			o := queryMatchesSlice(tc.sliceFunc(t), tc.query)
   328  			require.Equal(t, tc.expectedOutput, o)
   329  		})
   330  	}
   331  }