open-nomad/client/config/testing.go
Seth Hoenig 5dd8aa3e27 client: enforce max_kill_timeout client configuration
This PR fixes a bug where client configuration max_kill_timeout was
not being enforced. The feature was introduced in 9f44780 but seems
to have been removed during the major drivers refactoring.

We can make sure the value is enforced by pluming it through the DriverHandler,
which now uses the lesser of the task.killTimeout or client.maxKillTimeout.
Also updates Event.SetKillTimeout to require both the task.killTimeout and
client.maxKillTimeout so that we don't make the mistake of using the wrong
value - as it was being given only the task.killTimeout before.
2022-07-06 15:29:38 -05:00

74 lines
2 KiB
Go

package config
import (
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/helper"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/nomad/mock"
testing "github.com/mitchellh/go-testing-interface"
)
// 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()) {
conf := DefaultConfig()
conf.Node = mock.Node()
conf.Logger = testlog.HCLogger(t)
// 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)
// Create a tempdir to hold state and alloc subdirs
parent, err := ioutil.TempDir(tmpDir, "nomadtest")
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
// Use a minimal chroot environment
conf.ChrootEnv = ci.TinyChroot
// Helps make sure we are respecting configured parent
conf.CgroupParent = "testing.slice"
conf.VaultConfig.Enabled = helper.BoolToPtr(false)
conf.DevMode = true
// Loosen GC threshold
conf.GCDiskUsageThreshold = 98.0
conf.GCInodeUsageThreshold = 98.0
// Same as default; necessary for task Event messages
conf.MaxKillTimeout = 30 * time.Second
return conf, cleanup
}