pkg/podman: Prevent failing tests from causing a 'panic', and simplify code#1792
Conversation
Currently, if for some reason a test JSON is unmarshalled into an
unexpected number of Image instances, it leads to:
--- FAIL: TestImageImages (0.00s)
--- FAIL: TestImageImages/podman_1.1.2,_fedora-toolbox:29 (0.00s)
imageImages_test.go:974:
Error Trace: toolbox/src/pkg/podman/imageImages_test.go:974
Error: "[]" should have 2 item(s), but has 0
Test: TestImageImages/podman_1.1.2,_fedora-toolbox:29
panic: runtime error: index out of range [0] with length 0 [recovered,
repanicked]
goroutine 31 [running]:
testing.tRunner.func1.2({0x693b40, 0xc00001ea68})
/usr/lib/golang/src/testing/testing.go:1872 +0x237
testing.tRunner.func1()
/usr/lib/golang/src/testing/testing.go:1875 +0x35b
panic({0x693b40?, 0xc00001ea68?})
/usr/lib/golang/src/runtime/panic.go:783 +0x132
github.com/containers/toolbox/pkg/podman.TestImageImages.func1(0xc0001c5340)
toolbox/src/pkg/podman/imageImages_test.go:977 +0xfba
testing.tRunner(0xc0001c5340, 0xc0001cc2c0)
/usr/lib/golang/src/testing/testing.go:1934 +0xea
created by testing.(*T).Run in goroutine 30
/usr/lib/golang/src/testing/testing.go:1997 +0x465
Failing tests shouldn't cause a 'panic', because they are used to
self-document expectations of the code (eg., preconditions,
postconditions), and the 'panic' only leads to noise.
This changes the previous failure to:
--- FAIL: TestImageImages (0.00s)
--- FAIL: TestImageImages/podman_1.1.2,_fedora-toolbox:29 (0.00s)
imageImages_test.go:975:
Error Trace: toolbox/src/pkg/podman/imageImages_test.go:975
Error: "[]" should have 2 item(s), but has 0
Test: TestImageImages/podman_1.1.2,_fedora-toolbox:29
Fallout from aa1b533
containers#1792
6ffc55e to
116a7e2
Compare
There was a problem hiding this comment.
Code Review
This pull request simplifies the TestImageImages unit test by removing the redundant imagesCount field from the test case structure and instead deriving the expected count from the length of the expects slice. Additionally, it updates the assertion to use require.Len for stricter validation. Feedback suggests inlining the length calculation to further streamline the code.
|
Build succeeded. ✔️ unit-test SUCCESS in 2m 23s |
|
Build succeeded. ✔️ unit-test SUCCESS in 2m 19s |
Fallout from aa1b533