2020-08-07 22:30:32 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2020-08-07 23:24:51 +00:00
|
|
|
"fmt"
|
2020-08-07 22:30:32 +00:00
|
|
|
"io/ioutil"
|
2020-08-07 23:24:51 +00:00
|
|
|
"os"
|
2020-08-07 22:30:32 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/agent/config"
|
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2020-08-07 22:30:32 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2020-08-07 23:24:51 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-08-07 22:30:32 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2020-08-07 23:24:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-07 22:30:32 +00:00
|
|
|
)
|
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
func TestNewNodeIDFromConfig(t *testing.T) {
|
|
|
|
logger := hclog.New(nil)
|
|
|
|
tmpDir := testutil.TempDir(t, "")
|
|
|
|
t.Cleanup(func() {
|
|
|
|
os.RemoveAll(tmpDir)
|
|
|
|
})
|
|
|
|
cfg := &config.RuntimeConfig{
|
|
|
|
DataDir: tmpDir,
|
|
|
|
}
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
var randomNodeID types.NodeID
|
|
|
|
t.Run("a new ID is generated when none is specified", func(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
randomNodeID, err = newNodeIDFromConfig(cfg, logger)
|
|
|
|
require.NoError(t, err)
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
_, err = uuid.ParseUUID(string(randomNodeID))
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
t.Run("running again should get the NodeID that was persisted to disk", func(t *testing.T) {
|
|
|
|
nodeID, err := newNodeIDFromConfig(cfg, logger)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, nodeID, "")
|
|
|
|
require.Equal(t, nodeID, randomNodeID)
|
|
|
|
})
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
t.Run("invalid NodeID in config", func(t *testing.T) {
|
|
|
|
cfg.NodeID = "nope"
|
|
|
|
_, err := newNodeIDFromConfig(cfg, logger)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "specified NodeID is invalid")
|
|
|
|
})
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
t.Run("valid NodeID in config", func(t *testing.T) {
|
|
|
|
newID, err := uuid.GenerateUUID()
|
|
|
|
require.NoError(t, err)
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
cfg.NodeID = types.NodeID(strings.ToUpper(newID))
|
|
|
|
nodeID, err := newNodeIDFromConfig(cfg, logger)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(nodeID), newID)
|
|
|
|
})
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
t.Run("invalid NodeID in file", func(t *testing.T) {
|
|
|
|
cfg.NodeID = ""
|
|
|
|
filename := filepath.Join(cfg.DataDir, "node-id")
|
|
|
|
err := ioutil.WriteFile(filename, []byte("adf4238a!882b!9ddc!4a9d!5b6758e4159e"), 0600)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = newNodeIDFromConfig(cfg, logger)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), fmt.Sprintf("NodeID in %s is invalid", filename))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("valid NodeID in file", func(t *testing.T) {
|
|
|
|
cfg.NodeID = ""
|
|
|
|
filename := filepath.Join(cfg.DataDir, "node-id")
|
|
|
|
err := ioutil.WriteFile(filename, []byte("ADF4238a-882b-9ddc-4a9d-5b6758e4159e"), 0600)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
nodeID, err := newNodeIDFromConfig(cfg, logger)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(nodeID), "adf4238a-882b-9ddc-4a9d-5b6758e4159e")
|
|
|
|
})
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeNodeID(t *testing.T) {
|
2020-08-07 23:24:51 +00:00
|
|
|
logger := hclog.New(nil)
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
var randomID string
|
|
|
|
t.Run("Random ID when HostNodeID is disabled", func(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
randomID, err = makeNodeID(logger, true)
|
|
|
|
require.NoError(t, err)
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
_, err = uuid.ParseUUID(randomID)
|
|
|
|
require.NoError(t, err)
|
2020-08-07 22:30:32 +00:00
|
|
|
|
2020-08-07 23:24:51 +00:00
|
|
|
another, err := makeNodeID(logger, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, randomID, another)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("host-based ID when HostNodeID is enabled", func(t *testing.T) {
|
|
|
|
id, err := makeNodeID(logger, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqual(t, randomID, id)
|
|
|
|
|
|
|
|
another, err := makeNodeID(logger, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, id, another)
|
|
|
|
})
|
2020-08-07 22:30:32 +00:00
|
|
|
}
|