open-nomad/command/agent/consul/namespaces_client_test.go
Seth Hoenig 519429a2de consul: probe consul namespace feature before using namespace api
This PR changes Nomad's wrapper around the Consul NamespaceAPI so that
it will detect if the Consul Namespaces feature is enabled before making
a request to the Namespaces API. Namespaces are not enabled in Consul OSS,
and require a suitable license to be used with Consul ENT.

Previously Nomad would check for a 404 status code when makeing a request
to the Namespaces API to "detect" if Consul OSS was being used. This does
not work for Consul ENT with Namespaces disabled, which returns a 500.

Now we avoid requesting the namespace API altogether if Consul is detected
to be the OSS sku, or if the Namespaces feature is not licensed. Since
Consul can be upgraded from OSS to ENT, or a new license applied, we cache
the value for 1 minute, refreshing on demand if expired.

Fixes https://github.com/hashicorp/nomad-enterprise/issues/575

Note that the ticket originally describes using attributes from https://github.com/hashicorp/nomad/issues/10688.
This turns out not to be possible due to a chicken-egg situation between
bootstrapping the agent and setting up the consul client. Also fun: the
Consul fingerprinter creates its own Consul client, because there is no
[currently] no way to pass the agent's client through the fingerprint factory.
2021-06-07 12:19:25 -05:00

118 lines
3.3 KiB
Go

package consul
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestNamespacesClient_List(t *testing.T) {
t.Parallel()
t.Run("oss", func(t *testing.T) {
c := NewNamespacesClient(NewMockNamespaces(nil), NewMockAgent(Features{
Enterprise: false,
Namespaces: false,
}))
list, err := c.List()
require.NoError(t, err)
require.Equal(t, []string{"default"}, list) // todo(shoenig): change in followup PR
})
t.Run("ent without namespaces", func(t *testing.T) {
c := NewNamespacesClient(NewMockNamespaces(nil), NewMockAgent(Features{
Enterprise: true,
Namespaces: false,
}))
list, err := c.List()
require.NoError(t, err)
require.Equal(t, []string{"default"}, list) // todo(shoenig): change in followup PR
})
t.Run("ent with namespaces", func(t *testing.T) {
c := NewNamespacesClient(NewMockNamespaces([]string{"banana", "apple", "cherry"}), NewMockAgent(Features{
Enterprise: true,
Namespaces: true,
}))
list, err := c.List()
require.NoError(t, err)
// remember default always exists... if enterprise and namespaces are enabled
require.Equal(t, []string{"apple", "banana", "cherry", "default"}, list)
})
}
func TestNewNamespacesClient_stale(t *testing.T) {
t.Parallel()
t.Run("ok", func(t *testing.T) {
now := time.Now()
updated := now.Add(-59 * time.Second)
result := stale(updated, now)
require.False(t, result)
})
t.Run("stale", func(t *testing.T) {
now := time.Now()
updated := now.Add(-61 * time.Second)
result := stale(updated, now)
require.True(t, result)
})
}
func TestNewNamespacesClient_allowable(t *testing.T) {
t.Parallel()
try := func(ent, feature, enabled, exp bool, updated, now time.Time) {
expired := now.After(updated.Add(namespaceEnabledCacheTTL))
name := fmt.Sprintf("ent:%t_feature:%t_enabled:%t_exp:%t_expired:%t", ent, feature, enabled, exp, expired)
t.Run(name, func(t *testing.T) {
c := NewNamespacesClient(NewMockNamespaces([]string{"a", "b"}), NewMockAgent(Features{
Enterprise: ent,
Namespaces: feature,
}))
// put the client into the state we want
c.enabled = enabled
c.updated = updated
result := c.allowable(now)
require.Equal(t, exp, result)
require.Equal(t, exp, c.enabled) // cached value should match result
})
}
previous := time.Now()
over := previous.Add(namespaceEnabledCacheTTL + 1)
under := previous.Add(namespaceEnabledCacheTTL - 1)
// oss, no refresh, no state change
try(false, false, false, false, previous, under)
// oss, refresh, no state change
try(false, false, false, false, previous, over)
// ent->oss, refresh, state change
try(false, false, true, false, previous, over)
// ent, disabled, no refresh, no state change
try(true, false, false, false, previous, under)
// ent, disabled, refresh, no state change
try(true, false, false, false, previous, over)
// ent, enabled, no refresh, no state change
try(true, true, true, true, previous, under)
// ent, enabled, refresh, no state change
try(true, true, true, true, previous, over)
// ent, disabled, refresh, state change (i.e. new license with namespaces)
try(true, true, false, true, previous, over) // ???
// ent, disabled, refresh, no state change yet (i.e. new license with namespaces, still cached without)
try(true, true, false, false, previous, under)
}