From 693d9ffa3de8645b47a7f8aca6b5802858cae45b Mon Sep 17 00:00:00 2001 From: cskh Date: Sat, 8 Apr 2023 01:09:32 +0000 Subject: [PATCH] backport of commit 91be1c3e35880e856a7d19f5cba13c9736a836c1 --- test/integration/consul-container/go.mod | 1 + test/integration/consul-container/go.sum | 3 +++ .../consul-container/libs/cluster/agent.go | 13 ++++++++++++ .../consul-container/libs/cluster/builder.go | 5 +++++ .../libs/cluster/container.go | 21 +++++++++++++++---- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/test/integration/consul-container/go.mod b/test/integration/consul-container/go.mod index 35174f5fd0b4..e13d6e4f1fd3 100644 --- a/test/integration/consul-container/go.mod +++ b/test/integration/consul-container/go.mod @@ -15,6 +15,7 @@ require ( github.com/hashicorp/serf v0.10.1 github.com/itchyny/gojq v0.12.9 github.com/mitchellh/copystructure v1.2.0 + github.com/otiai10/copy v1.10.0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.8.1 github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 diff --git a/test/integration/consul-container/go.sum b/test/integration/consul-container/go.sum index e554f8ad3f70..010695dea029 100644 --- a/test/integration/consul-container/go.sum +++ b/test/integration/consul-container/go.sum @@ -630,6 +630,9 @@ github.com/opencontainers/selinux v1.6.0/go.mod h1:VVGKuOLlE7v4PJyT6h7mNWvq1rzqi github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3ogry1nUQF8Evvo= github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8= github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= +github.com/otiai10/copy v1.10.0 h1:znyI7l134wNg/wDktoVQPxPkgvhDfGCYUasey+h0rDQ= +github.com/otiai10/copy v1.10.0/go.mod h1:rSaLseMUsZFFbsFGc7wCJnnkTAvdc5L6VWxPE4308Ww= +github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= diff --git a/test/integration/consul-container/libs/cluster/agent.go b/test/integration/consul-container/libs/cluster/agent.go index 8dfa496d8bff..ec525780bb1d 100644 --- a/test/integration/consul-container/libs/cluster/agent.go +++ b/test/integration/consul-container/libs/cluster/agent.go @@ -37,6 +37,19 @@ type Agent interface { // // Constructed by (Builder).ToAgentConfig() type Config struct { + // NodeName is set for the consul agent name and container name + // Equivalent to the -node command-line flag. + // If empty, a randam name will be generated + NodeName string + // NodeID is used to configure node_id in agent config file + // Equivalent to the -node-id command-line flag. + // If empty, a randam name will be generated + NodeID string + + // ExternalDataDir is data directory to copy consul data from, if set. + // This directory contains subdirectories like raft, serf, services + ExternalDataDir string + ScratchDir string CertVolume string CACert string diff --git a/test/integration/consul-container/libs/cluster/builder.go b/test/integration/consul-container/libs/cluster/builder.go index 2807985e17e5..9cec08560da1 100644 --- a/test/integration/consul-container/libs/cluster/builder.go +++ b/test/integration/consul-container/libs/cluster/builder.go @@ -269,6 +269,11 @@ func (b *Builder) Peering(enable bool) *Builder { return b } +func (b *Builder) NodeID(nodeID string) *Builder { + b.conf.Set("node_id", nodeID) + return b +} + func (b *Builder) Partition(name string) *Builder { b.conf.Set("partition", name) return b diff --git a/test/integration/consul-container/libs/cluster/container.go b/test/integration/consul-container/libs/cluster/container.go index 4e3e1c39901c..dfda21615978 100644 --- a/test/integration/consul-container/libs/cluster/container.go +++ b/test/integration/consul-container/libs/cluster/container.go @@ -13,6 +13,7 @@ import ( goretry "github.com/avast/retry-go" dockercontainer "github.com/docker/docker/api/types/container" "github.com/hashicorp/go-multierror" + "github.com/otiai10/copy" "github.com/pkg/errors" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" @@ -91,11 +92,15 @@ func NewConsulContainer(ctx context.Context, config Config, cluster *Cluster, po return nil, err } - consulType := "client" - if pc.Server { - consulType = "server" + name := config.NodeName + if name == "" { + // Generate a random name for the agent + consulType := "client" + if pc.Server { + consulType = "server" + } + name = utils.RandName(fmt.Sprintf("%s-consul-%s-%d", pc.Datacenter, consulType, index)) } - name := utils.RandName(fmt.Sprintf("%s-consul-%s-%d", pc.Datacenter, consulType, index)) // Inject new Agent name config.Cmd = append(config.Cmd, "-node", name) @@ -108,6 +113,14 @@ func NewConsulContainer(ctx context.Context, config Config, cluster *Cluster, po return nil, fmt.Errorf("error chowning data directory %s: %w", tmpDirData, err) } + if config.ExternalDataDir != "" { + // copy consul persistent state from an external dir + err := copy.Copy(config.ExternalDataDir, tmpDirData) + if err != nil { + return nil, fmt.Errorf("error copying persistent data from %s: %w", config.ExternalDataDir, err) + } + } + var caCertFileForAPI string if config.CACert != "" { caCertFileForAPI = filepath.Join(config.ScratchDir, "ca.pem")