summaryrefslogtreecommitdiff
path: root/tests/run_tests.sh
blob: 36dff01076e1a51aa96561d8138ba37fbf755cfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash

if [ ! -f run_tests.sh ]
then
	echo "Test suite needs to be run from the tests directory" 1>&2
	exit 1
fi

if [ ! -f test_lib.sh ]
then
	echo "Could not find test_lib.sh" 1>&2
	exit 1
fi

compare_dirs() {

    # Temporary files used to compare the file permissions
    file1=$(mktemp)
    file2=$(mktemp)

    set +e
    (
        set -e
        # Getting the file permissions
        (cd "$1" && busybox find * -exec busybox stat -c '%n %a' {} + | sort) > "$file1"
        (cd "$2" && busybox find * -exec busybox stat -c '%n %a' {} + | sort) > "$file2"

        # Compare file contents
        diff -r "$1" "$2"

        # Compare permissions
        diff "$file1" "$file2"
    )
    local ret="$?"

    # Clean temporary files
    rm "$file1"
    rm "$file2"

    return $ret
}

echo "Starting baserock-system-config-sync tests"
merge_pass_folder="bscs-merge.pass"
merge_fail_folder="bscs-merge.fail"
sync_folder="bscs-sync"
bscs_script="../baserock-system-config-sync/baserock-system-config-sync"
bscs_log="bscs.log"
> "$bscs_log"

# test merge cases that should succeed
for folder in "$merge_pass_folder/"*.in; do
    echo -n "#### Running ${folder%.in}"
    echo "#### Running ${folder%.in}" >> "$bscs_log"
    out_folder=${folder%.in}.out
    TMPDIR=$(mktemp -d)
    TMPDIR=$TMPDIR mounting_script="./fake_mounting_script.sh" unmount=true \
        mounting_script_test_dir="$folder" "$bscs_script" "merge" \
        "version2" &>> "$bscs_log"
    exit_code="$?"
    if [ "$exit_code" -ne 0 ]; then
        echo ": FAILED (exit code "$exit_code")" 1>&2
        exit 1
    elif ! compare_dirs "$TMPDIR/"*/ "$out_folder/" &>> "$bscs_log"; then
        echo ": FAILED (different diff)" 1>&2
        exit 1
    else
        echo ": OK" 1>&2
    fi
    rm -rf $TMPDIR
done


# test merge changes that should fail
for folder in "$merge_fail_folder/"*.in; do
    echo -n "#### Running ${folder%.in}"
    echo "#### Running ${folder%.in}" >> "$bscs_log"
    TMPDIR=$(mktemp -d)
    TMPDIR=$TMPDIR mounting_script="./fake_mounting_script.sh" unmount=true \
        mounting_script_test_dir="$folder" "$bscs_script" "merge" \
        "version2" &>> "$bscs_log"
    if [ $? -eq 0 ]; then
        echo ": FAILED" 1>&2
        exit 1
    else
        echo ": OK" 1>&2
    fi
    rm -rf $TMPDIR
done


# test the sync mode
echo -n "#### Running sync test on $sync_folder"
echo "#### Running sync test on $sync_folder" >> "$bscs_log"
out_folder=${sync_folder}.out
TMPDIR=$(mktemp -d)
TMPDIR=$TMPDIR mounting_script="./fake_mounting_script.sh" unmount=true \
    mounting_script_test_dir="${sync_folder}.in" "$bscs_script" "sync" \
    "version3" &>> "$bscs_log"
exit_code="$?"
if [ "$exit_code" -ne 0 ]; then
    echo ": FAILED (exit code "$exit_code")" 1>&2
    exit 1
elif ! compare_dirs "$TMPDIR/"*/ "$out_folder/" &>> "$bscs_log"; then
    echo ": FAILED (different diff)" 1>&2a
    exit 1
else
   echo ": OK" 1>&2
fi
rm -rf $TMPDIR