github.com/matrixorigin/matrixone@v1.2.0/pkg/clusterservice/selector_test.go (about)

     1  // Copyright 2023 Matrix Origin
     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 clusterservice
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/pb/metadata"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func TestSelectByServiceID(t *testing.T) {
    25  	assert.Equal(t,
    26  		Selector{byServiceID: true, serviceID: "s1"},
    27  		NewServiceIDSelector("s1"))
    28  
    29  	assert.Equal(t,
    30  		Selector{byServiceID: true, serviceID: "s2"},
    31  		NewSelector().SelectByServiceID("s2"))
    32  
    33  }
    34  
    35  func TestSelectByLabel(t *testing.T) {
    36  	assert.Equal(t,
    37  		Selector{byLabel: true, labels: map[string]string{"l1": "v1"}, labelOp: EQ},
    38  		NewSelector().SelectByLabel(map[string]string{"l1": "v1"}, EQ))
    39  }
    40  
    41  func TestFilterWithServiceID(t *testing.T) {
    42  	assert.True(t,
    43  		NewSelector().filter("", nil))
    44  
    45  	assert.True(t,
    46  		NewServiceIDSelector("s1").filter("s1", nil))
    47  	assert.False(t,
    48  		NewServiceIDSelector("s2").filter("s1", nil))
    49  }
    50  
    51  func TestFilterWithLabel_EQ(t *testing.T) {
    52  	assert.True(t,
    53  		NewSelector().
    54  			SelectByLabel(map[string]string{"l1": "v2"}, EQ).
    55  			filter("", nil))
    56  
    57  	assert.False(t,
    58  		NewSelector().
    59  			SelectByLabel(map[string]string{"l1": "v2"}, EQ).
    60  			filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
    61  
    62  	assert.True(t,
    63  		NewSelector().
    64  			SelectByLabel(map[string]string{"l1": "v1"}, EQ).
    65  			filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
    66  
    67  	assert.False(t,
    68  		NewSelector().
    69  			SelectByLabel(map[string]string{"l1": "v1"}, EQ).
    70  			filter("", map[string]metadata.LabelList{
    71  				"l1": {Labels: []string{"v1"}},
    72  				"l2": {Labels: []string{"v2"}}},
    73  			))
    74  
    75  	// Test the nil cases.
    76  	assert.True(t, NewSelector().SelectByLabel(nil, EQ).filter("", map[string]metadata.LabelList{}))
    77  	assert.True(t, NewSelector().SelectByLabel(map[string]string{}, EQ).filter("", nil))
    78  	assert.True(t, NewSelector().SelectByLabel(nil, EQ).filter("", nil))
    79  	assert.False(t, NewSelector().SelectByLabel(nil, EQ).filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
    80  	assert.True(t, NewSelector().SelectByLabel(map[string]string{"li": "v1"}, EQ).filter("", map[string]metadata.LabelList{}))
    81  }
    82  
    83  func TestFilterWithLabel_Contain(t *testing.T) {
    84  	assert.True(t,
    85  		NewSelector().
    86  			SelectByLabel(map[string]string{"l1": "v2"}, Contain).
    87  			filter("", nil))
    88  
    89  	assert.False(t,
    90  		NewSelector().
    91  			SelectByLabel(map[string]string{"l1": "v2"}, Contain).
    92  			filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
    93  
    94  	assert.True(t,
    95  		NewSelector().
    96  			SelectByLabel(map[string]string{"l1": "v1"}, Contain).
    97  			filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
    98  
    99  	assert.True(t,
   100  		NewSelector().
   101  			SelectByLabel(map[string]string{"l1": "v1"}, Contain).
   102  			filter("", map[string]metadata.LabelList{
   103  				"l1": {Labels: []string{"v1"}},
   104  				"l2": {Labels: []string{"v2"}}},
   105  			))
   106  
   107  	// Test the nil cases.
   108  	assert.True(t, NewSelector().SelectByLabel(nil, Contain).filter("", map[string]metadata.LabelList{}))
   109  	assert.True(t, NewSelector().SelectByLabel(map[string]string{}, Contain).filter("", nil))
   110  	assert.True(t, NewSelector().SelectByLabel(nil, Contain).filter("", nil))
   111  	assert.False(t, NewSelector().SelectByLabel(nil, Contain).filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
   112  	assert.True(t, NewSelector().SelectByLabel(map[string]string{"li": "v1"}, Contain).filter("", map[string]metadata.LabelList{}))
   113  }
   114  
   115  func TestFilterWithLabel_EQ_Globbing(t *testing.T) {
   116  	assert.True(t,
   117  		NewSelector().
   118  			SelectByLabel(map[string]string{"l1": "v2"}, EQ_Globbing).
   119  			filter("", nil))
   120  
   121  	assert.False(t,
   122  		NewSelector().
   123  			SelectByLabel(map[string]string{"l1": "v2"}, EQ_Globbing).
   124  			filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
   125  
   126  	assert.True(t,
   127  		NewSelector().
   128  			SelectByLabel(map[string]string{"l1": "v1"}, EQ_Globbing).
   129  			filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
   130  
   131  	src := "abcdefg"
   132  	yDst := []string{"*", "ab*", "*defg", "a*c*efg"}
   133  	for _, dst := range yDst {
   134  		assert.True(t,
   135  			NewSelector().
   136  				SelectByLabel(map[string]string{"l1": src}, EQ_Globbing).
   137  				filter("", map[string]metadata.LabelList{
   138  					"l1": {Labels: []string{dst}}},
   139  				))
   140  	}
   141  
   142  	// Test the nil cases.
   143  	assert.True(t, NewSelector().SelectByLabel(nil, EQ_Globbing).filter("", map[string]metadata.LabelList{}))
   144  	assert.True(t, NewSelector().SelectByLabel(map[string]string{}, EQ_Globbing).filter("", nil))
   145  	assert.True(t, NewSelector().SelectByLabel(nil, EQ_Globbing).filter("", nil))
   146  	assert.False(t, NewSelector().SelectByLabel(nil, EQ_Globbing).filter("", map[string]metadata.LabelList{"l1": {Labels: []string{"v1"}}}))
   147  	assert.True(t, NewSelector().SelectByLabel(map[string]string{"li": "v1"}, EQ_Globbing).filter("", map[string]metadata.LabelList{}))
   148  }
   149  
   150  func TestSelector_SelectWithoutLabel(t *testing.T) {
   151  	assert.False(t,
   152  		NewSelector().
   153  			SelectByLabel(map[string]string{"l1": "v1", "l2": "v2"}, EQ).
   154  			filter("", map[string]metadata.LabelList{
   155  				"l2": {
   156  					Labels: []string{"v2"},
   157  				},
   158  			}))
   159  
   160  	assert.True(t,
   161  		NewSelector().
   162  			SelectByLabel(map[string]string{"l1": "v1", "l2": "v2"}, EQ).
   163  			SelectWithoutLabel(map[string]string{"l1": "v1"}).
   164  			filter("", map[string]metadata.LabelList{
   165  				"l2": {
   166  					Labels: []string{"v2"},
   167  				},
   168  			}))
   169  }
   170  
   171  func TestLabelNum(t *testing.T) {
   172  	assert.Equal(t, 2, NewSelector().SelectByLabel(map[string]string{
   173  		"a": "a",
   174  		"b": "b",
   175  	}, EQ).LabelNum())
   176  }
   177  
   178  func TestGlobbing(t *testing.T) {
   179  	src := "abcdefg"
   180  	yDst := []string{"*", "ab*", "*defg", "a*c*efg"}
   181  	nDst := []string{"*def", "aab*", "*f"}
   182  	for _, dst := range yDst {
   183  		assert.True(t, globbing(src, dst))
   184  	}
   185  	for _, dst := range nDst {
   186  		assert.False(t, globbing(src, dst))
   187  	}
   188  }