Skip to content

Commit

Permalink
fixup! fix(find): return detailed error instead of 500
Browse files Browse the repository at this point in the history
  • Loading branch information
msaf1980 committed Mar 14, 2024
1 parent 35974c8 commit 3b60941
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
36 changes: 26 additions & 10 deletions cmd/mockbackend/e2etesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func isRenderEqual(m1, m2 RenderResponse) error {
return nil
}

func doTest(logger *zap.Logger, t *Query) []error {
func doTest(logger *zap.Logger, t *Query, verbose bool) []error {
client := http.Client{}
failures := make([]error, 0)
d, err := time.ParseDuration(fmt.Sprintf("%v", t.Delay) + "s")
Expand Down Expand Up @@ -270,16 +270,24 @@ func doTest(logger *zap.Logger, t *Query) []error {
}

if len(t.ExpectedResponse.ExpectedResults) == 0 {
if len(res) > 0 {
failures = append(failures, merry2.Errorf("unexpected amount of results, got %v, expected 0", len(res)))
}
return failures
}

if len(res) != len(t.ExpectedResponse.ExpectedResults[0].MetricsFind) {
failures = append(failures, merry2.Errorf("unexpected amount of results, got %v, expected %v",
failures = append(failures, merry2.Errorf("unexpected amount of metrics find, got %v, expected %v",
len(res),
len(t.ExpectedResponse.ExpectedResults[0].MetricsFind)))
if verbose {
for i := range t.ExpectedResponse.ExpectedResults[0].MetricsFind {
if len(res) > i || !reflect.DeepEqual(res[i], t.ExpectedResponse.ExpectedResults[0].MetricsFind[i]) {
err = fmt.Errorf("metrics find[%d] are not equal, got=`%+v`, expected=`%+v`", i, res[i], t.ExpectedResponse.ExpectedResults[0].MetricsFind[i])
failures = append(failures, err)
} else {
err = fmt.Errorf("metrics find[%d] got unexpected=`%+v`", i, t.ExpectedResponse.ExpectedResults[0].MetricsFind[i])
}
failures = append(failures, err)
}
}
return failures
}

Expand All @@ -300,16 +308,24 @@ func doTest(logger *zap.Logger, t *Query) []error {
}

if len(t.ExpectedResponse.ExpectedResults) == 0 {
if len(res) > 0 {
failures = append(failures, merry2.Errorf("unexpected amount of results, got %v, expected 0", len(res)))
}
return failures
}

if len(res) != len(t.ExpectedResponse.ExpectedResults[0].Metrics) {
failures = append(failures, merry2.Errorf("unexpected amount of results, got %v, expected %v",
len(res),
len(t.ExpectedResponse.ExpectedResults[0].Metrics)))
if verbose {
for i := range t.ExpectedResponse.ExpectedResults[0].Metrics {
if len(res) > i || !reflect.DeepEqual(res[i], t.ExpectedResponse.ExpectedResults[0].Metrics[i]) {
err = fmt.Errorf("metrics[%d] are not equal, got=`%+v`, expected=`%+v`", i, res[i], t.ExpectedResponse.ExpectedResults[0].Metrics[i])
failures = append(failures, err)
} else {
err = fmt.Errorf("metrics[%d] got unexpected=`%+v`", i, t.ExpectedResponse.ExpectedResults[0].Metrics[i])
}
failures = append(failures, err)
}
}
return failures
}

Expand All @@ -331,7 +347,7 @@ func doTest(logger *zap.Logger, t *Query) []error {
return failures
}

func e2eTest(logger *zap.Logger, noapp, breakOnError bool) bool {
func e2eTest(logger *zap.Logger, noapp, breakOnError, verbose bool) bool {
failed := false
logger.Info("will run test",
zap.Any("config", cfg.Test),
Expand All @@ -355,7 +371,7 @@ func e2eTest(logger *zap.Logger, noapp, breakOnError bool) bool {
}

for _, t := range cfg.Test.Queries {
failures := doTest(logger, &t)
failures := doTest(logger, &t, verbose)
if len(failures) != 0 {
failed = true
logger.Error("test failed",
Expand Down
3 changes: 2 additions & 1 deletion cmd/mockbackend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type listener struct {

func main() {
config := flag.String("config", "average.yaml", "yaml where it would be possible to get data")
verbose := flag.Bool("verbose", false, "verbose reporting")
testonly := flag.Bool("testonly", false, "run only unit test")
noapp := flag.Bool("noapp", false, "do not run application")
test := flag.Bool("test", false, "run unit test if present")
Expand Down Expand Up @@ -120,7 +121,7 @@ func main() {

failed := false
if cfg.Test != nil && (*test || *testonly) {
failed = e2eTest(logger, *noapp, *breakOnError)
failed = e2eTest(logger, *noapp, *breakOnError, *verbose)
}

if !*testonly {
Expand Down

0 comments on commit 3b60941

Please sign in to comment.