github.com/google/cloudprober@v0.11.3/surfacers/common/options/options_test.go (about) 1 // Copyright 2021 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 options 16 17 import ( 18 "reflect" 19 "testing" 20 "time" 21 22 "github.com/google/cloudprober/metrics" 23 configpb "github.com/google/cloudprober/surfacers/proto" 24 "google.golang.org/protobuf/proto" 25 ) 26 27 var testEventMetrics = []*metrics.EventMetrics{ 28 metrics.NewEventMetrics(time.Now()). 29 AddMetric("total", metrics.NewInt(20)). 30 AddMetric("timeout", metrics.NewInt(2)). 31 AddLabel("ptype", "http"). 32 AddLabel("probe", "manugarg_homepage"), 33 metrics.NewEventMetrics(time.Now()). 34 AddMetric("total", metrics.NewInt(20)). 35 AddMetric("timeout", metrics.NewInt(2)). 36 AddLabel("ptype", "http"). 37 AddLabel("probe", "google_homepage"), 38 metrics.NewEventMetrics(time.Now()). 39 AddMetric("memory", metrics.NewInt(20)). 40 AddMetric("num_goroutines", metrics.NewInt(2)). 41 AddLabel("probe", "sysvars"), 42 } 43 44 func TestAllowEventMetrics(t *testing.T) { 45 tests := []struct { 46 desc string 47 allowFilter [][2]string 48 ignoreFilter [][2]string 49 wantAllowed []int 50 wantErr bool 51 }{ 52 { 53 desc: "all", 54 wantAllowed: []int{0, 1, 2}, 55 }, 56 { 57 desc: "ignore-sysvars-and-google-homepage", 58 ignoreFilter: [][2]string{ 59 {"probe", "sysvars"}, 60 {"probe", "google_homepage"}, 61 }, 62 wantAllowed: []int{0}, 63 }, 64 { 65 desc: "allow-google-homepage-and-sysvars", 66 allowFilter: [][2]string{ 67 {"probe", "sysvars"}, 68 {"probe", "google_homepage"}, 69 }, 70 wantAllowed: []int{1, 2}, 71 }, 72 { 73 desc: "ignore-takes-precedence-for-sysvars", 74 allowFilter: [][2]string{ 75 {"probe", "sysvars"}, 76 {"probe", "google_homepage"}, 77 }, 78 ignoreFilter: [][2]string{ 79 {"probe", "sysvars"}, 80 }, 81 wantAllowed: []int{1}, 82 }, 83 { 84 desc: "error-label-value-without-key", 85 allowFilter: [][2]string{{"", "sysvars"}}, 86 wantErr: true, 87 }, 88 } 89 90 for _, test := range tests { 91 t.Run(test.desc, func(t *testing.T) { 92 config := &configpb.SurfacerDef{} 93 for _, ignoreF := range test.ignoreFilter { 94 config.IgnoreMetricsWithLabel = append(config.IgnoreMetricsWithLabel, &configpb.LabelFilter{Key: proto.String(ignoreF[0]), Value: proto.String(ignoreF[1])}) 95 } 96 for _, allowF := range test.allowFilter { 97 config.AllowMetricsWithLabel = append(config.AllowMetricsWithLabel, &configpb.LabelFilter{Key: proto.String(allowF[0]), Value: proto.String(allowF[1])}) 98 } 99 100 opts, err := BuildOptionsFromConfig(config, nil) 101 if err != nil { 102 if !test.wantErr { 103 t.Errorf("Unexpected building options from the config: %v", err) 104 } 105 return 106 } 107 if test.wantErr { 108 t.Errorf("Expected error, but there were none") 109 return 110 } 111 112 var gotEM []int 113 for i, em := range testEventMetrics { 114 if opts.AllowEventMetrics(em) { 115 gotEM = append(gotEM, i) 116 } 117 } 118 119 if !reflect.DeepEqual(gotEM, test.wantAllowed) { 120 t.Errorf("Got EMs (index): %v, want EMs (index): %v", gotEM, test.wantAllowed) 121 } 122 }) 123 } 124 } 125 126 func TestAllowMetric(t *testing.T) { 127 tests := []struct { 128 desc string 129 metricName []string 130 allow string 131 ignore string 132 wantMetrics []string 133 wantErr bool 134 }{ 135 { 136 desc: "all", 137 metricName: []string{"total", "success"}, 138 wantMetrics: []string{"total", "success"}, 139 }, 140 { 141 desc: "bad-allow-regex", 142 metricName: []string{"total", "success"}, 143 allow: "(?badRe)", 144 wantErr: true, 145 }, 146 { 147 desc: "bad-ignore-regex", 148 metricName: []string{"total", "success"}, 149 ignore: "(?badRe)", 150 wantErr: true, 151 }, 152 { 153 desc: "ignore-total", 154 metricName: []string{"total", "success"}, 155 ignore: "tot.*", 156 wantMetrics: []string{"success"}, 157 }, 158 { 159 desc: "allow-total", 160 metricName: []string{"total", "success"}, 161 allow: "tot.*", 162 wantMetrics: []string{"total"}, 163 }, 164 } 165 166 for _, test := range tests { 167 t.Run(test.desc, func(t *testing.T) { 168 config := &configpb.SurfacerDef{ 169 IgnoreMetricsWithName: proto.String(test.ignore), 170 AllowMetricsWithName: proto.String(test.allow), 171 } 172 173 opts, err := BuildOptionsFromConfig(config, nil) 174 if err != nil { 175 if !test.wantErr { 176 t.Errorf("Unexpected building options from the config: %v", err) 177 } 178 return 179 } 180 if test.wantErr { 181 t.Errorf("Expected error, but there were none") 182 return 183 } 184 185 var gotMetrics []string 186 for _, m := range test.metricName { 187 if opts.AllowMetric(m) { 188 gotMetrics = append(gotMetrics, m) 189 } 190 } 191 192 if !reflect.DeepEqual(gotMetrics, test.wantMetrics) { 193 t.Errorf("Got metrics: %v, wanted: %v", gotMetrics, test.wantMetrics) 194 } 195 }) 196 } 197 }