2020-05-01 22:45:15 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-08-17 21:24:49 +00:00
|
|
|
"net"
|
2020-05-01 22:45:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-08-17 21:24:49 +00:00
|
|
|
"strings"
|
2020-05-01 22:45:15 +00:00
|
|
|
"testing"
|
2020-08-12 16:35:30 +00:00
|
|
|
"time"
|
2020-05-01 22:45:15 +00:00
|
|
|
|
2020-12-21 18:46:41 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-05-01 22:45:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-08-12 16:35:30 +00:00
|
|
|
func TestLoad(t *testing.T) {
|
|
|
|
// Basically just testing that injection of the extra
|
|
|
|
// source works.
|
|
|
|
devMode := true
|
2020-12-21 18:25:32 +00:00
|
|
|
builderOpts := LoadOpts{
|
2020-08-12 16:35:30 +00:00
|
|
|
// putting this in dev mode so that the config validates
|
|
|
|
// without having to specify a data directory
|
|
|
|
DevMode: &devMode,
|
2020-12-21 18:25:32 +00:00
|
|
|
DefaultConfig: FileSource{
|
|
|
|
Name: "test",
|
|
|
|
Format: "hcl",
|
|
|
|
Data: `node_name = "hobbiton"`,
|
|
|
|
},
|
|
|
|
Overrides: []Source{
|
|
|
|
FileSource{
|
|
|
|
Name: "overrides",
|
|
|
|
Format: "json",
|
|
|
|
Data: `{"check_reap_interval": "1ms"}`,
|
|
|
|
},
|
|
|
|
},
|
2020-08-12 16:35:30 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 18:25:32 +00:00
|
|
|
result, err := Load(builderOpts)
|
2020-08-12 16:35:30 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-21 18:25:32 +00:00
|
|
|
require.Empty(t, result.Warnings)
|
|
|
|
cfg := result.RuntimeConfig
|
2020-08-12 16:35:30 +00:00
|
|
|
require.NotNil(t, cfg)
|
|
|
|
require.Equal(t, "hobbiton", cfg.NodeName)
|
|
|
|
require.Equal(t, 1*time.Millisecond, cfg.CheckReapInterval)
|
|
|
|
}
|
|
|
|
|
2020-05-01 22:45:15 +00:00
|
|
|
func TestShouldParseFile(t *testing.T) {
|
|
|
|
var testcases = []struct {
|
|
|
|
filename string
|
|
|
|
configFormat string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{filename: "config.json", expected: true},
|
|
|
|
{filename: "config.hcl", expected: true},
|
|
|
|
{filename: "config", configFormat: "hcl", expected: true},
|
|
|
|
{filename: "config.js", configFormat: "json", expected: true},
|
|
|
|
{filename: "config.yaml", expected: false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
name := fmt.Sprintf("filename=%s, format=%s", tc.filename, tc.configFormat)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require.Equal(t, tc.expected, shouldParseFile(tc.filename, tc.configFormat))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBuilder_PopulatesSourcesFromConfigFiles(t *testing.T) {
|
|
|
|
paths := setupConfigFiles(t)
|
|
|
|
|
2020-12-21 18:55:53 +00:00
|
|
|
b, err := newBuilder(LoadOpts{ConfigFiles: paths})
|
2020-05-01 22:45:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []Source{
|
2020-08-10 16:46:28 +00:00
|
|
|
FileSource{Name: paths[0], Format: "hcl", Data: "content a"},
|
|
|
|
FileSource{Name: paths[1], Format: "json", Data: "content b"},
|
|
|
|
FileSource{Name: filepath.Join(paths[3], "a.hcl"), Format: "hcl", Data: "content a"},
|
|
|
|
FileSource{Name: filepath.Join(paths[3], "b.json"), Format: "json", Data: "content b"},
|
2020-05-01 22:45:15 +00:00
|
|
|
}
|
|
|
|
require.Equal(t, expected, b.Sources)
|
2020-05-02 00:17:27 +00:00
|
|
|
require.Len(t, b.Warnings, 2)
|
2020-05-01 22:45:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewBuilder_PopulatesSourcesFromConfigFiles_WithConfigFormat(t *testing.T) {
|
|
|
|
paths := setupConfigFiles(t)
|
|
|
|
|
2020-12-21 18:55:53 +00:00
|
|
|
b, err := newBuilder(LoadOpts{ConfigFiles: paths, ConfigFormat: "hcl"})
|
2020-05-01 22:45:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []Source{
|
2020-08-10 16:46:28 +00:00
|
|
|
FileSource{Name: paths[0], Format: "hcl", Data: "content a"},
|
|
|
|
FileSource{Name: paths[1], Format: "hcl", Data: "content b"},
|
|
|
|
FileSource{Name: paths[2], Format: "hcl", Data: "content c"},
|
|
|
|
FileSource{Name: filepath.Join(paths[3], "a.hcl"), Format: "hcl", Data: "content a"},
|
|
|
|
FileSource{Name: filepath.Join(paths[3], "b.json"), Format: "hcl", Data: "content b"},
|
|
|
|
FileSource{Name: filepath.Join(paths[3], "c.yaml"), Format: "hcl", Data: "content c"},
|
2020-05-01 22:45:15 +00:00
|
|
|
}
|
|
|
|
require.Equal(t, expected, b.Sources)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this would be much nicer with gotest.tools/fs
|
|
|
|
func setupConfigFiles(t *testing.T) []string {
|
|
|
|
t.Helper()
|
|
|
|
path, err := ioutil.TempDir("", t.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { os.RemoveAll(path) })
|
|
|
|
|
|
|
|
subpath := filepath.Join(path, "sub")
|
|
|
|
err = os.Mkdir(subpath, 0755)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for _, dir := range []string{path, subpath} {
|
|
|
|
err = ioutil.WriteFile(filepath.Join(dir, "a.hcl"), []byte("content a"), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(dir, "b.json"), []byte("content b"), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(dir, "c.yaml"), []byte("content c"), 0644)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
return []string{
|
|
|
|
filepath.Join(path, "a.hcl"),
|
|
|
|
filepath.Join(path, "b.json"),
|
|
|
|
filepath.Join(path, "c.yaml"),
|
|
|
|
subpath,
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 21:24:49 +00:00
|
|
|
|
|
|
|
func TestBuilder_BuildAndValidate_NodeName(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
nodeName string
|
|
|
|
expectedWarn string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(t *testing.T, tc testCase) {
|
2020-12-21 18:55:53 +00:00
|
|
|
b, err := newBuilder(LoadOpts{
|
2020-12-21 18:25:32 +00:00
|
|
|
FlagValues: Config{
|
2020-08-17 21:24:49 +00:00
|
|
|
NodeName: pString(tc.nodeName),
|
|
|
|
DataDir: pString("dir"),
|
|
|
|
},
|
|
|
|
})
|
2020-12-21 22:51:44 +00:00
|
|
|
patchLoadOptsShims(&b.opts)
|
2020-08-17 21:24:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = b.BuildAndValidate()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, b.Warnings, 1)
|
|
|
|
require.Contains(t, b.Warnings[0], tc.expectedWarn)
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCases = []testCase{
|
|
|
|
{
|
|
|
|
name: "invalid character - unicode",
|
|
|
|
nodeName: "🐼",
|
|
|
|
expectedWarn: `Node name "🐼" will not be discoverable via DNS due to invalid characters`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid character - slash",
|
|
|
|
nodeName: "thing/other/ok",
|
|
|
|
expectedWarn: `Node name "thing/other/ok" will not be discoverable via DNS due to invalid characters`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "too long",
|
|
|
|
nodeName: strings.Repeat("a", 66),
|
|
|
|
expectedWarn: "due to it being too long.",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
fn(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 22:51:44 +00:00
|
|
|
func patchLoadOptsShims(opts *LoadOpts) {
|
|
|
|
if opts.hostname == nil {
|
|
|
|
opts.hostname = func() (string, error) {
|
|
|
|
return "thehostname", nil
|
|
|
|
}
|
2020-08-17 21:24:49 +00:00
|
|
|
}
|
2020-12-21 22:51:44 +00:00
|
|
|
if opts.getPrivateIPv4 == nil {
|
|
|
|
opts.getPrivateIPv4 = func() ([]*net.IPAddr, error) {
|
|
|
|
return []*net.IPAddr{ipAddr("10.0.0.1")}, nil
|
|
|
|
}
|
2020-08-17 21:24:49 +00:00
|
|
|
}
|
2020-12-21 22:51:44 +00:00
|
|
|
if opts.getPublicIPv6 == nil {
|
|
|
|
opts.getPublicIPv6 = func() ([]*net.IPAddr, error) {
|
|
|
|
return []*net.IPAddr{ipAddr("dead:beef::1")}, nil
|
|
|
|
}
|
2020-08-17 21:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-21 18:46:41 +00:00
|
|
|
|
|
|
|
func TestLoad_HTTPMaxConnsPerClientExceedsRLimit(t *testing.T) {
|
|
|
|
hcl := `
|
|
|
|
limits{
|
|
|
|
# We put a very high value to be sure to fail
|
|
|
|
# This value is more than max on Windows as well
|
|
|
|
http_max_conns_per_client = 16777217
|
|
|
|
}`
|
|
|
|
|
|
|
|
opts := LoadOpts{
|
|
|
|
DefaultConfig: FileSource{
|
|
|
|
Name: "test",
|
|
|
|
Format: "hcl",
|
|
|
|
Data: `
|
|
|
|
ae_interval = "1m"
|
|
|
|
data_dir="/tmp/00000000001979"
|
|
|
|
bind_addr = "127.0.0.1"
|
|
|
|
advertise_addr = "127.0.0.1"
|
|
|
|
datacenter = "dc1"
|
|
|
|
bootstrap = true
|
|
|
|
server = true
|
|
|
|
node_id = "00000000001979"
|
|
|
|
node_name = "Node-00000000001979"
|
|
|
|
`,
|
|
|
|
},
|
|
|
|
HCL: []string{hcl},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := Load(opts)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "but limits.http_max_conns_per_client: 16777217 needs at least 16777237")
|
|
|
|
}
|