aboutsummaryrefslogtreecommitdiff
path: root/tests/cases/test_backup.sh
blob: 607942eaf2933427f6c3b0b343ae38d10166165a (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env bash
# ==============================================================================
# Backup and Rotation Tests
# ==============================================================================

source "$(dirname "${BASH_SOURCE[0]}")/../lib/test_helpers.sh"

# ------------------------------------------------------------------------------
# Test: Creates backup directory structure
# ------------------------------------------------------------------------------
test_creates_backup_structure() {
    setup_test_env

    create_test_config
    create_test_includes
    create_test_excludes

    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 >/dev/null 2>&1

    # Check directory structure
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME" "should create hostname dir" || {
        teardown_test_env
        return 1
    }
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest" "should create latest dir" || {
        teardown_test_env
        return 1
    }
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.0" "should create MANUAL.0 snapshot" || {
        teardown_test_env
        return 1
    }

    teardown_test_env
}

# ------------------------------------------------------------------------------
# Test: Copies files to backup
# ------------------------------------------------------------------------------
test_copies_files() {
    setup_test_env

    create_test_config
    create_test_includes
    create_test_excludes

    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 >/dev/null 2>&1

    # Check files were copied
    assert_file_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest$TEST_SOURCE_DIR/home/testuser/file1.txt" "should copy file1.txt" || {
        teardown_test_env
        return 1
    }
    assert_file_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest$TEST_SOURCE_DIR/etc/test.conf" "should copy test.conf" || {
        teardown_test_env
        return 1
    }

    teardown_test_env
}

# ------------------------------------------------------------------------------
# Test: Snapshot is read-only
# ------------------------------------------------------------------------------
test_snapshot_readonly() {
    setup_test_env

    create_test_config
    create_test_includes
    create_test_excludes

    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 >/dev/null 2>&1

    # Check snapshot directory has no write permission
    # Note: We use stat to check actual permissions because -w always returns true for root
    local perms
    perms=$(stat -c '%A' "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.0" 2>/dev/null)
    if [[ "$perms" == *w* ]]; then
        echo "FAIL: Snapshot should be read-only (perms: $perms)"
        teardown_test_env
        return 1
    fi

    teardown_test_env
    return 0
}

# ------------------------------------------------------------------------------
# Test: Rotation works correctly
# ------------------------------------------------------------------------------
test_rotation() {
    setup_test_env

    create_test_config
    create_test_includes
    create_test_excludes

    # Run backup twice
    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 3 >/dev/null 2>&1
    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 3 >/dev/null 2>&1

    # Should have MANUAL.0 and MANUAL.1
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.0" "should have MANUAL.0" || {
        teardown_test_env
        return 1
    }
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.1" "should have MANUAL.1 after rotation" || {
        teardown_test_env
        return 1
    }

    teardown_test_env
}

# ------------------------------------------------------------------------------
# Test: Deletes oldest snapshot beyond retention
# ------------------------------------------------------------------------------
test_retention_limit() {
    setup_test_env

    create_test_config
    create_test_includes
    create_test_excludes

    # Run backup 4 times with retention of 3
    for _ in 1 2 3 4; do
        sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 3 >/dev/null 2>&1
    done

    # Should have MANUAL.0, MANUAL.1, MANUAL.2 but NOT MANUAL.3
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.0" "should have MANUAL.0" || {
        teardown_test_env
        return 1
    }
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.1" "should have MANUAL.1" || {
        teardown_test_env
        return 1
    }
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.2" "should have MANUAL.2" || {
        teardown_test_env
        return 1
    }
    assert_dir_not_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.3" "should NOT have MANUAL.3 (beyond retention)" || {
        teardown_test_env
        return 1
    }

    teardown_test_env
}

# ------------------------------------------------------------------------------
# Test: Backup command works as alias
# ------------------------------------------------------------------------------
test_backup_command() {
    setup_test_env

    create_test_config
    create_test_includes
    create_test_excludes

    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" backup >/dev/null 2>&1

    # Should create MANUAL.0 (backup is alias for manual 1)
    assert_dir_exists "$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.0" "backup should create MANUAL.0" || {
        teardown_test_env
        return 1
    }

    teardown_test_env
}

# ------------------------------------------------------------------------------
# Test: Excludes files matching patterns
# ------------------------------------------------------------------------------
test_excludes_patterns() {
    setup_test_env

    create_test_config
    create_test_includes

    # Create exclude file
    cat > "$TEST_CONFIG_DIR/exclude.txt" << 'EOF'
*.tmp
*.log
EOF

    # Create files that should be excluded
    echo "temp" > "$TEST_SOURCE_DIR/home/testuser/temp.tmp"
    echo "log" > "$TEST_SOURCE_DIR/home/testuser/app.log"

    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 >/dev/null 2>&1

    # Excluded files should not exist in backup
    assert_file_not_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest$TEST_SOURCE_DIR/home/testuser/temp.tmp" "should exclude .tmp files" || {
        teardown_test_env
        return 1
    }
    assert_file_not_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest$TEST_SOURCE_DIR/home/testuser/app.log" "should exclude .log files" || {
        teardown_test_env
        return 1
    }
    # Regular files should still exist
    assert_file_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest$TEST_SOURCE_DIR/home/testuser/file1.txt" "should include regular files" || {
        teardown_test_env
        return 1
    }

    teardown_test_env
}

# ------------------------------------------------------------------------------
# Test: Same-basename sources don't collide (relative paths)
# ------------------------------------------------------------------------------
# Two includes with the same basename ("bin") previously both mapped to
# latest/bin, and the second sync's --delete wiped the first. With -R each is
# stored under its full path, so both survive.
test_no_basename_collision() {
    setup_test_env

    mkdir -p "$TEST_SOURCE_DIR/opt/bin" "$TEST_SOURCE_DIR/usr/bin"
    echo "from-opt" > "$TEST_SOURCE_DIR/opt/bin/optfile"
    echo "from-usr" > "$TEST_SOURCE_DIR/usr/bin/usrfile"

    cat > "$TEST_CONFIG_DIR/config" <<EOF
REMOTE_HOST=""
MOUNTDIR="$TEST_BACKUP_DIR"
EOF
    cat > "$TEST_CONFIG_DIR/include.txt" <<EOF
$TEST_SOURCE_DIR/opt/bin
$TEST_SOURCE_DIR/usr/bin
EOF
    create_test_excludes >/dev/null

    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 >/dev/null 2>&1

    assert_file_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest$TEST_SOURCE_DIR/opt/bin/optfile" "opt/bin should survive" || {
        teardown_test_env
        return 1
    }
    assert_file_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest$TEST_SOURCE_DIR/usr/bin/usrfile" "usr/bin should survive (no collision)" || {
        teardown_test_env
        return 1
    }

    teardown_test_env
}

# ------------------------------------------------------------------------------
# Test: nested directories in a snapshot are read-only (not just the top dir)
# ------------------------------------------------------------------------------
test_snapshot_nested_dirs_readonly() {
    setup_test_env

    create_test_config >/dev/null
    create_test_includes >/dev/null
    create_test_excludes >/dev/null

    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 >/dev/null 2>&1

    # A nested directory inside the snapshot (stored under its full path by -R)
    local nested="$TEST_BACKUP_DIR/$HOSTNAME/MANUAL.0$TEST_SOURCE_DIR/home"
    local perms
    perms=$(stat -c '%A' "$nested" 2>/dev/null)
    if [[ "$perms" == *w* ]]; then
        echo "FAIL: nested snapshot dir should be read-only (perms: $perms)"
        teardown_test_env
        return 1
    fi

    teardown_test_env
    return 0
}

# ------------------------------------------------------------------------------
# Test: status counts snapshots without counting the latest/ working dir
# ------------------------------------------------------------------------------
test_status_excludes_latest_from_count() {
    setup_test_env

    create_test_config >/dev/null
    create_test_includes >/dev/null
    create_test_excludes >/dev/null

    # One backup creates MANUAL.0 plus the latest/ working dir
    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 >/dev/null 2>&1

    local output
    output=$(sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" status 2>&1)

    assert_contains "$output" "1 snapshot(s) found" "status should count 1 snapshot, not counting latest/" || {
        teardown_test_env
        return 1
    }

    teardown_test_env
}

# ------------------------------------------------------------------------------
# Run tests
# ------------------------------------------------------------------------------
run_backup_tests() {
    echo ""
    echo "Running backup tests..."
    echo "------------------------------------------------------------"

    run_test "creates backup directory structure" test_creates_backup_structure
    run_test "copies files to backup" test_copies_files
    run_test "snapshot is read-only" test_snapshot_readonly
    run_test "rotation works correctly" test_rotation
    run_test "respects retention limit" test_retention_limit
    run_test "backup command works as alias" test_backup_command
    run_test "excludes files matching patterns" test_excludes_patterns
    run_test "same-basename sources don't collide" test_no_basename_collision
    run_test "status count excludes latest/" test_status_excludes_latest_from_count
    run_test "nested snapshot dirs are read-only" test_snapshot_nested_dirs_readonly
}

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