github.com/grafana/pyroscope@v1.18.0/pkg/tenant/allowed_tenants_test.go (about)

     1  // SPDX-License-Identifier: AGPL-3.0-only
     2  // Provenance-includes-location: https://github.com/cortexproject/cortex/blob/master/pkg/util/allowed_tenants_test.go
     3  // Provenance-includes-license: Apache-2.0
     4  // Provenance-includes-copyright: The Cortex Authors.
     5  
     6  package tenant
     7  
     8  import (
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestAllowedTenants_NoConfig(t *testing.T) {
    15  	a := NewAllowedTenants(nil, nil)
    16  	require.True(t, a.IsAllowed("all"))
    17  	require.True(t, a.IsAllowed("tenants"))
    18  	require.True(t, a.IsAllowed("allowed"))
    19  }
    20  
    21  func TestAllowedTenants_Enabled(t *testing.T) {
    22  	a := NewAllowedTenants([]string{"A", "B"}, nil)
    23  	require.True(t, a.IsAllowed("A"))
    24  	require.True(t, a.IsAllowed("B"))
    25  	require.False(t, a.IsAllowed("C"))
    26  	require.False(t, a.IsAllowed("D"))
    27  }
    28  
    29  func TestAllowedTenants_Disabled(t *testing.T) {
    30  	a := NewAllowedTenants(nil, []string{"A", "B"})
    31  	require.False(t, a.IsAllowed("A"))
    32  	require.False(t, a.IsAllowed("B"))
    33  	require.True(t, a.IsAllowed("C"))
    34  	require.True(t, a.IsAllowed("D"))
    35  }
    36  
    37  func TestAllowedTenants_Combination(t *testing.T) {
    38  	a := NewAllowedTenants([]string{"A", "B"}, []string{"B", "C"})
    39  	require.True(t, a.IsAllowed("A"))  // enabled, and not disabled
    40  	require.False(t, a.IsAllowed("B")) // enabled, but also disabled
    41  	require.False(t, a.IsAllowed("C")) // disabled
    42  	require.False(t, a.IsAllowed("D")) // not enabled
    43  }
    44  
    45  func TestAllowedTenants_Nil(t *testing.T) {
    46  	var a *AllowedTenants
    47  
    48  	// All tenants are allowed when using nil as allowed tenants.
    49  	require.True(t, a.IsAllowed("A"))
    50  	require.True(t, a.IsAllowed("B"))
    51  	require.True(t, a.IsAllowed("C"))
    52  }