2018-09-27 00:29:54 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-10-16 03:07:16 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-07-06 19:44:58 +00:00
|
|
|
"time"
|
2018-10-16 03:07:16 +00:00
|
|
|
|
2022-04-19 14:13:38 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2018-09-27 00:29:54 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-10-18 20:39:02 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/testlog"
|
2018-11-06 01:39:02 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2019-01-15 19:46:12 +00:00
|
|
|
testing "github.com/mitchellh/go-testing-interface"
|
2018-09-27 00:29:54 +00:00
|
|
|
)
|
|
|
|
|
2018-10-16 03:07:16 +00:00
|
|
|
// TestClientConfig returns a default client configuration for test clients and
|
|
|
|
// a cleanup func to remove the state and alloc dirs when finished.
|
|
|
|
func TestClientConfig(t testing.T) (*Config, func()) {
|
2018-09-27 00:29:54 +00:00
|
|
|
conf := DefaultConfig()
|
2018-11-06 01:39:02 +00:00
|
|
|
conf.Node = mock.Node()
|
2018-10-18 20:39:02 +00:00
|
|
|
conf.Logger = testlog.HCLogger(t)
|
2018-10-16 03:07:16 +00:00
|
|
|
|
2019-09-10 16:20:09 +00:00
|
|
|
// On macOS, os.TempDir returns a symlinked path under /var which
|
|
|
|
// is outside of the directories shared into the VM used for Docker.
|
|
|
|
// Expand the symlink to get the real path in /private, which is ok.
|
|
|
|
dirName := os.TempDir()
|
|
|
|
tmpDir, err := filepath.EvalSymlinks(dirName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not resolve temporary directory links for %s: %v", tmpDir, err)
|
|
|
|
}
|
|
|
|
tmpDir = filepath.Clean(tmpDir)
|
|
|
|
|
2018-10-16 03:07:16 +00:00
|
|
|
// Create a tempdir to hold state and alloc subdirs
|
2019-09-10 16:20:09 +00:00
|
|
|
parent, err := ioutil.TempDir(tmpDir, "nomadtest")
|
2018-10-16 03:07:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error creating client dir: %v", err)
|
|
|
|
}
|
|
|
|
cleanup := func() {
|
|
|
|
os.RemoveAll(parent)
|
|
|
|
}
|
|
|
|
|
|
|
|
allocDir := filepath.Join(parent, "allocs")
|
|
|
|
if err := os.Mkdir(allocDir, 0777); err != nil {
|
|
|
|
cleanup()
|
|
|
|
t.Fatalf("error creating alloc dir: %v", err)
|
|
|
|
}
|
|
|
|
conf.AllocDir = allocDir
|
|
|
|
|
|
|
|
stateDir := filepath.Join(parent, "client")
|
|
|
|
if err := os.Mkdir(stateDir, 0777); err != nil {
|
|
|
|
cleanup()
|
|
|
|
t.Fatalf("error creating alloc dir: %v", err)
|
|
|
|
}
|
|
|
|
conf.StateDir = stateDir
|
|
|
|
|
2022-04-19 14:13:38 +00:00
|
|
|
// Use a minimal chroot environment
|
|
|
|
conf.ChrootEnv = ci.TinyChroot
|
|
|
|
|
|
|
|
// Helps make sure we are respecting configured parent
|
|
|
|
conf.CgroupParent = "testing.slice"
|
|
|
|
|
2018-09-27 00:29:54 +00:00
|
|
|
conf.VaultConfig.Enabled = helper.BoolToPtr(false)
|
|
|
|
conf.DevMode = true
|
|
|
|
|
|
|
|
// Loosen GC threshold
|
|
|
|
conf.GCDiskUsageThreshold = 98.0
|
|
|
|
conf.GCInodeUsageThreshold = 98.0
|
2022-07-06 19:44:58 +00:00
|
|
|
|
|
|
|
// Same as default; necessary for task Event messages
|
|
|
|
conf.MaxKillTimeout = 30 * time.Second
|
|
|
|
|
2018-10-16 03:07:16 +00:00
|
|
|
return conf, cleanup
|
2018-09-27 00:29:54 +00:00
|
|
|
}
|