github.com/google/cloudprober@v0.11.3/rds/server/filter/filter_test.go (about) 1 // Copyright 2017-2019 The Cloudprober Authors. 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 filter 16 17 import ( 18 "testing" 19 ) 20 21 func TestLabelsFilter(t *testing.T) { 22 for _, testData := range []struct { 23 testLabels []map[string]string 24 labelFilters map[string]string 25 expectError bool 26 matchCount int 27 }{ 28 { 29 testLabels: []map[string]string{ 30 {"lA": "vAA", "lB": "vB"}, 31 {"lA": "vAB", "lB": "vB"}, 32 }, 33 labelFilters: map[string]string{"lA": "vA.", "lB": "vB"}, 34 expectError: false, 35 matchCount: 2, 36 }, 37 { 38 testLabels: []map[string]string{ 39 {"lA": "vAA", "lB": "vB"}, // Only this will match. 40 {"lA": "vBB", "lB": "vB"}, 41 }, 42 labelFilters: map[string]string{"lA": "vA.", "lB": "vB"}, 43 expectError: false, 44 matchCount: 1, 45 }, 46 { 47 // Label lC not on any instance, no match. 48 testLabels: []map[string]string{ 49 {"lA": "vAA", "lB": "vB"}, 50 {"lA": "vBB", "lB": "vB"}, 51 }, 52 labelFilters: map[string]string{"lC": "vC.", "lB": "vB"}, 53 expectError: false, 54 matchCount: 0, 55 }, 56 } { 57 lf, err := NewLabelsFilter(testData.labelFilters) 58 if err != nil { 59 t.Errorf("Got unexpected error while adding a label filter: %v", err) 60 } 61 62 var testMatchCount int 63 for _, testInstance := range testData.testLabels { 64 t.Logf("Matching instance with labels: %v", testInstance) 65 if lf.Match(testInstance, nil) { 66 testMatchCount++ 67 } 68 } 69 70 if testMatchCount != testData.matchCount { 71 t.Errorf("Match count doesn't match with expected match count. Got=%d, Expected=%d", testMatchCount, testData.matchCount) 72 } 73 } 74 }