Merge pull request #37183 from vdemeester/service-create-init
Add support for `init` on services Upstream-commit: f11f24958852986cfc996e6076fed75bfcbf1ed1 Component: engine
This commit is contained in:
@@ -86,6 +86,14 @@ func defaultServiceSpec() swarmtypes.ServiceSpec {
|
||||
return spec
|
||||
}
|
||||
|
||||
// ServiceWithInit sets whether the service should use init or not
|
||||
func ServiceWithInit(b *bool) func(*swarmtypes.ServiceSpec) {
|
||||
return func(spec *swarmtypes.ServiceSpec) {
|
||||
ensureContainerSpec(spec)
|
||||
spec.TaskTemplate.ContainerSpec.Init = b
|
||||
}
|
||||
}
|
||||
|
||||
// ServiceWithImage sets the image to use for the service
|
||||
func ServiceWithImage(image string) func(*swarmtypes.ServiceSpec) {
|
||||
return func(spec *swarmtypes.ServiceSpec) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package service // import "github.com/docker/docker/integration/service"
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -11,11 +12,64 @@ import (
|
||||
swarmtypes "github.com/docker/docker/api/types/swarm"
|
||||
"github.com/docker/docker/client"
|
||||
"github.com/docker/docker/integration/internal/swarm"
|
||||
"github.com/docker/docker/internal/test/daemon"
|
||||
"github.com/gotestyourself/gotestyourself/assert"
|
||||
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
||||
"github.com/gotestyourself/gotestyourself/poll"
|
||||
)
|
||||
|
||||
func TestServiceCreateInit(t *testing.T) {
|
||||
defer setupTest(t)()
|
||||
t.Run("daemonInitDisabled", testServiceCreateInit(false))
|
||||
t.Run("daemonInitEnabled", testServiceCreateInit(true))
|
||||
}
|
||||
|
||||
func testServiceCreateInit(daemonEnabled bool) func(t *testing.T) {
|
||||
return func(t *testing.T) {
|
||||
var ops = []func(*daemon.Daemon){}
|
||||
|
||||
if daemonEnabled {
|
||||
ops = append(ops, daemon.WithInit)
|
||||
}
|
||||
d := swarm.NewSwarm(t, testEnv, ops...)
|
||||
defer d.Stop(t)
|
||||
client := d.NewClientT(t)
|
||||
defer client.Close()
|
||||
|
||||
booleanTrue := true
|
||||
booleanFalse := false
|
||||
|
||||
serviceID := swarm.CreateService(t, d)
|
||||
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
|
||||
i := inspectServiceContainer(t, client, serviceID)
|
||||
// HostConfig.Init == nil means that it delegates to daemon configuration
|
||||
assert.Check(t, i.HostConfig.Init == nil)
|
||||
|
||||
serviceID = swarm.CreateService(t, d, swarm.ServiceWithInit(&booleanTrue))
|
||||
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
|
||||
i = inspectServiceContainer(t, client, serviceID)
|
||||
assert.Check(t, is.Equal(true, *i.HostConfig.Init))
|
||||
|
||||
serviceID = swarm.CreateService(t, d, swarm.ServiceWithInit(&booleanFalse))
|
||||
poll.WaitOn(t, serviceRunningTasksCount(client, serviceID, 1), swarm.ServicePoll)
|
||||
i = inspectServiceContainer(t, client, serviceID)
|
||||
assert.Check(t, is.Equal(false, *i.HostConfig.Init))
|
||||
}
|
||||
}
|
||||
|
||||
func inspectServiceContainer(t *testing.T, client client.APIClient, serviceID string) types.ContainerJSON {
|
||||
t.Helper()
|
||||
filter := filters.NewArgs()
|
||||
filter.Add("label", fmt.Sprintf("com.docker.swarm.service.id=%s", serviceID))
|
||||
containers, err := client.ContainerList(context.Background(), types.ContainerListOptions{Filters: filter})
|
||||
assert.NilError(t, err)
|
||||
assert.Check(t, is.Len(containers, 1))
|
||||
|
||||
i, err := client.ContainerInspect(context.Background(), containers[0].ID)
|
||||
assert.NilError(t, err)
|
||||
return i
|
||||
}
|
||||
|
||||
func TestCreateServiceMultipleTimes(t *testing.T) {
|
||||
defer setupTest(t)()
|
||||
d := swarm.NewSwarm(t, testEnv)
|
||||
|
||||
Reference in New Issue
Block a user