github.com/netdata/go.d.plugin@v0.58.1/agent/discovery/sd/pipeline/funcmap_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package pipeline
     4  
     5  import (
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func Test_globAny(t *testing.T) {
    13  	tests := map[string]struct {
    14  		patterns  []string
    15  		value     string
    16  		wantMatch bool
    17  	}{
    18  		"one param, matches": {
    19  			wantMatch: true,
    20  			patterns:  []string{"*"},
    21  			value:     "value",
    22  		},
    23  		"one param, matches with *": {
    24  			wantMatch: true,
    25  			patterns:  []string{"**/value"},
    26  			value:     "/one/two/three/value",
    27  		},
    28  		"one param, not matches": {
    29  			wantMatch: false,
    30  			patterns:  []string{"Value"},
    31  			value:     "value",
    32  		},
    33  		"several params, last one matches": {
    34  			wantMatch: true,
    35  			patterns:  []string{"not", "matches", "*"},
    36  			value:     "value",
    37  		},
    38  		"several params, no matches": {
    39  			wantMatch: false,
    40  			patterns:  []string{"not", "matches", "really"},
    41  			value:     "value",
    42  		},
    43  	}
    44  
    45  	for name, test := range tests {
    46  		name := fmt.Sprintf("name: %s, patterns: '%v', value: '%s'", name, test.patterns, test.value)
    47  		ok := globAny(test.value, test.patterns[0], test.patterns[1:]...)
    48  
    49  		if test.wantMatch {
    50  			assert.Truef(t, ok, name)
    51  		} else {
    52  			assert.Falsef(t, ok, name)
    53  		}
    54  	}
    55  }
    56  
    57  func Test_regexpAny(t *testing.T) {
    58  	tests := map[string]struct {
    59  		patterns  []string
    60  		value     string
    61  		wantMatch bool
    62  	}{
    63  		"one param, matches": {
    64  			wantMatch: true,
    65  			patterns:  []string{"^value$"},
    66  			value:     "value",
    67  		},
    68  		"one param, not matches": {
    69  			wantMatch: false,
    70  			patterns:  []string{"^Value$"},
    71  			value:     "value",
    72  		},
    73  		"several params, last one matches": {
    74  			wantMatch: true,
    75  			patterns:  []string{"not", "matches", "va[lue]{3}"},
    76  			value:     "value",
    77  		},
    78  		"several params, no matches": {
    79  			wantMatch: false,
    80  			patterns:  []string{"not", "matches", "val[^l]ue"},
    81  			value:     "value",
    82  		},
    83  	}
    84  
    85  	for name, test := range tests {
    86  		name := fmt.Sprintf("name: %s, patterns: '%v', value: '%s'", name, test.patterns, test.value)
    87  		ok := regexpAny(test.value, test.patterns[0], test.patterns[1:]...)
    88  
    89  		if test.wantMatch {
    90  			assert.Truef(t, ok, name)
    91  		} else {
    92  			assert.Falsef(t, ok, name)
    93  		}
    94  	}
    95  }