2015-09-22 21:04:40 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2021-10-13 22:50:46 +00:00
|
|
|
"math"
|
2019-01-08 15:07:36 +00:00
|
|
|
"path/filepath"
|
2015-09-22 21:04:40 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2015-09-22 21:04:40 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2021-10-13 22:50:46 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-05 14:52:07 +00:00
|
|
|
|
2022-01-12 20:04:31 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2022-05-03 22:38:32 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs/config"
|
2020-10-05 14:52:07 +00:00
|
|
|
"github.com/hashicorp/nomad/version"
|
2015-09-22 21:04:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCommand_Implements(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-09-22 21:04:40 +00:00
|
|
|
var _ cli.Command = &Command{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommand_Args(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2022-05-12 15:42:40 +00:00
|
|
|
tmpDir := t.TempDir()
|
2015-09-22 21:04:40 +00:00
|
|
|
|
|
|
|
type tcase struct {
|
|
|
|
args []string
|
|
|
|
errOut string
|
|
|
|
}
|
|
|
|
tcases := []tcase{
|
|
|
|
{
|
|
|
|
[]string{},
|
2015-10-01 23:46:42 +00:00
|
|
|
"Must specify either server, client or dev mode for the agent.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-client", "-data-dir=" + tmpDir, "-bootstrap-expect=1"},
|
2015-09-22 21:04:40 +00:00
|
|
|
"Bootstrap requires server mode to be enabled",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-data-dir=" + tmpDir, "-server", "-bootstrap-expect=1"},
|
|
|
|
"WARNING: Bootstrap mode enabled!",
|
|
|
|
},
|
2022-06-06 13:22:59 +00:00
|
|
|
{
|
|
|
|
[]string{"-data-dir=" + tmpDir, "-server", "-bootstrap-expect=2"},
|
|
|
|
"Number of bootstrap servers should ideally be set to an odd number",
|
|
|
|
},
|
2015-10-01 23:46:42 +00:00
|
|
|
{
|
|
|
|
[]string{"-server"},
|
2021-10-28 13:05:56 +00:00
|
|
|
"Must specify \"data_dir\" config option or \"data-dir\" CLI flag",
|
2015-10-01 23:46:42 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-client", "-alloc-dir="},
|
2018-08-30 20:43:09 +00:00
|
|
|
"Must specify the state, alloc dir, and plugin dir if data-dir is omitted.",
|
2015-10-01 23:46:42 +00:00
|
|
|
},
|
2019-01-07 16:42:44 +00:00
|
|
|
{
|
|
|
|
[]string{"-client", "-data-dir=" + tmpDir, "-meta=invalid..key=inaccessible-value"},
|
|
|
|
"Invalid Client.Meta key: invalid..key",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-client", "-data-dir=" + tmpDir, "-meta=.invalid=inaccessible-value"},
|
|
|
|
"Invalid Client.Meta key: .invalid",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[]string{"-client", "-data-dir=" + tmpDir, "-meta=invalid.=inaccessible-value"},
|
|
|
|
"Invalid Client.Meta key: invalid.",
|
|
|
|
},
|
2015-09-22 21:04:40 +00:00
|
|
|
}
|
|
|
|
for _, tc := range tcases {
|
2018-03-11 17:58:48 +00:00
|
|
|
// Make a new command. We preemptively close the shutdownCh
|
2015-09-22 21:04:40 +00:00
|
|
|
// so that the command exits immediately instead of blocking.
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2015-09-22 21:04:40 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
close(shutdownCh)
|
|
|
|
cmd := &Command{
|
2017-08-16 23:14:59 +00:00
|
|
|
Version: version.GetVersion(),
|
2015-09-22 21:04:40 +00:00
|
|
|
Ui: ui,
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
}
|
|
|
|
|
2016-11-09 00:22:04 +00:00
|
|
|
// To prevent test failures on hosts whose hostname resolves to
|
|
|
|
// a loopback address, we must append a bind address
|
2016-11-09 19:37:41 +00:00
|
|
|
tc.args = append(tc.args, "-bind=169.254.0.1")
|
2015-09-22 21:04:40 +00:00
|
|
|
if code := cmd.Run(tc.args); code != 1 {
|
|
|
|
t.Fatalf("args: %v\nexit: %d\n", tc.args, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
if expect := tc.errOut; expect != "" {
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(out, expect) {
|
|
|
|
t.Fatalf("expect to find %q\n\n%s", expect, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 15:07:36 +00:00
|
|
|
|
|
|
|
func TestCommand_MetaConfigValidation(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tmpDir := t.TempDir()
|
2019-01-08 15:07:36 +00:00
|
|
|
|
|
|
|
tcases := []string{
|
|
|
|
"foo..invalid",
|
|
|
|
".invalid",
|
|
|
|
"invalid.",
|
|
|
|
}
|
|
|
|
for _, tc := range tcases {
|
|
|
|
configFile := filepath.Join(tmpDir, "conf1.hcl")
|
2022-05-12 15:42:40 +00:00
|
|
|
err := ioutil.WriteFile(configFile, []byte(`client{
|
2019-01-08 15:07:36 +00:00
|
|
|
enabled = true
|
|
|
|
meta = {
|
|
|
|
"valid" = "yes"
|
|
|
|
"`+tc+`" = "kaboom!"
|
|
|
|
"nested.var" = "is nested"
|
|
|
|
"deeply.nested.var" = "is deeply nested"
|
|
|
|
}
|
|
|
|
}`), 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new command. We preemptively close the shutdownCh
|
|
|
|
// so that the command exits immediately instead of blocking.
|
2020-10-05 14:07:41 +00:00
|
|
|
ui := cli.NewMockUi()
|
2019-01-08 15:07:36 +00:00
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
close(shutdownCh)
|
|
|
|
cmd := &Command{
|
|
|
|
Version: version.GetVersion(),
|
|
|
|
Ui: ui,
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent test failures on hosts whose hostname resolves to
|
|
|
|
// a loopback address, we must append a bind address
|
|
|
|
args := []string{"-client", "-data-dir=" + tmpDir, "-config=" + configFile, "-bind=169.254.0.1"}
|
|
|
|
if code := cmd.Run(args); code != 1 {
|
|
|
|
t.Fatalf("args: %v\nexit: %d\n", args, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
expect := "Invalid Client.Meta key: " + tc
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
if !strings.Contains(out, expect) {
|
|
|
|
t.Fatalf("expect to find %q\n\n%s", expect, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 14:52:07 +00:00
|
|
|
|
|
|
|
func TestCommand_NullCharInDatacenter(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tmpDir := t.TempDir()
|
2020-10-05 14:52:07 +00:00
|
|
|
|
|
|
|
tcases := []string{
|
|
|
|
"char-\\000-in-the-middle",
|
|
|
|
"ends-with-\\000",
|
|
|
|
"\\000-at-the-beginning",
|
|
|
|
}
|
|
|
|
for _, tc := range tcases {
|
|
|
|
configFile := filepath.Join(tmpDir, "conf1.hcl")
|
2022-05-12 15:42:40 +00:00
|
|
|
err := ioutil.WriteFile(configFile, []byte(`
|
2020-10-05 14:52:07 +00:00
|
|
|
datacenter = "`+tc+`"
|
|
|
|
client{
|
|
|
|
enabled = true
|
|
|
|
}`), 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new command. We preemptively close the shutdownCh
|
|
|
|
// so that the command exits immediately instead of blocking.
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
close(shutdownCh)
|
|
|
|
cmd := &Command{
|
|
|
|
Version: version.GetVersion(),
|
|
|
|
Ui: ui,
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent test failures on hosts whose hostname resolves to
|
|
|
|
// a loopback address, we must append a bind address
|
|
|
|
args := []string{"-client", "-data-dir=" + tmpDir, "-config=" + configFile, "-bind=169.254.0.1"}
|
|
|
|
if code := cmd.Run(args); code != 1 {
|
|
|
|
t.Fatalf("args: %v\nexit: %d\n", args, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
exp := "Datacenter contains invalid characters"
|
|
|
|
if !strings.Contains(out, exp) {
|
|
|
|
t.Fatalf("expect to find %q\n\n%s", exp, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCommand_NullCharInRegion(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2022-05-12 15:42:40 +00:00
|
|
|
tmpDir := t.TempDir()
|
2020-10-05 14:52:07 +00:00
|
|
|
|
|
|
|
tcases := []string{
|
|
|
|
"char-\\000-in-the-middle",
|
|
|
|
"ends-with-\\000",
|
|
|
|
"\\000-at-the-beginning",
|
|
|
|
}
|
|
|
|
for _, tc := range tcases {
|
|
|
|
configFile := filepath.Join(tmpDir, "conf1.hcl")
|
2022-05-12 15:42:40 +00:00
|
|
|
err := ioutil.WriteFile(configFile, []byte(`
|
2020-10-05 14:52:07 +00:00
|
|
|
region = "`+tc+`"
|
|
|
|
client{
|
|
|
|
enabled = true
|
|
|
|
}`), 0600)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a new command. We preemptively close the shutdownCh
|
|
|
|
// so that the command exits immediately instead of blocking.
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
shutdownCh := make(chan struct{})
|
|
|
|
close(shutdownCh)
|
|
|
|
cmd := &Command{
|
|
|
|
Version: version.GetVersion(),
|
|
|
|
Ui: ui,
|
|
|
|
ShutdownCh: shutdownCh,
|
|
|
|
}
|
|
|
|
|
|
|
|
// To prevent test failures on hosts whose hostname resolves to
|
|
|
|
// a loopback address, we must append a bind address
|
|
|
|
args := []string{"-client", "-data-dir=" + tmpDir, "-config=" + configFile, "-bind=169.254.0.1"}
|
|
|
|
if code := cmd.Run(args); code != 1 {
|
|
|
|
t.Fatalf("args: %v\nexit: %d\n", args, code)
|
|
|
|
}
|
|
|
|
|
|
|
|
out := ui.ErrorWriter.String()
|
|
|
|
exp := "Region contains invalid characters"
|
|
|
|
if !strings.Contains(out, exp) {
|
|
|
|
t.Fatalf("expect to find %q\n\n%s", exp, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 22:50:46 +00:00
|
|
|
|
|
|
|
// TestIsValidConfig asserts that invalid configurations return false.
|
|
|
|
func TestIsValidConfig(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2021-10-13 22:50:46 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
conf Config // merged into DefaultConfig()
|
|
|
|
|
|
|
|
// err should appear in error output; success expected if err
|
|
|
|
// is empty
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Default",
|
|
|
|
conf: Config{
|
|
|
|
DataDir: "/tmp",
|
|
|
|
Client: &ClientConfig{Enabled: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NoMode",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{Enabled: false},
|
|
|
|
Server: &ServerConfig{Enabled: false},
|
|
|
|
},
|
|
|
|
err: "Must specify either",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InvalidRegion",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
Region: "Hello\000World",
|
|
|
|
},
|
|
|
|
err: "Region contains",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "InvalidDatacenter",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
Datacenter: "Hello\000World",
|
|
|
|
},
|
|
|
|
err: "Datacenter contains",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "RelativeDir",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
DataDir: "foo/bar",
|
|
|
|
},
|
|
|
|
err: "must be given as an absolute",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NegativeMinDynamicPort",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
MinDynamicPort: -1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: "min_dynamic_port",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "NegativeMaxDynamicPort",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
MaxDynamicPort: -1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: "max_dynamic_port",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BigMinDynamicPort",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
MinDynamicPort: math.MaxInt32,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: "min_dynamic_port",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BigMaxDynamicPort",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
MaxDynamicPort: math.MaxInt32,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: "max_dynamic_port",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "MinMaxDynamicPortSwitched",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
MinDynamicPort: 5000,
|
|
|
|
MaxDynamicPort: 4000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: "and max",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DynamicPortOk",
|
|
|
|
conf: Config{
|
|
|
|
DataDir: "/tmp",
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
MinDynamicPort: 4000,
|
|
|
|
MaxDynamicPort: 5000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-12 20:04:31 +00:00
|
|
|
{
|
|
|
|
name: "BadReservedPorts",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
Reserved: &Resources{
|
|
|
|
ReservedPorts: "3-2147483647",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: `reserved.reserved_ports "3-2147483647" invalid: port must be < 65536 but found 2147483647`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BadHostNetworkReservedPorts",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
HostNetworks: []*structs.ClientHostNetworkConfig{
|
|
|
|
&structs.ClientHostNetworkConfig{
|
|
|
|
Name: "test",
|
|
|
|
ReservedPorts: "3-2147483647",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: `host_network["test"].reserved_ports "3-2147483647" invalid: port must be < 65536 but found 2147483647`,
|
|
|
|
},
|
2022-05-03 22:38:32 +00:00
|
|
|
{
|
|
|
|
name: "BadArtifact",
|
|
|
|
conf: Config{
|
|
|
|
Client: &ClientConfig{
|
|
|
|
Enabled: true,
|
|
|
|
Artifact: &config.ArtifactConfig{
|
2022-08-17 16:26:34 +00:00
|
|
|
HTTPReadTimeout: pointer.Of("-10m"),
|
2022-05-03 22:38:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
err: "client.artifact stanza invalid: http_read_timeout must be > 0",
|
|
|
|
},
|
2021-10-13 22:50:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
mui := cli.NewMockUi()
|
|
|
|
cmd := &Command{Ui: mui}
|
|
|
|
config := DefaultConfig().Merge(&tc.conf)
|
2022-02-08 21:52:35 +00:00
|
|
|
result := cmd.IsValidConfig(config, DefaultConfig())
|
2021-10-13 22:50:46 +00:00
|
|
|
if tc.err == "" {
|
|
|
|
// No error expected
|
|
|
|
assert.True(t, result, mui.ErrorWriter.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error expected
|
|
|
|
assert.False(t, result)
|
|
|
|
require.Contains(t, mui.ErrorWriter.String(), tc.err)
|
|
|
|
t.Logf("%s returned: %s", tc.name, mui.ErrorWriter.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|