From be8ff186d608f38fa6dfec3b3d3786a4aa8f49f0 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 21 Jul 2023 20:52:48 +0200 Subject: [PATCH] libnetwork: refactor TestErrorInterfaces into a test This function was added in libnetwork through https://github.com/moby/libnetwork/commit/50964c994831c3a960d306ff130ff42983e2fe4a and, based on the name of the function and its signature, I think it was meant to be a test. This patch refactors it to be one. Changing it into a test made it slightly broken: go test -v -run TestErrorInterfaces === RUN TestErrorInterfaces errors_test.go:15: Failed to detect err network not found is of type BadRequestError. Got type: libnetwork.ErrNoSuchNetwork errors_test.go:15: Failed to detect err endpoint not found is of type BadRequestError. Got type: libnetwork.ErrNoSuchEndpoint errors_test.go:42: Failed to detect err unknown driver "" is of type ForbiddenError. Got type: libnetwork.NetworkTypeError errors_test.go:42: Failed to detect err unknown network id is of type ForbiddenError. Got type: *libnetwork.UnknownNetworkError errors_test.go:42: Failed to detect err unknown endpoint id is of type ForbiddenError. Got type: *libnetwork.UnknownEndpointError --- FAIL: TestErrorInterfaces (0.00s) FAIL This was because some errors were tested twice, but for the wrong type (`NetworkTypeError`, `UnknownNetworkError`, `UnknownEndpointError`). Moving them to the right test left no test-cases for `types.ForbiddenError`, so I added `ActiveContainerError` to not make that part of the code feel lonely. Other failures were because some errors were changed from `types.BadRequestError` to a `types.NotFoundError` error in commit https://github.com/moby/libnetwork/commit/ba012a703a42e3f6c77386cf995a4a09249fd58a, so I moved those to the right part. Before this patch: go test -v -run TestErrorInterfaces === RUN TestErrorInterfaces --- PASS: TestErrorInterfaces (0.00s) PASS ok github.com/docker/docker/libnetwork 0.013s After this patch: go test -v -run TestErrorInterfaces === RUN TestErrorInterfaces --- PASS: TestErrorInterfaces (0.00s) PASS ok github.com/docker/docker/libnetwork 0.013s Signed-off-by: Sebastiaan van Stijn --- libnetwork/errors_test.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/libnetwork/errors_test.go b/libnetwork/errors_test.go index 6e6c6c107c..90a30d3408 100644 --- a/libnetwork/errors_test.go +++ b/libnetwork/errors_test.go @@ -7,13 +7,12 @@ import ( ) func TestErrorInterfaces(t *testing.T) { - badRequestErrorList := []error{ErrInvalidID(""), ErrInvalidName(""), ErrInvalidJoin{}, ErrInvalidNetworkDriver(""), InvalidContainerIDError(""), ErrNoSuchNetwork(""), ErrNoSuchEndpoint("")} + badRequestErrorList := []error{ErrInvalidID(""), ErrInvalidName(""), ErrInvalidJoin{}, ErrInvalidNetworkDriver(""), InvalidContainerIDError("")} for _, err := range badRequestErrorList { switch u := err.(type) { case types.BadRequestError: - return default: - t.Fatalf("Failed to detect err %v is of type BadRequestError. Got type: %T", err, u) + t.Errorf("Failed to detect err %v is of type BadRequestError. Got type: %T", err, u) } } @@ -21,29 +20,26 @@ func TestErrorInterfaces(t *testing.T) { for _, err := range maskableErrorList { switch u := err.(type) { case types.MaskableError: - return default: - t.Fatalf("Failed to detect err %v is of type MaskableError. Got type: %T", err, u) + t.Errorf("Failed to detect err %v is of type MaskableError. Got type: %T", err, u) } } - notFoundErrorList := []error{NetworkTypeError(""), &UnknownNetworkError{}, &UnknownEndpointError{}} + notFoundErrorList := []error{NetworkTypeError(""), &UnknownNetworkError{}, &UnknownEndpointError{}, ErrNoSuchNetwork(""), ErrNoSuchEndpoint("")} for _, err := range notFoundErrorList { switch u := err.(type) { case types.NotFoundError: - return default: - t.Fatalf("Failed to detect err %v is of type NotFoundError. Got type: %T", err, u) + t.Errorf("Failed to detect err %v is of type NotFoundError. Got type: %T", err, u) } } - forbiddenErrorList := []error{NetworkTypeError(""), &UnknownNetworkError{}, &UnknownEndpointError{}} + forbiddenErrorList := []error{&ActiveContainerError{}} for _, err := range forbiddenErrorList { switch u := err.(type) { case types.ForbiddenError: - return default: - t.Fatalf("Failed to detect err %v is of type ForbiddenError. Got type: %T", err, u) + t.Errorf("Failed to detect err %v is of type ForbiddenError. Got type: %T", err, u) } } }