|
4 | 4 | "context" |
5 | 5 | "os" |
6 | 6 | "path/filepath" |
| 7 | + "syscall" |
7 | 8 | "testing" |
8 | 9 | "time" |
9 | 10 |
|
@@ -147,6 +148,109 @@ func TestCheckPermissions(t *testing.T) { |
147 | 148 | if !result.Passed { |
148 | 149 | t.Errorf("CheckPermissions failed: %s", result.Message) |
149 | 150 | } |
| 151 | + if result.Code != "PERMISSION_CHECK" { |
| 152 | + t.Errorf("expected Code PERMISSION_CHECK on success, got %q", result.Code) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestCheckPermissionsPermissionDenied(t *testing.T) { |
| 157 | + logger := logging.New(types.LogLevelInfo, false) |
| 158 | + tmpDir := t.TempDir() |
| 159 | + |
| 160 | + // When running tests as root, permission checks based on chmod may not |
| 161 | + // behave as expected because root can bypass filesystem permissions. |
| 162 | + // In that case, skip this test. |
| 163 | + if os.Geteuid() == 0 { |
| 164 | + t.Skip("skipping permission-denied check when running as root") |
| 165 | + } |
| 166 | + |
| 167 | + // Create a directory that is not writable |
| 168 | + protectedDir := filepath.Join(tmpDir, "protected") |
| 169 | + if err := os.Mkdir(protectedDir, 0755); err != nil { |
| 170 | + t.Fatalf("failed to create protected dir: %v", err) |
| 171 | + } |
| 172 | + if err := os.Chmod(protectedDir, 0555); err != nil { |
| 173 | + t.Fatalf("failed to chmod protected dir: %v", err) |
| 174 | + } |
| 175 | + |
| 176 | + config := &CheckerConfig{ |
| 177 | + BackupPath: protectedDir, |
| 178 | + LogPath: tmpDir, |
| 179 | + LockDirPath: tmpDir, |
| 180 | + SkipPermissionCheck: false, |
| 181 | + DryRun: false, |
| 182 | + } |
| 183 | + |
| 184 | + checker := NewChecker(logger, config) |
| 185 | + result := checker.CheckPermissions() |
| 186 | + |
| 187 | + if result.Passed { |
| 188 | + t.Fatalf("CheckPermissions should fail for non-writable directory") |
| 189 | + } |
| 190 | + if result.Code != "PERMISSION_DENIED" { |
| 191 | + t.Errorf("expected Code PERMISSION_DENIED, got %q", result.Code) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +func TestCheckPermissionsDryRun(t *testing.T) { |
| 196 | + logger := logging.New(types.LogLevelInfo, false) |
| 197 | + tmpDir := t.TempDir() |
| 198 | + |
| 199 | + config := &CheckerConfig{ |
| 200 | + BackupPath: tmpDir, |
| 201 | + LogPath: tmpDir, |
| 202 | + LockDirPath: tmpDir, |
| 203 | + SkipPermissionCheck: false, |
| 204 | + DryRun: true, |
| 205 | + } |
| 206 | + |
| 207 | + checker := NewChecker(logger, config) |
| 208 | + result := checker.CheckPermissions() |
| 209 | + |
| 210 | + if !result.Passed { |
| 211 | + t.Fatalf("CheckPermissions should pass in dry-run mode, got: %s", result.Message) |
| 212 | + } |
| 213 | + if result.Code != "PERMISSION_CHECK" { |
| 214 | + t.Errorf("expected Code PERMISSION_CHECK in dry-run mode, got %q", result.Code) |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +func TestCheckPermissionsEIORetryAndFailure(t *testing.T) { |
| 219 | + logger := logging.New(types.LogLevelInfo, false) |
| 220 | + tmpDir := t.TempDir() |
| 221 | + |
| 222 | + // Save and restore original createTestFile to avoid side effects |
| 223 | + origCreate := createTestFile |
| 224 | + defer func() { |
| 225 | + createTestFile = origCreate |
| 226 | + }() |
| 227 | + |
| 228 | + attempts := 0 |
| 229 | + createTestFile = func(name string) (*os.File, error) { |
| 230 | + attempts++ |
| 231 | + return nil, syscall.EIO |
| 232 | + } |
| 233 | + |
| 234 | + config := &CheckerConfig{ |
| 235 | + BackupPath: tmpDir, |
| 236 | + LogPath: tmpDir, |
| 237 | + LockDirPath: tmpDir, |
| 238 | + SkipPermissionCheck: false, |
| 239 | + DryRun: false, |
| 240 | + } |
| 241 | + |
| 242 | + checker := NewChecker(logger, config) |
| 243 | + result := checker.CheckPermissions() |
| 244 | + |
| 245 | + if result.Passed { |
| 246 | + t.Fatalf("CheckPermissions should fail when all attempts return EIO") |
| 247 | + } |
| 248 | + if result.Code != "FS_IO_ERROR" { |
| 249 | + t.Errorf("expected Code FS_IO_ERROR, got %q", result.Code) |
| 250 | + } |
| 251 | + if attempts != 3 { |
| 252 | + t.Errorf("expected 3 attempts on EIO, got %d", attempts) |
| 253 | + } |
150 | 254 | } |
151 | 255 |
|
152 | 256 | func TestCheckDirectories(t *testing.T) { |
@@ -257,6 +361,39 @@ func TestRunAllChecks(t *testing.T) { |
257 | 361 | checker.ReleaseLock() |
258 | 362 | } |
259 | 363 |
|
| 364 | +func TestRunAllChecksSkipPermissionCheck(t *testing.T) { |
| 365 | + logger := logging.New(types.LogLevelInfo, false) |
| 366 | + tmpDir := t.TempDir() |
| 367 | + |
| 368 | + config := &CheckerConfig{ |
| 369 | + BackupPath: tmpDir, |
| 370 | + LogPath: tmpDir, |
| 371 | + LockDirPath: tmpDir, |
| 372 | + MinDiskPrimaryGB: 0.001, |
| 373 | + MinDiskSecondaryGB: 0, |
| 374 | + MinDiskCloudGB: 0, |
| 375 | + LockFilePath: filepath.Join(tmpDir, ".backup.lock"), |
| 376 | + MaxLockAge: 1 * time.Hour, |
| 377 | + SkipPermissionCheck: true, |
| 378 | + DryRun: false, |
| 379 | + } |
| 380 | + |
| 381 | + checker := NewChecker(logger, config) |
| 382 | + ctx := context.Background() |
| 383 | + |
| 384 | + results, err := checker.RunAllChecks(ctx) |
| 385 | + if err != nil { |
| 386 | + t.Fatalf("RunAllChecks (SkipPermissionCheck=true) failed unexpectedly: %v", err) |
| 387 | + } |
| 388 | + |
| 389 | + // Permissions check should be skipped: assert that no result is named "Permissions". |
| 390 | + for _, r := range results { |
| 391 | + if r.Name == "Permissions" { |
| 392 | + t.Fatalf("expected Permissions check to be skipped, but it was executed") |
| 393 | + } |
| 394 | + } |
| 395 | +} |
| 396 | + |
260 | 397 | func TestDryRunMode(t *testing.T) { |
261 | 398 | logger := logging.New(types.LogLevelInfo, false) |
262 | 399 | tmpDir := t.TempDir() |
|
0 commit comments