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

🧹 Rename functions in blockdevices.go. Add more tests, sort suitable partitions by size before choosing one. #4110

Merged
merged 2 commits into from
May 28, 2024

Conversation

preslavgerchev
Copy link
Contributor

  • Rename GetDeviceForMounting to GetMountablePartition
  • Rename GetBlockByEntryName to GetMountablePartitionByDevice
  • Change the cmd we run to be more extensible, using lsblk -o OPTS allows us to include things easily.
  • Sort all suitable partitions and pick the largest one. In the case where we have non-mounted, non-boot partitions and we cannot distinguish between them, we simply take the biggest one
  • Update tests

…partitions by size before choosing one.

Signed-off-by: Preslav <[email protected]>
Copy link
Contributor

github-actions bot commented May 27, 2024

Test Results

3 007 tests  +7   3 006 ✅ +7   1m 25s ⏱️ ±0s
  331 suites ±0       1 💤 ±0 
   23 files   ±0       0 ❌ ±0 

Results for commit 827ec6b. ± Comparison against base commit be98dcb.

This pull request removes 1 and adds 8 tests. Note that renamed tests count towards both.
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMatchingBlockEntryByName
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMountablePartitionByDevice
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMountablePartitionByDevice/ignore_boot_partition_(EFI_label)
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMountablePartitionByDevice/ignore_boot_partition_(boot_label)
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMountablePartitionByDevice/match_by_exact_name
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMountablePartitionByDevice/match_by_interchangeable_name
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMountablePartitionByDevice/no_match_by_device_name
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMountablePartitionByDevice/no_suitable_partition
go.mondoo.com/cnquery/v11/providers/os/connection/snapshot ‑ TestGetMountablePartitionByDevice/return_biggest_partition

♻️ This comment has been updated with latest results.

Copy link
Member

@chris-rock chris-rock 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 @preslavgerchev

@@ -19,12 +20,12 @@ type BlockDevices struct {

type BlockDevice struct {
Name string `json:"name,omitempty"`
FsType string `json:"FsType,omitempty"`
FsType string `json:"fstype,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

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

Is that caused by the fact that we switch to specific fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am really not sure. This is also fstype if you run lsblk -f --json. Don't know why this was specified like that

@@ -33,7 +34,7 @@ type PartitionInfo struct {
}

func (cmdRunner *LocalCommandRunner) GetBlockDevices() (*BlockDevices, error) {
cmd, err := cmdRunner.RunCommand("lsblk -f --json")
cmd, err := cmdRunner.RunCommand("lsblk -bo NAME,SIZE,FSTYPE,MOUNTPOINT,LABEL,UUID --json")
Copy link
Member

Choose a reason for hiding this comment

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

Just for my understanding, why are we adding those additional arguments? Are those fields are not covered with the default output?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope, if you do lsblk -f --json the size isnt there. This also lets us extend this in the future. For example, some partitions have a PARTLABEL which we can use (e.g. in Azure some partitions can be labeled with the BOOT PARTLABEL so we can omit those)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This just makes it easier for us in the future since all we neeed is a an extra column and an extra field in the struct.

log.Debug().Str("name", name).Msg("get matching block entry")
// Searches all the partitions in the target device and finds one that can be mounted. It must be unmounted, non-boot partition
// If multiple partitions meet this criteria, the largest one is returned.
func (blockEntries BlockDevices) GetMountablePartitionByDevice(device string) (*PartitionInfo, error) {
Copy link
Member

Choose a reason for hiding this comment

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

I like the rename to device

Signed-off-by: Preslav <[email protected]>
@preslavgerchev preslavgerchev merged commit de49696 into main May 28, 2024
15 checks passed
@preslavgerchev preslavgerchev deleted the preslav/debug-lsblk branch May 28, 2024 06:21
@github-actions github-actions bot locked and limited conversation to collaborators May 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants