aboutsummaryrefslogtreecommitdiff
path: root/tests/cases/test_cron.sh
blob: 7e3806004739b8a39ccb04400bc06842f156c7f9 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/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 >/dev/null 2>&1 || 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 >/dev/null 2>&1 || true
    sudo INSTALLHOME="$TEST_CONFIG_DIR" SCRIPTLOC="/tmp/rsyncshot-test" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" setup >/dev/null 2>&1 || 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 >/dev/null 2>&1 || 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
}

# ------------------------------------------------------------------------------
# Test: Cron entries no longer wrap rsyncshot in an external flock
# ------------------------------------------------------------------------------
# rsyncshot locks internally now, so setup should write bare cron commands.
test_cron_no_flock_wrapper() {
    setup_test_env
    save_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

    sudo INSTALLHOME="$TEST_CONFIG_DIR" SCRIPTLOC="/tmp/rsyncshot-test" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" setup >/dev/null 2>&1 || true

    local crontab_content
    crontab_content=$(crontab -l 2>/dev/null)

    restore_crontab
    teardown_test_env

    assert_not_contains "$crontab_content" "flock" "cron entries should not wrap rsyncshot in flock (internal locking)" || return 1
    assert_contains "$crontab_content" "rsyncshot" "cron should still contain rsyncshot entries" || 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_test "cron entries have no flock wrapper" test_cron_no_flock_wrapper
}

# Run if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    run_cron_tests
    print_summary
fi