|
@@ -0,0 +1,115 @@
|
|
1
|
+// Copyright 2014 Google Inc. All Rights Reserved.
|
|
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 profile
|
|
16
|
+
|
|
17
|
+import (
|
|
18
|
+ "regexp"
|
|
19
|
+ "testing"
|
|
20
|
+)
|
|
21
|
+
|
|
22
|
+func TestFilter(t *testing.T) {
|
|
23
|
+ // Perform several forms of filtering on the test profile.
|
|
24
|
+
|
|
25
|
+ type filterTestcase struct {
|
|
26
|
+ focus, ignore, hide, show *regexp.Regexp
|
|
27
|
+ fm, im, hm, hnm bool
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ for tx, tc := range []filterTestcase{
|
|
31
|
+ {
|
|
32
|
+ fm: true, // nil focus matches every sample
|
|
33
|
+ },
|
|
34
|
+ {
|
|
35
|
+ focus: regexp.MustCompile("notfound"),
|
|
36
|
+ },
|
|
37
|
+ {
|
|
38
|
+ ignore: regexp.MustCompile("foo.c"),
|
|
39
|
+ fm: true,
|
|
40
|
+ im: true,
|
|
41
|
+ },
|
|
42
|
+ {
|
|
43
|
+ hide: regexp.MustCompile("lib.so"),
|
|
44
|
+ fm: true,
|
|
45
|
+ hm: true,
|
|
46
|
+ },
|
|
47
|
+ {
|
|
48
|
+ show: regexp.MustCompile("foo.c"),
|
|
49
|
+ fm: true,
|
|
50
|
+ hnm: true,
|
|
51
|
+ },
|
|
52
|
+ {
|
|
53
|
+ show: regexp.MustCompile("notfound"),
|
|
54
|
+ fm: true,
|
|
55
|
+ },
|
|
56
|
+ } {
|
|
57
|
+ prof := *testProfile1.Copy()
|
|
58
|
+ gf, gi, gh, gnh := prof.FilterSamplesByName(tc.focus, tc.ignore, tc.hide, tc.show)
|
|
59
|
+ if gf != tc.fm {
|
|
60
|
+ t.Errorf("Filter #%d, got fm=%v, want %v", tx, gf, tc.fm)
|
|
61
|
+ }
|
|
62
|
+ if gi != tc.im {
|
|
63
|
+ t.Errorf("Filter #%d, got im=%v, want %v", tx, gi, tc.im)
|
|
64
|
+ }
|
|
65
|
+ if gh != tc.hm {
|
|
66
|
+ t.Errorf("Filter #%d, got hm=%v, want %v", tx, gh, tc.hm)
|
|
67
|
+ }
|
|
68
|
+ if gnh != tc.hnm {
|
|
69
|
+ t.Errorf("Filter #%d, got hnm=%v, want %v", tx, gnh, tc.hnm)
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+}
|
|
73
|
+
|
|
74
|
+func TestTagFilter(t *testing.T) {
|
|
75
|
+ // Perform several forms of tag filtering on the test profile.
|
|
76
|
+
|
|
77
|
+ type filterTestcase struct {
|
|
78
|
+ include, exclude *regexp.Regexp
|
|
79
|
+ im, em bool
|
|
80
|
+ count int
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ countTags := func(p *Profile) map[string]bool {
|
|
84
|
+ tags := make(map[string]bool)
|
|
85
|
+
|
|
86
|
+ for _, s := range p.Sample {
|
|
87
|
+ for l := range s.Label {
|
|
88
|
+ tags[l] = true
|
|
89
|
+ }
|
|
90
|
+ for l := range s.NumLabel {
|
|
91
|
+ tags[l] = true
|
|
92
|
+ }
|
|
93
|
+ }
|
|
94
|
+ return tags
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ for tx, tc := range []filterTestcase{
|
|
98
|
+ {nil, nil, true, false, 3},
|
|
99
|
+ {regexp.MustCompile("notfound"), nil, false, false, 0},
|
|
100
|
+ {regexp.MustCompile("key1"), nil, true, false, 1},
|
|
101
|
+ {nil, regexp.MustCompile("key[12]"), true, true, 1},
|
|
102
|
+ } {
|
|
103
|
+ prof := testProfile1.Copy()
|
|
104
|
+ gim, gem := prof.FilterTagsByName(tc.include, tc.exclude)
|
|
105
|
+ if gim != tc.im {
|
|
106
|
+ t.Errorf("Filter #%d, got include match=%v, want %v", tx, gim, tc.im)
|
|
107
|
+ }
|
|
108
|
+ if gem != tc.em {
|
|
109
|
+ t.Errorf("Filter #%d, got exclude match=%v, want %v", tx, gem, tc.em)
|
|
110
|
+ }
|
|
111
|
+ if tags := countTags(prof); len(tags) != tc.count {
|
|
112
|
+ t.Errorf("Filter #%d, got %d tags[%v], want %d", tx, len(tags), tags, tc.count)
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+}
|