distribution/registry/storage/driver/errors_test.go
Milos Gajdos b3681c4cd3
feat: add tparallel linter to improve handling parallel tests
This linter both prevents parallel test races as well as
suggests parallel tests where appropriate:
See: https://github.com/moricho/tparallel

Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com>
2023-11-29 21:40:20 +00:00

81 lines
1.8 KiB
Go

package driver
import (
"encoding/json"
"errors"
"fmt"
"testing"
)
func TestErrorFormat(t *testing.T) {
drvName := "foo"
errMsg := "unexpected error"
e := Error{
DriverName: drvName,
Detail: errors.New(errMsg),
}
exp := fmt.Sprintf("%s: %s", drvName, errMsg)
if e.Error() != exp {
t.Errorf("expected: %s, got: %s", exp, e.Error())
}
b, err := json.Marshal(&e)
if err != nil {
t.Fatal(err)
}
expJson := `{"driver":"foo","detail":"unexpected error"}`
if gotJson := string(b); gotJson != expJson {
t.Fatalf("expected JSON: %s,\n got: %s", expJson, gotJson)
}
}
func TestErrors(t *testing.T) {
t.Parallel()
drvName := "foo"
testCases := []struct {
name string
errs Errors
exp string
expJson string
}{
{
name: "no details",
errs: Errors{DriverName: drvName},
exp: fmt.Sprintf("%s: <nil>", drvName),
expJson: `{"driver":"foo","details":[]}`,
},
{
name: "single detail",
errs: Errors{DriverName: drvName, Errs: []error{errors.New("err msg")}},
exp: fmt.Sprintf("%s: err msg", drvName),
expJson: `{"driver":"foo","details":["err msg"]}`,
},
{
name: "multiple details",
errs: Errors{DriverName: drvName, Errs: []error{errors.New("err msg1"), errors.New("err msg2")}},
exp: fmt.Sprintf("%s: errors:\nerr msg1\nerr msg2\n", drvName),
expJson: `{"driver":"foo","details":["err msg1","err msg2"]}`,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if got := tc.errs.Error(); got != tc.exp {
t.Errorf("got error: %s, expected: %s", got, tc.exp)
}
b, err := json.Marshal(&tc.errs)
if err != nil {
t.Fatal(err)
}
if gotJson := string(b); gotJson != tc.expJson {
t.Errorf("expected JSON: %s,\n got: %s", tc.expJson, gotJson)
}
})
}
}