Skip to content

Commit d2d7c32

Browse files
lubaihua33LiliDeng
authored andcommitted
Add swap file checking in verify_no_swap_on_osdisk
1 parent f7a0725 commit d2d7c32

File tree

2 files changed

+57
-24
lines changed

2 files changed

+57
-24
lines changed

lisa/tools/swap.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT license.
33
import re
4+
from dataclasses import dataclass
45
from typing import List, Optional, Type
56

67
from lisa.base_tools import Cat
78
from lisa.executable import Tool
9+
from lisa.tools.df import Df
810
from lisa.tools.lsblk import Lsblk
911
from lisa.tools.rm import Rm
1012
from lisa.util import (
@@ -14,6 +16,13 @@
1416
)
1517

1618

19+
@dataclass
20+
class SwapPartition(object):
21+
filename: str = ""
22+
type: str = ""
23+
partition: str = ""
24+
25+
1726
class SwapOn(Tool):
1827
@property
1928
def command(self) -> str:
@@ -66,7 +75,7 @@ def is_swap_enabled(self) -> bool:
6675
lsblk = self.node.tools[Lsblk].run().stdout
6776
return "SWAP" in lsblk
6877

69-
def get_swap_partitions(self) -> List[str]:
78+
def get_swap_partitions(self) -> List[SwapPartition]:
7079
# run 'cat /proc/swaps' or 'swapon -s' and parse the output
7180
# The output is in the following format:
7281
# <Filename> <Type> <Size> <Used> <Priority>
@@ -82,11 +91,25 @@ def get_swap_partitions(self) -> List[str]:
8291
raise LisaException("Failed to get swap information")
8392

8493
output = swap_result.stdout
85-
swap_parts: List[str] = []
94+
swap_parts: List[SwapPartition] = []
8695
swap_entries = find_patterns_groups_in_lines(output, [self._SWAPS_PATTERN])[0]
8796
for swap_entry in swap_entries:
88-
if swap_entry["type"] == "partition":
89-
swap_parts.append(swap_entry["filename"])
97+
filename = swap_entry["filename"]
98+
file_type = swap_entry["type"]
99+
if file_type == "partition":
100+
partition = filename
101+
else:
102+
# For swap files, get the partition information
103+
part = self.node.tools[Df].get_partition_by_path(filename)
104+
if not part:
105+
raise LisaException(
106+
f"Failed to get partition information for {filename}"
107+
)
108+
partition = part.name
109+
swap_parts.append(
110+
SwapPartition(filename=filename, type=file_type, partition=partition)
111+
)
112+
90113
return swap_parts
91114

92115
def create_swap(
@@ -121,7 +144,7 @@ def is_swap_enabled(self) -> bool:
121144

122145
return False
123146

124-
def get_swap_partitions(self) -> List[str]:
147+
def get_swap_partitions(self) -> List[SwapPartition]:
125148
# run 'swapinfo -k' and parse the output
126149
# The output is in the following format:
127150
# <Device> <1K-blocks> <Used> <Avail> <Capacity>
@@ -130,9 +153,15 @@ def get_swap_partitions(self) -> List[str]:
130153
raise LisaException("Failed to get swap information")
131154

132155
output = swap_result.stdout
133-
swap_parts: List[str] = []
156+
swap_parts: List[SwapPartition] = []
134157
swap_entries = find_patterns_groups_in_lines(output, [self._SWAP_ENTRIES])[0]
135158
# FreeBSD doesn't have swap files, only partitions
136159
for swap_entry in swap_entries:
137-
swap_parts.append(swap_entry["device"])
160+
swap_parts.append(
161+
SwapPartition(
162+
filename=swap_entry["device"],
163+
type="partition",
164+
partition=swap_entry["device"],
165+
)
166+
)
138167
return swap_parts

microsoft/testsuites/core/azure_image_standard.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,21 +1645,22 @@ def verify_omi_version(self, node: Node) -> None:
16451645

16461646
@TestCaseMetadata(
16471647
description="""
1648-
This test verifies that there is no swap partition on the OS disk.
1648+
This test verifies that there is no swap partition or swap file on the OS disk
16491649
16501650
Azure's policy 200.3.3 Linux:
16511651
No swap partition on the OS disk. Swap can be requested for creation on the
16521652
local resource disk by the Linux Agent. It is recommended that a single root
16531653
partition is created for the OS disk.
16541654
1655-
There should be no Swap Partition on OS Disk. OS disk has IOPS limit. When
1656-
memory pressure causes swapping, IOPS limit may be reached easily and cause VM
1657-
performance to go down disastrously, because aside from memory issues in now
1658-
also has IO issues.
1655+
There should be no Swap Partition or swap file on OS Disk. OS disk has IOPS
1656+
limit. When memory pressure causes swapping, IOPS limit may be reached easily
1657+
and cause VM performance to go down disastrously, because aside from memory
1658+
issues it now also has IO issues.
16591659
16601660
Steps:
1661-
1. Use 'cat /proc/swaps' or 'swapon -s' to list all swap devices
1662-
Note: For FreeBSD, use 'swapinfo -k'.
1661+
1. Use 'cat /proc/swaps' or 'swapon -s' to list all swap devices and swap files
1662+
If it is a swap file, use 'df <swap_file>' to get the partition name.
1663+
Note: For FreeBSD, use 'swapinfo -k'. FreeBSD only supports swap partition.
16631664
2. Use 'lsblk <swap_part> -P -o NAME' to get the real block device name for
16641665
each swap partition. If there is no swap partition, pass the case.
16651666
3. Use 'lsblk' to identify the OS disk and get all its partitions and logical
@@ -1685,12 +1686,15 @@ def verify_no_swap_on_osdisk(self, node: Node) -> None:
16851686
lsblk = node.tools[Lsblk]
16861687
os_disk = lsblk.find_disk_by_mountpoint("/")
16871688
for swap_part in swap_parts:
1688-
block_name = lsblk.get_block_name(swap_part)
1689+
block_name = lsblk.get_block_name(swap_part.partition)
16891690
if block_name == "":
16901691
raise LisaException(
1691-
f"Failed to get the device name for swap partition '{swap_part}'."
1692+
"Failed to get the device name for swap partition/file "
1693+
f"'{swap_part.filename}'."
16921694
)
1693-
node.log.info(f"Swap partition '{swap_part}' is on device '{block_name}'.")
1695+
node.log.info(
1696+
f"Swap partition '{swap_part.filename}' is on device '{block_name}'."
1697+
)
16941698
for part in os_disk.partitions:
16951699
# e.g. 'sda1', 'vg-root', 'vg-home'
16961700
parts = [part] + part.logical_devices
@@ -1700,13 +1704,13 @@ def verify_no_swap_on_osdisk(self, node: Node) -> None:
17001704
# "Swap partition .* is found on OS disk" is a failure triage
17011705
# pattern. Please be careful when changing this string.
17021706
raise LisaException(
1703-
f"Swap partition '{swap_part}' is found on OS disk "
1704-
f"partition or logical device '{p.name}'. There should be "
1705-
"no Swap Partition on OS Disk. OS disk has IOPS limit. "
1706-
"When memory pressure causes swapping, IOPS limit may be "
1707-
"reached easily and cause VM performance to go down "
1708-
"disastrously, as aside from memory issues in now also has"
1709-
" IO issues."
1707+
f"Swap partition/file '{swap_part.filename}' is found on "
1708+
f"OS disk partition or logical device '{p.name}'. There "
1709+
"should be no Swap Partition on OS Disk. OS disk has IOPS"
1710+
" limit. When memory pressure causes swapping, IOPS limit"
1711+
" may be reached easily and cause VM performance to go "
1712+
"down disastrously, as aside from memory issues it now "
1713+
"also has IO issues."
17101714
)
17111715

17121716
@TestCaseMetadata(

0 commit comments

Comments
 (0)