1// Copyright 2015 The Prometheus Authors 2// Licensed under the Apache License, Version 2.0 (the "License"); 3// you may not use this file except in compliance with the License. 4// You may obtain a copy of the License at 5// 6// http://www.apache.org/licenses/LICENSE-2.0 7// 8// Unless required by applicable law or agreed to in writing, software 9// distributed under the License is distributed on an "AS IS" BASIS, 10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11// See the License for the specific language governing permissions and 12// limitations under the License. 13 14package model 15 16import ( 17 "strings" 18 "testing" 19 "time" 20) 21 22func TestMatcherValidate(t *testing.T) { 23 var cases = []struct { 24 matcher *Matcher 25 err string 26 }{ 27 { 28 matcher: &Matcher{ 29 Name: "name", 30 Value: "value", 31 }, 32 }, 33 { 34 matcher: &Matcher{ 35 Name: "name", 36 Value: "value", 37 IsRegex: true, 38 }, 39 }, 40 { 41 matcher: &Matcher{ 42 Name: "name!", 43 Value: "value", 44 }, 45 err: "invalid name", 46 }, 47 { 48 matcher: &Matcher{ 49 Name: "", 50 Value: "value", 51 }, 52 err: "invalid name", 53 }, 54 { 55 matcher: &Matcher{ 56 Name: "name", 57 Value: "value\xff", 58 }, 59 err: "invalid value", 60 }, 61 { 62 matcher: &Matcher{ 63 Name: "name", 64 Value: "", 65 }, 66 err: "invalid value", 67 }, 68 } 69 70 for i, c := range cases { 71 err := c.matcher.Validate() 72 if err == nil { 73 if c.err == "" { 74 continue 75 } 76 t.Errorf("%d. Expected error %q but got none", i, c.err) 77 continue 78 } 79 if c.err == "" { 80 t.Errorf("%d. Expected no error but got %q", i, err) 81 continue 82 } 83 if !strings.Contains(err.Error(), c.err) { 84 t.Errorf("%d. Expected error to contain %q but got %q", i, c.err, err) 85 } 86 } 87} 88 89func TestSilenceValidate(t *testing.T) { 90 ts := time.Now() 91 92 var cases = []struct { 93 sil *Silence 94 err string 95 }{ 96 { 97 sil: &Silence{ 98 Matchers: []*Matcher{ 99 {Name: "name", Value: "value"}, 100 }, 101 StartsAt: ts, 102 EndsAt: ts, 103 CreatedAt: ts, 104 CreatedBy: "name", 105 Comment: "comment", 106 }, 107 }, 108 { 109 sil: &Silence{ 110 Matchers: []*Matcher{ 111 {Name: "name", Value: "value"}, 112 {Name: "name", Value: "value"}, 113 {Name: "name", Value: "value"}, 114 {Name: "name", Value: "value", IsRegex: true}, 115 }, 116 StartsAt: ts, 117 EndsAt: ts, 118 CreatedAt: ts, 119 CreatedBy: "name", 120 Comment: "comment", 121 }, 122 }, 123 { 124 sil: &Silence{ 125 Matchers: []*Matcher{ 126 {Name: "name", Value: "value"}, 127 }, 128 StartsAt: ts, 129 EndsAt: ts.Add(-1 * time.Minute), 130 CreatedAt: ts, 131 CreatedBy: "name", 132 Comment: "comment", 133 }, 134 err: "start time must be before end time", 135 }, 136 { 137 sil: &Silence{ 138 Matchers: []*Matcher{ 139 {Name: "name", Value: "value"}, 140 }, 141 StartsAt: ts, 142 CreatedAt: ts, 143 CreatedBy: "name", 144 Comment: "comment", 145 }, 146 err: "end time missing", 147 }, 148 { 149 sil: &Silence{ 150 Matchers: []*Matcher{ 151 {Name: "name", Value: "value"}, 152 }, 153 EndsAt: ts, 154 CreatedAt: ts, 155 CreatedBy: "name", 156 Comment: "comment", 157 }, 158 err: "start time missing", 159 }, 160 { 161 sil: &Silence{ 162 Matchers: []*Matcher{ 163 {Name: "!name", Value: "value"}, 164 }, 165 StartsAt: ts, 166 EndsAt: ts, 167 CreatedAt: ts, 168 CreatedBy: "name", 169 Comment: "comment", 170 }, 171 err: "invalid matcher", 172 }, 173 { 174 sil: &Silence{ 175 Matchers: []*Matcher{ 176 {Name: "name", Value: "value"}, 177 }, 178 StartsAt: ts, 179 EndsAt: ts, 180 CreatedAt: ts, 181 CreatedBy: "name", 182 }, 183 err: "comment missing", 184 }, 185 { 186 sil: &Silence{ 187 Matchers: []*Matcher{ 188 {Name: "name", Value: "value"}, 189 }, 190 StartsAt: ts, 191 EndsAt: ts, 192 CreatedBy: "name", 193 Comment: "comment", 194 }, 195 err: "creation timestamp missing", 196 }, 197 { 198 sil: &Silence{ 199 Matchers: []*Matcher{ 200 {Name: "name", Value: "value"}, 201 }, 202 StartsAt: ts, 203 EndsAt: ts, 204 CreatedAt: ts, 205 Comment: "comment", 206 }, 207 err: "creator information missing", 208 }, 209 { 210 sil: &Silence{ 211 Matchers: []*Matcher{}, 212 StartsAt: ts, 213 EndsAt: ts, 214 CreatedAt: ts, 215 Comment: "comment", 216 }, 217 err: "at least one matcher required", 218 }, 219 } 220 221 for i, c := range cases { 222 err := c.sil.Validate() 223 if err == nil { 224 if c.err == "" { 225 continue 226 } 227 t.Errorf("%d. Expected error %q but got none", i, c.err) 228 continue 229 } 230 if c.err == "" { 231 t.Errorf("%d. Expected no error but got %q", i, err) 232 continue 233 } 234 if !strings.Contains(err.Error(), c.err) { 235 t.Errorf("%d. Expected error to contain %q but got %q", i, c.err, err) 236 } 237 } 238} 239