github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/client/config/testing.go (about)

     1  package config
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/hashicorp/nomad/helper"
     9  	"github.com/hashicorp/nomad/helper/testlog"
    10  	"github.com/hashicorp/nomad/nomad/mock"
    11  	testing "github.com/mitchellh/go-testing-interface"
    12  )
    13  
    14  // TestClientConfig returns a default client configuration for test clients and
    15  // a cleanup func to remove the state and alloc dirs when finished.
    16  func TestClientConfig(t testing.T) (*Config, func()) {
    17  	conf := DefaultConfig()
    18  	conf.Node = mock.Node()
    19  	conf.Logger = testlog.HCLogger(t)
    20  
    21  	// On macOS, os.TempDir returns a symlinked path under /var which
    22  	// is outside of the directories shared into the VM used for Docker.
    23  	// Expand the symlink to get the real path in /private, which is ok.
    24  	dirName := os.TempDir()
    25  	tmpDir, err := filepath.EvalSymlinks(dirName)
    26  	if err != nil {
    27  		t.Fatalf("Could not resolve temporary directory links for %s: %v", tmpDir, err)
    28  	}
    29  	tmpDir = filepath.Clean(tmpDir)
    30  
    31  	// Create a tempdir to hold state and alloc subdirs
    32  	parent, err := ioutil.TempDir(tmpDir, "nomadtest")
    33  	if err != nil {
    34  		t.Fatalf("error creating client dir: %v", err)
    35  	}
    36  	cleanup := func() {
    37  		os.RemoveAll(parent)
    38  	}
    39  
    40  	allocDir := filepath.Join(parent, "allocs")
    41  	if err := os.Mkdir(allocDir, 0777); err != nil {
    42  		cleanup()
    43  		t.Fatalf("error creating alloc dir: %v", err)
    44  	}
    45  	conf.AllocDir = allocDir
    46  
    47  	stateDir := filepath.Join(parent, "client")
    48  	if err := os.Mkdir(stateDir, 0777); err != nil {
    49  		cleanup()
    50  		t.Fatalf("error creating alloc dir: %v", err)
    51  	}
    52  	conf.StateDir = stateDir
    53  
    54  	conf.VaultConfig.Enabled = helper.BoolToPtr(false)
    55  	conf.DevMode = true
    56  
    57  	// Loosen GC threshold
    58  	conf.GCDiskUsageThreshold = 98.0
    59  	conf.GCInodeUsageThreshold = 98.0
    60  	return conf, cleanup
    61  }