github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/util/allowed_tenants_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  )
     8  
     9  func TestAllowedTenants_NoConfig(t *testing.T) {
    10  	a := NewAllowedTenants(nil, nil)
    11  	require.True(t, a.IsAllowed("all"))
    12  	require.True(t, a.IsAllowed("tenants"))
    13  	require.True(t, a.IsAllowed("allowed"))
    14  }
    15  
    16  func TestAllowedTenants_Enabled(t *testing.T) {
    17  	a := NewAllowedTenants([]string{"A", "B"}, nil)
    18  	require.True(t, a.IsAllowed("A"))
    19  	require.True(t, a.IsAllowed("B"))
    20  	require.False(t, a.IsAllowed("C"))
    21  	require.False(t, a.IsAllowed("D"))
    22  }
    23  
    24  func TestAllowedTenants_Disabled(t *testing.T) {
    25  	a := NewAllowedTenants(nil, []string{"A", "B"})
    26  	require.False(t, a.IsAllowed("A"))
    27  	require.False(t, a.IsAllowed("B"))
    28  	require.True(t, a.IsAllowed("C"))
    29  	require.True(t, a.IsAllowed("D"))
    30  }
    31  
    32  func TestAllowedTenants_Combination(t *testing.T) {
    33  	a := NewAllowedTenants([]string{"A", "B"}, []string{"B", "C"})
    34  	require.True(t, a.IsAllowed("A"))  // enabled, and not disabled
    35  	require.False(t, a.IsAllowed("B")) // enabled, but also disabled
    36  	require.False(t, a.IsAllowed("C")) // disabled
    37  	require.False(t, a.IsAllowed("D")) // not enabled
    38  }
    39  
    40  func TestAllowedTenants_Nil(t *testing.T) {
    41  	var a *AllowedTenants
    42  
    43  	// All tenants are allowed when using nil as allowed tenants.
    44  	require.True(t, a.IsAllowed("A"))
    45  	require.True(t, a.IsAllowed("B"))
    46  	require.True(t, a.IsAllowed("C"))
    47  }