github.com/netdata/go.d.plugin@v0.58.1/modules/vsphere/match/match_test.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package match
     4  
     5  import (
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/netdata/go.d.plugin/modules/vsphere/resources"
    10  	"github.com/netdata/go.d.plugin/pkg/matcher"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  var (
    16  	trueHostDC  = hostDCMatcher{matcher.TRUE()}
    17  	falseHostDC = hostDCMatcher{matcher.FALSE()}
    18  	trueVMDC    = vmDCMatcher{matcher.TRUE()}
    19  	falseVMDC   = vmDCMatcher{matcher.FALSE()}
    20  )
    21  
    22  func TestOrHostMatcher_Match(t *testing.T) {
    23  	tests := map[string]struct {
    24  		expected bool
    25  		lhs      HostMatcher
    26  		rhs      HostMatcher
    27  	}{
    28  		"true, true":   {expected: true, lhs: trueHostDC, rhs: trueHostDC},
    29  		"true, false":  {expected: true, lhs: trueHostDC, rhs: falseHostDC},
    30  		"false, true":  {expected: true, lhs: falseHostDC, rhs: trueHostDC},
    31  		"false, false": {expected: false, lhs: falseHostDC, rhs: falseHostDC},
    32  	}
    33  
    34  	var host resources.Host
    35  	for name, test := range tests {
    36  		t.Run(name, func(t *testing.T) {
    37  			m := newOrHostMatcher(test.lhs, test.rhs)
    38  			assert.Equal(t, test.expected, m.Match(&host))
    39  		})
    40  	}
    41  }
    42  
    43  func TestAndHostMatcher_Match(t *testing.T) {
    44  	tests := map[string]struct {
    45  		expected bool
    46  		lhs      HostMatcher
    47  		rhs      HostMatcher
    48  	}{
    49  		"true, true":   {expected: true, lhs: trueHostDC, rhs: trueHostDC},
    50  		"true, false":  {expected: false, lhs: trueHostDC, rhs: falseHostDC},
    51  		"false, true":  {expected: false, lhs: falseHostDC, rhs: trueHostDC},
    52  		"false, false": {expected: false, lhs: falseHostDC, rhs: falseHostDC},
    53  	}
    54  
    55  	var host resources.Host
    56  	for name, test := range tests {
    57  		t.Run(name, func(t *testing.T) {
    58  			m := newAndHostMatcher(test.lhs, test.rhs)
    59  			assert.Equal(t, test.expected, m.Match(&host))
    60  		})
    61  	}
    62  }
    63  
    64  func TestOrVMMatcher_Match(t *testing.T) {
    65  	tests := map[string]struct {
    66  		expected bool
    67  		lhs      VMMatcher
    68  		rhs      VMMatcher
    69  	}{
    70  		"true, true":   {expected: true, lhs: trueVMDC, rhs: trueVMDC},
    71  		"true, false":  {expected: true, lhs: trueVMDC, rhs: falseVMDC},
    72  		"false, true":  {expected: true, lhs: falseVMDC, rhs: trueVMDC},
    73  		"false, false": {expected: false, lhs: falseVMDC, rhs: falseVMDC},
    74  	}
    75  
    76  	var vm resources.VM
    77  	for name, test := range tests {
    78  		t.Run(name, func(t *testing.T) {
    79  			m := newOrVMMatcher(test.lhs, test.rhs)
    80  			assert.Equal(t, test.expected, m.Match(&vm))
    81  		})
    82  	}
    83  }
    84  
    85  func TestAndVMMatcher_Match(t *testing.T) {
    86  	tests := map[string]struct {
    87  		expected bool
    88  		lhs      VMMatcher
    89  		rhs      VMMatcher
    90  	}{
    91  		"true, true":   {expected: true, lhs: trueVMDC, rhs: trueVMDC},
    92  		"true, false":  {expected: false, lhs: trueVMDC, rhs: falseVMDC},
    93  		"false, true":  {expected: false, lhs: falseVMDC, rhs: trueVMDC},
    94  		"false, false": {expected: false, lhs: falseVMDC, rhs: falseVMDC},
    95  	}
    96  
    97  	var vm resources.VM
    98  	for name, test := range tests {
    99  		t.Run(name, func(t *testing.T) {
   100  			m := newAndVMMatcher(test.lhs, test.rhs)
   101  			assert.Equal(t, test.expected, m.Match(&vm))
   102  		})
   103  	}
   104  }
   105  
   106  func TestHostIncludes_Parse(t *testing.T) {
   107  	tests := map[string]struct {
   108  		valid    bool
   109  		expected HostMatcher
   110  	}{
   111  		"":        {valid: false},
   112  		"*/C1/H1": {valid: false},
   113  		"/":       {valid: true, expected: falseHostDC},
   114  		"/*":      {valid: true, expected: trueHostDC},
   115  		"/!*":     {valid: true, expected: falseHostDC},
   116  		"/!*/":    {valid: true, expected: falseHostDC},
   117  		"/!*/ ": {
   118  			valid: true,
   119  			expected: andHostMatcher{
   120  				lhs: falseHostDC,
   121  				rhs: hostClusterMatcher{matcher.FALSE()},
   122  			},
   123  		},
   124  		"/DC1* DC2* !*/Cluster*": {
   125  			valid: true,
   126  			expected: andHostMatcher{
   127  				lhs: hostDCMatcher{mustSP("DC1* DC2* !*")},
   128  				rhs: hostClusterMatcher{mustSP("Cluster*")},
   129  			},
   130  		},
   131  		"/*/*/HOST1*": {
   132  			valid: true,
   133  			expected: andHostMatcher{
   134  				lhs: andHostMatcher{
   135  					lhs: trueHostDC,
   136  					rhs: hostClusterMatcher{matcher.TRUE()},
   137  				},
   138  				rhs: hostHostMatcher{mustSP("HOST1*")},
   139  			},
   140  		},
   141  		"/*/*/HOST1*/*/*": {
   142  			valid: true,
   143  			expected: andHostMatcher{
   144  				lhs: andHostMatcher{
   145  					lhs: trueHostDC,
   146  					rhs: hostClusterMatcher{matcher.TRUE()},
   147  				},
   148  				rhs: hostHostMatcher{mustSP("HOST1*")},
   149  			},
   150  		},
   151  		"[/DC1*,/DC2*]": {
   152  			valid: true,
   153  			expected: orHostMatcher{
   154  				lhs: hostDCMatcher{mustSP("DC1*")},
   155  				rhs: hostDCMatcher{mustSP("DC2*")},
   156  			},
   157  		},
   158  		"[/DC1*,/DC2*,/DC3*/Cluster1*/H*]": {
   159  			valid: true,
   160  			expected: orHostMatcher{
   161  				lhs: orHostMatcher{
   162  					lhs: hostDCMatcher{mustSP("DC1*")},
   163  					rhs: hostDCMatcher{mustSP("DC2*")},
   164  				},
   165  				rhs: andHostMatcher{
   166  					lhs: andHostMatcher{
   167  						lhs: hostDCMatcher{mustSP("DC3*")},
   168  						rhs: hostClusterMatcher{mustSP("Cluster1*")},
   169  					},
   170  					rhs: hostHostMatcher{mustSP("H*")},
   171  				},
   172  			},
   173  		},
   174  	}
   175  
   176  	for name, test := range tests {
   177  		t.Run(name, func(t *testing.T) {
   178  			includes := prepareIncludes(name)
   179  			m, err := HostIncludes(includes).Parse()
   180  
   181  			if !test.valid {
   182  				assert.Error(t, err)
   183  			} else {
   184  				assert.Equal(t, test.expected, m)
   185  			}
   186  		})
   187  	}
   188  }
   189  
   190  func TestVMIncludes_Parse(t *testing.T) {
   191  	tests := map[string]struct {
   192  		valid    bool
   193  		includes []string
   194  		expected VMMatcher
   195  	}{
   196  		"":           {valid: false},
   197  		"*/C1/H1/V1": {valid: false},
   198  		"/*":         {valid: true, expected: trueVMDC},
   199  		"/!*":        {valid: true, expected: falseVMDC},
   200  		"/!*/":       {valid: true, expected: falseVMDC},
   201  		"/!*/ ": {
   202  			valid: true,
   203  			expected: andVMMatcher{
   204  				lhs: falseVMDC,
   205  				rhs: vmClusterMatcher{matcher.FALSE()},
   206  			},
   207  		},
   208  		"/DC1* DC2* !*/Cluster*": {
   209  			valid: true,
   210  			expected: andVMMatcher{
   211  				lhs: vmDCMatcher{mustSP("DC1* DC2* !*")},
   212  				rhs: vmClusterMatcher{mustSP("Cluster*")},
   213  			},
   214  		},
   215  		"/*/*/HOST1": {
   216  			valid: true,
   217  			expected: andVMMatcher{
   218  				lhs: andVMMatcher{
   219  					lhs: trueVMDC,
   220  					rhs: vmClusterMatcher{matcher.TRUE()},
   221  				},
   222  				rhs: vmHostMatcher{mustSP("HOST1")},
   223  			},
   224  		},
   225  		"/*/*/HOST1*/*/*": {
   226  			valid: true,
   227  			expected: andVMMatcher{
   228  				lhs: andVMMatcher{
   229  					lhs: andVMMatcher{
   230  						lhs: trueVMDC,
   231  						rhs: vmClusterMatcher{matcher.TRUE()},
   232  					},
   233  					rhs: vmHostMatcher{mustSP("HOST1*")},
   234  				},
   235  				rhs: vmVMMatcher{matcher.TRUE()},
   236  			},
   237  		},
   238  		"[/DC1*,/DC2*]": {
   239  			valid: true,
   240  			expected: orVMMatcher{
   241  				lhs: vmDCMatcher{mustSP("DC1*")},
   242  				rhs: vmDCMatcher{mustSP("DC2*")},
   243  			},
   244  		},
   245  		"[/DC1*,/DC2*,/DC3*/Cluster1*/H*/VM*]": {
   246  			valid: true,
   247  			expected: orVMMatcher{
   248  				lhs: orVMMatcher{
   249  					lhs: vmDCMatcher{mustSP("DC1*")},
   250  					rhs: vmDCMatcher{mustSP("DC2*")},
   251  				},
   252  				rhs: andVMMatcher{
   253  					lhs: andVMMatcher{
   254  						lhs: andVMMatcher{
   255  							lhs: vmDCMatcher{mustSP("DC3*")},
   256  							rhs: vmClusterMatcher{mustSP("Cluster1*")},
   257  						},
   258  						rhs: vmHostMatcher{mustSP("H*")},
   259  					},
   260  					rhs: vmVMMatcher{mustSP("VM*")},
   261  				},
   262  			},
   263  		},
   264  	}
   265  
   266  	for name, test := range tests {
   267  		t.Run(name, func(t *testing.T) {
   268  			includes := prepareIncludes(name)
   269  			m, err := VMIncludes(includes).Parse()
   270  
   271  			if !test.valid {
   272  				assert.Error(t, err)
   273  			} else {
   274  				assert.Equal(t, test.expected, m)
   275  			}
   276  		})
   277  	}
   278  }
   279  
   280  func prepareIncludes(include string) []string {
   281  	trimmed := strings.Trim(include, "[]")
   282  	return strings.Split(trimmed, ",")
   283  }
   284  
   285  func mustSP(expr string) matcher.Matcher {
   286  	return matcher.Must(matcher.NewSimplePatternsMatcher(expr))
   287  }