-
Notifications
You must be signed in to change notification settings - Fork 124
improve error handling around disk partitioning #430
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||
| "errors" | ||||||
| "fmt" | ||||||
| "strings" | ||||||
| "time" | ||||||
|
|
||||||
| "github.com/cloudfoundry/bosh-utils/system/fakes" | ||||||
| . "github.com/onsi/ginkgo/v2" | ||||||
|
|
@@ -264,5 +265,45 @@ | |||||
| Expect(err).To(HaveOccurred()) | ||||||
| Expect(err.Error()).To(ContainSubstring(`AssignDriveLetter: invalid partition number "2;Invoke-Expression"`)) | ||||||
| }) | ||||||
|
|
||||||
| It("retries when Get-Partition reports char(0) before returning the valid letter", func() { | ||||||
| // Simulate WMI returning the null character on the first poll, then the | ||||||
| // real letter on the second. Without the retry the function would return "" | ||||||
| // and the caller would create a junction pointing at bare ":", which Windows | ||||||
| // resolves to the current directory on the default drive (often C:\). | ||||||
| partitioner.DriveLetterPollInterval = time.Millisecond | ||||||
|
|
||||||
| addKey := strings.Join([]string{"-NoProfile", "-NonInteractive", "-Command", | ||||||
| `Add-PartitionAccessPath -DiskNumber 1 -PartitionNumber 2 -AssignDriveLetter`}, " ") | ||||||
| getKey := strings.Join([]string{"-NoProfile", "-NonInteractive", "-Command", | ||||||
| `Get-Partition -DiskNumber 1 -PartitionNumber 2 | Select-Object -ExpandProperty DriveLetter`}, " ") | ||||||
|
|
||||||
| cmdRunner.AddCmdResult(addKey, fakes.FakeCmdResult{}) | ||||||
| cmdRunner.AddCmdResult(getKey, fakes.FakeCmdResult{Stdout: "\x00\n"}) // null char — unassigned | ||||||
| cmdRunner.AddCmdResult(getKey, fakes.FakeCmdResult{Stdout: "E\n"}) // assigned on second poll | ||||||
|
Check failure on line 283 in platform/windows/disk/partitioner_test.go
|
||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix goimports formatting at this line to unblock lint. CI is currently failing goimports on this line; please run goimports on the file and commit the formatted result. Likely formatting-only adjustment- cmdRunner.AddCmdResult(getKey, fakes.FakeCmdResult{Stdout: "E\n"}) // assigned on second poll
+ cmdRunner.AddCmdResult(getKey, fakes.FakeCmdResult{Stdout: "E\n"}) // assigned on second poll📝 Committable suggestion
Suggested change
🧰 Tools🪛 GitHub Check: lint (macos-latest)[failure] 283-283: 🪛 GitHub Check: lint (ubuntu-latest)[failure] 283-283: 🤖 Prompt for AI Agents |
||||||
|
|
||||||
| driveLetter, err := partitioner.AssignDriveLetter(diskNumber, partitionNumber) | ||||||
| Expect(err).NotTo(HaveOccurred()) | ||||||
| Expect(driveLetter).To(Equal("E")) | ||||||
| }) | ||||||
|
|
||||||
| It("returns an error when DriveLetter stays null across all retry attempts", func() { | ||||||
| partitioner.DriveLetterPollInterval = time.Millisecond | ||||||
|
|
||||||
| addKey := strings.Join([]string{"-NoProfile", "-NonInteractive", "-Command", | ||||||
| `Add-PartitionAccessPath -DiskNumber 1 -PartitionNumber 2 -AssignDriveLetter`}, " ") | ||||||
| getKey := strings.Join([]string{"-NoProfile", "-NonInteractive", "-Command", | ||||||
| `Get-Partition -DiskNumber 1 -PartitionNumber 2 | Select-Object -ExpandProperty DriveLetter`}, " ") | ||||||
|
|
||||||
| cmdRunner.AddCmdResult(addKey, fakes.FakeCmdResult{}) | ||||||
| // Sticky so every Get-Partition call returns the null char. | ||||||
| cmdRunner.AddCmdResult(getKey, fakes.FakeCmdResult{Stdout: "\x00\n", Sticky: true}) | ||||||
|
|
||||||
| _, err := partitioner.AssignDriveLetter(diskNumber, partitionNumber) | ||||||
| Expect(err).To(MatchError(ContainSubstring( | ||||||
| fmt.Sprintf("drive letter was not assigned to partition %s on disk %s after 10 attempts", | ||||||
| partitionNumber, diskNumber), | ||||||
| ))) | ||||||
| }) | ||||||
| }) | ||||||
| }) | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid sleeping after the last retry attempt.
The loop sleeps even on the final failed attempt, adding an unnecessary delay before returning the error.
Proposed fix
for attempt := 0; attempt < 10; attempt++ { stdout, _, _, err := p.ps(driveScript) if err != nil { return "", fmt.Errorf( "failed to find drive letter for partition %s on disk %s: %s", partitionNumber, diskNumber, err, ) } // The DriveLetter property is a char; an unassigned partition returns the null // character (\x00). Strip it along with whitespace before checking. letter := strings.Trim(strings.TrimSpace(stdout), "\x00") if letter != "" { return letter, nil } - time.Sleep(pollInterval) + if attempt < 9 { + time.Sleep(pollInterval) + } }📝 Committable suggestion
🤖 Prompt for AI Agents