/
validators.go
148 lines (129 loc) · 4.34 KB
/
validators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2018-2019 The Cloudprober Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package validators provides an entrypoint for the cloudprober's validators
// framework.
package validators
import (
"fmt"
"github.com/cloudprober/cloudprober/logger"
"github.com/cloudprober/cloudprober/metrics"
"github.com/cloudprober/cloudprober/validators/http"
"github.com/cloudprober/cloudprober/validators/integrity"
"github.com/cloudprober/cloudprober/validators/json"
configpb "github.com/cloudprober/cloudprober/validators/proto"
"github.com/cloudprober/cloudprober/validators/regex"
)
// Validator implements a validator.
//
// A validator runs a test on the provided input, usually the probe response,
// and returns the test result. If test cannot be run successfully for some
// reason (e.g. for malformed input), an error is returned.
type Validator struct {
Name string
Validate func(input *Input) (bool, error)
}
// Init initializes the validators defined in the config.
func Init(validatorConfs []*configpb.Validator, l *logger.Logger) ([]*Validator, error) {
var validators []*Validator
names := make(map[string]bool)
for _, vc := range validatorConfs {
if names[vc.GetName()] {
return nil, fmt.Errorf("validator %s is defined twice", vc.GetName())
}
v, err := initValidator(vc, l)
if err != nil {
return nil, err
}
validators = append(validators, v)
names[vc.GetName()] = true
}
return validators, nil
}
func initValidator(validatorConf *configpb.Validator, l *logger.Logger) (validator *Validator, err error) {
validator = &Validator{Name: validatorConf.GetName()}
switch validatorConf.Type.(type) {
case *configpb.Validator_HttpValidator:
v := &http.Validator{}
if err := v.Init(validatorConf.GetHttpValidator(), l); err != nil {
return nil, err
}
validator.Validate = func(input *Input) (bool, error) {
return v.Validate(input.Response, input.ResponseBody)
}
return
case *configpb.Validator_IntegrityValidator:
v := &integrity.Validator{}
if err := v.Init(validatorConf.GetIntegrityValidator(), l); err != nil {
return nil, err
}
validator.Validate = func(input *Input) (bool, error) {
return v.Validate(input.ResponseBody)
}
return
case *configpb.Validator_JsonValidator:
v := &json.Validator{}
if err := v.Init(validatorConf.GetJsonValidator(), l); err != nil {
return nil, err
}
validator.Validate = func(input *Input) (bool, error) {
return v.Validate(input.ResponseBody)
}
return
case *configpb.Validator_Regex:
v := ®ex.Validator{}
if err := v.Init(validatorConf.GetRegex(), l); err != nil {
return nil, err
}
validator.Validate = func(input *Input) (bool, error) {
return v.Validate(input.ResponseBody)
}
return
default:
err = fmt.Errorf("unknown validator type: %v", validatorConf.Type)
return
}
}
// Input encapsulates the input for validators.
type Input struct {
Response interface{}
ResponseBody []byte
}
// RunValidators runs the list of validators on the given response and
// responseBody, updates the given validationFailure map and returns the list
// of failures.
func RunValidators(vs []*Validator, input *Input, validationFailure *metrics.Map, l *logger.Logger) []string {
var failures []string
for _, v := range vs {
success, err := v.Validate(input)
if err != nil {
l.Error("Error while running the validator ", v.Name, ": ", err.Error())
continue
}
if !success {
validationFailure.IncKey(v.Name)
failures = append(failures, v.Name)
}
}
return failures
}
// ValidationFailureMap returns an initialized validation failures map.
func ValidationFailureMap(vs []*Validator) *metrics.Map {
m := metrics.NewMap("validator", metrics.NewInt(0))
// Initialize validation failure map with validator keys, so that we always
// export the metrics.
for _, v := range vs {
m.IncKeyBy(v.Name, metrics.NewInt(0))
}
return m
}