Skip to content

Commit 0bb4286

Browse files
authored
feature(frontend): Adding support for dynamic TracetestAPI instance (#3075)
* feature(frontend): Adding support for dynamic TracetestAPI instance * feature(frontend): Adding support for dynamic TracetestAPI instance * Tenant ID queries fix * Removing test for the moment * Cleanup and test fixing
1 parent ab8f000 commit 0bb4286

File tree

87 files changed

+1060
-1037
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1060
-1037
lines changed

node_modules/.package-lock.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

package-lock.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

server/executor/queue.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/kubeshop/tracetest/server/datastore"
1212
"github.com/kubeshop/tracetest/server/executor/pollingprofile"
13+
"github.com/kubeshop/tracetest/server/http/middleware"
1314
"github.com/kubeshop/tracetest/server/pkg/id"
1415
"github.com/kubeshop/tracetest/server/subscription"
1516
"github.com/kubeshop/tracetest/server/test"
@@ -277,6 +278,10 @@ func (q *Queue) SetDriver(driver QueueDriver) {
277278
driver.SetQueue(q)
278279
}
279280

281+
func propagator() propagation.TextMapPropagator {
282+
return propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, tenantPropagator{})
283+
}
284+
280285
func (q Queue) Enqueue(ctx context.Context, job Job) {
281286
select {
282287
default:
@@ -287,8 +292,7 @@ func (q Queue) Enqueue(ctx context.Context, job Job) {
287292
if job.Headers == nil {
288293
job.Headers = &headers{}
289294
}
290-
propagator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
291-
propagator.Inject(ctx, propagation.MapCarrier(*job.Headers))
295+
propagator().Inject(ctx, propagation.MapCarrier(*job.Headers))
292296

293297
newJob := Job{
294298
Headers: job.Headers,
@@ -308,8 +312,7 @@ func (q Queue) Enqueue(ctx context.Context, job Job) {
308312

309313
func (q Queue) Listen(job Job) {
310314
// this is called when a new job is put in the queue and we need to process it
311-
propagator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})
312-
ctx := propagator.Extract(context.Background(), propagation.MapCarrier(*job.Headers))
315+
ctx := propagator().Extract(context.Background(), propagation.MapCarrier(*job.Headers))
313316

314317
ctx, cancelCtx := context.WithCancel(ctx)
315318
q.listenForStopRequests(context.Background(), cancelCtx, job)
@@ -470,3 +473,29 @@ func (q Queue) resolveDataStore(ctx context.Context, job Job) datastore.DataStor
470473

471474
return ds
472475
}
476+
477+
type tenantPropagator struct{}
478+
479+
var _ propagation.TextMapPropagator = tenantPropagator{}
480+
481+
func (b tenantPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) {
482+
tenantID := middleware.TenantIDFromContext(ctx)
483+
if tenantID != "" {
484+
carrier.Set(string(middleware.TenantIDKey), tenantID)
485+
}
486+
}
487+
488+
// Extract returns a copy of parent with the baggage from the carrier added.
489+
func (b tenantPropagator) Extract(parent context.Context, carrier propagation.TextMapCarrier) context.Context {
490+
tenantID := carrier.Get(string(middleware.TenantIDKey))
491+
if tenantID == "" {
492+
return parent
493+
}
494+
495+
return context.WithValue(parent, middleware.TenantIDKey, tenantID)
496+
}
497+
498+
// Fields returns the keys who's values are set with Inject.
499+
func (b tenantPropagator) Fields() []string {
500+
return []string{string(middleware.TenantIDKey)}
501+
}

server/http/controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ func (c *controller) RunTest(ctx context.Context, testID string, runInfo openapi
261261
requiredGates := c.mappers.In.RequiredGates(runInfo.RequiredGates)
262262

263263
run := c.testRunner.Run(ctx, test, metadata(runInfo.Metadata), environment, requiredGates)
264-
265264
return openapi.Response(200, c.mappers.Out.Run(&run)), nil
266265
}
267266

server/http/middleware/tenant.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ func isValidUUID(value string) bool {
4242
_, err := uuid.Parse(value)
4343
return err == nil
4444
}
45+
46+
func TenantIDFromContext(ctx context.Context) string {
47+
tenantID := ctx.Value(TenantIDKey)
48+
49+
if tenantID == nil {
50+
return ""
51+
}
52+
53+
return tenantID.(string)
54+
}

server/pkg/sqlutil/tenant.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Tenant(ctx context.Context, query string, params ...any) (string, []any) {
1919
paramNumber := len(params) + 1
2020
condition := fmt.Sprintf(" %s tenant_id = $%d", prefix, paramNumber)
2121

22-
return query + condition, append(params, tenantID)
22+
return query + condition, append(params, *tenantID)
2323
}
2424

2525
func TenantWithPrefix(ctx context.Context, query string, prefix string, params ...any) (string, []any) {
@@ -30,9 +30,9 @@ func TenantWithPrefix(ctx context.Context, query string, prefix string, params .
3030

3131
queryPrefix := getQueryPrefix(query)
3232
paramNumber := len(params) + 1
33-
condition := fmt.Sprintf(" %s %stenant_id = $%d)", queryPrefix, prefix, paramNumber)
33+
condition := fmt.Sprintf(" %s %stenant_id = $%d", queryPrefix, prefix, paramNumber)
3434

35-
return query + condition, append(params, tenantID)
35+
return query + condition, append(params, *tenantID)
3636
}
3737

3838
func TenantID(ctx context.Context) *string {

server/test/run_repository.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ FROM
384384
`
385385

386386
func (r *runRepository) GetRun(ctx context.Context, testID id.ID, runID int) (Run, error) {
387-
query, params := sqlutil.Tenant(ctx, selectRunQuery+" WHERE id = $1 AND test_id = $2", runID, testID)
387+
query, params := sqlutil.TenantWithPrefix(ctx, selectRunQuery+" WHERE id = $1 AND test_id = $2", "test_runs.", runID, testID)
388388

389389
run, err := readRunRow(r.db.QueryRowContext(ctx, query, params...))
390390
if err != nil {
@@ -394,7 +394,7 @@ func (r *runRepository) GetRun(ctx context.Context, testID id.ID, runID int) (Ru
394394
}
395395

396396
func (r *runRepository) GetTestRuns(ctx context.Context, test Test, take, skip int32) ([]Run, error) {
397-
query, params := sqlutil.Tenant(ctx, selectRunQuery+" WHERE test_id = $1", test.ID, take, skip)
397+
query, params := sqlutil.TenantWithPrefix(ctx, selectRunQuery+" WHERE test_id = $1", "test_runs.", test.ID, take, skip)
398398
stmt, err := r.db.Prepare(query + " ORDER BY created_at DESC LIMIT $2 OFFSET $3")
399399
if err != nil {
400400
return []Run{}, err
@@ -430,8 +430,8 @@ func (r *runRepository) GetRunByTraceID(ctx context.Context, traceID trace.Trace
430430
}
431431

432432
func (r *runRepository) GetLatestRunByTestVersion(ctx context.Context, testID id.ID, version int) (Run, error) {
433-
query, params := sqlutil.Tenant(ctx, selectRunQuery+" WHERE test_id = $1 AND test_version = $2 ORDER BY created_at DESC LIMIT 1", testID.String(), version)
434-
stmt, err := r.db.Prepare(query)
433+
query, params := sqlutil.TenantWithPrefix(ctx, selectRunQuery+" WHERE test_id = $1 AND test_version = $2", "test_runs.", testID.String(), version)
434+
stmt, err := r.db.Prepare(query + " ORDER BY created_at DESC LIMIT 1")
435435

436436
if err != nil {
437437
return Run{}, err
@@ -601,9 +601,9 @@ func readRunRow(row scanner) (Run, error) {
601601
func (r *runRepository) GetTestSuiteRunSteps(ctx context.Context, id id.ID, runID int) ([]Run, error) {
602602
query := selectRunQuery + `
603603
WHERE test_suite_run_steps.test_suite_run_id = $1 AND test_suite_run_steps.test_suite_run_test_suite_id = $2
604-
ORDER BY test_runs.completed_at ASC
605604
`
606-
query, params := sqlutil.Tenant(ctx, query, strconv.Itoa(runID), id)
605+
query, params := sqlutil.TenantWithPrefix(ctx, query, "test_runs.", strconv.Itoa(runID), id)
606+
query += ` ORDER BY test_runs.completed_at ASC`
607607

608608
stmt, err := r.db.Prepare(query)
609609
if err != nil {

server/test/test_repository.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (r *repository) GetAugmented(ctx context.Context, id id.ID) (Test, error) {
193193
const sortQuery = `ORDER BY t.version DESC LIMIT 1`
194194

195195
func (r *repository) get(ctx context.Context, id id.ID) (Test, error) {
196-
query, params := sqlutil.Tenant(ctx, getTestSQL+" WHERE t.id = $1", id)
196+
query, params := sqlutil.TenantWithPrefix(ctx, getTestSQL+" WHERE t.id = $1", "t.", id)
197197

198198
test, err := r.readRow(ctx, r.db.QueryRowContext(ctx, query+sortQuery, params...))
199199
if err != nil {
@@ -205,8 +205,8 @@ func (r *repository) get(ctx context.Context, id id.ID) (Test, error) {
205205

206206
func (r *repository) GetTestSuiteSteps(ctx context.Context, id id.ID, version int) ([]Test, error) {
207207
sortQuery := `ORDER BY ts.step_number ASC`
208-
query, params := sqlutil.Tenant(ctx, getTestSQL+testMaxVersionQuery+` INNER JOIN test_suite_steps ts ON t.id = ts.test_id
209-
WHERE ts.test_suite_id = $1 AND ts.test_suite_version = $2`, id, version)
208+
query, params := sqlutil.TenantWithPrefix(ctx, getTestSQL+testMaxVersionQuery+` INNER JOIN test_suite_steps ts ON t.id = ts.test_id
209+
WHERE ts.test_suite_id = $1 AND ts.test_suite_version = $2`, "t.", id, version)
210210
stmt, err := r.db.Prepare(query + sortQuery)
211211
if err != nil {
212212
return []Test{}, fmt.Errorf("prepare 2: %w", err)
@@ -566,7 +566,7 @@ func (r *repository) Exists(ctx context.Context, id id.ID) (bool, error) {
566566
}
567567

568568
func (r *repository) GetVersion(ctx context.Context, id id.ID, version int) (Test, error) {
569-
query, params := sqlutil.Tenant(ctx, getTestSQL+" WHERE t.id = $1 AND t.version = $2", id, version)
569+
query, params := sqlutil.TenantWithPrefix(ctx, getTestSQL+" WHERE t.id = $1 AND t.version = $2", "t.", id, version)
570570
stmt, err := r.db.Prepare(query)
571571
if err != nil {
572572
return Test{}, fmt.Errorf("prepare: %w", err)

server/testsuite/testsuite_repository.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func (r *Repository) Get(ctx context.Context, id id.ID) (TestSuite, error) {
272272
}
273273

274274
func (r *Repository) get(ctx context.Context, id id.ID, augmented bool) (TestSuite, error) {
275-
query, params := sqlutil.Tenant(ctx, querySelect()+" WHERE t.id = $1", id)
275+
query, params := sqlutil.TenantWithPrefix(ctx, querySelect()+" WHERE t.id = $1", "t.", id)
276276
stmt, err := r.db.Prepare(query + "ORDER BY t.version DESC LIMIT 1")
277277
if err != nil {
278278
return TestSuite{}, fmt.Errorf("prepare: %w", err)
@@ -288,7 +288,7 @@ func (r *Repository) get(ctx context.Context, id id.ID, augmented bool) (TestSui
288288
}
289289

290290
func (r *Repository) GetVersion(ctx context.Context, id id.ID, version int) (TestSuite, error) {
291-
query, params := sqlutil.Tenant(ctx, querySelect()+" WHERE t.id = $1 AND t.version = $2", id, version)
291+
query, params := sqlutil.TenantWithPrefix(ctx, querySelect()+" WHERE t.id = $1 AND t.version = $2", "t.", id, version)
292292
stmt, err := r.db.Prepare(query)
293293
if err != nil {
294294
return TestSuite{}, fmt.Errorf("prepare 1: %w", err)
@@ -309,13 +309,13 @@ func listQuery(ctx context.Context, baseSQL, query string, params []any) (string
309309

310310
sql := baseSQL + testSuiteMaxVersionQuery
311311
sql, params = sqlutil.Search(sql, condition, query, params)
312-
sql, params = sqlutil.Tenant(ctx, sql, params...)
312+
sql, params = sqlutil.TenantWithPrefix(ctx, sql, "t.", params...)
313313

314314
return sql, params
315315
}
316316

317317
func (r *Repository) GetLatestVersion(ctx context.Context, id id.ID) (TestSuite, error) {
318-
query, params := sqlutil.Tenant(ctx, querySelect()+" WHERE t.id = $1", id)
318+
query, params := sqlutil.TenantWithPrefix(ctx, querySelect()+" WHERE t.id = $1", "t.", id)
319319
stmt, err := r.db.Prepare(query + " ORDER BY t.version DESC LIMIT 1")
320320
if err != nil {
321321
return TestSuite{}, fmt.Errorf("prepare: %w", err)

server/testsuite/testsuite_run_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (td *RunRepository) GetLatestRunByTestSuiteVersion(ctx context.Context, ID
364364
}
365365

366366
func (td *RunRepository) GetTestSuiteRuns(ctx context.Context, ID id.ID, take, skip int32) ([]TestSuiteRun, error) {
367-
sortQuery := "ORDER BY created_at DESC LIMIT $2 OFFSET $3"
367+
sortQuery := " ORDER BY created_at DESC LIMIT $2 OFFSET $3"
368368
query, params := sqlutil.Tenant(ctx, selectTestSuiteRunQuery+" WHERE test_suite_id = $1", ID.String(), take, skip)
369369
stmt, err := td.db.Prepare(query + sortQuery)
370370
if err != nil {

0 commit comments

Comments
 (0)