github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/agent/structs/acl_cache_test.go (about)

     1  package structs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/consul/acl"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestStructs_ACLCaches(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	t.Run("New", func(t *testing.T) {
    14  		t.Parallel()
    15  
    16  		t.Run("Valid Sizes", func(t *testing.T) {
    17  			t.Parallel()
    18  			// 1 isn't valid due to a bug in golang-lru library
    19  			config := ACLCachesConfig{2, 2, 2, 2}
    20  
    21  			cache, err := NewACLCaches(&config)
    22  			require.NoError(t, err)
    23  			require.NotNil(t, cache)
    24  			require.NotNil(t, cache.identities)
    25  			require.NotNil(t, cache.policies)
    26  			require.NotNil(t, cache.parsedPolicies)
    27  			require.NotNil(t, cache.authorizers)
    28  		})
    29  
    30  		t.Run("Zero Sizes", func(t *testing.T) {
    31  			t.Parallel()
    32  			// 1 isn't valid due to a bug in golang-lru library
    33  			config := ACLCachesConfig{0, 0, 0, 0}
    34  
    35  			cache, err := NewACLCaches(&config)
    36  			require.NoError(t, err)
    37  			require.NotNil(t, cache)
    38  			require.Nil(t, cache.identities)
    39  			require.Nil(t, cache.policies)
    40  			require.Nil(t, cache.parsedPolicies)
    41  			require.Nil(t, cache.authorizers)
    42  		})
    43  	})
    44  
    45  	t.Run("Identities", func(t *testing.T) {
    46  		t.Parallel()
    47  		// 1 isn't valid due to a bug in golang-lru library
    48  		config := ACLCachesConfig{Identities: 4}
    49  
    50  		cache, err := NewACLCaches(&config)
    51  		require.NoError(t, err)
    52  		require.NotNil(t, cache)
    53  
    54  		cache.PutIdentity("foo", &ACLToken{})
    55  		entry := cache.GetIdentity("foo")
    56  		require.NotNil(t, entry)
    57  		require.NotNil(t, entry.Identity)
    58  	})
    59  
    60  	t.Run("Policies", func(t *testing.T) {
    61  		t.Parallel()
    62  		// 1 isn't valid due to a bug in golang-lru library
    63  		config := ACLCachesConfig{Policies: 4}
    64  
    65  		cache, err := NewACLCaches(&config)
    66  		require.NoError(t, err)
    67  		require.NotNil(t, cache)
    68  
    69  		cache.PutPolicy("foo", &ACLPolicy{})
    70  		entry := cache.GetPolicy("foo")
    71  		require.NotNil(t, entry)
    72  		require.NotNil(t, entry.Policy)
    73  	})
    74  
    75  	t.Run("ParsedPolicies", func(t *testing.T) {
    76  		t.Parallel()
    77  		// 1 isn't valid due to a bug in golang-lru library
    78  		config := ACLCachesConfig{ParsedPolicies: 4}
    79  
    80  		cache, err := NewACLCaches(&config)
    81  		require.NoError(t, err)
    82  		require.NotNil(t, cache)
    83  
    84  		cache.PutParsedPolicy("foo", &acl.Policy{})
    85  		entry := cache.GetParsedPolicy("foo")
    86  		require.NotNil(t, entry)
    87  		require.NotNil(t, entry.Policy)
    88  	})
    89  
    90  	t.Run("Authorizers", func(t *testing.T) {
    91  		t.Parallel()
    92  		// 1 isn't valid due to a bug in golang-lru library
    93  		config := ACLCachesConfig{Authorizers: 4}
    94  
    95  		cache, err := NewACLCaches(&config)
    96  		require.NoError(t, err)
    97  		require.NotNil(t, cache)
    98  
    99  		cache.PutAuthorizer("foo", acl.DenyAll())
   100  		entry := cache.GetAuthorizer("foo")
   101  		require.NotNil(t, entry)
   102  		require.NotNil(t, entry.Authorizer)
   103  		require.True(t, entry.Authorizer == acl.DenyAll())
   104  	})
   105  }