Skip to content

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

Open
parthdagia05 wants to merge 1 commit intourunc-dev:mainfrom
parthdagia05:test/hypervisors-utils
Open

test(hypervisors): add unit tests for utils.go helpers#584
parthdagia05 wants to merge 1 commit intourunc-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 canceled.

Name Link
🔨 Latest commit 29f39cb
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/69f139903d624d000749202b

@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

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