Skip to content

Commit 77cd3e2

Browse files
committed
Modifed code to just log Execstart and fix import
1 parent 0f3f19a commit 77cd3e2

4 files changed

Lines changed: 33 additions & 40 deletions

File tree

src/core/src/package_managers/AptitudePackageManager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def __read_one_line_style_list_format(self, file_path, max_patch_published_date,
183183

184184
for line in sources_content_lines:
185185
if len(line.strip()) != 0 and not line.strip().startswith("#"):
186-
if base_classification == Constants.PackageClassification.SECURITY and "security" not in line:
186+
if base_classification == Constants.PackageClassification.SECURITY and "security" not in line and "fips-updates" not in line:
187187
continue
188188
std_source_list_content += "\n" + self.__apply_max_patch_publish_date(sources_content=line, max_patch_publish_date=max_patch_published_date)
189189

@@ -207,7 +207,7 @@ def __read_deb882_style_format(self, file_path, max_patch_published_date, base_c
207207

208208
if len(line.strip()) == 0: # stanza separating line
209209
if stanza != str():
210-
if base_classification == str() or (base_classification == Constants.PackageClassification.SECURITY and "security" in stanza):
210+
if base_classification == str() or (base_classification == Constants.PackageClassification.SECURITY and ("security" in stanza or "fips-updates" in stanza)):
211211
std_source_parts_content += self.__apply_max_patch_publish_date(sources_content=stanza, max_patch_publish_date=max_patch_published_date) + '\n'
212212
stanza = str()
213213
continue

src/core/src/package_managers/Dnf5PackageManager.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
"""Dnf5PackageManager for Azure Linux 4 or above and RHEL"""
1818
import json
1919
import re
20-
from abc import ABCMeta, abstractmethod
2120

22-
from core.src.bootstrap.Constants import Constants
21+
from abc import ABCMeta, abstractmethod
2322
from core.src.core_logic.VersionComparator import VersionComparator
23+
from core.src.bootstrap.Constants import Constants
2424
from core.src.package_managers.PackageManager import PackageManager
2525

2626

@@ -41,14 +41,12 @@ def __init__(self, env_layer, execution_config, composite_logger, telemetry_writ
4141

4242
# Install update
4343
self.single_package_upgrade_cmd = 'sudo dnf5 -y upgrade'
44-
4544
# Support to check if reboot is required
4645
# dnf-utils not required (needs-restarting is built into dnf5)
4746
self.needs_restarting_with_flag = 'sudo LANG=en_US.UTF8 dnf5 needs-restarting -r'
4847

4948
# DNF5 exit codes
5049
self.dnf_exitcode_ok = [0, 100]
51-
5250
# DNF5 valid exit codes for simulation commands
5351
self.dnf5_simulation_valid_exit_codes = [0, 1]
5452
self.dnf5_dependency_failure_text = ["Skipping packages with broken dependencies", "Nothing to do."]
@@ -469,10 +467,6 @@ def __init_constants_for_dnf5_automatic(self):
469467
self.dnf5_automatic_config_pattern_match_text = ' = (no|yes)'
470468
self.dnf5_automatic_download_updates_identifier_text = "download_updates"
471469
self.dnf5_automatic_apply_updates_identifier_text = "apply_updates"
472-
# ExecStart flag identifiers
473-
self.dnf5_automatic_download_updates_flag = '--downloadupdates'
474-
self.dnf5_automatic_apply_updates_flag = '--installupdates'
475-
476470
self.dnf5_automatic_enable_on_reboot_identifier_text = "enable_on_reboot"
477471
self.dnf5_automatic_installation_state_identifier_text = "installation_state"
478472
self.dnf5_auto_os_update_service = "dnf5-automatic"
@@ -534,7 +528,13 @@ def __get_current_auto_os_updates_setting_on_machine(self):
534528
return is_service_installed, enable_on_reboot_value, download_updates_value, apply_updates_value
535529

536530
code, service_output = self.env_layer.run_command_output(self.dnf5_automatic_configuration_service, False, False)
537-
self.composite_logger.log_debug("[DNF5] dnf5-automatic ExecStart details. [Command={0}][Code={1}][Output={2}]".format(self.dnf5_automatic_configuration_service, str(code), service_output))
531+
exec_start_line = ""
532+
#Only print ExecStart details
533+
for line in service_output.splitlines():
534+
if line.strip().startswith("ExecStart"):
535+
exec_start_line = line.strip()
536+
break
537+
self.composite_logger.log_debug("[DNF5] dnf5-automatic ExecStart details. [Command={0}][Code={1}][Output={2}]".format(self.dnf5_automatic_configuration_service, str(code), exec_start_line))
538538

539539
is_service_installed = True
540540
enable_on_reboot_value = self.is_service_set_to_enable_on_reboot(self.enable_on_reboot_check_cmd)
@@ -670,7 +670,6 @@ def backup_image_default_patch_configuration_if_not_exists(self):
670670
self.installation_state_identifier_text: is_service_installed
671671
}
672672
}
673-
674673
image_default_patch_configuration_backup.update(backup_image_default_patch_configuration_json_to_add)
675674

676675
self.composite_logger.log_debug("[DNF5] Logging default system configuration settings for auto OS updates. ""[Settings={0}] [Log file path={1}]".format(str(image_default_patch_configuration_backup),self.image_default_patch_configuration_backup_path))
@@ -701,8 +700,7 @@ def update_os_patch_configuration_sub_setting(self, patch_configuration_sub_sett
701700
# note: adding space between the patch_configuration_sub_setting and value since, we will have to do that if we have to add a patch_configuration_sub_setting that did not exist before
702701
self.composite_logger.log_debug("[DNF5] Updating system configuration settings for auto OS updates. [Patch Configuration Sub Setting={0}] [Value={1}]".format(
703702
str(patch_configuration_sub_setting), value))
704-
os_patch_configuration_settings = self.env_layer.file_system.read_with_retry(
705-
self.os_patch_configuration_settings_file_path)
703+
os_patch_configuration_settings = self.env_layer.file_system.read_with_retry(self.os_patch_configuration_settings_file_path)
706704
patch_configuration_sub_setting_to_update = patch_configuration_sub_setting + ' = ' + value
707705
patch_configuration_sub_setting_found_in_file = False
708706
updated_patch_configuration_sub_setting = ""
@@ -720,9 +718,7 @@ def update_os_patch_configuration_sub_setting(self, patch_configuration_sub_sett
720718
if not patch_configuration_sub_setting_found_in_file:
721719
updated_patch_configuration_sub_setting += patch_configuration_sub_setting_to_update + "\n"
722720

723-
self.env_layer.file_system.write_with_retry(self.os_patch_configuration_settings_file_path,
724-
'{0}'.format(updated_patch_configuration_sub_setting.lstrip()),
725-
mode='w+')
721+
self.env_layer.file_system.write_with_retry(self.os_patch_configuration_settings_file_path,'{0}'.format(updated_patch_configuration_sub_setting.lstrip()),mode='w+')
726722
except Exception as error:
727723
error_msg = "[DNF5] Error occurred while updating system configuration settings for auto OS updates. [Patch Configuration={0}] [Error={1}]".format(
728724
str(patch_configuration_sub_setting), repr(error))
@@ -789,8 +785,7 @@ def __get_image_default_patch_configuration_backup(self):
789785
# read existing backup since it also contains backup from other update services. We need to preserve any existing data within the backup file
790786
if self.image_default_patch_configuration_backup_exists():
791787
try:
792-
image_default_patch_configuration_backup = json.loads(
793-
self.env_layer.file_system.read_with_retry(self.image_default_patch_configuration_backup_path))
788+
image_default_patch_configuration_backup = json.loads(self.env_layer.file_system.read_with_retry(self.image_default_patch_configuration_backup_path))
794789
except Exception as error:
795790
self.composite_logger.log_error("[DNF5] Unable to read backup for default patch state. Will attempt to re-write. [Exception={0}]".format(repr(error)))
796791
return image_default_patch_configuration_backup

src/core/tests/Test_AptitudePackageManagerCustomSources.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def __lib_test_custom_sources_with(self, include_sources_list=False, include_sou
9999
# Checks swapping and caching: All -> Security -> All -> All
100100
for i in range(3):
101101
# All
102-
expected_debstyle_entry_count = 2 if include_source_parts_debstyle else 0 # 2 entries in the debstyle mock
103-
expected_sources_list_entry_count = (4 if include_sources_list else 0) + (5 if include_source_parts_list else 0) # 4 in regular file, 3 in mock folder
102+
expected_debstyle_entry_count = 3 if include_source_parts_debstyle else 0 # 3 entries in the debstyle mock(incl. fips-updates)
103+
expected_sources_list_entry_count = (4 if include_sources_list else 0) + (6 if include_source_parts_list else 0) # 4 in regular file, 6 in mock folder (incl. fips-updates)
104104
sources_dir, sources_list = package_manager._AptitudePackageManager__get_custom_sources_to_spec(include_max_patch_publish_date)
105105
self.__check_custom_sources(sources_dir, sources_list,
106106
sources_debstyle_expected=include_source_parts_debstyle,
@@ -115,8 +115,8 @@ def __lib_test_custom_sources_with(self, include_sources_list=False, include_sou
115115
continue
116116

117117
# Security
118-
expected_debstyle_entry_count = 1 if include_source_parts_debstyle else 0 # 1 security entry in the debstyle mock
119-
expected_sources_list_entry_count = (1 if include_sources_list else 0) + (1 if include_source_parts_list else 0) # 1 security entry in regular file, 1 security entry in mock folder
118+
expected_debstyle_entry_count = 2 if include_source_parts_debstyle else 0 # 2 security entries in the debstyle mock (security + fips-updates)
119+
expected_sources_list_entry_count = (1 if include_sources_list else 0) + (2 if include_source_parts_list else 0) # 1 security entry in regular file, 2 security entries in mock folder (security + fips-updates)
120120
sources_dir, sources_list = package_manager._AptitudePackageManager__get_custom_sources_to_spec(include_max_patch_publish_date, "Security")
121121
self.__check_custom_sources(sources_dir, sources_list,
122122
sources_debstyle_expected=include_source_parts_debstyle,
@@ -146,8 +146,8 @@ def __check_custom_sources(self, sources_dir, sources_list, sources_debstyle_exp
146146
self.assertEqual(len(data), sources_debstyle_entry_count)
147147
for entry in data:
148148
if security_only:
149-
self.assertTrue("security" in entry)
150-
if max_patch_publish_date != str():
149+
self.assertTrue("security" in entry or "fips-updates" in entry)
150+
if max_patch_publish_date != str() and "fips-updates" not in entry: # exception for unsupported repo:
151151
self.assertTrue(max_patch_publish_date in entry)
152152
else:
153153
self.assertFalse(os.path.isdir(sources_dir))
@@ -159,8 +159,8 @@ def __check_custom_sources(self, sources_dir, sources_list, sources_debstyle_exp
159159
self.assertEqual(len(data), sources_list_entry_count)
160160
for entry in data:
161161
if security_only:
162-
self.assertTrue("security" in entry)
163-
if max_patch_publish_date != str() and "ppa" not in entry: # exception for unsupported repo
162+
self.assertTrue("security" in entry or "fips-updates" in entry)
163+
if max_patch_publish_date != str() and "ppa" not in entry and "fips-updates" not in entry: # exception for unsupported repo
164164
self.assertTrue(max_patch_publish_date in entry)
165165

166166
# region - Mock sources preparation and clean up
@@ -212,7 +212,8 @@ def __get_sources_data_one_line_style_ext():
212212
"deb http://ppa.launchpad.net/upubuntu-com/web/ubuntu focal main\n" + \
213213
"deb http://azure.archive.ubuntu.com/ubuntu/ focal-security universe\n" + \
214214
"deb http://in.archive.ubuntu.com/ubuntu/ focal multiverse\n" + \
215-
"deb http://cn.archive.ubuntu.com/ubuntu/ focal main\n"
215+
"deb http://cn.archive.ubuntu.com/ubuntu/ focal main\n" + \
216+
"deb https://esm.ubuntu.com/fips-updates/ubuntu jammy-updates main\n"
216217

217218
@staticmethod
218219
def __get_sources_data_debstyle():
@@ -229,7 +230,13 @@ def __get_sources_data_debstyle():
229230
"URIs: http://azure.archive.ubuntu.com/ubuntu/ \n" + \
230231
"Suites: noble-security \n" + \
231232
"Components: main universe restricted multiverse \n" + \
232-
"Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg \n"
233+
"Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg \n" + \
234+
"\n" + \
235+
"Types: deb \n" + \
236+
"URIs: https://esm.ubuntu.com/fips-updates/ubuntu \n" + \
237+
"Suites: jammy-updates \n" + \
238+
"Components: main \n" + \
239+
"Signed-By: /usr/share/keyrings/ubuntu-pro-fips-updates.gpg \n"
233240
# endregion
234241

235242
if __name__ == '__main__':

src/core/tests/Test_EnvLayer.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
import platform
1818
import sys
1919
import unittest
20-
# Conditional import for StringIO
21-
try:
22-
from StringIO import StringIO # Python 2
23-
except ImportError:
24-
from io import StringIO # Python 3
25-
2620
from core.src.bootstrap.EnvLayer import EnvLayer
2721
from core.src.bootstrap.Constants import Constants
2822
from core.src.external_dependencies import distro
@@ -171,16 +165,13 @@ def test_get_package_manager_azure_linux_4_and_rhel10_not_supported(self):
171165
]
172166

173167
for row in test_input_output_table:
174-
captured_output = StringIO()
175-
original_output = sys.stdout
176-
sys.stdout = captured_output # Redirect stdout to the StringIO object
177-
178168
self.envlayer.platform.linux_distribution = row[0]
179169
distro.os_release_attr = row[1]
180170

171+
captured_output = io.StringIO()
181172
sys.stdout = captured_output
182173
result = self.envlayer.get_package_manager()
183-
sys.stdout = original_output
174+
sys.stdout = sys.__stdout__
184175
self.assertEqual(row[2], captured_output.getvalue())
185176
self.assertEqual(result, "")
186177

0 commit comments

Comments
 (0)