Skip to content

Commit

Permalink
[CICD-761] Glob the current dir instead of checking mount paths
Browse files Browse the repository at this point in the history
Simplifies the logic for determining what files were copied to workdir by globbing for certain dirs instead of parsing proc/self/mountinfo.

This is also better for other mounted dir structures (such as when a GHA runner mounts the image); we are simply checking what is in the current dir instead of inferring the structure based off mounted source path.
  • Loading branch information
mike-day committed Jan 2, 2025
1 parent bdd52b4 commit b5bfbff
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 134 deletions.
Empty file.
124 changes: 32 additions & 92 deletions tests/test_generate_path_excludes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,91 +21,37 @@ test_determine_source_exclude_paths() {
} <<< "$(determine_source_exclude_paths)"

if [[ "${base_path}" != "${expected_base_path}" || "${mu_dir_path}" != "${expected_mu_dir_path}" ]]; then
read -r mounted_source_path _ <<< "$(print_mount_paths "tests/fixtures/src")"
echo -e "${RED}Test failed for SRC_PATH='${SRC_PATH}': expected '$expected_base_path, $expected_mu_dir_path', got '$base_path, $mu_dir_path'.${NC}"
echo -e "${BLUE}INFO: mounted_source_path='${mounted_source_path}, pwd=$(pwd)'${NC}"
echo -e "${RED}Test failed for SRC_PATH='${SRC_PATH}', pwd='$(pwd)': expected '$expected_base_path, $expected_mu_dir_path', got '$base_path, $mu_dir_path'.${NC}"
exit 1
fi
}

mount_from_root() {
echo -e "${BLUE}INFO: mounting from /site...${NC}"

# Mock the print_mount_paths function
print_mount_paths() {
# shellcheck disable=SC2317
printf "/path/to/site /site"
}

# Workdir -> src directory
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content" "/wp-content/" "/wp-content/mu-plugins/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/" "/" "/mu-plugins/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/mu-plugins" "/" "/mu-plugins/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/mu-plugins/" "" "/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/plugins" "" ""
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/plugins/" "" ""
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "/wp-content/" "/wp-content/mu-plugins/"

# Workdir -> wp-content directory
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "mu-plugins" "/" "/mu-plugins/"
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "mu-plugins/" "" "/"
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "plugins" "" ""
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "plugins/" "" ""
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "." "/" "/mu-plugins/"
}

mount_from_wp_content() {
echo -e "${BLUE}INFO: mounting from /site/wp-content...${NC}"

# Mock the print_mount_paths function
print_mount_paths() {
# shellcheck disable=SC2317
printf "/path/to/site/wp-content /site"
}

# Workdir -> src directory
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "/" "/mu-plugins/"

# Workdir -> wp-content directory
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "." "/" "/mu-plugins/"
}

mount_from_mu_plugins() {
echo -e "${BLUE}INFO: mounting from /site/mu-plugins...${NC}"

# Mock the print_mount_paths function
print_mount_paths() {
# shellcheck disable=SC2317
printf "/path/to/site/wp-content/mu-plugins /site"
}

# Workdir -> src directory
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "" "/"

# Workdir -> mu-plugins directory
change_working_dir "tests/fixtures/src/wp-content/mu-plugins" run_test_determine_source_exclude_paths "." "" "/"
}

mount_from_plugins() {
echo -e "${BLUE}INFO: mounting from /site/plugins...${NC}"

# Mock the print_mount_paths function
print_mount_paths() {
# shellcheck disable=SC2317
printf "/path/to/site/wp-content/plugins /site"
}

# Workdir -> src directory
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "" ""

# Workdir -> plugins directory
change_working_dir "tests/fixtures/src/wp-content/plugins" run_test_determine_source_exclude_paths "." "" ""
}

mount_from_root
mount_from_wp_content
mount_from_mu_plugins
mount_from_plugins
# Workdir -> src directory
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content" "/wp-content/" "/wp-content/mu-plugins/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/" "/" "/mu-plugins/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/mu-plugins" "/" "/mu-plugins/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/mu-plugins/" "" "/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/plugins" "" ""
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "wp-content/plugins/" "" ""
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "." "/wp-content/" "/wp-content/mu-plugins/"
change_working_dir "tests/fixtures/src" run_test_determine_source_exclude_paths "" "/wp-content/" "/wp-content/mu-plugins/"

# Workdir -> wp-content directory
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "mu-plugins" "/" "/mu-plugins/"
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "mu-plugins/" "" "/"
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "plugins" "" ""
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "plugins/" "" ""
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "." "/" "/mu-plugins/"
change_working_dir "tests/fixtures/src/wp-content" run_test_determine_source_exclude_paths "" "/" "/mu-plugins/"

# Workdir -> mu-plugins directory
change_working_dir "tests/fixtures/src/wp-content/mu-plugins" run_test_determine_source_exclude_paths "." "" "/"

# Workdir -> plugins directory
change_working_dir "tests/fixtures/src/wp-content/plugins" run_test_determine_source_exclude_paths "." "" ""

# Workdir -> fixtures directory (checks that glob_current_dir is working (wpengine-common should be detected))
change_working_dir "tests/fixtures" run_test_determine_source_exclude_paths "." "" "/"

echo -e "${GREEN}All tests passed for determining the source excludes path.${NC}"
}
Expand All @@ -122,7 +68,7 @@ test_determine_remote_exclude_paths() {
} <<< "$(determine_remote_exclude_paths)"

if [[ "${base_path}" != "${expected_base_path}" || "${mu_dir_path}" != "${expected_mu_dir_path}" ]]; then
echo -e "${RED}Test failed for REMOTE_PATH='${SRC_PATH}': expected '$expected_base_path, $expected_mu_dir_path', got '$base_path, $mu_dir_path'.${NC}"
echo -e "${RED}Test failed for REMOTE_PATH='${SRC_PATH}', pwd='$(pwd)': expected '$expected_base_path, $expected_mu_dir_path', got '$base_path, $mu_dir_path'.${NC}"
exit 1
fi
}
Expand All @@ -142,17 +88,11 @@ test_generate_source_exclude_from() {
local output
local expected_output

# Mock the print_mount_paths function
print_mount_paths() {
# shellcheck disable=SC2317
printf "/path/to/site /site"
}

output=$(generate_source_exclude_from)
output=$(change_working_dir "tests/fixtures/src" generate_source_exclude_from)
expected_output=$(cat "tests/fixtures/excludes/source_exclude_from.txt")

if [[ "$output" != "$expected_output" ]]; then
echo -e "${RED}Test failed: generated output does not match expected output.${NC}"
echo -e "${RED}Test failed for pwd='$(pwd)': generated output does not match expected output.${NC}"
echo -e "${BLUE}Generated output:${NC}"
echo "$output"
echo -e "${BLUE}Expected output:${NC}"
Expand All @@ -168,11 +108,11 @@ test_generate_remote_excludes() {
local output
local expected_output

output=$(generate_remote_excludes)
output=$(change_working_dir "tests/fixtures/src" generate_remote_excludes)
expected_output=$(cat "tests/fixtures/excludes/remote_excludes.txt")

if [[ "$output" != "$expected_output" ]]; then
echo -e "${RED}Test failed: generated output does not match expected output.${NC}"
echo -e "${RED}Test failed for pwd='$(pwd)': generated output does not match expected output.${NC}"
echo -e "${BLUE}Generated output:${NC}"
echo "$output"
echo -e "${BLUE}Expected output:${NC}"
Expand Down
76 changes: 43 additions & 33 deletions utils/generate_path_excludes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,11 @@ shopt -s extglob
# (such as some of the WP Engine managed plugins) might be useful in rare
# circumstances to have as a reference for debugging purposes.

# Get the directory of the current script
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Source the print_mount_paths.sh file relative to the current script's location
source "${SCRIPT_DIR}/print_mount_paths.sh"

# Determine the source paths to exclude from the deployment
determine_source_exclude_paths() {
local base_path
local mu_dir_path

read -r mounted_source_path _ <<< "$(print_mount_paths "wp-content")"

if [[ -n "${SRC_PATH}" && "${SRC_PATH}" != '.' ]]; then
parse_src_path() {
case "${SRC_PATH}" in
wp-content )
base_path="/wp-content/"
Expand All @@ -58,25 +49,40 @@ determine_source_exclude_paths() {
mu_dir_path="/"
;;
esac
elif [[ "$(pwd)" != *wp-content* && "${mounted_source_path}" != *wp-content* ]]; then
base_path="/wp-content/"
mu_dir_path="/wp-content/mu-plugins/"
}

parse_current_path() {
case "$(pwd)" in
*wp-content )
base_path="/"
mu_dir_path="/mu-plugins/"
;;
*mu-plugins )
mu_dir_path="/"
;;
esac
}

glob_current_dir() {
if [[ -d wp-content ]]; then
base_path="/wp-content/"
mu_dir_path="/wp-content/mu-plugins/"
elif [[ -d mu-plugins ]]; then
base_path="/"
mu_dir_path="/mu-plugins/"
elif [[ -d wpengine-common ]]; then
mu_dir_path="/"
fi
}

# If SRC_PATH is set, use it to determine base and mu-plugins paths.
# Otherwise, use the current directory or check its contents to set the paths.
if [[ -n "${SRC_PATH}" && "${SRC_PATH}" != '.' ]]; then
parse_src_path
elif [[ "$(pwd)" == *wp-content* ]]; then
parse_current_path
else
# Iterate over the possible paths and break when a match is found
# !!! Ordering is important for the switch cases !!!
for value in "$(pwd)" "$mounted_source_path"; do
case "$value" in
*wp-content )
base_path="/"
mu_dir_path="/mu-plugins/"
break
;;
*mu-plugins )
mu_dir_path="/"
break
;;
esac
done
glob_current_dir
fi

printf "%s\n%s\n" "$base_path" "$mu_dir_path"
Expand All @@ -86,10 +92,7 @@ determine_remote_exclude_paths() {
local base_path
local mu_dir_path

if [[ -z "${REMOTE_PATH}" ]]; then
base_path="/wp-content/"
mu_dir_path="/wp-content/mu-plugins/"
else
parse_remote_path() {
case "$REMOTE_PATH" in
wp-content?(/) )
base_path="/"
Expand All @@ -99,12 +102,19 @@ determine_remote_exclude_paths() {
mu_dir_path="/"
;;
esac
}

if [[ -z "${REMOTE_PATH}" ]]; then
base_path="/wp-content/"
mu_dir_path="/wp-content/mu-plugins/"
else
parse_remote_path
fi

printf "%s\n%s\n" "$base_path" "$mu_dir_path"
}

# Generate the dynamic list of files to exclude from the deployment
# Generate the dynamic list of paths to exclude from the deployment
print_dynamic_excludes() {
local func=$1
local delimiter=$2
Expand Down
9 changes: 0 additions & 9 deletions utils/print_mount_paths.sh

This file was deleted.

0 comments on commit b5bfbff

Please sign in to comment.