2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/integration-cli/daemon"
|
2016-06-14 02:54:20 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2016-06-14 02:54:20 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 18:18:12 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2017-09-19 20:12:29 +00:00
|
|
|
"github.com/docker/docker/client"
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2016-06-14 02:54:20 +00:00
|
|
|
"github.com/go-check/check"
|
2018-06-11 13:32:11 +00:00
|
|
|
"gotest.tools/assert"
|
2016-06-14 02:54:20 +00:00
|
|
|
)
|
|
|
|
|
2016-12-15 21:56:32 +00:00
|
|
|
// CheckServiceTasksInState returns the number of tasks with a matching state,
|
|
|
|
// and optional message substring.
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckServiceTasksInState(service string, state swarm.TaskState, message string) func(*check.C) (interface{}, check.CommentInterface) {
|
2016-09-02 21:12:05 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 09:17:53 +00:00
|
|
|
tasks := d.GetServiceTasks(c, service)
|
2016-12-15 21:56:32 +00:00
|
|
|
var count int
|
2016-07-21 02:16:19 +00:00
|
|
|
for _, task := range tasks {
|
2016-12-15 21:56:32 +00:00
|
|
|
if task.Status.State == state {
|
|
|
|
if message == "" || strings.Contains(task.Status.Message, message) {
|
|
|
|
count++
|
|
|
|
}
|
2016-07-21 02:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 21:56:32 +00:00
|
|
|
return count, nil
|
2016-07-21 02:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 14:07:17 +00:00
|
|
|
// CheckServiceTasksInStateWithError returns the number of tasks with a matching state,
|
|
|
|
// and optional message substring.
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckServiceTasksInStateWithError(service string, state swarm.TaskState, errorMessage string) func(*check.C) (interface{}, check.CommentInterface) {
|
2017-08-07 14:07:17 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
tasks := d.GetServiceTasks(c, service)
|
|
|
|
var count int
|
|
|
|
for _, task := range tasks {
|
|
|
|
if task.Status.State == state {
|
|
|
|
if errorMessage == "" || strings.Contains(task.Status.Err, errorMessage) {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 21:56:32 +00:00
|
|
|
// CheckServiceRunningTasks returns the number of running tasks for the specified service
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckServiceRunningTasks(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
2016-12-15 21:56:32 +00:00
|
|
|
return d.CheckServiceTasksInState(service, swarm.TaskStateRunning, "")
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckServiceUpdateState returns the current update state for the specified service
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckServiceUpdateState(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
2016-09-02 21:12:05 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 09:17:53 +00:00
|
|
|
service := d.GetService(c, service)
|
2016-12-01 22:08:06 +00:00
|
|
|
if service.UpdateStatus == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
2016-09-02 21:12:05 +00:00
|
|
|
return service.UpdateStatus.State, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:07:01 +00:00
|
|
|
// CheckPluginRunning returns the runtime state of the plugin
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckPluginRunning(plugin string) func(c *check.C) (interface{}, check.CommentInterface) {
|
2017-06-07 17:07:01 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2018-12-22 14:53:02 +00:00
|
|
|
apiclient := d.NewClientT(c)
|
2017-09-19 20:12:29 +00:00
|
|
|
resp, _, err := apiclient.PluginInspectWithRaw(context.Background(), plugin)
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return false, check.Commentf("%v", err)
|
2017-06-07 17:07:01 +00:00
|
|
|
}
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(c, err)
|
2017-09-19 20:12:29 +00:00
|
|
|
return resp.Enabled, check.Commentf("%+v", resp)
|
2017-06-07 17:07:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckPluginImage returns the runtime state of the plugin
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckPluginImage(plugin string) func(c *check.C) (interface{}, check.CommentInterface) {
|
2017-06-07 17:07:01 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2018-12-22 14:53:02 +00:00
|
|
|
apiclient := d.NewClientT(c)
|
2017-09-19 20:12:29 +00:00
|
|
|
resp, _, err := apiclient.PluginInspectWithRaw(context.Background(), plugin)
|
|
|
|
if client.IsErrNotFound(err) {
|
|
|
|
return false, check.Commentf("%v", err)
|
2017-06-07 17:07:01 +00:00
|
|
|
}
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(c, err)
|
2017-09-19 20:12:29 +00:00
|
|
|
return resp.PluginReference, check.Commentf("%+v", resp)
|
2017-06-07 17:07:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckServiceTasks returns the number of tasks for the specified service
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckServiceTasks(service string) func(*check.C) (interface{}, check.CommentInterface) {
|
2016-09-02 21:12:05 +00:00
|
|
|
return func(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 09:17:53 +00:00
|
|
|
tasks := d.GetServiceTasks(c, service)
|
2016-07-21 02:16:19 +00:00
|
|
|
return len(tasks), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-24 00:51:57 +00:00
|
|
|
// CheckRunningTaskNetworks returns the number of times each network is referenced from a task.
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckRunningTaskNetworks(c *check.C) (interface{}, check.CommentInterface) {
|
2018-12-22 14:53:02 +00:00
|
|
|
cli := d.NewClientT(c)
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
2017-03-24 00:51:57 +00:00
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2017-03-24 00:51:57 +00:00
|
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
|
for _, task := range tasks {
|
|
|
|
for _, network := range task.Spec.Networks {
|
|
|
|
result[network.Target]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckRunningTaskImages returns the times each image is running as a task.
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) {
|
2018-12-22 14:53:02 +00:00
|
|
|
cli := d.NewClientT(c)
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
2016-07-12 00:15:30 +00:00
|
|
|
|
|
|
|
filterArgs := filters.NewArgs()
|
|
|
|
filterArgs.Add("desired-state", "running")
|
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
options := types.TaskListOptions{
|
|
|
|
Filters: filterArgs,
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := cli.TaskList(context.Background(), options)
|
|
|
|
c.Assert(err, checker.IsNil)
|
2016-07-12 00:15:30 +00:00
|
|
|
|
|
|
|
result := make(map[string]int)
|
|
|
|
for _, task := range tasks {
|
2017-06-07 17:07:01 +00:00
|
|
|
if task.Status.State == swarm.TaskStateRunning && task.Spec.ContainerSpec != nil {
|
2016-07-12 00:15:30 +00:00
|
|
|
result[task.Spec.ContainerSpec.Image]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckNodeReadyCount returns the number of ready node on the swarm
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckNodeReadyCount(c *check.C) (interface{}, check.CommentInterface) {
|
2016-12-09 09:17:53 +00:00
|
|
|
nodes := d.ListNodes(c)
|
2016-07-12 00:15:30 +00:00
|
|
|
var readyCount int
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.Status.State == swarm.NodeStateReady {
|
|
|
|
readyCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return readyCount, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckLocalNodeState returns the current swarm node state
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckLocalNodeState(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
info := d.SwarmInfo(c)
|
2016-07-08 21:17:32 +00:00
|
|
|
return info.LocalNodeState, nil
|
|
|
|
}
|
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckControlAvailable returns the current swarm control available
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckControlAvailable(c *check.C) (interface{}, check.CommentInterface) {
|
|
|
|
info := d.SwarmInfo(c)
|
2016-07-08 21:17:32 +00:00
|
|
|
c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive)
|
|
|
|
return info.ControlAvailable, nil
|
|
|
|
}
|
2016-07-27 01:32:56 +00:00
|
|
|
|
2016-12-09 09:17:53 +00:00
|
|
|
// CheckLeader returns whether there is a leader on the swarm or not
|
2018-04-11 10:10:17 +00:00
|
|
|
func (d *Daemon) CheckLeader(c *check.C) (interface{}, check.CommentInterface) {
|
2018-12-22 14:53:02 +00:00
|
|
|
cli := d.NewClientT(c)
|
2017-05-24 03:56:26 +00:00
|
|
|
defer cli.Close()
|
|
|
|
|
2016-07-27 01:32:56 +00:00
|
|
|
errList := check.Commentf("could not get node list")
|
|
|
|
|
2017-05-24 03:56:26 +00:00
|
|
|
ls, err := cli.NodeList(context.Background(), types.NodeListOptions{})
|
|
|
|
if err != nil {
|
2016-07-27 01:32:56 +00:00
|
|
|
return err, errList
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, node := range ls {
|
|
|
|
if node.ManagerStatus != nil && node.ManagerStatus.Leader {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("no leader"), check.Commentf("could not find leader")
|
|
|
|
}
|