diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 55b06e4..4236510 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -94,6 +94,7 @@ func initNomad() { return } createDefaultEnvironment() + waitForDefaultEnvironment() } func createDefaultEnvironment() { @@ -123,6 +124,28 @@ func createDefaultEnvironment() { } } +func waitForDefaultEnvironment() { + path := helpers.BuildURL(api.BasePath, api.RunnersPath) + body := &dto.RunnerRequest{ + ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger, + InactivityTimeout: 1, + } + var code int + const maxRetries = 60 + for count := 0; count < maxRetries && code != http.StatusOK; count++ { + <-time.After(time.Second) + if resp, err := helpers.HTTPPostJSON(path, body); err == nil { + code = resp.StatusCode + log.WithField("count", count).WithField("statusCode", code).Info("Waiting for idle runners") + } else { + log.WithField("count", count).WithError(err).Warn("Waiting for idle runners") + } + } + if code != http.StatusOK { + log.Fatal("Failed to provide a runner") + } +} + func deleteE2EEnvironments() { for _, id := range environmentIDs { deleteEnvironment(&testing.T{}, id.ToString()) diff --git a/tests/helpers/test_helpers.go b/tests/helpers/test_helpers.go index 32dfb01..17c4349 100644 --- a/tests/helpers/test_helpers.go +++ b/tests/helpers/test_helpers.go @@ -180,6 +180,26 @@ func HTTPPutJSON(url string, body interface{}) (response *http.Response, err err return HTTPPut(url, reader) } +func HTTPPostJSON(url string, body interface{}) (response *http.Response, err error) { + requestByteString, err := json.Marshal(body) + if err != nil { + return nil, fmt.Errorf("cannot marshal passed http post body: %w", err) + } + bodyReader := bytes.NewReader(requestByteString) + + //nolint:noctx // we don't need a http.NewRequestWithContext in our tests + req, err := httpRequest(http.MethodPost, url, bodyReader) + if err != nil { + return nil, err + } + client := &http.Client{} + resp, err := client.Do(req) + if err != nil { + return nil, fmt.Errorf("error executing request: %w", err) + } + return resp, nil +} + const templateJobPriority = 100 func CreateTemplateJob() (base, job *nomadApi.Job) {