Skip to content

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

Merged
cmainas merged 2 commits into
urunc-dev:main-pr584from
parthdagia05:test/hypervisors-utils
May 15, 2026
Merged

test(hypervisors): add unit tests for utils.go helpers#584
cmainas merged 2 commits into
urunc-dev:main-pr584from
parthdagia05:test/hypervisors-utils

Conversation

@parthdagia05
Copy link
Copy Markdown
Contributor

@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.

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 28, 2026

Deploy Preview for urunc canceled.

Name Link
🔨 Latest commit 04c6f6d
🔍 Latest deploy log https://app.netlify.com/projects/urunc/deploys/6a06037a34840c0008e82ce3

@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
Contributor 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
Contributor 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.

@parthdagia05
Copy link
Copy Markdown
Contributor 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.

@parthdagia05
Copy link
Copy Markdown
Contributor Author

@cmainas removed the test

@cmainas
Copy link
Copy Markdown
Contributor

cmainas commented May 14, 2026

Thank you @parthdagia05 ,

you will also need to add yourself here https://github.com/urunc-dev/urunc/blob/main/.github/contributors.yaml and rebase over main

@parthdagia05 parthdagia05 force-pushed the test/hypervisors-utils branch from 76d44c9 to 9046782 Compare May 14, 2026 09:27
@parthdagia05
Copy link
Copy Markdown
Contributor Author

done @cmainas

@cmainas
Copy link
Copy Markdown
Contributor

cmainas commented May 14, 2026

Hello @parthdagia05 ,

can you also squash the first three commits? They do not add any value as single commits and they all related to a specific change.

Adds table-driven tests for the two byte-conversion helpers in
pkg/unikontainers/hypervisors/utils.go, using testify/assert. Each
helper is covered for zero, sub-unit, exact-unit, multiple, and
large-value inputs.

Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
@parthdagia05 parthdagia05 force-pushed the test/hypervisors-utils branch from 9046782 to 8acfc93 Compare May 14, 2026 09:41
@parthdagia05
Copy link
Copy Markdown
Contributor Author

@cmainas done

Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
@parthdagia05 parthdagia05 force-pushed the test/hypervisors-utils branch from 8acfc93 to 04c6f6d Compare May 14, 2026 17:16
@parthdagia05
Copy link
Copy Markdown
Contributor Author

@cmainas the commitlint spellcheck flagged my name, can you once check it and rerun the workflows again

@urunc-bot urunc-bot Bot changed the base branch from main to main-pr584 May 15, 2026 08:27
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.

Thank you @parthdagia05 for the new unit tests.

@cmainas cmainas merged commit 3bd9a52 into urunc-dev:main-pr584 May 15, 2026
51 of 53 checks passed
github-actions Bot pushed a commit that referenced this pull request May 15, 2026
Adds table-driven tests for the two byte-conversion helpers in
pkg/unikontainers/hypervisors/utils.go, using testify/assert. Each
helper is covered for zero, sub-unit, exact-unit, multiple, and
large-value inputs.

PR: #584
Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk>
Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
github-actions Bot pushed a commit that referenced this pull request May 15, 2026
PR: #584
Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk>
Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
urunc-bot Bot pushed a commit that referenced this pull request May 15, 2026
Adds table-driven tests for the two byte-conversion helpers in
pkg/unikontainers/hypervisors/utils.go, using testify/assert. Each
helper is covered for zero, sub-unit, exact-unit, multiple, and
large-value inputs.

PR: #584
Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk>
Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
urunc-bot Bot pushed a commit that referenced this pull request May 15, 2026
PR: #584
Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
Reviewed-by: Charalampos Mainas <cmainas@nubificus.co.uk>
Approved-by: Charalampos Mainas <cmainas@nubificus.co.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants