Skip to content

test(hypervisors): add unit tests for utils.go helpers#584

Open
parthdagia05 wants to merge 3 commits into
urunc-dev:mainfrom
parthdagia05:test/hypervisors-utils
Open

test(hypervisors): add unit tests for utils.go helpers#584
parthdagia05 wants to merge 3 commits into
urunc-dev:mainfrom
parthdagia05:test/hypervisors-utils

Conversation

@parthdagia05
Copy link
Copy Markdown

@parthdagia05 parthdagia05 commented Apr 28, 2026

Description

I have added unit tests for pkg/unikontainers/hypervisors/utils.go, which currently has no test coverage.

The tests use testify/assert and a table-driven, t.Parallel-friendly style consistent with the existing vmm_test.go in the same package.

Functions covered:

  • cpuArch - verified against runtime.GOARCH so the same test works on both amd64 and aarch64 builds.
  • appendNonEmpty - six cases covering empty/non-empty body, prefix and value combinations.
  • bytesToMiB - exact MiB boundary, sub-MiB truncation, large values.
  • bytesToMB - same coverage shape with the decimal MB constant.
  • BytesToStringMB - the three logical branches: zero argument falls back to DefaultMemory, sub-MB value falls back to DefaultMemory, and a normal value passes through.

I also added one extra test (TestBytesToMiBVsMBDistinction) that asserts the binary vs decimal megabyte constants are not interchangeable. Its goal is to catch a refactor that accidentally swaps the two unit helpers - they have the same signature so the compiler won't notice.

killProcess is intentionally not covered in this PR. It requires spawning a real subprocess, and several of its branches cannot be reproduced deterministically without privileged operations or refactoring the function to inject the signaller. Happy to address it in a follow-up if useful.

Related issues

Refs #96

How was this tested?

  • go test -v ./pkg/unikontainers/hypervisors/... - 6 tests, 23 subtests, all pass.
  • go test -count=1 ./... (excluding tests/e2e which needs Docker/QEMU/crictl) - every previously-passing package still passes.
  • gofmt -l pkg/unikontainers/hypervisors/utils_test.go - clean.
  • go vet ./pkg/unikontainers/hypervisors/... - clean.
  • golangci-lint run ./pkg/unikontainers/hypervisors/... (using the project's .golangci.yml v2 config) - 0 issues.
  • make lint - passes locally.

LLM usage

Anthropic Claude Opus 4.6 was used to assist with planning the test scope, identifying edge cases for each helper. All code was reviewed and tested by me before submission.

Checklist

  • I have read the contribution guide.
  • The linter passes locally (make lint).
  • The e2e tests of at least one tool pass locally (make test_ctr, make test_nerdctl, make test_docker, make test_crictl).
  • If LLMs were used: I have read the llm policy.

Adds table-driven unit tests covering the pure helpers in
pkg/unikontainers/hypervisors/utils.go: cpuArch, appendNonEmpty,
bytesToMiB, bytesToMB and BytesToStringMB. Also includes a guard
test that exercises the MB vs MiB distinction, so a refactor that
swaps the two unit constants would fail loudly.

killProcess is intentionally not covered here, since it requires
spawning a real subprocess; it can be addressed in a follow-up
contribution.

Tests follow the existing style in vmm_test.go (testify/assert,
t.Parallel, package-internal access).

Refs: urunc-dev#96
Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 28, 2026

Deploy Preview for urunc ready!

Name Link
🔨 Latest commit 76d44c9
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/6a046b73db59de00085467e3
😎 Deploy Preview https://deploy-preview-584--urunc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@cmainas
Copy link
Copy Markdown
Contributor

cmainas commented Apr 29, 2026

Hello @parthdagia05 ,

thank you for this PR> Please do not overwrite the PR template.

@parthdagia05
Copy link
Copy Markdown
Author

parthdagia05 commented Apr 29, 2026

Hello @parthdagia05 ,

thank you for this PR> Please do not overwrite the PR template.

Sure, made the changes according to the PR template could you take a look again

Copy link
Copy Markdown
Contributor

@cmainas cmainas left a comment

Choose a reason for hiding this comment

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

Hello @parthdagia05 ,

thank you for updating the description.

  • The functions cpuArch, appenNonEmpty and BytesToStringMB seem useless and we need to remove them (not in this PR). So let's drop their testing.
  • Please replace in and want with input and output / expected respectively. Or use some better names than in and want.

// TestBytesToMiBVsMBDistinction guards against a refactor that swaps the two
// constants — 1 MB (10^6) is strictly less than 1 MiB (2^20), so a value of
// exactly 1 MB must round to 0 MiB while reporting 1 MB.
func TestBytesToMiBVsMBDistinction(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not really understand this unit test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I do not really understand this unit test.

Ensures the binary unit and decimal-unit variants stay distinct a copy paste or refactor that collapses them into one would silently produce wrong values. Uses 1,000,000 bytes as input, where binary and decimal results must diverge.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not really agree with such a test. The other unit tests already cover such cases.

Drops tests for cpuArch, appendNonEmpty, and BytesToStringMB —
those helpers are slated for removal, so covering them here
isn't useful. Renames the table-driven `in`/`want` fields to
`input`/`expected` for clarity.

Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
@parthdagia05
Copy link
Copy Markdown
Author

@cmainas removed the tests for cpuArch, appenNonEmpty and BytesToStringM.

Copy link
Copy Markdown
Contributor

@cmainas cmainas left a comment

Choose a reason for hiding this comment

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

Hello @parthdagia05 , le;ts remove the test in question.

// TestBytesToMiBVsMBDistinction guards against a refactor that swaps the two
// constants — 1 MB (10^6) is strictly less than 1 MiB (2^20), so a value of
// exactly 1 MB must round to 0 MiB while reporting 1 MB.
func TestBytesToMiBVsMBDistinction(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I do not really agree with such a test. The other unit tests already cover such cases.

Per review feedback: the existing TestBytesToMiB and TestBytesToMB
cases already pin down the boundary at 1 MB / 1 MiB, so a constant
swap would already be caught by either of them.

Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
@parthdagia05
Copy link
Copy Markdown
Author

@cmainas removed the test

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.

2 participants