From be892deab81b91deacc2c2eb0c19ec21929fd248 Mon Sep 17 00:00:00 2001 From: Manabu McCloskey Date: Sat, 2 Nov 2024 11:03:56 -0700 Subject: [PATCH] add retry for getting credentials (#431) Signed-off-by: Manabu McCloskey --- tests/e2e/e2e.go | 60 +++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/tests/e2e/e2e.go b/tests/e2e/e2e.go index 96b99e24..86c355b8 100644 --- a/tests/e2e/e2e.go +++ b/tests/e2e/e2e.go @@ -253,30 +253,46 @@ func TestArgoCDEndpoints(ctx context.Context, t *testing.T, baseUrl string) { } func GetBasicAuth(ctx context.Context, name string) (BasicAuth, error) { + var lastErr error - b, err := RunCommand(ctx, fmt.Sprintf("%s get secrets -o json", IdpbuilderBinaryLocation), 10*time.Second) - if err != nil { - return BasicAuth{}, err - } - out := BasicAuth{} + for attempt := 0; attempt < 5; attempt++ { + select { + case <-ctx.Done(): + return BasicAuth{}, ctx.Err() + default: + b, err := RunCommand(ctx, fmt.Sprintf("%s get secrets -o json", IdpbuilderBinaryLocation), 10*time.Second) + if err != nil { + lastErr = err + time.Sleep(httpRetryDelay) + continue + } - secs := make([]get.TemplateData, 2) - err = json.Unmarshal(b, &secs) - if err != nil { - return BasicAuth{}, err - } + out := BasicAuth{} + secs := make([]get.TemplateData, 2) + if err = json.Unmarshal(b, &secs); err != nil { + lastErr = err + time.Sleep(httpRetryDelay) + continue + } - for i := range secs { - if secs[i].Name == name { - out.Password = secs[i].Data["password"] - out.Username = secs[i].Data["username"] - break + for i := range secs { + if secs[i].Name == name { + out.Password = secs[i].Data["password"] + out.Username = secs[i].Data["username"] + break + } + } + + if out.Password == "" || out.Username == "" { + time.Sleep(httpRetryDelay) + continue + } + + return out, nil } } - if out.Password == "" || out.Username == "" { - return BasicAuth{}, fmt.Errorf("could not find argocd or gitea credentials: %s", b) - } - return out, nil + + return BasicAuth{}, fmt.Errorf("failed after 5 attempts: %w", lastErr) } func GetArgoCDSessionToken(ctx context.Context, endpoint string) (string, error) { @@ -350,11 +366,7 @@ func isArgoAppSyncedAndHealthy(ctx context.Context, kubeClient client.Client, na return false, err } - if app.Status.Health.Status == "Healthy" && app.Status.Sync.Status == "Synced" { - return true, nil - } - - return false, nil + return app.Status.Health.Status == "Healthy" && app.Status.Sync.Status == "Synced", nil } func GetKubeClient() (client.Client, error) {