| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package scrapererror |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
) |
| 14 |
|
| 15 |
func TestScrapeErrorsAddPartial(t *testing.T) { |
| 16 |
err1 := errors.New("err 1") |
| 17 |
err2 := errors.New("err 2") |
| 18 |
expected := []error{ |
| 19 |
PartialScrapeError{error: err1, Failed: 1}, |
| 20 |
PartialScrapeError{error: err2, Failed: 10}, |
| 21 |
} |
| 22 |
|
| 23 |
var errs ScrapeErrors |
| 24 |
errs.AddPartial(1, err1) |
| 25 |
errs.AddPartial(10, err2) |
| 26 |
assert.Equal(t, expected, errs.errs) |
| 27 |
} |
| 28 |
|
| 29 |
func TestScrapeErrorsAdd(t *testing.T) { |
| 30 |
err1 := errors.New("err a") |
| 31 |
err2 := errors.New("err b") |
| 32 |
expected := []error{err1, err2} |
| 33 |
|
| 34 |
var errs ScrapeErrors |
| 35 |
errs.Add(err1) |
| 36 |
errs.Add(err2) |
| 37 |
assert.Equal(t, expected, errs.errs) |
| 38 |
} |
| 39 |
|
| 40 |
func TestScrapeErrorsCombine(t *testing.T) { |
| 41 |
testCases := []struct { |
| 42 |
errs func() ScrapeErrors |
| 43 |
expectedErr string |
| 44 |
expectedFailedCount int |
| 45 |
expectNil bool |
| 46 |
expectedScrape bool |
| 47 |
}{ |
| 48 |
{ |
| 49 |
errs: func() ScrapeErrors { |
| 50 |
var errs ScrapeErrors |
| 51 |
return errs |
| 52 |
}, |
| 53 |
expectNil: true, |
| 54 |
}, |
| 55 |
{ |
| 56 |
errs: func() ScrapeErrors { |
| 57 |
var errs ScrapeErrors |
| 58 |
errs.AddPartial(10, errors.New("bad scrapes")) |
| 59 |
errs.AddPartial(1, fmt.Errorf("err: %w", errors.New("bad scrape"))) |
| 60 |
return errs |
| 61 |
}, |
| 62 |
expectedErr: "bad scrapes; err: bad scrape", |
| 63 |
expectedFailedCount: 11, |
| 64 |
expectedScrape: true, |
| 65 |
}, |
| 66 |
{ |
| 67 |
errs: func() ScrapeErrors { |
| 68 |
var errs ScrapeErrors |
| 69 |
errs.Add(errors.New("bad regular")) |
| 70 |
errs.Add(fmt.Errorf("err: %w", errors.New("bad reg"))) |
| 71 |
return errs |
| 72 |
}, |
| 73 |
expectedErr: "bad regular; err: bad reg", |
| 74 |
}, |
| 75 |
{ |
| 76 |
errs: func() ScrapeErrors { |
| 77 |
var errs ScrapeErrors |
| 78 |
errs.AddPartial(2, errors.New("bad two scrapes")) |
| 79 |
errs.AddPartial(10, fmt.Errorf("%d scrapes failed: %w", 10, errors.New("bad things happened"))) |
| 80 |
errs.Add(errors.New("bad event")) |
| 81 |
errs.Add(fmt.Errorf("event: %w", errors.New("something happened"))) |
| 82 |
return errs |
| 83 |
}, |
| 84 |
expectedErr: "bad two scrapes; 10 scrapes failed: bad things happened; bad event; event: something happened", |
| 85 |
expectedFailedCount: 12, |
| 86 |
expectedScrape: true, |
| 87 |
}, |
| 88 |
} |
| 89 |
|
| 90 |
for _, tt := range testCases { |
| 91 |
scrapeErrs := tt.errs() |
| 92 |
if tt.expectNil { |
| 93 |
require.NoError(t, scrapeErrs.Combine()) |
| 94 |
continue |
| 95 |
} |
| 96 |
require.EqualError(t, scrapeErrs.Combine(), tt.expectedErr) |
| 97 |
if tt.expectedScrape { |
| 98 |
var partialScrapeErr PartialScrapeError |
| 99 |
if !errors.As(scrapeErrs.Combine(), &partialScrapeErr) { |
| 100 |
t.Errorf("%+v.Combine() = %q. Want: PartialScrapeError", scrapeErrs, scrapeErrs.Combine()) |
| 101 |
} else if tt.expectedFailedCount != partialScrapeErr.Failed { |
| 102 |
t.Errorf("%+v.Combine().Failed. Got %d Failed count. Want: %d", scrapeErrs, partialScrapeErr.Failed, tt.expectedFailedCount) |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |