Browse code

libnetwork: TestErrorInterfaces: use errdefs helpers to check error-types

Make sure these errors are properly detected by the errdefs helpers to
implement the right interface / definition.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/01/21 23:37:14
Showing 1 changed files
... ...
@@ -3,17 +3,16 @@ package libnetwork
3 3
 import (
4 4
 	"testing"
5 5
 
6
+	"github.com/docker/docker/errdefs"
6 7
 	"github.com/docker/docker/libnetwork/types"
8
+	"gotest.tools/v3/assert"
9
+	is "gotest.tools/v3/assert/cmp"
7 10
 )
8 11
 
9 12
 func TestErrorInterfaces(t *testing.T) {
10 13
 	badRequestErrorList := []error{ErrInvalidID(""), ErrInvalidName("")}
11 14
 	for _, err := range badRequestErrorList {
12
-		switch u := err.(type) {
13
-		case types.InvalidParameterError:
14
-		default:
15
-			t.Errorf("Failed to detect err %v is of type InvalidParameterError. Got type: %T", err, u)
16
-		}
15
+		assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
17 16
 	}
18 17
 
19 18
 	maskableErrorList := []error{ManagerRedirectError("")}
... ...
@@ -27,19 +26,11 @@ func TestErrorInterfaces(t *testing.T) {
27 27
 
28 28
 	notFoundErrorList := []error{&UnknownNetworkError{}, ErrNoSuchNetwork(""), ErrNoSuchEndpoint("")}
29 29
 	for _, err := range notFoundErrorList {
30
-		switch u := err.(type) {
31
-		case types.NotFoundError:
32
-		default:
33
-			t.Errorf("Failed to detect err %v is of type NotFoundError. Got type: %T", err, u)
34
-		}
30
+		assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
35 31
 	}
36 32
 
37 33
 	forbiddenErrorList := []error{&ActiveContainerError{}}
38 34
 	for _, err := range forbiddenErrorList {
39
-		switch u := err.(type) {
40
-		case types.ForbiddenError:
41
-		default:
42
-			t.Errorf("Failed to detect err %v is of type ForbiddenError. Got type: %T", err, u)
43
-		}
35
+		assert.Check(t, is.ErrorType(err, errdefs.IsForbidden))
44 36
 	}
45 37
 }