From 212e891f6876567e23d8dd6a41f076b3f02048a1 Mon Sep 17 00:00:00 2001 From: Tobias Brick <39196763+tobiasb-ms@users.noreply.github.com> Date: Thu, 27 Jul 2023 15:43:35 -0700 Subject: [PATCH] test: remove dependency on diff from tests (#3483) --- parts/linux/cloud-init/artifacts/cis.sh | 3 +- pkg/templates/templates_generated.go | 3 +- .../packer/test/linux-vhd-content-test.sh | 37 +++++++++++-------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/parts/linux/cloud-init/artifacts/cis.sh b/parts/linux/cloud-init/artifacts/cis.sh index d9a89246683..818955ace86 100644 --- a/parts/linux/cloud-init/artifacts/cis.sh +++ b/parts/linux/cloud-init/artifacts/cis.sh @@ -169,9 +169,10 @@ fixUmaskSettings() { # that doesn't set umask correctly. So we can't just comment out all the lines or have any comments that explain what we're doing. # So since we can't delete the file, we just overwrite it with the correct umask setting. This duplicates what /etc/profile does, but # it does no harm and works with the tools. + # Note that we use printf to avoid a trailing newline. local umask_sh="/etc/profile.d/umask.sh" if [[ "${OS}" == "${MARINER_OS_NAME}" && "${OS_VERSION}" == "2.0" && -f "${umask_sh}" ]]; then - echo "umask 027" >${umask_sh} + printf "umask 027" >${umask_sh} fi } diff --git a/pkg/templates/templates_generated.go b/pkg/templates/templates_generated.go index 5130a4eb63c..e8023467eed 100644 --- a/pkg/templates/templates_generated.go +++ b/pkg/templates/templates_generated.go @@ -1371,9 +1371,10 @@ fixUmaskSettings() { # that doesn't set umask correctly. So we can't just comment out all the lines or have any comments that explain what we're doing. # So since we can't delete the file, we just overwrite it with the correct umask setting. This duplicates what /etc/profile does, but # it does no harm and works with the tools. + # Note that we use printf to avoid a trailing newline. local umask_sh="/etc/profile.d/umask.sh" if [[ "${OS}" == "${MARINER_OS_NAME}" && "${OS_VERSION}" == "2.0" && -f "${umask_sh}" ]]; then - echo "umask 027" >${umask_sh} + printf "umask 027" >${umask_sh} fi } diff --git a/vhdbuilder/packer/test/linux-vhd-content-test.sh b/vhdbuilder/packer/test/linux-vhd-content-test.sh index 9b58d596e5d..8aa1165452c 100644 --- a/vhdbuilder/packer/test/linux-vhd-content-test.sh +++ b/vhdbuilder/packer/test/linux-vhd-content-test.sh @@ -479,23 +479,30 @@ testNetworkSettings() { # Ensures that the content /etc/profile.d/umask.sh is correct, per code in # /parts/linux/cloud-init/artifacts/cis.sh testUmaskSettings() { - local test="testUmaskSettings" - local settings_file=/etc/profile.d/umask.sh - local expected_settings_file_content='umask 027' - echo "$test:Start" - - # If the settings file exists, it must just be a single line that sets umask properly. - if [[ -f "${settings_file}" && -s "${settings_file}" ]]; then - echo "${test}: Checking that ${settings_file} contains '${expected_settings_file_content}'" - - if ! diff "${settings_file}" <(echo "${expected_settings_file_content}") ; then - err $test "The content of the file '${settings_file}' must exactly match '${expected_settings_file_content}'. See above for differences" + local test="testUmaskSettings" + local settings_file=/etc/profile.d/umask.sh + local expected_settings_file_content='umask 027' + echo "$test:Start" + + # If the settings file exists, it must just be a single line that sets umask properly. + if [[ -f "${settings_file}" ]]; then + echo "${test}: Checking that the contents of ${settings_file} is exactly '${expected_settings_file_content}'" + + # Command substitution (like file_contents=$(cat "${settings_file}")) strips trailing newlines, so we use mapfile instead. + # This creates an array of the lines in the file, and then we join them back together by expanding the array into a single string. + local file_contents_array=() + mapfile <"${settings_file}" file_contents_array + local file_contents="${file_contents_array[*]}" + if [[ "${file_contents}" != "${expected_settings_file_content}" ]]; then + err $test "The content of the file '${settings_file}' is '${file_contents}', which does not exactly match '${expected_settings_file_content}'. " + else + echo "${test}: The content of the file '${settings_file}' exactly matches the expected contents '${expected_settings_file_content}'." + fi + else + echo "${test}: Settings file '${settings_file}' does not exist, so not testing contents." fi - else - echo "${test}: Settings file '${settings_file}' does not exist or is empty, so not testing contents." - fi - echo "$test:End" + echo "$test:End" } # Tests that the modes on the cron-related files and directories in /etc are set correctly, per the