Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Commit

Permalink
Add regex validator to cloudprober.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 223062931
  • Loading branch information
manugarg committed Nov 30, 2018
1 parent 06ec432 commit cd0610a
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 0 deletions.
4 changes: 4 additions & 0 deletions validators/proto/config.proto
Expand Up @@ -2,6 +2,7 @@ syntax = "proto2";

import "github.com/google/cloudprober/validators/http/proto/config.proto";
import "github.com/google/cloudprober/validators/integrity/proto/config.proto";
import "github.com/google/cloudprober/validators/regex/proto/config.proto";

package cloudprober.validators;

Expand All @@ -12,5 +13,8 @@ message Validator {

// Data integrity validator
integrity.Validator integrity_validator = 3;

// Regex validator
regex.Validator regex_validator = 4;
}
}
7 changes: 7 additions & 0 deletions validators/regex/proto/config.proto
@@ -0,0 +1,7 @@
syntax = "proto2";

package cloudprober.validators.regex;

message Validator {
optional string regex = 1;
}
61 changes: 61 additions & 0 deletions validators/regex/regex.go
@@ -0,0 +1,61 @@
// Copyright 2018 Google Inc.
//
// 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 regex provides regex validator for the Cloudprober's
// validator framework.
package regex

import (
"errors"
"fmt"
"regexp"

"github.com/google/cloudprober/logger"
configpb "github.com/google/cloudprober/validators/regex/proto"
)

// Validator implements a regex validator.
type Validator struct {
r *regexp.Regexp
l *logger.Logger
}

// Init initializes the regex validator.
// It compiles the regex in the configuration and returns an error if regex
// doesn't compile for some reason.
func (v *Validator) Init(config interface{}, l *logger.Logger) error {
c, ok := config.(*configpb.Validator)
if !ok {
return fmt.Errorf("%v is not a valid regex validator config", config)
}

if c.GetRegex() == "" {
return errors.New("validator regex string cannot be empty")
}

r, err := regexp.Compile(c.GetRegex())
if err != nil {
return fmt.Errorf("error compiling the given regex (%s): %v", c.GetRegex(), err)
}

v.r = r
v.l = l
return nil
}

// Validate the provided responseBody and return true if responseBody matches
// the configured regex.
func (v *Validator) Validate(unusedResponseObj interface{}, responseBody []byte) (bool, error) {
return v.r.Match(responseBody), nil
}
92 changes: 92 additions & 0 deletions validators/regex/regex_test.go
@@ -0,0 +1,92 @@
// Copyright 2018 Google Inc.
//
// 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 regex

import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/google/cloudprober/logger"
configpb "github.com/google/cloudprober/validators/regex/proto"
)

func TestInvalidConfig(t *testing.T) {
// Empty config
testConfig := &configpb.Validator{}
v := Validator{}
err := v.Init(testConfig, &logger.Logger{})
if err == nil {
t.Errorf("v.Init(%v, l): expected error but got nil", testConfig)
}

// Invalid regex as Go regex doesn't support negative lookaheads.
testConfig = &configpb.Validator{
Regex: proto.String("(?!cloudprober)"),
}
v = Validator{}
err = v.Init(testConfig, &logger.Logger{})
if err == nil {
t.Errorf("v.Init(%v, l): expected error but got nil", testConfig)
}
}

func verifyValidate(t *testing.T, respBody []byte, regex string, expected bool) {
t.Helper()
// Test initializing with pattern string.
testConfig := &configpb.Validator{
Regex: proto.String(regex),
}

v := Validator{}
err := v.Init(testConfig, &logger.Logger{})
if err != nil {
t.Errorf("v.Init(%v, l): got error: %v", testConfig, err)
}

result, err := v.Validate(nil, respBody)
if err != nil {
t.Errorf("v.Validate(nil, %s): got error: %v", string(respBody), err)
}

if result != expected {
if err != nil {
t.Errorf("v.Validate(nil, %s): result: %v, expected: %v", string(respBody), result, expected)
}
}
}

func TestPatternString(t *testing.T) {
rows := []struct {
regex string
respBody []byte
expected bool
}{
{
regex: "cloud.*",
respBody: []byte("cloudprober"),
expected: true,
},
{
regex: "[Cc]loud.*",
respBody: []byte("Cloudprober"),
expected: false,
},
}

for _, r := range rows {
verifyValidate(t, r.respBody, r.regex, r.expected)
}

}
4 changes: 4 additions & 0 deletions validators/validators.go
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/google/cloudprober/validators/http"
"github.com/google/cloudprober/validators/integrity"
configpb "github.com/google/cloudprober/validators/proto"
"github.com/google/cloudprober/validators/regex"
)

// Validator interface represents a validator.
Expand Down Expand Up @@ -60,6 +61,9 @@ func initValidator(validatorConf *configpb.Validator, l *logger.Logger) (validat
case *configpb.Validator_IntegrityValidator:
validator = &integrity.Validator{}
c = validatorConf.GetIntegrityValidator()
case *configpb.Validator_RegexValidator:
validator = &regex.Validator{}
c = validatorConf.GetRegexValidator()
default:
err = fmt.Errorf("unknown validator type: %v", validatorConf.Type)
return
Expand Down

0 comments on commit cd0610a

Please sign in to comment.