Skip to content

Commit

Permalink
Fix for WithTenant() lost orig context
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Snible <snible@us.ibm.com>
  • Loading branch information
esnible committed May 16, 2022
1 parent 2fd214e commit c076573
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
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

0 comments on commit c076573

Please sign in to comment.