blob: 2fdb6f5502fed665d75acf105fa16c29e5efc3b2 (
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#!/usr/bin/env bash
# ==============================================================================
# Cron Job Management Tests
# ==============================================================================
source "$(dirname "${BASH_SOURCE[0]}")/../lib/test_helpers.sh"
# Save original crontab to restore later
ORIGINAL_CRONTAB=""
save_crontab() {
ORIGINAL_CRONTAB=$(crontab -l 2>/dev/null || true)
}
restore_crontab() {
if [ -n "$ORIGINAL_CRONTAB" ]; then
echo "$ORIGINAL_CRONTAB" | crontab -
else
crontab -r 2>/dev/null || true
fi
}
# ------------------------------------------------------------------------------
# Test: Setup adds cron jobs
# ------------------------------------------------------------------------------
test_setup_adds_cron_jobs() {
setup_test_env
save_crontab
# Clear existing crontab
crontab -r 2>/dev/null || true
# Create minimal config
cat > "$TEST_CONFIG_DIR/config" << EOF
REMOTE_HOST=""
MOUNTDIR="$TEST_BACKUP_DIR"
EOF
create_test_includes
create_test_excludes
# Run setup (will fail on some checks but should still add cron)
sudo INSTALLHOME="$TEST_CONFIG_DIR" SCRIPTLOC="/tmp/rsyncshot-test" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" setup 2>&1 >/dev/null || true
# Check crontab contains rsyncshot entries
local crontab_content
crontab_content=$(crontab -l 2>/dev/null)
restore_crontab
teardown_test_env
assert_contains "$crontab_content" "rsyncshot" "crontab should contain rsyncshot" || return 1
assert_contains "$crontab_content" "hourly" "crontab should contain hourly job" || return 1
assert_contains "$crontab_content" "daily" "crontab should contain daily job" || return 1
assert_contains "$crontab_content" "weekly" "crontab should contain weekly job" || return 1
}
# ------------------------------------------------------------------------------
# Test: Repeated setup doesn't duplicate entries
# ------------------------------------------------------------------------------
test_no_duplicate_cron_entries() {
setup_test_env
save_crontab
# Clear existing crontab
crontab -r 2>/dev/null || true
cat > "$TEST_CONFIG_DIR/config" << EOF
REMOTE_HOST=""
MOUNTDIR="$TEST_BACKUP_DIR"
EOF
create_test_includes
create_test_excludes
# Run setup twice
sudo INSTALLHOME="$TEST_CONFIG_DIR" SCRIPTLOC="/tmp/rsyncshot-test" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" setup 2>&1 >/dev/null || true
sudo INSTALLHOME="$TEST_CONFIG_DIR" SCRIPTLOC="/tmp/rsyncshot-test" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" setup 2>&1 >/dev/null || true
# Count rsyncshot entries
local crontab_content hourly_count
crontab_content=$(crontab -l 2>/dev/null)
hourly_count=$(echo "$crontab_content" | grep -c "hourly" || echo 0)
restore_crontab
teardown_test_env
# Should only have 1 hourly entry, not 2
assert_equals "1" "$hourly_count" "should have exactly 1 hourly entry after repeated setup" || return 1
}
# ------------------------------------------------------------------------------
# Test: Setup preserves existing cron jobs
# ------------------------------------------------------------------------------
test_preserves_existing_cron() {
setup_test_env
save_crontab
# Add a custom cron job
(crontab -l 2>/dev/null || true; echo "0 5 * * * /custom/job.sh") | crontab -
cat > "$TEST_CONFIG_DIR/config" << EOF
REMOTE_HOST=""
MOUNTDIR="$TEST_BACKUP_DIR"
EOF
create_test_includes
create_test_excludes
# Run setup
sudo INSTALLHOME="$TEST_CONFIG_DIR" SCRIPTLOC="/tmp/rsyncshot-test" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" setup 2>&1 >/dev/null || true
# Check custom job still exists
local crontab_content
crontab_content=$(crontab -l 2>/dev/null)
restore_crontab
teardown_test_env
assert_contains "$crontab_content" "/custom/job.sh" "should preserve existing cron jobs" || return 1
assert_contains "$crontab_content" "rsyncshot" "should also have rsyncshot jobs" || return 1
}
# ------------------------------------------------------------------------------
# Run tests
# ------------------------------------------------------------------------------
run_cron_tests() {
echo ""
echo "Running cron tests..."
echo "------------------------------------------------------------"
run_test "setup adds cron jobs" test_setup_adds_cron_jobs
run_test "repeated setup doesn't duplicate entries" test_no_duplicate_cron_entries
run_test "setup preserves existing cron jobs" test_preserves_existing_cron
}
# Run if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
run_cron_tests
print_summary
fi
|