Browse code

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 <github@gone.nl>

Sebastiaan van Stijn authored on 2023/07/22 03:52:48
Showing 1 changed files
... ...
@@ -7,13 +7,12 @@ import (
7 7
 )
8 8
 
9 9
 func TestErrorInterfaces(t *testing.T) {
10
-	badRequestErrorList := []error{ErrInvalidID(""), ErrInvalidName(""), ErrInvalidJoin{}, ErrInvalidNetworkDriver(""), InvalidContainerIDError(""), ErrNoSuchNetwork(""), ErrNoSuchEndpoint("")}
10
+	badRequestErrorList := []error{ErrInvalidID(""), ErrInvalidName(""), ErrInvalidJoin{}, ErrInvalidNetworkDriver(""), InvalidContainerIDError("")}
11 11
 	for _, err := range badRequestErrorList {
12 12
 		switch u := err.(type) {
13 13
 		case types.BadRequestError:
14
-			return
15 14
 		default:
16
-			t.Fatalf("Failed to detect err %v is of type BadRequestError. Got type: %T", err, u)
15
+			t.Errorf("Failed to detect err %v is of type BadRequestError. Got type: %T", err, u)
17 16
 		}
18 17
 	}
19 18
 
... ...
@@ -21,29 +20,26 @@ func TestErrorInterfaces(t *testing.T) {
21 21
 	for _, err := range maskableErrorList {
22 22
 		switch u := err.(type) {
23 23
 		case types.MaskableError:
24
-			return
25 24
 		default:
26
-			t.Fatalf("Failed to detect err %v is of type MaskableError. Got type: %T", err, u)
25
+			t.Errorf("Failed to detect err %v is of type MaskableError. Got type: %T", err, u)
27 26
 		}
28 27
 	}
29 28
 
30
-	notFoundErrorList := []error{NetworkTypeError(""), &UnknownNetworkError{}, &UnknownEndpointError{}}
29
+	notFoundErrorList := []error{NetworkTypeError(""), &UnknownNetworkError{}, &UnknownEndpointError{}, ErrNoSuchNetwork(""), ErrNoSuchEndpoint("")}
31 30
 	for _, err := range notFoundErrorList {
32 31
 		switch u := err.(type) {
33 32
 		case types.NotFoundError:
34
-			return
35 33
 		default:
36
-			t.Fatalf("Failed to detect err %v is of type NotFoundError. Got type: %T", err, u)
34
+			t.Errorf("Failed to detect err %v is of type NotFoundError. Got type: %T", err, u)
37 35
 		}
38 36
 	}
39 37
 
40
-	forbiddenErrorList := []error{NetworkTypeError(""), &UnknownNetworkError{}, &UnknownEndpointError{}}
38
+	forbiddenErrorList := []error{&ActiveContainerError{}}
41 39
 	for _, err := range forbiddenErrorList {
42 40
 		switch u := err.(type) {
43 41
 		case types.ForbiddenError:
44
-			return
45 42
 		default:
46
-			t.Fatalf("Failed to detect err %v is of type ForbiddenError. Got type: %T", err, u)
43
+			t.Errorf("Failed to detect err %v is of type ForbiddenError. Got type: %T", err, u)
47 44
 		}
48 45
 	}
49 46
 }