github.com/hernad/nomad@v1.6.112/command/agent/consul/namespaces_client_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package consul 5 6 import ( 7 "fmt" 8 "testing" 9 "time" 10 11 "github.com/hernad/nomad/ci" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestNamespacesClient_List(t *testing.T) { 16 ci.Parallel(t) 17 18 t.Run("oss", func(t *testing.T) { 19 c := NewNamespacesClient(NewMockNamespaces(nil), NewMockAgent(Features{ 20 Enterprise: false, 21 Namespaces: false, 22 })) 23 list, err := c.List() 24 require.NoError(t, err) 25 require.Equal(t, []string{"default"}, list) // todo(shoenig): change in followup PR 26 }) 27 28 t.Run("ent without namespaces", func(t *testing.T) { 29 c := NewNamespacesClient(NewMockNamespaces(nil), NewMockAgent(Features{ 30 Enterprise: true, 31 Namespaces: false, 32 })) 33 list, err := c.List() 34 require.NoError(t, err) 35 require.Equal(t, []string{"default"}, list) // todo(shoenig): change in followup PR 36 }) 37 38 t.Run("ent with namespaces", func(t *testing.T) { 39 c := NewNamespacesClient(NewMockNamespaces([]string{"banana", "apple", "cherry"}), NewMockAgent(Features{ 40 Enterprise: true, 41 Namespaces: true, 42 })) 43 list, err := c.List() 44 require.NoError(t, err) 45 46 // remember default always exists... if enterprise and namespaces are enabled 47 require.Equal(t, []string{"apple", "banana", "cherry", "default"}, list) 48 }) 49 } 50 51 func TestNewNamespacesClient_stale(t *testing.T) { 52 ci.Parallel(t) 53 54 t.Run("ok", func(t *testing.T) { 55 now := time.Now() 56 updated := now.Add(-59 * time.Second) 57 result := stale(updated, now) 58 require.False(t, result) 59 }) 60 61 t.Run("stale", func(t *testing.T) { 62 now := time.Now() 63 updated := now.Add(-61 * time.Second) 64 result := stale(updated, now) 65 require.True(t, result) 66 }) 67 } 68 69 func TestNewNamespacesClient_allowable(t *testing.T) { 70 ci.Parallel(t) 71 72 try := func(ent, feature, enabled, exp bool, updated, now time.Time) { 73 expired := now.After(updated.Add(namespaceEnabledCacheTTL)) 74 name := fmt.Sprintf("ent:%t_feature:%t_enabled:%t_exp:%t_expired:%t", ent, feature, enabled, exp, expired) 75 t.Run(name, func(t *testing.T) { 76 c := NewNamespacesClient(NewMockNamespaces([]string{"a", "b"}), NewMockAgent(Features{ 77 Enterprise: ent, 78 Namespaces: feature, 79 })) 80 81 // put the client into the state we want 82 c.enabled = enabled 83 c.updated = updated 84 85 result := c.allowable(now) 86 require.Equal(t, exp, result) 87 require.Equal(t, exp, c.enabled) // cached value should match result 88 }) 89 } 90 91 previous := time.Now() 92 over := previous.Add(namespaceEnabledCacheTTL + 1) 93 under := previous.Add(namespaceEnabledCacheTTL - 1) 94 95 // oss, no refresh, no state change 96 try(false, false, false, false, previous, under) 97 98 // oss, refresh, no state change 99 try(false, false, false, false, previous, over) 100 101 // ent->oss, refresh, state change 102 try(false, false, true, false, previous, over) 103 104 // ent, disabled, no refresh, no state change 105 try(true, false, false, false, previous, under) 106 107 // ent, disabled, refresh, no state change 108 try(true, false, false, false, previous, over) 109 110 // ent, enabled, no refresh, no state change 111 try(true, true, true, true, previous, under) 112 113 // ent, enabled, refresh, no state change 114 try(true, true, true, true, previous, over) 115 116 // ent, disabled, refresh, state change (i.e. new license with namespaces) 117 try(true, true, false, true, previous, over) // ??? 118 119 // ent, disabled, refresh, no state change yet (i.e. new license with namespaces, still cached without) 120 try(true, true, false, false, previous, under) 121 }