Skip to content

Commit 8fb0abf

Browse files
codesmith25103cmainas
authored andcommitted
test(e2e): add retry logic for image pulling
Currently, end-to-end tests fail intermittently during the setup phase due to image pulling failures (e.g., registry timeouts or network issues). This commit adds a retry mechanism with exponential backoff to the image pulling logic in the test setup. It attempts to pull the required images up to 5 times before failing the test. Signed-off-by: Sankalp <sankalp25103@gmail.com>
1 parent 246492c commit 8fb0abf

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

tests/e2e/setup_test.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ import (
2121
"os"
2222
"strings"
2323
"testing"
24+
"time"
2425
)
2526

2627
const (
27-
testNerdctl = "TestNerdctl"
28-
testCtr = "TestCtr"
29-
testCrictl = "TestCrictl"
30-
testDocker = "TestDocker"
28+
testNerdctl = "TestNerdctl"
29+
testCtr = "TestCtr"
30+
testCrictl = "TestCrictl"
31+
testDocker = "TestDocker"
32+
maxPullRetries = 5
33+
pullRetryDelay = 2 * time.Second
3134
)
3235

3336
func TestMain(m *testing.M) {
@@ -114,7 +117,7 @@ func getTestImages(cases []containerTestArgs) []string {
114117
func pullAllImages(testFunc string, images []string) error {
115118
for _, image := range images {
116119
log.Printf("Pulling image: %s", image)
117-
if err := pullImageForTest(testFunc, image); err != nil {
120+
if err := pullImageWithRetry(testFunc, image); err != nil {
118121
return fmt.Errorf("failed to pull %s: %w", image, err)
119122
}
120123
}
@@ -130,6 +133,20 @@ func removeAllImages(testFunc string, images []string) {
130133
}
131134
}
132135

136+
func pullImageWithRetry(testFunc string, image string) error {
137+
var err error
138+
for i := 0; i < maxPullRetries; i++ {
139+
err = pullImageForTest(testFunc, image)
140+
if err == nil {
141+
return nil
142+
}
143+
144+
fmt.Printf("Attempt %d/%d failed to pull %s: %v. Retrying in %v...\n", i+1, maxPullRetries, image, err, pullRetryDelay)
145+
time.Sleep(pullRetryDelay)
146+
}
147+
return fmt.Errorf("failed to pull %s after %d attempts: %w", image, maxPullRetries, err)
148+
}
149+
133150
func pullImageForTest(testFunc string, image string) error {
134151
switch testFunc {
135152
case testCrictl:

0 commit comments

Comments
 (0)