Skip to content

Commit 09a68f9

Browse files
authored
test: fix flaky test (#4218)
test: fix flaky test TestHTPasswdWatcher See: https://github.com/project-zot/zot/actions/runs/29072040791/job/86295357131?pr=4216 ``` ......{"time":"2026-07-10T05:55:15.087452573Z","level":"info","message":"loaded htpasswd file","htpasswd-file":"/tmp/TestHTPasswdWatcher3310482795/011/htpasswd","users":1,"caller":"zotregistry.dev/zot/v2/pkg/api/htpasswd.go:60","func":"zotregistry.dev/zot/v2/pkg/api.(*HTPasswd).Reload","goroutine":269} ...{"time":"2026-07-10T05:55:15.806575031Z","level":"warn","message":"loaded htpasswd file appears to have zero users","htpasswd-file":"/tmp/TestHTPasswdWatcher3310482795/012/htpasswd","caller":"zotregistry.dev/zot/v2/pkg/api/htpasswd.go:58","func":"zotregistry.dev/zot/v2/pkg/api.(*HTPasswd).Reload","goroutine":269} ...{"time":"2026-07-10T05:55:16.526216362Z","level":"info","message":"loaded htpasswd file","htpasswd-file":"/tmp/TestHTPasswdWatcher3310482795/013/htpasswd","users":1,"caller":"zotregistry.dev/zot/v2/pkg/api/htpasswd.go:60","func":"zotregistry.dev/zot/v2/pkg/api.(*HTPasswd).Reload","goroutine":269} .... Failures: * zotregistry.dev/zot/v2/pkg/api/htpasswd_test.go Line 352: Expected: true Actual: false 444 total assertions --- FAIL: TestHTPasswdWatcher (22.64s) ``` Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
1 parent bd3d363 commit 09a68f9

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

pkg/api/htpasswd_test.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,12 @@ func TestHTPasswdWatcher(t *testing.T) {
323323
time.Sleep(50 * time.Millisecond)
324324
So(func() { htw.Close() }, ShouldNotPanic)
325325
So(test.WaitForLogMessages(logBuffer, "htpasswd watcher terminating...", 1, 5*time.Second), ShouldBeTrue)
326+
time.Sleep(100 * time.Millisecond) // let watcher goroutine finish cleanup before restart
326327

327328
// Test concurrent ChangeFile() operations
328329
htw.Run()
329330
defer htw.Close()
331+
time.Sleep(10 * time.Millisecond)
330332

331333
go func() {
332334
for range 3 {
@@ -344,11 +346,22 @@ func TestHTPasswdWatcher(t *testing.T) {
344346
}
345347
}()
346348

347-
time.Sleep(50 * time.Millisecond)
349+
// Poll until at least one concurrent ChangeFile() reload authenticates.
350+
deadline := time.Now().Add(5 * time.Second)
351+
352+
var ok1, present1, ok2, present2 bool
353+
354+
for time.Now().Before(deadline) {
355+
ok1, present1 = htp.Authenticate(username1, password1)
356+
ok2, present2 = htp.Authenticate(username2, password2)
357+
if ok1 || ok2 {
358+
break
359+
}
360+
361+
time.Sleep(10 * time.Millisecond)
362+
}
348363

349364
// At least one user should be present
350-
ok1, present1 := htp.Authenticate(username1, password1)
351-
ok2, present2 := htp.Authenticate(username2, password2)
352365
So(present1 || present2, ShouldBeTrue)
353366
So(ok1 || ok2, ShouldBeTrue)
354367

0 commit comments

Comments
 (0)