From 22bcaf0a1df66b1f6f6ee49e76e8509808302650 Mon Sep 17 00:00:00 2001 From: Alex Plischke Date: Wed, 17 May 2023 08:53:39 -0700 Subject: [PATCH] Optimize Project ID retrieval for APIT (#774) * Don't retrieve project IDs multiple times * Remove redundant log fields --- go.mod | 2 +- internal/apitest/config.go | 10 ++++++-- internal/apitest/runner.go | 42 ++++++++++++++++++--------------- internal/apitest/runner_test.go | 4 ++++ 4 files changed, 36 insertions(+), 22 deletions(-) diff --git a/go.mod b/go.mod index 190055c01..cce64180f 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,7 @@ require ( github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/go-git/gcfg v1.5.0 // indirect github.com/go-git/go-billy/v5 v5.0.0 // indirect - github.com/google/go-cmp v0.5.9 // indirect + github.com/google/go-cmp v0.5.9 github.com/gorilla/websocket v1.4.2 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/hcl v1.0.0 // indirect diff --git a/internal/apitest/config.go b/internal/apitest/config.go index 330a72282..8fab52d4a 100644 --- a/internal/apitest/config.go +++ b/internal/apitest/config.go @@ -39,9 +39,15 @@ type Suite struct { TestMatch []string `yaml:"testMatch,omitempty"` Env map[string]string `yaml:"env,omitempty"` - // HookID is a technical ID unique to a project that's required by the APIs that execute API tests. - // The HookID is retrieved dynamically before calling those endpoints. + // HookID is a technical ID unique to a project that's required by the APIs + // that execute API tests. The HookID is retrieved dynamically based on + // ProjectName before calling those endpoints. HookID string `yaml:"-"` + + // ProjectID is a technical ID unique to a project that's required by the + // APIs that execute API tests. The ProjectID is retrieved dynamically based + // on ProjectName before calling those endpoints. + ProjectID string `yaml:"-"` } // FromFile creates a new apitest Project based on the filepath cfgPath. diff --git a/internal/apitest/runner.go b/internal/apitest/runner.go index 592a1ef47..36fba9bb7 100644 --- a/internal/apitest/runner.go +++ b/internal/apitest/runner.go @@ -290,10 +290,15 @@ func (r *Runner) runLocalTests(s Suite, results chan []TestResult) int { expected++ } + projectMeta := ProjectMeta{ + ID: s.ProjectID, + Name: s.ProjectName, + } + if r.Async { - r.buildLocalTestDetails(s.HookID, eventIDs, testNames, results) + r.buildLocalTestDetails(projectMeta, eventIDs, testNames, results) } else { - r.startPollingAsyncResponse(s.HookID, eventIDs, results, maximumWaitTime) + r.startPollingAsyncResponse(projectMeta, s.HookID, eventIDs, results, maximumWaitTime) } return expected } @@ -305,6 +310,11 @@ func (r *Runner) runRemoteTests(s Suite, results chan []TestResult) int { pollWaitTime = s.Timeout } + projectMeta := ProjectMeta{ + ID: s.ProjectID, + Name: s.ProjectName, + } + if len(s.Tags) == 0 && len(s.Tests) == 0 { log.Info().Str("projectName", s.ProjectName).Msg("Running project.") @@ -314,9 +324,9 @@ func (r *Runner) runRemoteTests(s Suite, results chan []TestResult) int { } if r.Async { - r.fetchTestDetails(s.HookID, resp.EventIDs, resp.TestIDs, results) + r.fetchTestDetails(projectMeta, s.HookID, resp.EventIDs, resp.TestIDs, results) } else { - r.startPollingAsyncResponse(s.HookID, resp.EventIDs, results, maximumWaitTime) + r.startPollingAsyncResponse(projectMeta, s.HookID, resp.EventIDs, results, maximumWaitTime) } return len(resp.EventIDs) } @@ -330,10 +340,11 @@ func (r *Runner) runRemoteTests(s Suite, results chan []TestResult) int { if err != nil { log.Error().Err(err).Msg("Failed to run test.") } + if r.Async { - r.fetchTestDetails(s.HookID, resp.EventIDs, resp.TestIDs, results) + r.fetchTestDetails(projectMeta, s.HookID, resp.EventIDs, resp.TestIDs, results) } else { - r.startPollingAsyncResponse(s.HookID, resp.EventIDs, results, maximumWaitTime) + r.startPollingAsyncResponse(projectMeta, s.HookID, resp.EventIDs, results, maximumWaitTime) } expected += len(resp.EventIDs) } @@ -347,9 +358,9 @@ func (r *Runner) runRemoteTests(s Suite, results chan []TestResult) int { log.Error().Err(err).Msg("Failed to run tag.") } if r.Async { - r.fetchTestDetails(s.HookID, resp.EventIDs, resp.TestIDs, results) + r.fetchTestDetails(projectMeta, s.HookID, resp.EventIDs, resp.TestIDs, results) } else { - r.startPollingAsyncResponse(s.HookID, resp.EventIDs, results, maximumWaitTime) + r.startPollingAsyncResponse(projectMeta, s.HookID, resp.EventIDs, results, maximumWaitTime) } expected += len(resp.EventIDs) } @@ -378,14 +389,11 @@ func (r *Runner) runSuites() bool { return r.collectResults(expected, results) } -func (r *Runner) buildLocalTestDetails(hookID string, eventIDs []string, testNames []string, results chan []TestResult) { - project, _ := r.Client.GetProject(context.Background(), hookID) +func (r *Runner) buildLocalTestDetails(project ProjectMeta, eventIDs []string, testNames []string, results chan []TestResult) { for _, eventID := range eventIDs { - reportURL := fmt.Sprintf("%s/api-testing/project/%s/event/%s", r.Region.AppBaseURL(), project.ID, eventID) log.Info(). Str("project", project.Name). Str("report", fmt.Sprintf("%s/api-testing/project/%s/event/%s", r.Region.AppBaseURL(), project.ID, eventID)). - Str("report", reportURL). Msg("Async test started.") } @@ -400,14 +408,11 @@ func (r *Runner) buildLocalTestDetails(hookID string, eventIDs []string, testNam } } -func (r *Runner) fetchTestDetails(hookID string, eventIDs []string, testIDs []string, results chan []TestResult) { - project, _ := r.Client.GetProject(context.Background(), hookID) +func (r *Runner) fetchTestDetails(project ProjectMeta, hookID string, eventIDs []string, testIDs []string, results chan []TestResult) { for _, eventID := range eventIDs { - reportURL := fmt.Sprintf("%s/api-testing/project/%s/event/%s", r.Region.AppBaseURL(), project.ID, eventID) log.Info(). Str("project", project.Name). Str("report", fmt.Sprintf("%s/api-testing/project/%s/event/%s", r.Region.AppBaseURL(), project.ID, eventID)). - Str("report", reportURL). Msg("Async test started.") } @@ -423,9 +428,7 @@ func (r *Runner) fetchTestDetails(hookID string, eventIDs []string, testIDs []st } } -func (r *Runner) startPollingAsyncResponse(hookID string, eventIDs []string, results chan []TestResult, pollMaximumWait time.Duration) { - project, _ := r.Client.GetProject(context.Background(), hookID) - +func (r *Runner) startPollingAsyncResponse(project ProjectMeta, hookID string, eventIDs []string, results chan []TestResult, pollMaximumWait time.Duration) { for _, eventID := range eventIDs { go func(lEventID string) { timeout := (time.Now()).Add(pollMaximumWait) @@ -590,6 +593,7 @@ func (r *Runner) ResolveHookIDs() error { } r.Project.Suites[idx].HookID = hook.Identifier + r.Project.Suites[idx].ProjectID = project.ID } if hasErrors { diff --git a/internal/apitest/runner_test.go b/internal/apitest/runner_test.go index 36516fa1e..d53c96987 100644 --- a/internal/apitest/runner_test.go +++ b/internal/apitest/runner_test.go @@ -540,6 +540,7 @@ func TestRunner_ResolveHookIDs(t *testing.T) { { Name: "Suite #1", ProjectName: "Project SingleHook", + ProjectID: "single", }, }, }, @@ -549,6 +550,7 @@ func TestRunner_ResolveHookIDs(t *testing.T) { { Name: "Suite #1", ProjectName: "Project SingleHook", + ProjectID: "single", HookID: "uuid1", }, }, @@ -563,6 +565,7 @@ func TestRunner_ResolveHookIDs(t *testing.T) { { Name: "Suite #1", ProjectName: "Project MultipleHooks", + ProjectID: "multiple", }, }, }, @@ -572,6 +575,7 @@ func TestRunner_ResolveHookIDs(t *testing.T) { { Name: "Suite #1", ProjectName: "Project MultipleHooks", + ProjectID: "multiple", HookID: "uuid1", }, },