aboutsummaryrefslogtreecommitdiffstats
path: root/errors_test.go
blob: e35e5b3c89168d61f69d272a0d14fbdeb1b5c100 (plain) (blame)
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
package main

import (
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"syscall"
	"testing"
)

func TestCopyFile_Normal(t *testing.T) {
	src := filepath.Join(t.TempDir(), "src.txt")
	dst := filepath.Join(t.TempDir(), "dst.txt")
	os.WriteFile(src, []byte("hello"), 0644)

	if err := copyFile(src, dst, 0644); err != nil {
		t.Fatal(err)
	}

	data, _ := os.ReadFile(dst)
	if string(data) != "hello" {
		t.Errorf("content = %q, want %q", string(data), "hello")
	}
}

func TestCopyFile_SrcNotExist(t *testing.T) {
	dst := filepath.Join(t.TempDir(), "dst.txt")
	err := copyFile("/nonexistent/file", dst, 0644)
	if err == nil {
		t.Fatal("expected error for nonexistent source")
	}
	if !os.IsNotExist(err) {
		t.Errorf("expected ErrNotExist, got: %v", err)
	}
}

func TestCopyFile_DstPermDenied(t *testing.T) {
	if os.Getuid() == 0 {
		t.Skip("running as root, permission test not applicable")
	}

	src := filepath.Join(t.TempDir(), "src.txt")
	os.WriteFile(src, []byte("hello"), 0644)

	dst := "/proc/nonexistent/file"
	err := copyFile(src, dst, 0644)
	if err == nil {
		t.Fatal("expected error for invalid dst")
	}
}

func TestRetry_TransientError(t *testing.T) {
	calls := 0
	err := retry("test", func() error {
		calls++
		if calls < 3 {
			return &fs.PathError{Op: "open", Path: "/tmp/test", Err: fs.ErrNotExist}
		}
		return nil
	})
	if err != nil {
		t.Errorf("expected success after retries, got: %v", err)
	}
	if calls != 3 {
		t.Errorf("calls = %d, want 3", calls)
	}
}

func TestRetry_PermanentError(t *testing.T) {
	calls := 0
	err := retry("test", func() error {
		calls++
		return &fs.PathError{Op: "write", Path: "/tmp/test", Err: syscall.ENOSPC}
	})
	if err == nil {
		t.Fatal("expected error")
	}
	if calls != 1 {
		t.Errorf("calls = %d, want 1 (no retries for ENOSPC)", calls)
	}
}

func TestIsRetryable(t *testing.T) {
	tests := []struct {
		err      error
		expected bool
	}{
		{fs.ErrNotExist, true},
		{&fs.PathError{Op: "open", Path: "/tmp", Err: fs.ErrNotExist}, true},
		{&fs.PathError{Op: "write", Path: "/tmp", Err: syscall.ENOSPC}, false},
		{syscall.ENOSPC, false},
		{syscall.EACCES, false},
	}

	for _, tt := range tests {
		if got := isRetryable(tt.err); got != tt.expected {
			t.Errorf("isRetryable(%v) = %v, want %v", tt.err, got, tt.expected)
		}
	}
}

func TestLogError_DiskFull(t *testing.T) {
	logSyncError("copy", "test.txt", syscall.ENOSPC)
}

func TestLogError_PermissionDenied(t *testing.T) {
	logSyncError("write", "test.txt", syscall.EACCES)
}

func TestLogError_NotExist(t *testing.T) {
	logSyncError("stat", "test.txt", fs.ErrNotExist)
}

func TestLogError_Nil(t *testing.T) {
	logSyncError("test", "file.txt", nil)
}

func TestSyncError(t *testing.T) {
	err := &SyncError{
		Path: "test.txt",
		Op:   "copy",
		Err:  syscall.ENOSPC,
	}

	if !strings.Contains(err.Error(), "copy") {
		t.Errorf("error should contain op, got: %s", err.Error())
	}
	if !strings.Contains(err.Error(), "test.txt") {
		t.Errorf("error should contain path, got: %s", err.Error())
	}
	if err.Unwrap() != syscall.ENOSPC {
		t.Errorf("unwrap should return underlying error")
	}
}

func TestCopyFileWithRetry_Success(t *testing.T) {
	src := filepath.Join(t.TempDir(), "src.txt")
	dst := filepath.Join(t.TempDir(), "dst.txt")
	os.WriteFile(src, []byte("content"), 0644)

	if err := copyFileWithRetry(src, dst, 0644); err != nil {
		t.Fatal(err)
	}

	data, _ := os.ReadFile(dst)
	if string(data) != "content" {
		t.Errorf("content = %q, want %q", string(data), "content")
	}
}