Files
nomad/client/config/testing.go
Tim Gross 50f0ce5412 config: remove old Vault/Consul config blocks from client (#18994)
Remove the now-unused original configuration blocks for Consul and Vault from
the client. When the client needs to refer to a Consul or Vault block it will
always be for a specific cluster for the task/service. Add a helper for
accessing the default clusters (for the client's own use).

This is two of three changesets for this work. The remainder will implement the
same changes in the `command/agent` package.

As part of this work I discovered and fixed two bugs:

* The gRPC proxy socket that we create for Envoy is only ever created using the
  default Consul cluster's configuration. This will prevent Connect from being
  used with the non-default cluster.
* The Consul configuration we use for templates always comes from the default
  Consul cluster's configuration, but will use the correct Consul token for the
  non-default cluster. This will prevent templates from being used with the
  non-default cluster.

Ref: https://github.com/hashicorp/nomad/issues/18947
Ref: https://github.com/hashicorp/nomad/pull/18991
Fixes: https://github.com/hashicorp/nomad/issues/18984
Fixes: https://github.com/hashicorp/nomad/issues/18983
2023-11-07 09:15:37 -05:00

92 lines
2.4 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package config
import (
"context"
"net"
"os"
"path/filepath"
"time"
"github.com/hashicorp/nomad/ci"
"github.com/hashicorp/nomad/helper/pointer"
"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 := os.MkdirTemp(tmpDir, "nomadtest")
if err != nil {
t.Fatalf("error creating client dir: %v", err)
}
cleanup := func() {
os.RemoveAll(parent)
}
// Fixup nomadtest dir permissions
if err = os.Chmod(parent, 0777); err != nil {
t.Fatalf("error updating permissions on nomadtest dir")
}
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.GetDefaultVault().Enabled = pointer.Of(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
// Provide a stub APIListenerRegistrar implementation
conf.APIListenerRegistrar = NoopAPIListenerRegistrar{}
return conf, cleanup
}
type NoopAPIListenerRegistrar struct{}
func (NoopAPIListenerRegistrar) Serve(_ context.Context, _ net.Listener) error {
return nil
}