Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for WithTenant() lost orig context #3685

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion storage/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const (

// WithTenant creates a Context with a tenant association
func WithTenant(ctx context.Context, tenant string) context.Context {
return context.WithValue(context.Background(), tenantKey, tenant)
return context.WithValue(ctx, tenantKey, tenant)
}

// GetTenant retrieves a tenant associated with a Context
Expand Down
11 changes: 11 additions & 0 deletions storage/tenant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ import (
"github.com/stretchr/testify/assert"
)

type testContextKey string

func TestContextTenantHandling(t *testing.T) {
ctxWithTenant := WithTenant(context.Background(), "tenant1")
assert.Equal(t, "tenant1", GetTenant(ctxWithTenant))
}

func TestContextPreserved(t *testing.T) {
key := testContextKey("expected-key")
val := "expected-value"
ctxWithValue := context.WithValue(context.Background(), key, val)
ctxWithTenant := WithTenant(ctxWithValue, "tenant1")
assert.Equal(t, "tenant1", GetTenant(ctxWithTenant))
assert.Equal(t, val, ctxWithTenant.Value(key))
}

func TestNoTenant(t *testing.T) {
// If no tenant in context, GetTenant should return the empty string
assert.Equal(t, "", GetTenant(context.Background()))
Expand Down