internal/testutil/daemon: remove "Config" utilities

These were just shallow wrappers around the api-client, and some
were not used; inline them to help transition these tests to the
integration-suite in future.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-21 22:40:47 +02:00
parent 2401bd1e12
commit 8cd377bd9c
3 changed files with 43 additions and 98 deletions

View File

@@ -12,6 +12,7 @@ import (
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/mount"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
"github.com/moby/moby/v2/integration-cli/checker"
"github.com/moby/moby/v2/internal/testutil"
"gotest.tools/v3/assert"
@@ -222,16 +223,20 @@ func (s *DockerSwarmSuite) TestServiceCreateWithSecretReferencedTwice(c *testing
func (s *DockerSwarmSuite) TestServiceCreateWithConfigSimple(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
apiClient := d.NewClientT(c)
serviceName := "test-service-config"
testName := "test_config"
id := d.CreateConfig(c, swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: testName,
result, err := apiClient.ConfigCreate(ctx, client.ConfigCreateOptions{
Spec: swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: testName,
},
Data: []byte("TESTINGDATA"),
},
Data: []byte("TESTINGDATA"),
})
assert.Assert(c, id != "", "configs: %s", id)
assert.NilError(c, err)
assert.Assert(c, result.ID != "", "configs: %s", result.ID)
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--config", testName, "busybox", "top")
assert.NilError(c, err, out)
@@ -251,7 +256,8 @@ func (s *DockerSwarmSuite) TestServiceCreateWithConfigSimple(c *testing.T) {
out, err = d.Cmd("service", "rm", serviceName)
assert.NilError(c, err, out)
d.DeleteConfig(c, testName)
_, err = apiClient.ConfigRemove(ctx, testName, client.ConfigRemoveOptions{})
assert.NilError(c, err)
}
func (s *DockerSwarmSuite) TestServiceCreateWithConfigSourceTargetPaths(c *testing.T) {
@@ -266,15 +272,18 @@ func (s *DockerSwarmSuite) TestServiceCreateWithConfigSourceTargetPaths(c *testi
var configFlags []string
apiClient := d.NewClientT(c)
for testName, testTarget := range testPaths {
id := d.CreateConfig(c, swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: testName,
result, err := apiClient.ConfigCreate(ctx, client.ConfigCreateOptions{
Spec: swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: testName,
},
Data: []byte("TESTINGDATA " + testName + " " + testTarget),
},
Data: []byte("TESTINGDATA " + testName + " " + testTarget),
})
assert.Assert(c, id != "", "configs: %s", id)
assert.NilError(c, err)
assert.Assert(c, result.ID != "", "configs: %s", result.ID)
configFlags = append(configFlags, "--config", fmt.Sprintf("source=%s,target=%s", testName, testTarget))
}
@@ -323,14 +332,17 @@ func (s *DockerSwarmSuite) TestServiceCreateWithConfigSourceTargetPaths(c *testi
func (s *DockerSwarmSuite) TestServiceCreateWithConfigReferencedTwice(c *testing.T) {
ctx := testutil.GetContext(c)
d := s.AddDaemon(ctx, c, true, true)
id := d.CreateConfig(c, swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: "myconfig",
apiClient := d.NewClientT(c)
result, err := apiClient.ConfigCreate(ctx, client.ConfigCreateOptions{
Spec: swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: "myconfig",
},
Data: []byte("TESTINGDATA"),
},
Data: []byte("TESTINGDATA"),
})
assert.Assert(c, id != "", "configs: %s", id)
assert.NilError(c, err)
assert.Assert(c, result.ID != "", "configs: %s", result.ID)
serviceName := "svc"
out, err := d.Cmd("service", "create", "--detach", "--no-resolve-image", "--name", serviceName, "--config", "source=myconfig,target=target1", "--config", "source=myconfig,target=target2", "busybox", "top")

View File

@@ -21,6 +21,7 @@ import (
"github.com/cloudflare/cfssl/helpers"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/api/types/versions"
"github.com/moby/moby/client"
"github.com/moby/moby/v2/daemon/libnetwork/driverapi"
"github.com/moby/moby/v2/daemon/libnetwork/ipamapi"
remoteipam "github.com/moby/moby/v2/daemon/libnetwork/ipams/remote/api"
@@ -2008,18 +2009,24 @@ func (s *DockerSwarmSuite) TestSwarmClusterEventsConfig(c *testing.T) {
d := s.AddDaemon(ctx, c, true, true)
testName := "test_config"
id := d.CreateConfig(c, swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: testName,
apiClient := d.NewClientT(c)
result, err := apiClient.ConfigCreate(ctx, client.ConfigCreateOptions{
Spec: swarm.ConfigSpec{
Annotations: swarm.Annotations{
Name: testName,
},
Data: []byte("TESTINGDATA"),
},
Data: []byte("TESTINGDATA"),
})
assert.Assert(c, id != "", "configs: %s", id)
assert.NilError(c, err)
assert.Assert(c, result.ID != "", "configs: %s", result.ID)
id := result.ID
waitForEvent(c, d, "0", "-f scope=swarm", "config create "+id, defaultRetryCount)
t1 := daemonUnixTime(c)
d.DeleteConfig(c, id)
_, err = apiClient.ConfigRemove(ctx, id, client.ConfigRemoveOptions{})
assert.NilError(c, err)
// filtered by config
waitForEvent(c, d, t1, "-f type=config", "config remove "+id, defaultRetryCount)
}

View File

@@ -1,74 +0,0 @@
package daemon
import (
"context"
"testing"
"github.com/moby/moby/api/types/swarm"
"github.com/moby/moby/client"
"gotest.tools/v3/assert"
)
// ConfigConstructor defines a swarm config constructor
type ConfigConstructor func(*swarm.Config)
// CreateConfig creates a config given the specified spec
func (d *Daemon) CreateConfig(t testing.TB, configSpec swarm.ConfigSpec) string {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
result, err := cli.ConfigCreate(context.Background(), client.ConfigCreateOptions{
Spec: configSpec,
})
assert.NilError(t, err)
return result.ID
}
// ListConfigs returns the list of the current swarm configs
func (d *Daemon) ListConfigs(t testing.TB) []swarm.Config {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
result, err := cli.ConfigList(context.Background(), client.ConfigListOptions{})
assert.NilError(t, err)
return result.Items
}
// GetConfig returns a swarm config identified by the specified id
func (d *Daemon) GetConfig(t testing.TB, id string) *swarm.Config {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
result, err := cli.ConfigInspect(context.Background(), id, client.ConfigInspectOptions{})
assert.NilError(t, err)
return &result.Config
}
// DeleteConfig removes the swarm config identified by the specified id
func (d *Daemon) DeleteConfig(t testing.TB, id string) {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
_, err := cli.ConfigRemove(context.Background(), id, client.ConfigRemoveOptions{})
assert.NilError(t, err)
}
// UpdateConfig updates the swarm config identified by the specified id
// Currently, only label update is supported.
func (d *Daemon) UpdateConfig(t testing.TB, id string, f ...ConfigConstructor) {
t.Helper()
cli := d.NewClientT(t)
defer cli.Close()
config := d.GetConfig(t, id)
for _, fn := range f {
fn(config)
}
_, err := cli.ConfigUpdate(context.Background(), config.ID, client.ConfigUpdateOptions{Version: config.Version, Spec: config.Spec})
assert.NilError(t, err)
}