From b1594b39c021de3f3059d752044f400e5614393e Mon Sep 17 00:00:00 2001 From: Matthew McGovern Date: Mon, 14 Feb 2022 09:13:19 -0800 Subject: [PATCH 1/2] refactor find_partition_with_freespace and hoist to node level --- lisa/node.py | 16 ++++++++++++- lisa/tools/df.py | 14 +++++++++++ microsoft/testsuites/nested/common.py | 34 ++------------------------- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/lisa/node.py b/lisa/node.py index 115c8da348..c8d23ef82a 100644 --- a/lisa/node.py +++ b/lisa/node.py @@ -12,7 +12,7 @@ from lisa.feature import Features from lisa.nic import Nics from lisa.operating_system import OperatingSystem -from lisa.tools import Echo, Reboot +from lisa.tools import Df, Echo, Reboot from lisa.util import ( ContextMixin, InitializableMixin, @@ -426,6 +426,20 @@ def get_working_path(self) -> PurePath: return self.get_pure_path(result.stdout) + def find_partition_with_freespace(self, size_in_gb: int) -> str: + df = self.tools[Df] + home_partition = df.get_partition_by_mountpoint("/home") + if home_partition and df.check_partition_size(home_partition, size_in_gb): + return home_partition.mountpoint + + mnt_partition = df.get_partition_by_mountpoint("/mnt") + if mnt_partition and df.check_partition_size(mnt_partition, size_in_gb): + return mnt_partition.mountpoint + + raise LisaException( + f"No partition with Required disk space of {size_in_gb}GB found" + ) + class LocalNode(Node): def __init__( diff --git a/lisa/tools/df.py b/lisa/tools/df.py index 251f8a5179..f9b03a314b 100644 --- a/lisa/tools/df.py +++ b/lisa/tools/df.py @@ -54,3 +54,17 @@ def get_partition_by_mountpoint( if partition.mountpoint == mountpoint: return partition return None + + def check_partition_size( + self, + partition: PartitionInfo, + size: int, + ) -> bool: + # check if the partition has enough space to download nested image file + unused_partition_size_in_gb = ( + partition.total_blocks - partition.used_blocks + ) / (1024 * 1024) + if unused_partition_size_in_gb > size: + return True + + return False diff --git a/microsoft/testsuites/nested/common.py b/microsoft/testsuites/nested/common.py index b41e51c7c0..49a8d2c526 100644 --- a/microsoft/testsuites/nested/common.py +++ b/microsoft/testsuites/nested/common.py @@ -7,8 +7,7 @@ from lisa.operating_system import Debian, Fedora, Suse from lisa.schema import Node from lisa.tools import Lscpu, Qemu, Wget -from lisa.tools.df import Df, PartitionInfo -from lisa.util import LisaException, SkippedException +from lisa.util import SkippedException NESTED_VM_IMAGE_NAME = "image.qcow2" NESTED_VM_TEST_FILE_NAME = "message.txt" @@ -44,7 +43,7 @@ def connect_nested_vm( "run on Debian, Fedora and Suse distros." ) - image_folder_path = find_partition_with_freespace(host, image_size) + image_folder_path = host.find_partition_with_freespace(image_size) host.tools[Wget].get( url=guest_image_url, @@ -69,21 +68,6 @@ def connect_nested_vm( return nested_vm -def find_partition_with_freespace(node: RemoteNode, size_in_gb: int) -> str: - df = node.tools[Df] - home_partition = df.get_partition_by_mountpoint("/home") - if home_partition and _is_partition_capable(home_partition, size_in_gb): - return home_partition.mountpoint - - mnt_partition = df.get_partition_by_mountpoint("/mnt") - if mnt_partition and _is_partition_capable(mnt_partition, size_in_gb): - return mnt_partition.mountpoint - - raise LisaException( - f"No partition with Required disk space of {size_in_gb}GB found" - ) - - def parse_nested_image_variables( variables: Dict[str, Any] ) -> Tuple[str, str, int, str]: @@ -107,17 +91,3 @@ def parse_nested_image_variables( nested_image_port, nested_image_url, ) - - -def _is_partition_capable( - partition: PartitionInfo, - size: int, -) -> bool: - # check if the partition has enough space to download nested image file - unused_partition_size_in_gb = (partition.total_blocks - partition.used_blocks) / ( - 1024 * 1024 - ) - if unused_partition_size_in_gb > size: - return True - - return False From ce6ce544cf9949e2becb9ea395ec2d39ed6625bf Mon Sep 17 00:00:00 2001 From: Matthew G McGovern Date: Mon, 14 Feb 2022 13:06:33 -0800 Subject: [PATCH 2/2] updates from code comments --- lisa/node.py | 3 +++ lisa/tools/df.py | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lisa/node.py b/lisa/node.py index c8d23ef82a..b4540db030 100644 --- a/lisa/node.py +++ b/lisa/node.py @@ -427,6 +427,9 @@ def get_working_path(self) -> PurePath: return self.get_pure_path(result.stdout) def find_partition_with_freespace(self, size_in_gb: int) -> str: + if self.os.is_windows: + raise NotImplementedError() + df = self.tools[Df] home_partition = df.get_partition_by_mountpoint("/home") if home_partition and df.check_partition_size(home_partition, size_in_gb): diff --git a/lisa/tools/df.py b/lisa/tools/df.py index f9b03a314b..ea14435fe1 100644 --- a/lisa/tools/df.py +++ b/lisa/tools/df.py @@ -58,13 +58,13 @@ def get_partition_by_mountpoint( def check_partition_size( self, partition: PartitionInfo, - size: int, + size_in_gb: int, ) -> bool: # check if the partition has enough space to download nested image file unused_partition_size_in_gb = ( partition.total_blocks - partition.used_blocks ) / (1024 * 1024) - if unused_partition_size_in_gb > size: + if unused_partition_size_in_gb > size_in_gb: return True return False