|
4 | 4 | "fmt"
|
5 | 5 | "os"
|
6 | 6 | "path/filepath"
|
| 7 | + "strings" |
7 | 8 | "testing"
|
8 | 9 | "time"
|
9 | 10 |
|
@@ -90,7 +91,15 @@ func TestWatcher(t *testing.T) {
|
90 | 91 |
|
91 | 92 | // nolint:gocritic
|
92 | 93 | func TestWatcherPaths(t *testing.T) {
|
93 |
| - dir := t.TempDir() |
| 94 | + originalDir := t.TempDir() |
| 95 | + |
| 96 | + dirWithLink := t.TempDir() |
| 97 | + dir := filepath.Join(dirWithLink, "symlink") |
| 98 | + err := os.Symlink(originalDir, dir) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("Failed to create symlink: %v", err) |
| 101 | + } |
| 102 | + |
94 | 103 | shouldCreate := atomic.Int64{}
|
95 | 104 | notifyFn := func(_ notify.Event, _ string, _ os.FileInfo) {
|
96 | 105 | shouldCreate.Inc()
|
@@ -124,47 +133,47 @@ func TestWatcherPaths(t *testing.T) {
|
124 | 133 | shouldNotify bool
|
125 | 134 | }{
|
126 | 135 | {
|
127 |
| - filename: filepath.Join(dir, "nginx-ingress-0/error.log"), |
| 136 | + filename: filepath.Join(originalDir, "nginx-ingress-0/error.log"), |
128 | 137 | shouldNotify: true,
|
129 | 138 | },
|
130 | 139 | {
|
131 |
| - filename: filepath.Join(dir, "log/errors.log"), |
| 140 | + filename: filepath.Join(originalDir, "log/errors.log"), |
132 | 141 | shouldNotify: true,
|
133 | 142 | },
|
134 | 143 | {
|
135 |
| - filename: filepath.Join(dir, "log/0/errors.log"), |
| 144 | + filename: filepath.Join(originalDir, "log/0/errors.log"), |
136 | 145 | shouldNotify: true,
|
137 | 146 | },
|
138 | 147 | {
|
139 |
| - filename: filepath.Join(dir, "log/0/0/errors.log"), |
| 148 | + filename: filepath.Join(originalDir, "log/0/0/errors.log"), |
140 | 149 | shouldNotify: true,
|
141 | 150 | },
|
142 | 151 | {
|
143 |
| - filename: filepath.Join(dir, "access.log"), |
| 152 | + filename: filepath.Join(originalDir, "access.log"), |
144 | 153 | shouldNotify: true,
|
145 | 154 | },
|
146 | 155 | {
|
147 |
| - filename: filepath.Join(dir, "sub_access.log"), |
| 156 | + filename: filepath.Join(originalDir, "sub_access.log"), |
148 | 157 | shouldNotify: true,
|
149 | 158 | },
|
150 | 159 | {
|
151 |
| - filename: filepath.Join(dir, "access1.log"), |
| 160 | + filename: filepath.Join(originalDir, "access1.log"), |
152 | 161 | shouldNotify: false,
|
153 | 162 | },
|
154 | 163 | {
|
155 |
| - filename: filepath.Join(dir, "nginx/errors.log"), |
| 164 | + filename: filepath.Join(originalDir, "nginx/errors.log"), |
156 | 165 | shouldNotify: false,
|
157 | 166 | },
|
158 | 167 | {
|
159 |
| - filename: filepath.Join(dir, "log/payments/errors.log"), |
| 168 | + filename: filepath.Join(originalDir, "log/payments/errors.log"), |
160 | 169 | shouldNotify: false,
|
161 | 170 | },
|
162 | 171 | {
|
163 |
| - filename: filepath.Join(dir, "log/payments/nginx-ingress-0/errors.log"), |
| 172 | + filename: filepath.Join(originalDir, "log/payments/nginx-ingress-0/errors.log"), |
164 | 173 | shouldNotify: false,
|
165 | 174 | },
|
166 | 175 | {
|
167 |
| - filename: filepath.Join(dir, "nginx-ingress-payments/error.log"), |
| 176 | + filename: filepath.Join(originalDir, "nginx-ingress-payments/error.log"), |
168 | 177 | shouldNotify: false,
|
169 | 178 | },
|
170 | 179 | }
|
@@ -204,3 +213,25 @@ func TestCommonPathPrefix(t *testing.T) {
|
204 | 213 | result := commonPathPrefix(paths)
|
205 | 214 | a.Equal("/var", result)
|
206 | 215 | }
|
| 216 | + |
| 217 | +func TestResolvePathLinks(t *testing.T) { |
| 218 | + a := assert.New(t) |
| 219 | + originalDir := t.TempDir() |
| 220 | + |
| 221 | + dirWithLink := t.TempDir() |
| 222 | + dirLink := filepath.Join(dirWithLink, "symlink") |
| 223 | + err := os.Symlink(originalDir, dirLink) |
| 224 | + if err != nil { |
| 225 | + t.Fatalf("Failed to create symlink: %v", err) |
| 226 | + } |
| 227 | + linkSubDir := filepath.Join(dirLink, "dir") |
| 228 | + os.Mkdir(linkSubDir, os.FileMode(0o755)) |
| 229 | + |
| 230 | + resultDir := filepath.Join(linkSubDir, "dir") |
| 231 | + result, err := resolvePathLinks(resultDir) |
| 232 | + a.Equal( |
| 233 | + strings.Replace(resultDir, dirLink, originalDir, 1), |
| 234 | + result, |
| 235 | + ) |
| 236 | + a.Equal(nil, err) |
| 237 | +} |
0 commit comments