diff --git a/bootstrap-salt.sh b/bootstrap-salt.sh index 83f7e6e64..d409fa94c 100755 --- a/bootstrap-salt.sh +++ b/bootstrap-salt.sh @@ -676,6 +676,33 @@ __salt_version_string() { echo "$1" } +#--- FUNCTION ------------------------------------------------------------------------------------------------------- +# NAME: __salt_repo_exclude_majors +# DESCRIPTION: Print the exclude= value for a salt.repo section that must +# stay on the major version of the Salt version in $1 (e.g. +# 3006.26 or 3008.1-1). The baseurl serves every major, so +# without it the first yum/dnf update moves to the newest +# Salt. Same list as the official salt.repo: 3006 through 3010 +# minus the given major. For majors past 3008 the list runs to +# the major + 2, so the next two majors are still excluded. +#---------------------------------------------------------------------------------------------------------------------- +__salt_repo_exclude_majors() { + _EXCLUDE_MAJOR=$(echo "$1" | cut -d '.' -f 1) + _EXCLUDE_LAST=3010 + if [ "$_EXCLUDE_MAJOR" -gt 3008 ]; then + _EXCLUDE_LAST=$((_EXCLUDE_MAJOR + 2)) + fi + _EXCLUDE_LIST="" + _EXCLUDE_REV=3006 + while [ "$_EXCLUDE_REV" -le "$_EXCLUDE_LAST" ]; do + if [ "$_EXCLUDE_REV" -ne "$_EXCLUDE_MAJOR" ]; then + _EXCLUDE_LIST="${_EXCLUDE_LIST} *${_EXCLUDE_REV}*" + fi + _EXCLUDE_REV=$((_EXCLUDE_REV + 1)) + done + echo "${_EXCLUDE_LIST# }" +} + # check if systemd is functional __check_services_systemd_functional @@ -4220,6 +4247,7 @@ __install_saltstack_fedora_onedir_repository() { echo "enabled=1" >> "${YUM_REPO_FILE}" echo "enabled_metadata=1" >> "${YUM_REPO_FILE}" echo "gpgcheck=1" >> "${YUM_REPO_FILE}" + echo "exclude=$(__salt_repo_exclude_majors "$ONEDIR_REV_DOT")" >> "${YUM_REPO_FILE}" echo "gpgkey=https://${_REPO_URL}/api/security/keypair/SaltProjectKey/public" >> "${YUM_REPO_FILE}" fi else @@ -4559,6 +4587,7 @@ __install_saltstack_rhel_onedir_repository() { echo "enabled=1" >> "${YUM_REPO_FILE}" echo "enabled_metadata=1" >> "${YUM_REPO_FILE}" echo "gpgcheck=1" >> "${YUM_REPO_FILE}" + echo "exclude=$(__salt_repo_exclude_majors "$ONEDIR_REV_DOT")" >> "${YUM_REPO_FILE}" echo "gpgkey=https://${_REPO_URL}/api/security/keypair/SaltProjectKey/public" >> "${YUM_REPO_FILE}" fi else @@ -6041,6 +6070,7 @@ install_amazon_linux_ami_2_deps() { echo "enabled=1" >> "${YUM_REPO_FILE}" echo "enabled_metadata=1" >> "${YUM_REPO_FILE}" echo "gpgcheck=1" >> "${YUM_REPO_FILE}" + echo "exclude=$(__salt_repo_exclude_majors "$STABLE_REV_DOT")" >> "${YUM_REPO_FILE}" echo "gpgkey=https://${_REPO_URL}/api/security/keypair/SaltProjectKey/public" >> "${YUM_REPO_FILE}" fi else @@ -6142,6 +6172,7 @@ install_amazon_linux_ami_2_onedir_deps() { echo "enabled=1" >> "${YUM_REPO_FILE}" echo "enabled_metadata=1" >> "${YUM_REPO_FILE}" echo "gpgcheck=1" >> "${YUM_REPO_FILE}" + echo "exclude=$(__salt_repo_exclude_majors "$ONEDIR_REV_DOT")" >> "${YUM_REPO_FILE}" echo "gpgkey=https://${_REPO_URL}/api/security/keypair/SaltProjectKey/public" >> "${YUM_REPO_FILE}" fi else @@ -6335,6 +6366,7 @@ install_amazon_linux_ami_2023_onedir_deps() { echo "enabled=1" >> "${YUM_REPO_FILE}" echo "enabled_metadata=1" >> "${YUM_REPO_FILE}" echo "gpgcheck=1" >> "${YUM_REPO_FILE}" + echo "exclude=$(__salt_repo_exclude_majors "$ONEDIR_REV_DOT")" >> "${YUM_REPO_FILE}" echo "gpgkey=https://${_REPO_URL}/api/security/keypair/SaltProjectKey/public" >> "${YUM_REPO_FILE}" fi else diff --git a/tests/integration/test_installation.py b/tests/integration/test_installation.py index e621fa60d..d873e3285 100644 --- a/tests/integration/test_installation.py +++ b/tests/integration/test_installation.py @@ -1,3 +1,4 @@ +import configparser import json import logging import os @@ -317,6 +318,40 @@ def test_rpm_repo_file_rewrite_custom_repo_url(tmp_path): assert result.count("enabled=1") == 1 and result.count("enabled=0") == 1 +def test_rpm_point_release_repo_excludes_other_majors(): + """ + Regression test for https://github.com/saltstack/salt-bootstrap/issues/2147 + For a point release (e.g. "stable 3006.24") bootstrap writes its own + salt.repo section. Its baseurl serves every major, so the section must + exclude the other majors or the next "dnf update" moves Salt to the newest + one in the repo. + """ + repo_file = "/etc/yum.repos.d/salt.repo" + if platform.system() != "Linux" or not os.path.exists(repo_file): + pytest.skip("No salt.repo file present") + if os.path.exists("/etc/photon-release"): + pytest.skip("tdnf doesn't read exclude= from a .repo file") + + repo = configparser.ConfigParser(interpolation=None) + repo.read(repo_file) + # Only the point release path names the section after the full version, + # e.g. [salt-repo-3006.24-lts] + sections = [] + for section in repo.sections(): + match = re.match(r"salt-repo-(\d{4})\.\S+-lts$", section) + if match: + sections.append((section, int(match.group(1)))) + if not sections: + pytest.skip("salt.repo was not written for a point release") + + for section, major in sections: + exclude = repo[section].get("exclude", "").split() + assert f"*{major}*" not in exclude, f"[{section}] excludes its own major" + for other in range(3006, 3011): + if other != major: + assert f"*{other}*" in exclude, f"[{section}] does not exclude {other}" + + DETECTION_FUNCTIONS = [ "__gather_linux_system_info", "__sort_release_files", diff --git a/tests/shell/point_release_repo_spec.sh b/tests/shell/point_release_repo_spec.sh new file mode 100644 index 000000000..6305e04fa --- /dev/null +++ b/tests/shell/point_release_repo_spec.sh @@ -0,0 +1,43 @@ +# shellcheck shell=sh + +# Regression test for #2147: for a point release the RPM repository functions +# write their own salt.repo section, and its baseurl serves every major. Without +# an exclude= line the first yum/dnf update moves to the newest Salt. +Describe 'point release salt.repo' + Include "$(bootstrap_functions __salt_version_string __salt_repo_exclude_majors __rewrite_repo_url __install_saltstack_rhel_onedir_repository __install_saltstack_fedora_onedir_repository)" + + BS_TRUE=1 + BS_FALSE=0 + _FORCE_OVERWRITE=$BS_FALSE + _PY_EXE="python3" + _PY_MAJOR_VERSION=3 + _REPO_URL="packages.broadcom.com/artifactory" + + # Mocks: no network, no package manager + __fetch_url() { printf '[salt-repo-latest]\n' > "$1"; } + yum() { :; } + dnf() { :; } + echodebug() { :; } + + setup() { + WORK=$(mktemp -d) + YUM_REPO_FILE="$WORK/salt.repo" + } + cleanup() { rm -rf "$WORK"; } + BeforeEach setup + AfterEach cleanup + + Parameters + __install_saltstack_rhel_onedir_repository "3006.26" "*3007* *3008* *3009* *3010*" + __install_saltstack_fedora_onedir_repository "3007.15" "*3006* *3008* *3009* *3010*" + __install_saltstack_rhel_onedir_repository "3008.1-1" "*3006* *3007* *3009* *3010*" + End + + It "$1 keeps $2 on its major" + ONEDIR_REV="$2" + When call "$1" + The status should be success + The contents of file "$YUM_REPO_FILE" should include "[salt-repo-$2-lts]" + The contents of file "$YUM_REPO_FILE" should include "exclude=$3" + End +End diff --git a/tests/shell/version_helpers_spec.sh b/tests/shell/version_helpers_spec.sh index 8e8df2c64..f1bf30fb5 100644 --- a/tests/shell/version_helpers_spec.sh +++ b/tests/shell/version_helpers_spec.sh @@ -1,7 +1,7 @@ # shellcheck shell=sh Describe 'version and string helpers' - Include "$(bootstrap_functions __parse_version_string __unquote_string __camelcase_split __derive_debian_numeric_version)" + Include "$(bootstrap_functions __parse_version_string __unquote_string __camelcase_split __derive_debian_numeric_version __salt_repo_exclude_majors)" Describe '__parse_version_string' Parameters @@ -57,4 +57,21 @@ Describe 'version and string helpers' The output should eq "" End End + + Describe '__salt_repo_exclude_majors' + # 3006 to 3008 match the exclude= lines in the official salt.repo + Parameters + "3006" "*3007* *3008* *3009* *3010*" + "3006.26" "*3007* *3008* *3009* *3010*" + "3007.15" "*3006* *3008* *3009* *3010*" + "3008.1-1" "*3006* *3007* *3009* *3010*" + "3009.2" "*3006* *3007* *3008* *3010* *3011*" + "3012.1" "*3006* *3007* *3008* *3009* *3010* *3011* *3013* *3014*" + End + + It "excludes every major but the one in $1" + When call __salt_repo_exclude_majors "$1" + The output should eq "$2" + End + End End