github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/testing/match/match.go (about)

     1  // Copyright 2019-2024 The Inspektor Gadget authors
     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 match provides various helper functions for matching actual output to expected output.
    16  package match
    17  
    18  import (
    19  	"encoding/json"
    20  	"reflect"
    21  	"regexp"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func ParseMultiJSONOutput[T any](t *testing.T, output string, normalize func(*T)) []*T {
    29  	ret := []*T{}
    30  
    31  	decoder := json.NewDecoder(strings.NewReader(output))
    32  	for decoder.More() {
    33  		var entry T
    34  		if err := decoder.Decode(&entry); err != nil {
    35  			require.NoError(t, err, "decoding json")
    36  		}
    37  		// To be able to use reflect.DeepEqual and cmp.Diff, we need to
    38  		// "normalize" the output so that it only includes non-default values
    39  		// for the fields we are able to verify.
    40  		if normalize != nil {
    41  			normalize(&entry)
    42  		}
    43  
    44  		ret = append(ret, &entry)
    45  	}
    46  
    47  	return ret
    48  }
    49  
    50  func ExpectNormalizedEntriesToMatch[T any](t *testing.T, entries []*T, expectedEntries ...*T) {
    51  out:
    52  	for _, expectedEntry := range expectedEntries {
    53  		for _, entry := range entries {
    54  			if reflect.DeepEqual(expectedEntry, entry) {
    55  				continue out
    56  			}
    57  		}
    58  
    59  		var str strings.Builder
    60  
    61  		str.WriteString("output doesn't contain the expected entry\n")
    62  		str.WriteString("captured:\n")
    63  		for _, entry := range entries {
    64  			entryJson, _ := json.Marshal(entry)
    65  			str.WriteString(string(entryJson))
    66  			str.WriteString("\n")
    67  		}
    68  		expectedEntryJson, _ := json.Marshal(expectedEntry)
    69  		str.WriteString("expected:\n")
    70  		str.WriteString(string(expectedEntryJson))
    71  		t.Fatal(str.String())
    72  	}
    73  }
    74  
    75  // ExpectEntriesToMatch verifies that all the entries in expectedEntries are
    76  // matched by at least one entry in the output (Lines of independent JSON objects).
    77  func ExpectEntriesToMatch[T any](t *testing.T, output string, normalize func(*T), expectedEntries ...*T) {
    78  	entries := ParseMultiJSONOutput(t, output, normalize)
    79  	ExpectNormalizedEntriesToMatch(t, entries, expectedEntries...)
    80  }
    81  
    82  // ExpectStringToMatch verifies that the output string matches the expectedString.
    83  // This function can be directly used as ValidateOutput function.
    84  func ExpectStringToMatch(t *testing.T, expectedString string) func(t *testing.T, output string) {
    85  	return func(t *testing.T, output string) {
    86  		require.Equal(t, expectedString, output, "output didn't match the expected string")
    87  	}
    88  }
    89  
    90  // ExpectRegexpToMatch verifies that the output string matches the expected regular expression.
    91  // This function can be directly used as ValidateOutput function.
    92  func ExpectRegexpToMatch(t *testing.T, expectedRegexp string) func(t *testing.T, output string) {
    93  	return func(t *testing.T, output string) {
    94  		r := regexp.MustCompile(expectedRegexp)
    95  		if !r.MatchString(output) {
    96  			t.Fatalf("output didn't match the expected regexp: %s", expectedRegexp)
    97  		}
    98  	}
    99  }