test(hypervisors): add unit tests for utils.go helpers#584
test(hypervisors): add unit tests for utils.go helpers#584parthdagia05 wants to merge 3 commits into
Conversation
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>
✅ Deploy Preview for urunc ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
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 |
cmainas
left a comment
There was a problem hiding this comment.
Hello @parthdagia05 ,
thank you for updating the description.
- The functions
cpuArch,appenNonEmptyandBytesToStringMBseem useless and we need to remove them (not in this PR). So let's drop their testing. - Please replace
inandwantwithinputandoutput/expectedrespectively. Or use some better names thaninandwant.
| // 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) { |
There was a problem hiding this comment.
I do not really understand this unit test.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
|
@cmainas removed the tests for cpuArch, appenNonEmpty and BytesToStringM. |
cmainas
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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>
|
@cmainas removed the test |
Description
I have added unit tests for
pkg/unikontainers/hypervisors/utils.go, which currently has no test coverage.The tests use
testify/assertand a table-driven,t.Parallel-friendly style consistent with the existingvmm_test.goin the same package.Functions covered:
cpuArch- verified againstruntime.GOARCHso 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 toDefaultMemory, sub-MB value falls back toDefaultMemory, 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.killProcessis 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 ./...(excludingtests/e2ewhich 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.ymlv2 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
make lint).make test_ctr,make test_nerdctl,make test_docker,make test_crictl).