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

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package pipeline
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/netdata/go.d.plugin/agent/discovery/sd/model"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  	"gopkg.in/yaml.v2"
    13  )
    14  
    15  func TestTargetClassificator_classify(t *testing.T) {
    16  	config := `
    17  - selector: "rule1"
    18    tags: "foo1"
    19    match:
    20      - tags: "bar1"
    21        expr: '{{ glob .Name "mock*1*" }}'
    22      - tags: "bar2"
    23        expr: '{{ glob .Name "mock*2*" }}'
    24  - selector: "rule2"
    25    tags: "foo2"
    26    match:
    27      - tags: "bar3"
    28        expr: '{{ glob .Name "mock*3*" }}'
    29      - tags: "bar4"
    30        expr: '{{ glob .Name "mock*4*" }}'
    31  - selector: "rule3"
    32    tags: "foo3"
    33    match:
    34      - tags: "bar5"
    35        expr: '{{ glob .Name "mock*5*" }}'
    36      - tags: "bar6"
    37        expr: '{{ glob .Name "mock*6*" }}'
    38  `
    39  	tests := map[string]struct {
    40  		target   model.Target
    41  		wantTags model.Tags
    42  	}{
    43  		"no rules match": {
    44  			target:   newMockTarget("mock1"),
    45  			wantTags: nil,
    46  		},
    47  		"one rule one match": {
    48  			target:   newMockTarget("mock4", "rule2"),
    49  			wantTags: mustParseTags("foo2 bar4"),
    50  		},
    51  		"one rule two match": {
    52  			target:   newMockTarget("mock56", "rule3"),
    53  			wantTags: mustParseTags("foo3 bar5 bar6"),
    54  		},
    55  		"all rules all matches": {
    56  			target:   newMockTarget("mock123456", "rule1 rule2 rule3"),
    57  			wantTags: mustParseTags("foo1 foo2 foo3 bar1 bar2 bar3 bar4 bar5 bar6"),
    58  		},
    59  	}
    60  
    61  	for name, test := range tests {
    62  		t.Run(name, func(t *testing.T) {
    63  			var cfg []ClassifyRuleConfig
    64  
    65  			err := yaml.Unmarshal([]byte(config), &cfg)
    66  			require.NoErrorf(t, err, "yaml unmarshalling of config")
    67  
    68  			clr, err := newTargetClassificator(cfg)
    69  			require.NoErrorf(t, err, "targetClassificator creation")
    70  
    71  			assert.Equal(t, test.wantTags, clr.classify(test.target))
    72  		})
    73  	}
    74  }