Skip to content

Commit

Permalink
Test mac os (#53)
Browse files Browse the repository at this point in the history
* add: test on MacOS

Add test for MacOS in Github action

Maybe see this to add test for FreeBSD :
https://github.com/marketplace/actions/freebsd-vm

* add: log to test

adding log to the test for better understanding

* fix: shellcheck

remove the yellow color that is unused in the test

* fix: fail on lower-casting

the `"${variable,,}"` seam to not work on macOS so y remove it for the `skip_hardlink_flag` because it's always lowercase

* fix: macOS

remove all the lower-case casting

* fix(perf): going to `stat` remove `lsattr` macOS

The command `lsattr` is not supported on macOS so I have remove it
switch to `stat` instead of `ls` for better performance and no subshell
use of parameter expansion with  `%` in place of `awk` for performance and no subshell

* fix: forgot to remove variable
  • Loading branch information
meo-pill authored Dec 16, 2024
1 parent 9225318 commit bd8d447
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 33 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ on:
branches: [ master ]

jobs:
shellcheck:
name: Test
linuxTest:
name: Test on Linux
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run testing script
run: ./testing.sh

macOsTest:
name: Test on macOS
runs-on: macos-latest
steps:
- uses: actions/checkout@v4

- name: Run testing script on macOS
run: ./testing.sh
35 changes: 34 additions & 1 deletion testing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ log_error_file=./error.log
test_data_src=./test/pool
test_pool_data_path=./testing_data

## Color Constants

# Reset
Color_Off='\033[0m' # Text Reset

# Regular Colors
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
Cyan='\033[0;36m' # Cyan

## Functions

# print a given text entirely in a given color
function color_echo () {
color=$1
text=$2
echo -e "${color}${text}${Color_Off}"
}

function prepare() {
# cleanup
rm -f $log_std_file
Expand All @@ -24,7 +43,7 @@ function prepare() {
function assertions() {
# check error log is empty
if grep -q '[^[:space:]]' $log_error_file; then
echo "error log is not empty!"
color_echo "$Red" "error log is not empty!"
cat $log_error_file
exit 1
fi
Expand All @@ -44,21 +63,30 @@ function assert_matching_file_not_copied() {
fi
}

color_echo "$Cyan" "Running tests..."

color_echo "$Cyan" "Running tests with default options..."
prepare
./zfs-inplace-rebalancing.sh $test_pool_data_path >> $log_std_file 2>> $log_error_file
cat $log_std_file
assertions
color_echo "$Green" "Tests passed!"

color_echo "$Cyan" "Running tests with checksum true and 1 pass..."
prepare
./zfs-inplace-rebalancing.sh --checksum true --passes 1 $test_pool_data_path >> $log_std_file 2>> $log_error_file
cat $log_std_file
assertions
color_echo "$Green" "Tests passed!"

color_echo "$Cyan" "Running tests with checksum false..."
prepare
./zfs-inplace-rebalancing.sh --checksum false $test_pool_data_path >> $log_std_file 2>> $log_error_file
cat $log_std_file
assertions
color_echo "$Green" "Tests passed!"

color_echo "$Cyan" "Running tests with skip-hardlinks false..."
prepare
ln "$test_pool_data_path/projects/[2020] some project/mp4.txt" "$test_pool_data_path/projects/[2020] some project/mp4.txt.link"
./zfs-inplace-rebalancing.sh --skip-hardlinks false $test_pool_data_path >> $log_std_file 2>> $log_error_file
Expand All @@ -67,7 +95,9 @@ cat $log_std_file
assert_matching_file_copied "mp4.txt"
assert_matching_file_copied "mp4.txt.link"
assertions
color_echo "$Green" "Tests passed!"

color_echo "$Cyan" "Running tests with skip-hardlinks true..."
prepare
ln "$test_pool_data_path/projects/[2020] some project/mp4.txt" "$test_pool_data_path/projects/[2020] some project/mp4.txt.link"
./zfs-inplace-rebalancing.sh --skip-hardlinks true $test_pool_data_path >> $log_std_file 2>> $log_error_file
Expand All @@ -76,3 +106,6 @@ cat $log_std_file
assert_matching_file_not_copied "mp4.txt.link"
assert_matching_file_not_copied "mp4.txt"
assertions
color_echo "$Green" "Tests passed!"

color_echo "$Green" "All tests passed!"
63 changes: 33 additions & 30 deletions zfs-inplace-rebalancing.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ function rebalance() {
# check if file has >=2 links in the case of --skip-hardlinks
# this shouldn't be needed in the typical case of `find` only finding files with links == 1
# but this can run for a long time, so it's good to double check if something changed
if [[ "${skip_hardlinks_flag,,}" == "true"* ]]; then
if [[ "${OSTYPE,,}" == "linux-gnu"* ]]; then
if [[ "${skip_hardlinks_flag}" == "true"* ]]; then
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
# Linux
#
# -c --format=FORMAT
Expand All @@ -68,7 +68,7 @@ function rebalance() {
# %h number of hard links

hardlink_count=$(stat -c "%h" "${file_path}")
elif [[ "${OSTYPE,,}" == "darwin"* ]] || [[ "${OSTYPE,,}" == "freebsd"* ]]; then
elif [[ "${OSTYPE}" == "darwin"* ]] || [[ "${OSTYPE}" == "freebsd"* ]]; then
# Mac OS
# FreeBSD
# -f format
Expand Down Expand Up @@ -108,15 +108,15 @@ function rebalance() {
tmp_file_path="${file_path}${tmp_extension}"

echo "Copying '${file_path}' to '${tmp_file_path}'..."
if [[ "${OSTYPE,,}" == "linux-gnu"* ]]; then
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
# Linux

# --reflink=never -- force standard copy (see ZFS Block Cloning)
# -a -- keep attributes, includes -d -- keep symlinks (dont copy target) and
# -p -- preserve ACLs to
# -x -- stay on one system
cp --reflink=never -ax "${file_path}" "${tmp_file_path}"
elif [[ "${OSTYPE,,}" == "darwin"* ]] || [[ "${OSTYPE,,}" == "freebsd"* ]]; then
elif [[ "${OSTYPE}" == "darwin"* ]] || [[ "${OSTYPE}" == "freebsd"* ]]; then
# Mac OS
# FreeBSD

Expand All @@ -131,45 +131,48 @@ function rebalance() {
fi

# compare copy against original to make sure nothing went wrong
if [[ "${checksum_flag,,}" == "true"* ]]; then
if [[ "${checksum_flag}" == "true"* ]]; then
echo "Comparing copy against original..."
if [[ "${OSTYPE,,}" == "linux-gnu"* ]]; then
if [[ "${OSTYPE}" == "linux-gnu"* ]]; then
# Linux

# file attributes
original_md5=$(lsattr "${file_path}" | awk '{print $1}')
# file permissions, owner, group
# shellcheck disable=SC2012
original_md5="${original_md5} $(ls -lha "${file_path}" | awk '{print $1 " " $3 " " $4}')"
original_md5=$(lsattr "${file_path}")
# remove anything after the last space
original_md5=${original_md5% *}
# file permissions, owner, group, size, modification time
original_md5="${original_md5} $(stat -c "%A %U %G %s %Y" "${file_path}")"
# file content
original_md5="${original_md5} $(md5sum -b "${file_path}" | awk '{print $1}')"
original_md5="${original_md5} $(md5sum -b "${file_path}")"


# file attributes
copy_md5=$(lsattr "${tmp_file_path}" | awk '{print $1}')
# file permissions, owner, group
# shellcheck disable=SC2012
copy_md5="${copy_md5} $(ls -lha "${tmp_file_path}" | awk '{print $1 " " $3 " " $4}')"
copy_md5=$(lsattr "${tmp_file_path}")
# remove anything after the last space
copy_md5=${copy_md5% *}
# file permissions, owner, group, size, modification time
copy_md5="${copy_md5} $(stat -c "%A %U %G %s %Y" "${tmp_file_path}")"
# file content
copy_md5="${copy_md5} $(md5sum -b "${tmp_file_path}" | awk '{print $1}')"
elif [[ "${OSTYPE,,}" == "darwin"* ]] || [[ "${OSTYPE,,}" == "freebsd"* ]]; then
copy_md5="${copy_md5} $(md5sum -b "${tmp_file_path}")"
# remove the temporary extension
copy_md5=${copy_md5%"${tmp_extension}"}
elif [[ "${OSTYPE}" == "darwin"* ]] || [[ "${OSTYPE}" == "freebsd"* ]]; then
# Mac OS
# FreeBSD

# file attributes
original_md5=$(lsattr "${file_path}" | awk '{print $1}')
# file permissions, owner, group
# shellcheck disable=SC2012
original_md5="${original_md5} $(ls -lha "${file_path}" | awk '{print $1 " " $3 " " $4}')"
# note: no lsattr on Mac OS or FreeBSD

# file permissions, owner, group size, modification time
original_md5="$(stat -f "%Sp %Su %Sg %z %m" "${file_path}")"
# file content
original_md5="${original_md5} $(md5 -q "${file_path}")"

# file attributes
copy_md5=$(lsattr "${tmp_file_path}" | awk '{print $1}')
# file permissions, owner, group
# shellcheck disable=SC2012
copy_md5="${copy_md5} $(ls -lha "${tmp_file_path}" | awk '{print $1 " " $3 " " $4}')"
# file permissions, owner, group size, modification time
copy_md5="$(stat -f "%Sp %Su %Sg %z %m" "${tmp_file_path}")"
# file content
copy_md5="${copy_md5} $(md5 -q "${tmp_file_path}")"
# remove the temporary extension
copy_md5=${copy_md5%"${tmp_extension}"}
else
echo "Unsupported OS type: $OSTYPE"
exit 1
Expand Down Expand Up @@ -254,7 +257,7 @@ color_echo "$Cyan" " Use Checksum: ${checksum_flag}"
color_echo "$Cyan" " Skip Hardlinks: ${skip_hardlinks_flag}"

# count files
if [[ "${skip_hardlinks_flag,,}" == "true"* ]]; then
if [[ "${skip_hardlinks_flag}" == "true"* ]]; then
file_count=$(find "${root_path}" -type f -links 1 | wc -l)
else
file_count=$(find "${root_path}" -type f | wc -l)
Expand All @@ -269,7 +272,7 @@ fi

# recursively scan through files and execute "rebalance" procedure
# in the case of --skip-hardlinks, only find files with links == 1
if [[ "${skip_hardlinks_flag,,}" == "true"* ]]; then
if [[ "${skip_hardlinks_flag}" == "true"* ]]; then
find "$root_path" -type f -links 1 -print0 | while IFS= read -r -d '' file; do rebalance "$file"; done
else
find "$root_path" -type f -print0 | while IFS= read -r -d '' file; do rebalance "$file"; done
Expand Down

0 comments on commit bd8d447

Please sign in to comment.