github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/logging/logconfig/filter_test.go (about)

     1  package logconfig
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestBuildKeyValuesPredicateMatchAll(t *testing.T) {
    10  	conf := []*KeyValuePredicateConfig{
    11  		{
    12  			KeyRegex:   "Foo",
    13  			ValueRegex: "bar",
    14  		},
    15  	}
    16  	kvp, err := BuildKeyValuesPredicate(conf, true)
    17  	assert.NoError(t, err)
    18  	assert.True(t, kvp([]interface{}{"Foo", "bar", "Bosh", "Bish"}))
    19  }
    20  
    21  func TestBuildKeyValuesPredicateMatchAny(t *testing.T) {
    22  	conf := []*KeyValuePredicateConfig{
    23  		{
    24  			KeyRegex:   "Bosh",
    25  			ValueRegex: "Bish",
    26  		},
    27  	}
    28  	kvp, err := BuildKeyValuesPredicate(conf, false)
    29  	assert.NoError(t, err)
    30  	assert.True(t, kvp([]interface{}{"Foo", "bar", "Bosh", "Bish"}))
    31  }
    32  
    33  func TestExcludeAllFilterPredicate(t *testing.T) {
    34  	fc := &FilterConfig{
    35  		FilterMode: ExcludeWhenAllMatch,
    36  		Predicates: []*KeyValuePredicateConfig{
    37  			{
    38  				KeyRegex:   "Bosh",
    39  				ValueRegex: "Bish",
    40  			},
    41  			{
    42  				KeyRegex:   "Bosh",
    43  				ValueRegex: "Bash",
    44  			},
    45  		},
    46  	}
    47  	fp, err := BuildFilterPredicate(fc)
    48  	assert.NoError(t, err)
    49  	assert.False(t, fp([]interface{}{"Bosh", "Bash", "Shoes", 42}))
    50  	assert.True(t, fp([]interface{}{"Bosh", "Bash", "Foo", "bar", "Shoes", 42, "Bosh", "Bish"}))
    51  	assert.False(t, fp([]interface{}{"Food", 0.2, "Shoes", 42}))
    52  
    53  }
    54  func TestExcludeAnyFilterPredicate(t *testing.T) {
    55  	fc := &FilterConfig{
    56  		FilterMode: ExcludeWhenAnyMatches,
    57  		Predicates: []*KeyValuePredicateConfig{
    58  			{
    59  				KeyRegex:   "Bosh",
    60  				ValueRegex: "Bish",
    61  			},
    62  			{
    63  				KeyRegex:   "Bosh",
    64  				ValueRegex: "Bash",
    65  			},
    66  		},
    67  	}
    68  	fp, err := BuildFilterPredicate(fc)
    69  	assert.NoError(t, err)
    70  	assert.False(t, fp([]interface{}{"Foo", "bar", "Shoes", 42}))
    71  	assert.True(t, fp([]interface{}{"Foo", "bar", "Shoes", 42, "Bosh", "Bish"}))
    72  	assert.True(t, fp([]interface{}{"Food", 0.2, "Shoes", 42, "Bosh", "Bish"}))
    73  
    74  }
    75  
    76  func TestIncludeAllFilterPredicate(t *testing.T) {
    77  	fc := &FilterConfig{
    78  		FilterMode: IncludeWhenAllMatch,
    79  		Predicates: []*KeyValuePredicateConfig{
    80  			{
    81  				KeyRegex:   "Bosh",
    82  				ValueRegex: "Bish",
    83  			},
    84  			{
    85  				KeyRegex:   "Planks",
    86  				ValueRegex: "^0.2$",
    87  			},
    88  		},
    89  	}
    90  	fp, err := BuildFilterPredicate(fc)
    91  	assert.NoError(t, err)
    92  	assert.True(t, fp([]interface{}{"Foo", "bar", "Shoes", 42}))
    93  	// Don't filter, it has all the required key values
    94  	assert.False(t, fp([]interface{}{"Foo", "bar", "Planks", 0.2, "Shoes", 42, "imBoshy", "unBishy"}))
    95  	assert.True(t, fp([]interface{}{"Foo", "bar", "Planks", 0.23, "Shoes", 42, "imBoshy", "unBishy"}))
    96  	assert.True(t, fp([]interface{}{"Food", 0.2, "Shoes", 42}))
    97  }
    98  
    99  func TestIncludeAnyFilterPredicate(t *testing.T) {
   100  	fc := &FilterConfig{
   101  		FilterMode: IncludeWhenAnyMatches,
   102  		Predicates: []*KeyValuePredicateConfig{
   103  			{
   104  				KeyRegex:   "Bosh",
   105  				ValueRegex: "Bish",
   106  			},
   107  			{
   108  				KeyRegex:   "^Shoes$",
   109  				ValueRegex: "42",
   110  			},
   111  		},
   112  	}
   113  	fp, err := BuildFilterPredicate(fc)
   114  	assert.NoError(t, err)
   115  	assert.False(t, fp([]interface{}{"Foo", "bar", "Shoes", 3427}))
   116  	assert.False(t, fp([]interface{}{"Foo", "bar", "Shoes", 42, "Bosh", "Bish"}))
   117  	assert.False(t, fp([]interface{}{"Food", 0.2, "Shoes", 42}))
   118  }
   119  
   120  func TestKeyOnlyPredicate(t *testing.T) {
   121  
   122  	fc := &FilterConfig{
   123  		FilterMode: IncludeWhenAnyMatches,
   124  		Predicates: []*KeyValuePredicateConfig{
   125  			{
   126  				KeyRegex: "Bosh",
   127  			},
   128  		},
   129  	}
   130  	fp, err := BuildFilterPredicate(fc)
   131  	assert.NoError(t, err)
   132  	assert.True(t, fp([]interface{}{"Foo", "bar", "Shoes", 3427}))
   133  	assert.False(t, fp([]interface{}{"Foo", "bar", "Shoes", 42, "Bosh", "Bish"}))
   134  	assert.True(t, fp([]interface{}{"Food", 0.2, "Shoes", 42}))
   135  }