github.com/observiq/bindplane-agent@v1.51.0/internal/report/snapshot/filter_logs_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  	"path/filepath"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
    23  	"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/plogtest"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  var unfilteredLogsDir = filepath.Join("testdata", "logs", "before")
    28  var filteredLogsDir = filepath.Join("testdata", "logs", "after")
    29  
    30  func TestFilterLogs(t *testing.T) {
    31  	testCases := []struct {
    32  		name             string
    33  		fileIn           string
    34  		query            *string
    35  		minimumTimestamp *time.Time
    36  		expectedFileOut  string
    37  	}{
    38  		{
    39  			name:            "Query matches resource attribute",
    40  			fileIn:          filepath.Join(unfilteredLogsDir, "w3c-logs.yaml"),
    41  			query:           asPtr("Brandons-Legit-Windows-PC-Not-From-Mac-I-Swear"),
    42  			expectedFileOut: filepath.Join(filteredLogsDir, "matches-resource.yaml"),
    43  		},
    44  		{
    45  			name:            "Query matches attribute",
    46  			fileIn:          filepath.Join(unfilteredLogsDir, "w3c-logs.yaml"),
    47  			query:           asPtr("unique-value"),
    48  			expectedFileOut: filepath.Join(filteredLogsDir, "matches-attribute-value.yaml"),
    49  		},
    50  		{
    51  			name:            "Query matches attribute key",
    52  			fileIn:          filepath.Join(unfilteredLogsDir, "w3c-logs.yaml"),
    53  			query:           asPtr("unique-attribute"),
    54  			expectedFileOut: filepath.Join(filteredLogsDir, "matches-attribute-key.yaml"),
    55  		},
    56  		{
    57  			name:            "Query matches field on body",
    58  			fileIn:          filepath.Join(unfilteredLogsDir, "w3c-logs.yaml"),
    59  			query:           asPtr("19.25.92.15"),
    60  			expectedFileOut: filepath.Join(filteredLogsDir, "matches-body.yaml"),
    61  		},
    62  		{
    63  			name:            "No filters",
    64  			fileIn:          filepath.Join(unfilteredLogsDir, "w3c-logs.yaml"),
    65  			expectedFileOut: filepath.Join(filteredLogsDir, "no-filters.yaml"),
    66  		},
    67  		{
    68  			name:            "Query matches string body",
    69  			fileIn:          filepath.Join(unfilteredLogsDir, "w3c-logs.yaml"),
    70  			query:           asPtr("This is a string body"),
    71  			expectedFileOut: filepath.Join(filteredLogsDir, "matches-string-body.yaml"),
    72  		},
    73  		{
    74  			name:            "Filters GET, no timestamp",
    75  			fileIn:          filepath.Join(unfilteredLogsDir, "w3c-logs.yaml"),
    76  			query:           asPtr("GET"),
    77  			expectedFileOut: filepath.Join(filteredLogsDir, "filters-get.yaml"),
    78  		},
    79  		{
    80  			name:             "Filters GET before timestamp",
    81  			fileIn:           filepath.Join(unfilteredLogsDir, "w3c-logs.yaml"),
    82  			query:            asPtr("GET"),
    83  			minimumTimestamp: asPtr(time.Unix(0, 1706632434906303999)),
    84  			expectedFileOut:  filepath.Join(filteredLogsDir, "filters-get-timestamp.yaml"),
    85  		},
    86  	}
    87  
    88  	for _, tc := range testCases {
    89  		t.Run(tc.name, func(t *testing.T) {
    90  			logsIn, err := golden.ReadLogs(tc.fileIn)
    91  			require.NoError(t, err)
    92  
    93  			logsOut := filterLogs(logsIn, tc.query, tc.minimumTimestamp)
    94  
    95  			expectedLogsOut, err := golden.ReadLogs(tc.expectedFileOut)
    96  			require.NoError(t, err)
    97  
    98  			err = plogtest.CompareLogs(expectedLogsOut, logsOut)
    99  			require.NoError(t, err)
   100  		})
   101  	}
   102  }
   103  
   104  func asPtr[T any](t T) *T {
   105  	return &t
   106  }