Skip to content

pkg/podman: Prevent failing tests from causing a 'panic', and simplify code#1792

Merged
debarshiray merged 2 commits into
containers:mainfrom
debarshiray:wip/rishi/pkg-podman-imageImages_test-panic-imagesCount
May 6, 2026
Merged

pkg/podman: Prevent failing tests from causing a 'panic', and simplify code#1792
debarshiray merged 2 commits into
containers:mainfrom
debarshiray:wip/rishi/pkg-podman-imageImages_test-panic-imagesCount

Conversation

@debarshiray
Copy link
Copy Markdown
Member

Fallout from aa1b533

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
@debarshiray debarshiray force-pushed the wip/rishi/pkg-podman-imageImages_test-panic-imagesCount branch from 6ffc55e to 116a7e2 Compare May 5, 2026 21:20
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/pkg/podman/imageImages_test.go Outdated
@debarshiray debarshiray merged commit 71d2b01 into containers:main May 6, 2026
3 checks passed
@debarshiray debarshiray deleted the wip/rishi/pkg-podman-imageImages_test-panic-imagesCount branch May 6, 2026 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant