Skip to content

Commit db9e5a9

Browse files
committed
feat(connection): add GetTestEndpoint method for optimal endpoint selection in tests
1 parent 5198f55 commit db9e5a9

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

core/dbio/api/api.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,49 @@ func (ac *APIConnection) ListEndpoints(patterns ...string) (endpoints Endpoints,
148148
return endpoints, nil
149149
}
150150

151+
// GetTestEndpoint returns the best endpoint for testing connectivity/authentication.
152+
// Ranking: (1) non-incremental with auth, (2) incremental with auth, (3) non-incremental, (4) first endpoint
153+
func (ac *APIConnection) GetTestEndpoint() string {
154+
endpoints, err := ac.ListEndpoints()
155+
if err != nil || len(endpoints) == 0 {
156+
return ""
157+
}
158+
159+
hasSpecAuth := ac.Spec.Authentication.Type() != AuthTypeNone
160+
161+
var nonIncrementalWithAuth, incrementalWithAuth, nonIncremental string
162+
163+
for _, ep := range endpoints {
164+
// Determine if incremental (has sync values or update key)
165+
isIncremental := len(ep.Sync) > 0 || ep.Response.Records.UpdateKey != ""
166+
167+
// Determine if has auth (spec-level or endpoint-level)
168+
hasAuth := hasSpecAuth || ep.Authentication != nil
169+
170+
if !isIncremental && hasAuth && nonIncrementalWithAuth == "" {
171+
nonIncrementalWithAuth = ep.Name
172+
} else if isIncremental && hasAuth && incrementalWithAuth == "" {
173+
incrementalWithAuth = ep.Name
174+
} else if !isIncremental && nonIncremental == "" {
175+
nonIncremental = ep.Name
176+
}
177+
}
178+
179+
// Return by priority
180+
if nonIncrementalWithAuth != "" {
181+
return nonIncrementalWithAuth
182+
}
183+
if incrementalWithAuth != "" {
184+
return incrementalWithAuth
185+
}
186+
if nonIncremental != "" {
187+
return nonIncremental
188+
}
189+
190+
// Fallback: first endpoint (already sorted alphabetically)
191+
return endpoints[0].Name
192+
}
193+
151194
// compile all spec endpoints
152195
func (ac *APIConnection) CompileEndpoints() (compiledEndpoints Endpoints, err error) {
153196
// render dynamic endpoint if needed

core/dbio/connection/connection_discover.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ func (c *Connection) Test() (ok bool, err error) {
9595
g.Debug(env.MagentaString(g.F("testing endpoints with a max requests: %d. Set env var SLING_TEST_ENDPOINT_MAX_REQUESTS to modify.", maxRequests)))
9696
}
9797

98+
// obtain the best endpoint for testing one (for connectivity/authentication)
99+
if cast.ToBool(g.Getenv("SLING_TEST_SINGLE_ENDPOINT")) {
100+
testEndpoints = []string{apiClient.GetTestEndpoint()}
101+
}
102+
98103
for _, endpoint := range endpoints {
99104
// check for match to test (if provided)
100105
allowTest := len(testEndpoints) == 0

0 commit comments

Comments
 (0)