Skip to content

Commit

Permalink
Optimize Project ID retrieval for APIT (#774)
Browse files Browse the repository at this point in the history
* Don't retrieve project IDs multiple times

* Remove redundant log fields
  • Loading branch information
alexplischke authored May 17, 2023
1 parent c4e6f63 commit 22bcaf0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions internal/apitest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 23 additions & 19 deletions internal/apitest/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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.")

Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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.")
}

Expand All @@ -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.")
}

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions internal/apitest/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ func TestRunner_ResolveHookIDs(t *testing.T) {
{
Name: "Suite #1",
ProjectName: "Project SingleHook",
ProjectID: "single",
},
},
},
Expand All @@ -549,6 +550,7 @@ func TestRunner_ResolveHookIDs(t *testing.T) {
{
Name: "Suite #1",
ProjectName: "Project SingleHook",
ProjectID: "single",
HookID: "uuid1",
},
},
Expand All @@ -563,6 +565,7 @@ func TestRunner_ResolveHookIDs(t *testing.T) {
{
Name: "Suite #1",
ProjectName: "Project MultipleHooks",
ProjectID: "multiple",
},
},
},
Expand All @@ -572,6 +575,7 @@ func TestRunner_ResolveHookIDs(t *testing.T) {
{
Name: "Suite #1",
ProjectName: "Project MultipleHooks",
ProjectID: "multiple",
HookID: "uuid1",
},
},
Expand Down

0 comments on commit 22bcaf0

Please sign in to comment.