Skip to content

Commit 63033e3

Browse files
authored
unit test updates (#56)
- simplify unit test plugin fixtures and example data
1 parent bd7edf2 commit 63033e3

File tree

8 files changed

+73
-182
lines changed

8 files changed

+73
-182
lines changed
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"arch_rel": "NAME=\"Arch Linux\"\nPRETTY_NAME=\"Arch Linux\"\nID=arch\nID_LIKE=arch\nVERSION_ID=20241124.0.282387",
3-
"arch_package": "acl 2.3.2-1\nargon2 20190702-6\nattr 2.5.2-1",
3+
"arch_package": "test-arch-package-a 1.11-1\ntest-arch-package-b 2.11-1",
44

55
"deb_rel": "PRETTY_NAME=\"Debian GNU/Linux 12 (bookworm)\"\nID=debian\nID_LIKE=debian\nVERSION_ID=\"12\"",
6-
"debian_package": "adduser 3.134\napt 2.6.1\nbase-files 12.4+deb12u8\nbash 5.2.15-2+b7",
6+
"debian_package": "test-deb-package-a.x86_64 3.11-1\ntest-deb-package-b.x86_64 4.11-1",
77

88
"ubuntu_rel": "DISTRIB_DESCRIPTION=\"Ubuntu 22.04.4 LTS\"\nPRETTY_NAME=\"Ubuntu 22.04.4 LTS\"\nID=ubuntu\nID_LIKE=debian\nVERSION_ID=\"22.04\"",
9-
"ubuntu_package": "alternatives.x86_64 1.30-1.fc41\naudit-libs.x86_64 4.0.2-1.fc41\nauthselect.x86_64 1.5.0-8.fc41\nauthselect-libs.x86_64 1.5.0-8.fc41",
9+
"ubuntu_package": "test-ubuntu-package-a.x86_64 5.11-1\ntest-ubuntu-package-b.x86_64 6.11-1",
1010

1111
"centos_rel": "CentOS Linux release 8.4.2105\nPRETTY_NAME=\"CentOS Linux 8\"\nID=centos\nID_LIKE=\"rhel fedora\"\nVERSION_ID=\"8\"",
12-
"centos_package": "acl.x86_64 2.2.53-1.el8\naudit-libs.x86_64 3.0-0.17.20191104git1c2f876.el8\nbind-export-libs.x86_64 32:9.11.26-3.el8",
12+
"centos_package": "test-centos-package-a.x86_64 7.11-1\ntest-centos-package-b.x86_64 8.11-1",
1313

1414
"fedora_rel": "Fedora Linux release 41 (Container Image)\nPRETTY_NAME=\"Fedora Linux 41 (Container Image)\"\nID=fedora\nID_LIKE=fedora\nVERSION_ID=41",
15-
"fedora_package": "alternatives.x86_64 1.31-1.fc41\naudit-libs.x86_64 4.0.3-1.fc41\nbzip2.x86_64 1.0.8-19.fc41",
15+
"fedora_package": "test-fed-package-a.x86_64 9.11-1\ntest-fed-package-b.x86_64 10.11-1",
1616

1717
"ol8_rel": "Oracle Linux Server release 8.8\nPRETTY_NAME=\"Oracle Linux Server 8.8\"\nID=ol\nID_LIKE=\"rhel fedora\"\nVERSION_ID=\"8.8\"",
18-
"ol8_package": "NetworkManager.x86_64 1:1.40.16-15.0.1.el8_9\nNetworkManager-tui.x86_64 1:1.40.16-15.0.1.el8_9",
18+
"ol8_package": "test-ocl-package-a.x86_64 11.11-1\ntest-ocl-package-b.x86_64 12.11-1",
1919

20-
"windows_package": "Name Version\nMicrosoft Policy Platform 68.1.9086.1017"
20+
"windows_package": "Name Version\nTest Windows Package 11.1.11.1111"
2121
}

test/unit/plugin/fixtures/package_example_data.json

Lines changed: 0 additions & 101 deletions
This file was deleted.

test/unit/plugin/test_bios_analyzer.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@
3535

3636
@pytest.fixture
3737
def bios_model():
38-
return BiosDataModel(bios_version="RMP1004BS")
38+
return BiosDataModel(bios_version="TESTBIOS")
3939

4040

4141
def test_nominal_with_config(bios_model, system_info):
4242
analyzer = BiosAnalyzer(system_info=system_info)
43-
args = BiosAnalyzerArgs(exp_bios_version=["RMP1004BS"])
43+
args = BiosAnalyzerArgs(exp_bios_version=["TESTBIOS"])
4444
res = analyzer.analyze_data(bios_model, args)
4545
assert res.status == ExecutionStatus.OK
4646
assert len(res.events) == 0
4747

4848

4949
def test_single_string_exp_bios_version(bios_model, system_info):
5050
analyzer = BiosAnalyzer(system_info=system_info)
51-
args = BiosAnalyzerArgs(exp_bios_version="RMP1004BS") # string instead of list
51+
args = BiosAnalyzerArgs(exp_bios_version="TESTBIOS") # string instead of list
5252
res = analyzer.analyze_data(bios_model, args)
5353
assert res.status == ExecutionStatus.OK
5454
assert len(res.events) == 0
@@ -64,7 +64,7 @@ def test_no_config(bios_model, system_info):
6464
def test_invalid_bios(system_info):
6565
model = BiosDataModel(bios_version="some_invalid_bios")
6666
analyzer = BiosAnalyzer(system_info=system_info)
67-
args = BiosAnalyzerArgs(exp_bios_version=["RMP1004BS"])
67+
args = BiosAnalyzerArgs(exp_bios_version=["TESTBIOS"])
6868
res = analyzer.analyze_data(model, args)
6969
assert res.status == ExecutionStatus.ERROR
7070
assert len(res.events) == 1
@@ -73,7 +73,7 @@ def test_invalid_bios(system_info):
7373

7474

7575
def test_unexpected_bios(system_info):
76-
model = BiosDataModel(bios_version="RMP1004BS")
76+
model = BiosDataModel(bios_version="TESTBIOS")
7777
analyzer = BiosAnalyzer(system_info=system_info)
7878
args = BiosAnalyzerArgs(exp_bios_version=["some_other_bios"])
7979
res = analyzer.analyze_data(model, args)
@@ -84,30 +84,29 @@ def test_unexpected_bios(system_info):
8484

8585

8686
def test_bios_regex_match(system_info):
87-
model = BiosDataModel(bios_version="RMP1004BS")
87+
model = BiosDataModel(bios_version="TEST1234BIOS")
8888
analyzer = BiosAnalyzer(system_info=system_info)
89-
args = BiosAnalyzerArgs(exp_bios_version=[r"RMP\d{4}BS"], regex_match=True)
89+
args = BiosAnalyzerArgs(exp_bios_version=[r"TEST\d{4}BIOS"], regex_match=True)
9090
res = analyzer.analyze_data(model, args)
9191
assert res.status == ExecutionStatus.OK
9292
assert len(res.events) == 0
9393

9494

9595
def test_bios_regex_no_match(system_info):
96-
model = BiosDataModel(bios_version="RMP1004BS")
96+
model = BiosDataModel(bios_version="TEST1234BIOS")
9797
analyzer = BiosAnalyzer(system_info=system_info)
98-
args = BiosAnalyzerArgs(exp_bios_version=[r"RMP\d{3}BS"], regex_match=True)
98+
args = BiosAnalyzerArgs(exp_bios_version=[r"TEST\d{3}BIOS"], regex_match=True)
9999
res = analyzer.analyze_data(model, args)
100100
assert res.status == ExecutionStatus.ERROR
101101
assert len(res.events) == 1
102102
assert res.events[0].category == EventCategory.BIOS.value
103103
assert res.events[0].priority == EventPriority.ERROR
104104

105105

106-
# Which node do we run on, whats the bios_version?
107106
def test_invalid_regex(system_info):
108-
model = BiosDataModel(bios_version="RMP1004BS")
107+
model = BiosDataModel(bios_version="TEST1234BIOS")
109108
analyzer = BiosAnalyzer(system_info=system_info)
110-
args = BiosAnalyzerArgs(exp_bios_version=[r"R[MP\d{4}B{S"], regex_match=True)
109+
args = BiosAnalyzerArgs(exp_bios_version=[r"TE[S\d{4}B{S"], regex_match=True)
111110
res = analyzer.analyze_data(model, args)
112111
assert res.status == ExecutionStatus.ERROR
113112
assert len(res.events) == 2

test/unit/plugin/test_bios_collector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ def test_task_body_windows(system_info, bios_collector):
5151
bios_collector._run_sut_cmd = MagicMock(
5252
return_value=MagicMock(
5353
exit_code=0,
54-
stdout="\n\nSMBIOSBIOSVersion=R23ET70W (1.40 )\n\n\n\n",
54+
stdout="\n\nSMBIOSBIOSVersion=TESTBIOS (1.40 )\n\n\n\n",
5555
)
5656
)
5757

58-
exp_data = BiosDataModel(bios_version="R23ET70W (1.40 )")
58+
exp_data = BiosDataModel(bios_version="TESTBIOS (1.40 )")
5959

6060
res, data = bios_collector.collect_data()
6161
assert data == exp_data

test/unit/plugin/test_cmdline_analyzer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
@pytest.fixture
3737
def model_obj():
3838
return CmdlineDataModel(
39-
cmdline="BOOT_IMAGE=/boot/vmlinuz-5.18.2-mi300-build-140423-ubuntu-22.04+ root=UUID=90d7083e-0eb6-4675-ab9a-c8df4d753a29 ro panic=0 nowatchdog msr.allow_writes=on nokaslr amdgpu.noretry=1 pci=realloc=off numa_balancing=disable console=ttyS1,115200"
39+
cmdline="BOOT_IMAGE=/boot/testimage-1234 root=UUID=1234 ro panic=0 nowatchdog msr.allow_writes=on nokaslr amdgpu.noretry=1 pci=realloc=off numa_balancing=disable console=ttyS1,115200"
4040
)
4141

4242

4343
@pytest.fixture
4444
def config():
4545
return {
4646
"required_cmdline": [
47-
"BOOT_IMAGE=/boot/vmlinuz-5.18.2-mi300-build-140423-ubuntu-22.04+",
47+
"BOOT_IMAGE=/boot/testimage-1234",
4848
"ro",
4949
],
5050
"banned_cmdline": ["example"],
@@ -78,7 +78,7 @@ def test_banned_found(system_info, model_obj, config):
7878
analyzer = CmdlineAnalyzer(system_info=system_info)
7979
args = CmdlineAnalyzerArgs(
8080
required_cmdline=config["required_cmdline"],
81-
banned_cmdline=["root=UUID=90d7083e-0eb6-4675-ab9a-c8df4d753a29"],
81+
banned_cmdline=["root=UUID=1234"],
8282
)
8383
res = analyzer.analyze_data(model_obj, args)
8484
assert res.status == ExecutionStatus.ERROR
@@ -98,7 +98,7 @@ def test_banned_found_required_missing(system_info, model_obj, config):
9898
analyzer = CmdlineAnalyzer(system_info=system_info)
9999
args = CmdlineAnalyzerArgs(
100100
required_cmdline=config["required_cmdline"],
101-
banned_cmdline=["root=UUID=90d7083e-0eb6-4675-ab9a-c8df4d753a29"],
101+
banned_cmdline=["root=UUID=1234"],
102102
)
103103
res = analyzer.analyze_data(model_obj, args)
104104
assert res.status == ExecutionStatus.ERROR

test/unit/plugin/test_os_collector.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def test_os_collector_linux(
9696

9797
def test_os_collector_windows(system_info, conn_mock):
9898
system_info.os_family = OSFamily.WINDOWS
99-
collector = OsCollector(
99+
collector_inst = OsCollector(
100100
system_info=system_info,
101101
system_interaction_level=SystemInteractionLevel.PASSIVE,
102102
connection=conn_mock,
@@ -114,8 +114,8 @@ def test_os_collector_windows(system_info, conn_mock):
114114
),
115115
]
116116

117-
result, data = collector.collect_data()
118-
assert collector.result.status == ExecutionStatus.OK
117+
_, data = collector_inst.collect_data()
118+
assert collector_inst.result.status == ExecutionStatus.OK
119119
assert data == OsDataModel(os_name="Microsoft Windows 11 Enterprise", os_version="10.0.22621")
120120

121121

@@ -126,5 +126,5 @@ def test_os_collector_error(collector, conn_mock, system_info):
126126
CommandArtifact(exit_code=1, stdout="Ubuntu 22.04.4 LTS", stderr="", command="cmd2"),
127127
]
128128

129-
result, data = collector.collect_data()
129+
_, data = collector.collect_data()
130130
assert data is None

test/unit/plugin/test_package_analyzer.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# SOFTWARE.
2424
#
2525
###############################################################################
26-
import json
2726

2827
import pytest
2928

@@ -39,14 +38,8 @@ def package_analyzer(system_info):
3938

4039

4140
@pytest.fixture
42-
def package_data(plugin_fixtures_path):
43-
with (plugin_fixtures_path / "package_example_data.json").open() as fid:
44-
return json.load(fid)
45-
46-
47-
@pytest.fixture
48-
def default_data_lib(package_data):
49-
return PackageDataModel(**package_data["ubuntu"])
41+
def default_data_lib():
42+
return PackageDataModel(version_info={"test-ubuntu-package.x86_64": "1.11-1.xx11"})
5043

5144

5245
def test_no_data(package_analyzer, default_data_lib):
@@ -68,24 +61,32 @@ def test_empty_data_exact(package_analyzer, default_data_lib):
6861

6962
def test_data_exact(package_analyzer, default_data_lib):
7063
args = PackageAnalyzerArgs(
71-
exp_package_ver={"alternatives.x86_64": "1.11-1.xx11"}, regex_match=False
64+
exp_package_ver={"test-ubuntu-package.x86_64": "1.11-1.xx11"}, regex_match=False
7265
)
7366
res = package_analyzer.analyze_data(default_data_lib, args=args)
7467
assert res.status == ExecutionStatus.OK
7568

7669

77-
def test_data_version_mismatch_regex(package_analyzer, default_data_lib):
70+
def test_data_version_regex(package_analyzer, default_data_lib):
7871
args = PackageAnalyzerArgs(
79-
exp_package_ver={"alternatives\\.x86_64": "2\\.\\d+-\\d+\\.\\w+\\d+"}, regex_match=True
72+
exp_package_ver={"test-ubuntu-package\\.x86_64": "2\\.\\d+-\\d+\\.\\w+\\d+"},
73+
regex_match=True,
8074
)
8175
res = package_analyzer.analyze_data(default_data_lib, args=args)
8276
assert res.status == ExecutionStatus.ERROR
8377
assert len(res.events) == 1
8478
assert res.events[0].data == {
85-
"expected_package_search": "alternatives\\.x86_64",
79+
"expected_package_search": "test-ubuntu-package\\.x86_64",
8680
"expected_version_search": "2\\.\\d+-\\d+\\.\\w+\\d+",
87-
"found_package": "alternatives.x86_64",
81+
"found_package": "test-ubuntu-package.x86_64",
8882
"found_version": "1.11-1.xx11",
8983
"task_name": "PackageAnalyzer",
9084
"task_type": "DATA_ANALYZER",
9185
}
86+
87+
args = PackageAnalyzerArgs(
88+
exp_package_ver={"test-ubuntu-package\\.x86_64": "1\\.\\d+-\\d+\\.\\w+\\d+"},
89+
regex_match=True,
90+
)
91+
res = package_analyzer.analyze_data(default_data_lib, args=args)
92+
assert res.status == ExecutionStatus.OK

0 commit comments

Comments
 (0)