From 0e36e957ad1acda2c6af0f1092ecb0da6944337c Mon Sep 17 00:00:00 2001 From: Kristina Pathak Date: Thu, 6 May 2021 14:19:44 -0700 Subject: [PATCH] added unit tests --- basculechecks/metricoptions.go | 2 +- basculechecks/metricoptions_test.go | 73 ++++++++++++++++++++++++++++- basculechecks/parse_test.go | 42 +++++++++++++++++ 3 files changed, 114 insertions(+), 3 deletions(-) diff --git a/basculechecks/metricoptions.go b/basculechecks/metricoptions.go index a8d2d21..74e83e3 100644 --- a/basculechecks/metricoptions.go +++ b/basculechecks/metricoptions.go @@ -37,7 +37,7 @@ func WithServer(s string) MetricOption { func WithEndpoints(e []*regexp.Regexp) MetricOption { return func(m *MetricValidator) { - if e != nil { + if len(e) != 0 { m.endpoints = e } } diff --git a/basculechecks/metricoptions_test.go b/basculechecks/metricoptions_test.go index a34d767..28128b9 100644 --- a/basculechecks/metricoptions_test.go +++ b/basculechecks/metricoptions_test.go @@ -17,6 +17,75 @@ package basculechecks -import "testing" +import ( + "errors" + "fmt" + "regexp" + "testing" -func TestNewMetricValidator(t *testing.T) {} + "github.com/stretchr/testify/assert" +) + +func TestNewMetricValidator(t *testing.T) { + c := &CapabilitiesValidator{} + m := &AuthCapabilityCheckMeasures{} + e := []*regexp.Regexp{regexp.MustCompile(".*")} + s := "testserverrr" + tests := []struct { + description string + checker CapabilitiesChecker + measures *AuthCapabilityCheckMeasures + options []MetricOption + expectedValidator *MetricValidator + expectedErr error + }{ + { + description: "Success", + checker: c, + measures: m, + options: []MetricOption{ + MonitorOnly(), + WithServer(s), + WithServer(""), + WithEndpoints(e), + WithEndpoints(nil), + }, + expectedValidator: &MetricValidator{ + c: c, + measures: m, + server: s, + endpoints: e, + errorOut: false, + }, + }, + { + description: "Success with defaults", + checker: c, + measures: m, + expectedValidator: &MetricValidator{ + c: c, + measures: m, + errorOut: true, + }, + }, + { + description: "Nil Checker Error", + measures: m, + expectedErr: ErrNilChecker, + }, + { + description: "Nil Measures Error", + checker: c, + expectedErr: ErrNilMeasures, + }, + } + for _, tc := range tests { + assert := assert.New(t) + m, err := NewMetricValidator(tc.checker, tc.measures, tc.options...) + assert.Equal(tc.expectedValidator, m) + assert.True(errors.Is(err, tc.expectedErr), + fmt.Errorf("error [%v] doesn't match expected error [%v]", + err, tc.expectedErr), + ) + } +} diff --git a/basculechecks/parse_test.go b/basculechecks/parse_test.go index 881f191..6d54cc6 100644 --- a/basculechecks/parse_test.go +++ b/basculechecks/parse_test.go @@ -18,6 +18,7 @@ package basculechecks import ( + "regexp" "testing" "github.com/stretchr/testify/assert" @@ -65,4 +66,45 @@ func TestDeterminePartnerMetric(t *testing.T) { } func TestDetermineEndpointMetric(t *testing.T) { + var ( + goodURL = "/asnkfn/aefkijeoij/aiogj" + matchingURL = "/fnvvds jkfji/mac:12345544322345334/geigosj" + matchingEndpoint = `/fnvvds jkfji/.*/geigosj\b` + matchingRegex = regexp.MustCompile(matchingEndpoint) + matchingParsed = `/fnvvds_jkfji/.*/geigosj\b` + unusedEndpoint = `/a/b\b` + unusedRegex = regexp.MustCompile(unusedEndpoint) + ) + + tests := []struct { + description string + endpoints []*regexp.Regexp + u string + expectedEndpoint string + }{ + { + description: "No Endpoints", + u: goodURL, + expectedEndpoint: NoneEndpoint, + }, + { + description: "Endpoint Not Recognized", + endpoints: []*regexp.Regexp{unusedRegex, matchingRegex}, + u: goodURL, + expectedEndpoint: NotRecognizedEndpoint, + }, + { + description: "Endpoint Matched", + endpoints: []*regexp.Regexp{unusedRegex, matchingRegex}, + u: matchingURL, + expectedEndpoint: matchingParsed, + }, + } + for _, tc := range tests { + t.Run(tc.description, func(t *testing.T) { + assert := assert.New(t) + endpoint := determineEndpointMetric(tc.endpoints, tc.u) + assert.Equal(tc.expectedEndpoint, endpoint) + }) + } }