2018-04-18 10:18:53 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
2018-12-27 12:04:10 -05:00
|
|
|
"strings"
|
2018-04-18 10:18:53 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-12-27 12:04:10 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2018-04-18 10:18:53 -04:00
|
|
|
swarmtypes "github.com/docker/docker/api/types/swarm"
|
|
|
|
"github.com/docker/docker/api/types/swarm/runtime"
|
|
|
|
"github.com/docker/docker/integration/internal/swarm"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/daemon"
|
|
|
|
"github.com/docker/docker/testutil/fixtures/plugin"
|
|
|
|
"github.com/docker/docker/testutil/registry"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/poll"
|
|
|
|
"gotest.tools/skip"
|
2018-04-18 10:18:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestServicePlugin(t *testing.T) {
|
2018-04-25 05:03:43 -04:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
|
2018-04-18 10:18:53 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
|
|
|
skip.If(t, os.Getenv("DOCKER_ENGINE_GOARCH") != "amd64")
|
|
|
|
defer setupTest(t)()
|
|
|
|
|
|
|
|
reg := registry.NewV2(t)
|
|
|
|
defer reg.Close()
|
|
|
|
|
2019-09-11 20:09:54 -04:00
|
|
|
name := "test-" + strings.ToLower(t.Name())
|
|
|
|
repo := path.Join(registry.DefaultURL, "swarm", name+":v1")
|
|
|
|
repo2 := path.Join(registry.DefaultURL, "swarm", name+":v2")
|
2018-04-18 10:18:53 -04:00
|
|
|
|
|
|
|
d := daemon.New(t)
|
|
|
|
d.StartWithBusybox(t)
|
|
|
|
apiclient := d.NewClientT(t)
|
|
|
|
err := plugin.Create(context.Background(), apiclient, repo)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
r, err := apiclient.PluginPush(context.Background(), repo, "")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = io.Copy(ioutil.Discard, r)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = apiclient.PluginRemove(context.Background(), repo, types.PluginRemoveOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = plugin.Create(context.Background(), apiclient, repo2)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
r, err = apiclient.PluginPush(context.Background(), repo2, "")
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = io.Copy(ioutil.Discard, r)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
err = apiclient.PluginRemove(context.Background(), repo2, types.PluginRemoveOptions{})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
d.Stop(t)
|
|
|
|
|
2019-09-30 08:23:56 -04:00
|
|
|
d1 := swarm.NewSwarm(t, testEnv, daemon.WithExperimental())
|
2018-04-18 10:18:53 -04:00
|
|
|
defer d1.Stop(t)
|
2019-09-30 08:23:56 -04:00
|
|
|
d2 := daemon.New(t, daemon.WithExperimental(), daemon.WithSwarmPort(daemon.DefaultSwarmPort+1))
|
2018-04-18 10:18:53 -04:00
|
|
|
d2.StartAndSwarmJoin(t, d1, true)
|
|
|
|
defer d2.Stop(t)
|
2019-09-30 08:23:56 -04:00
|
|
|
d3 := daemon.New(t, daemon.WithExperimental(), daemon.WithSwarmPort(daemon.DefaultSwarmPort+2))
|
2018-04-18 10:18:53 -04:00
|
|
|
d3.StartAndSwarmJoin(t, d1, false)
|
|
|
|
defer d3.Stop(t)
|
|
|
|
|
|
|
|
id := d1.CreateService(t, makePlugin(repo, name, nil))
|
2019-02-06 18:33:07 -05:00
|
|
|
poll.WaitOn(t, d1.PluginIsRunning(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d2.PluginIsRunning(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d3.PluginIsRunning(t, name), swarm.ServicePoll)
|
2018-04-18 10:18:53 -04:00
|
|
|
|
2018-12-27 12:04:10 -05:00
|
|
|
// test that environment variables are passed from plugin service to plugin instance
|
2018-04-18 10:18:53 -04:00
|
|
|
service := d1.GetService(t, id)
|
2018-12-27 12:04:10 -05:00
|
|
|
tasks := d1.GetServiceTasks(t, service.Spec.Annotations.Name, filters.Arg("runtime", "plugin"))
|
|
|
|
if len(tasks) == 0 {
|
|
|
|
t.Log("No tasks found for plugin service")
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
plugin, _, err := d1.NewClientT(t).PluginInspectWithRaw(context.Background(), name)
|
|
|
|
assert.NilError(t, err, "Error inspecting service plugin")
|
|
|
|
found := false
|
|
|
|
for _, env := range plugin.Settings.Env {
|
|
|
|
assert.Equal(t, strings.HasPrefix(env, "baz"), false, "Environment variable entry %q is invalid and should not be present", "baz")
|
|
|
|
if strings.HasPrefix(env, "foo=") {
|
|
|
|
found = true
|
|
|
|
assert.Equal(t, env, "foo=bar")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, true, found, "Environment variable %q not found in plugin", "foo")
|
|
|
|
|
2018-04-18 10:18:53 -04:00
|
|
|
d1.UpdateService(t, service, makePlugin(repo2, name, nil))
|
2019-02-06 18:33:07 -05:00
|
|
|
poll.WaitOn(t, d1.PluginReferenceIs(t, name, repo2), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d2.PluginReferenceIs(t, name, repo2), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d3.PluginReferenceIs(t, name, repo2), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d1.PluginIsRunning(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d2.PluginIsRunning(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d3.PluginIsRunning(t, name), swarm.ServicePoll)
|
2018-04-18 10:18:53 -04:00
|
|
|
|
|
|
|
d1.RemoveService(t, id)
|
2019-02-06 18:33:07 -05:00
|
|
|
poll.WaitOn(t, d1.PluginIsNotPresent(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d2.PluginIsNotPresent(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d3.PluginIsNotPresent(t, name), swarm.ServicePoll)
|
2018-04-18 10:18:53 -04:00
|
|
|
|
|
|
|
// constrain to managers only
|
|
|
|
id = d1.CreateService(t, makePlugin(repo, name, []string{"node.role==manager"}))
|
2019-02-06 18:33:07 -05:00
|
|
|
poll.WaitOn(t, d1.PluginIsRunning(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d2.PluginIsRunning(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d3.PluginIsNotPresent(t, name), swarm.ServicePoll)
|
2018-04-18 10:18:53 -04:00
|
|
|
|
|
|
|
d1.RemoveService(t, id)
|
2019-02-06 18:33:07 -05:00
|
|
|
poll.WaitOn(t, d1.PluginIsNotPresent(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d2.PluginIsNotPresent(t, name), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d3.PluginIsNotPresent(t, name), swarm.ServicePoll)
|
2018-04-18 10:18:53 -04:00
|
|
|
|
|
|
|
// with no name
|
|
|
|
id = d1.CreateService(t, makePlugin(repo, "", nil))
|
2019-02-06 18:33:07 -05:00
|
|
|
poll.WaitOn(t, d1.PluginIsRunning(t, repo), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d2.PluginIsRunning(t, repo), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d3.PluginIsRunning(t, repo), swarm.ServicePoll)
|
2018-04-18 10:18:53 -04:00
|
|
|
|
|
|
|
d1.RemoveService(t, id)
|
2019-02-06 18:33:07 -05:00
|
|
|
poll.WaitOn(t, d1.PluginIsNotPresent(t, repo), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d2.PluginIsNotPresent(t, repo), swarm.ServicePoll)
|
|
|
|
poll.WaitOn(t, d3.PluginIsNotPresent(t, repo), swarm.ServicePoll)
|
2018-04-18 10:18:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func makePlugin(repo, name string, constraints []string) func(*swarmtypes.Service) {
|
|
|
|
return func(s *swarmtypes.Service) {
|
2018-07-17 14:41:25 -04:00
|
|
|
s.Spec.TaskTemplate.Runtime = swarmtypes.RuntimePlugin
|
2018-04-18 10:18:53 -04:00
|
|
|
s.Spec.TaskTemplate.PluginSpec = &runtime.PluginSpec{
|
|
|
|
Name: name,
|
|
|
|
Remote: repo,
|
2018-12-27 12:04:10 -05:00
|
|
|
Env: []string{
|
|
|
|
"baz", // invalid environment variable entries are ignored
|
|
|
|
"foo=bar", // "foo" will be the single environment variable
|
|
|
|
},
|
2018-04-18 10:18:53 -04:00
|
|
|
}
|
|
|
|
if constraints != nil {
|
|
|
|
s.Spec.TaskTemplate.Placement = &swarmtypes.Placement{
|
|
|
|
Constraints: constraints,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|