github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/config/testing.go (about) 1 package config 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "time" 8 9 "github.com/hashicorp/nomad/ci" 10 "github.com/hashicorp/nomad/helper/pointer" 11 "github.com/hashicorp/nomad/helper/testlog" 12 "github.com/hashicorp/nomad/nomad/mock" 13 testing "github.com/mitchellh/go-testing-interface" 14 ) 15 16 // TestClientConfig returns a default client configuration for test clients and 17 // a cleanup func to remove the state and alloc dirs when finished. 18 func TestClientConfig(t testing.T) (*Config, func()) { 19 conf := DefaultConfig() 20 conf.Node = mock.Node() 21 conf.Logger = testlog.HCLogger(t) 22 23 // On macOS, os.TempDir returns a symlinked path under /var which 24 // is outside of the directories shared into the VM used for Docker. 25 // Expand the symlink to get the real path in /private, which is ok. 26 dirName := os.TempDir() 27 tmpDir, err := filepath.EvalSymlinks(dirName) 28 if err != nil { 29 t.Fatalf("Could not resolve temporary directory links for %s: %v", tmpDir, err) 30 } 31 tmpDir = filepath.Clean(tmpDir) 32 33 // Create a tempdir to hold state and alloc subdirs 34 parent, err := ioutil.TempDir(tmpDir, "nomadtest") 35 if err != nil { 36 t.Fatalf("error creating client dir: %v", err) 37 } 38 cleanup := func() { 39 os.RemoveAll(parent) 40 } 41 42 // Fixup nomadtest dir permissions 43 if err = os.Chmod(parent, 0777); err != nil { 44 t.Fatalf("error updating permissions on nomadtest dir") 45 } 46 47 allocDir := filepath.Join(parent, "allocs") 48 if err := os.Mkdir(allocDir, 0777); err != nil { 49 cleanup() 50 t.Fatalf("error creating alloc dir: %v", err) 51 } 52 conf.AllocDir = allocDir 53 54 stateDir := filepath.Join(parent, "client") 55 if err := os.Mkdir(stateDir, 0777); err != nil { 56 cleanup() 57 t.Fatalf("error creating alloc dir: %v", err) 58 } 59 conf.StateDir = stateDir 60 61 // Use a minimal chroot environment 62 conf.ChrootEnv = ci.TinyChroot 63 64 // Helps make sure we are respecting configured parent 65 conf.CgroupParent = "testing.slice" 66 67 conf.VaultConfig.Enabled = pointer.Of(false) 68 conf.DevMode = true 69 70 // Loosen GC threshold 71 conf.GCDiskUsageThreshold = 98.0 72 conf.GCInodeUsageThreshold = 98.0 73 74 // Same as default; necessary for task Event messages 75 conf.MaxKillTimeout = 30 * time.Second 76 77 return conf, cleanup 78 }