aboutsummaryrefslogtreecommitdiff
path: root/tests/cases/test_backup.sh
blob: 1ab4fbdeae77f0b23a7951c0d43b17269a24e5da (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
#!/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

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

    # 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

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

    # Check files were copied
    assert_file_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest/home/testuser/file1.txt" "should copy file1.txt" || {
        teardown_test_env
        return 1
    }
    assert_file_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest/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

    local output
    output=$(sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 1 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 2>&1 >/dev/null
    sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 3 2>&1 >/dev/null

    # 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 i in 1 2 3 4; do
        sudo INSTALLHOME="$TEST_CONFIG_DIR" RSYNCSHOT_SKIP_MOUNT_CHECK=1 "$SCRIPT_PATH" manual 3 2>&1 >/dev/null
    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

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

    # 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"

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

    # Excluded files should not exist in backup
    assert_file_not_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest/home/testuser/temp.tmp" "should exclude .tmp files" || {
        teardown_test_env
        return 1
    }
    assert_file_not_exists "$TEST_BACKUP_DIR/$HOSTNAME/latest/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/home/testuser/file1.txt" "should include regular files" || {
        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 if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    run_backup_tests
    print_summary
fi