Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More refactors, fixes, etc #141

Merged
merged 12 commits into from
May 2, 2024
Merged

Conversation

mmlb
Copy link
Member

@mmlb mmlb commented May 1, 2024

I've split these commits out from #136 where they came up. The commits here are grouped into fixes and qol stuff. The QoL stuff are changes to Executor and some missed code comments/fixes to nvme that were meant to go in during #129.

The other big chunk of changes is to utils/watermark where I ran into incorrect params to rand.Int that would cause us to seek to a negative position. I fixed that and added some documentation and renamed some variables for clarity. Also tweaked the tests a little and added a new test case.

@mmlb mmlb force-pushed the refactors-fixes-etc branch 2 times, most recently from b12973c to 413d493 Compare May 1, 2024 17:31
@mmlb
Copy link
Member Author

mmlb commented May 1, 2024

depends on #137

Copy link
Member

@joelrebel joelrebel left a comment

Choose a reason for hiding this comment

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

These changes seem fine to me

mmlb added 12 commits May 2, 2024 09:00
Since there's only one Exec func there's no need to have WithContext in
the name to differentiate against a non-existent context-less Exec.
Callers might want to treat ineffective wipes differently than other
errors, for example fallback to a different option/tool.

I've also changed the error message a little bit to mimic what it looks
like following stdlib errors. For example:

```
 	fmt.Println(os.Open("non-existent-file"))
```

returns:

```
 <nil> open non-existent-file: no such file or directory
```
Mostly to move file size checks into writeWatermarks and get rid of
global variables.
A few simple refactors to make the tests easier to reason about. Instead
of sharing the testfile and wiping it we now use a small function and
t.TempDir that creates the testfile per test and cleans up after exit.
No need to wipe the test file at the beginning of each test.

Used more assertions from stretchr/testify instead of `if` checks, they
just read better.

Added a test for unsuccessful wipe.

Renamed the subtests to be more descriptive.
The code was not quite right before, even though there was a test that
seemed to be checking this. The test actually failed when seeking past
the end of the file. Some might say same thing but it breaks other code
and test that I was writing that fixes something else in here.
Just looks like a better order to debug/read through.

0 < too small < fails < success!
The previous code was subtly wrong and only caught by random chance when
I ran some tests. We need to limit the random offset so that we can be
sure to write the full buffer without crossing over into the next chunk,
but this needs to happen when getting the random offset and not
calculating the position.

Consider the following situation:

* Disk size is 5120B (chunks are 512B)
* First iteration of the loop so chunkStart = 0
* rand.Int randomly returns 1
* randomPosition is calculated as (0 + 1 - 512) == -511
* We try to seek to position -511 of the disk
* Error

Where now it looks like:

* Disk size is 5120B (chunks are 512B)
* First iteration of the loop so chunkStart = 0
* rand.Int randomly returns 1
* randomPosition is calculated as (0 + 1) == 1
* We try to seek to position 1 of the disk
* Ok

Likewise we are still save at the other extreme:

* Disk size is 5120B (chunks are 512B)
* First iteration of the loop so chunkStart = 0
* rand.Int will only ever return 0 since
  chunkSize-watermarksSize=512-512=0
* randomPosition is calculated as (0 + 0) == 0
* We try to seek to position 0 of the disk
* Ok

Or at the end of the disk:

* Disk size is 5120B (chunks are 512B)
* Last iteration of the loop so chunkStart = 4608
* rand.Int will only ever return 0 since
  chunkSize-watermarksSize=512-512=0
* randomPosition is calculated as (4608 + 0) == 4608
* We try to seek to position 4608 of the disk
* Ok
* Write 512B
* Cursor is now at 5120, right at the end of the disk and Ok
Bit twiddling/parsing isn't very common in Go code so lets add a simple
blurb for anyone checking out the code.
No need to right shift when we are just confirming if the value is
all-zeros or not, 4096 is just as much !0 as 1 is.
I added this in here to prematurely appease a linter. No such linter
complaint exists so lets drop it.
@mmlb mmlb force-pushed the refactors-fixes-etc branch from 413d493 to 9dcc9c0 Compare May 2, 2024 13:00
@mmlb mmlb merged commit f526bc1 into metal-toolbox:main May 2, 2024
5 checks passed
@mmlb mmlb deleted the refactors-fixes-etc branch May 2, 2024 14:08
mmlb referenced this pull request in metal-toolbox/vogelkop Jun 14, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[github.com/metal-toolbox/ironlib](https://github.com/metal-toolbox/ironlib)
| `v0.2.18-0.20240611133518-3514176030a4` -> `v0.2.18` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fmetal-toolbox%2fironlib/v0.2.18?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fmetal-toolbox%2fironlib/v0.2.18?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fmetal-toolbox%2fironlib/v0.2.18-0.20240611133518-3514176030a4/v0.2.18?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fmetal-toolbox%2fironlib/v0.2.18-0.20240611133518-3514176030a4/v0.2.18?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>metal-toolbox/ironlib
(github.com/metal-toolbox/ironlib)</summary>

###
[`v0.2.18`](https://github.com/metal-toolbox/ironlib/releases/tag/v0.2.18)

[Compare
Source](https://github.com/metal-toolbox/ironlib/compare/v0.2.17...v0.2.18)

#### What's Changed

- Vc/instrument firmware by
[@&#8203;DoctorVin](https://github.com/DoctorVin) in
[https://github.com/metal-toolbox/ironlib/pull/123](https://github.com/metal-toolbox/ironlib/pull/123)
- Dockerfile: add support to build non-dist image by
[@&#8203;joelrebel](https://github.com/joelrebel) in
[https://github.com/metal-toolbox/ironlib/pull/124](https://github.com/metal-toolbox/ironlib/pull/124)
- Update actions/checkout action to v4 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/112](https://github.com/metal-toolbox/ironlib/pull/112)
- House/spring cleaning by [@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/128](https://github.com/metal-toolbox/ironlib/pull/128)
- ironlib is able to fill a disk with all zeros by
[@&#8203;turegano-equinix](https://github.com/turegano-equinix) in
[https://github.com/metal-toolbox/ironlib/pull/131](https://github.com/metal-toolbox/ironlib/pull/131)
- ironlib is able to detect ineffective wipes by
[@&#8203;turegano-equinix](https://github.com/turegano-equinix) in
[https://github.com/metal-toolbox/ironlib/pull/135](https://github.com/metal-toolbox/ironlib/pull/135)
- House Cleaning Part 2 - Electric Boogaloo! by
[@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/133](https://github.com/metal-toolbox/ironlib/pull/133)
- Better nvme capability detection by
[@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/129](https://github.com/metal-toolbox/ironlib/pull/129)
- Quiet down tests by [@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/137](https://github.com/metal-toolbox/ironlib/pull/137)
- More refactors, fixes, etc by
[@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/141](https://github.com/metal-toolbox/ironlib/pull/141)
- chore(deps): update docker/login-action action to v3 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/118](https://github.com/metal-toolbox/ironlib/pull/118)
- chore(deps): update docker/build-push-action action to v5 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/113](https://github.com/metal-toolbox/ironlib/pull/113)
- fix(deps): update module github.com/r3labs/diff/v2 to v3 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/108](https://github.com/metal-toolbox/ironlib/pull/108)
- chore(deps): update docker/metadata-action action to v5 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/144](https://github.com/metal-toolbox/ironlib/pull/144)
- chore(deps): update actions/setup-go action to v5 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/143](https://github.com/metal-toolbox/ironlib/pull/143)
- chore(deps): update module google.golang.org/protobuf to v1.33.0
\[security] by [@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/126](https://github.com/metal-toolbox/ironlib/pull/126)
- fix(deps): update module golang.org/x/net to v0.23.0 \[security] by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/116](https://github.com/metal-toolbox/ironlib/pull/116)
- chore(deps): update docker/setup-buildx-action action to v3 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/146](https://github.com/metal-toolbox/ironlib/pull/146)
- fix(deps): update module github.com/sirupsen/logrus to v1.9.3 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/100](https://github.com/metal-toolbox/ironlib/pull/100)
- chore(deps): update anchore/sbom-action action to v0.15.11 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/99](https://github.com/metal-toolbox/ironlib/pull/99)
- fix(deps): update module github.com/tidwall/gjson to v1.17.1 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/109](https://github.com/metal-toolbox/ironlib/pull/109)
- fix(deps): update module golang.org/x/net to v0.24.0 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/145](https://github.com/metal-toolbox/ironlib/pull/145)
- chore(deps): update golangci/golangci-lint-action action to v5 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/147](https://github.com/metal-toolbox/ironlib/pull/147)
- fix(deps): update module github.com/stretchr/testify to v1.9.0 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/101](https://github.com/metal-toolbox/ironlib/pull/101)
- fix(deps): update module github.com/beevik/etree to v1.3.0 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/97](https://github.com/metal-toolbox/ironlib/pull/97)
- fix(deps): update module golang.org/x/net to v0.25.0 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/148](https://github.com/metal-toolbox/ironlib/pull/148)
- chore(deps): update github/codeql-action action to v3 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/149](https://github.com/metal-toolbox/ironlib/pull/149)
- Update CODEOWNERS by
[@&#8203;DoctorVin](https://github.com/DoctorVin) in
[https://github.com/metal-toolbox/ironlib/pull/152](https://github.com/metal-toolbox/ironlib/pull/152)
- Update modules that renovate is having trouble with by
[@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/150](https://github.com/metal-toolbox/ironlib/pull/150)
- chore(deps): update golangci/golangci-lint-action action to v6 by
[@&#8203;renovate](https://github.com/renovate) in
[https://github.com/metal-toolbox/ironlib/pull/153](https://github.com/metal-toolbox/ironlib/pull/153)
- Add DiskWipe support to nvme using sanitize by
[@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/136](https://github.com/metal-toolbox/ironlib/pull/136)
- Add format support to nvme WipeDisk by
[@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/142](https://github.com/metal-toolbox/ironlib/pull/142)
- Add ns delete/create support to nvme WipeDisk by
[@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/154](https://github.com/metal-toolbox/ironlib/pull/154)
- More clean ups by [@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/158](https://github.com/metal-toolbox/ironlib/pull/158)
- Add basic support for blkdiscard by
[@&#8203;ScottGarman](https://github.com/ScottGarman) in
[https://github.com/metal-toolbox/ironlib/pull/159](https://github.com/metal-toolbox/ironlib/pull/159)
- Some more clean ups by [@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/161](https://github.com/metal-toolbox/ironlib/pull/161)
- Rename Disk stuff -> Drive and teach diskwipe example more tricks by
[@&#8203;mmlb](https://github.com/mmlb) in
[https://github.com/metal-toolbox/ironlib/pull/163](https://github.com/metal-toolbox/ironlib/pull/163)

#### New Contributors

- [@&#8203;turegano-equinix](https://github.com/turegano-equinix) made
their first contribution in
[https://github.com/metal-toolbox/ironlib/pull/131](https://github.com/metal-toolbox/ironlib/pull/131)
- [@&#8203;ScottGarman](https://github.com/ScottGarman) made their
first contribution in
[https://github.com/metal-toolbox/ironlib/pull/159](https://github.com/metal-toolbox/ironlib/pull/159)

**Full Changelog**:
metal-toolbox/ironlib@v0.2.17...v0.2.18

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/metal-toolbox/vogelkop).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zOTMuMCIsInVwZGF0ZWRJblZlciI6IjM3LjM5My4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
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