test(hypervisors): add unit tests for utils.go helpers#584
Conversation
✅ Deploy Preview for urunc canceled.
|
|
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.
|
@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.
|
@cmainas removed the test |
|
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 |
76d44c9 to
9046782
Compare
|
done @cmainas |
|
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>
9046782 to
8acfc93
Compare
|
@cmainas done |
Signed-off-by: Parth Dagia <parth.24bcs10414@sst.scaler.com>
8acfc93 to
04c6f6d
Compare
|
@cmainas the commitlint spellcheck flagged my name, can you once check it and rerun the workflows again |
cmainas
left a comment
There was a problem hiding this comment.
Thank you @parthdagia05 for the new unit tests.
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>
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>
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>
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>
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).