github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/integration-cli/daemon/daemon_swarm.go (about) 1 package daemon // import "github.com/docker/docker/integration-cli/daemon" 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/docker/docker/api/types" 8 "github.com/docker/docker/api/types/filters" 9 "github.com/docker/docker/api/types/swarm" 10 "github.com/docker/docker/client" 11 "github.com/docker/docker/integration-cli/checker" 12 "github.com/go-check/check" 13 "github.com/gotestyourself/gotestyourself/assert" 14 "golang.org/x/net/context" 15 ) 16 17 // CheckServiceTasksInState returns the number of tasks with a matching state, 18 // and optional message substring. 19 func (d *Daemon) CheckServiceTasksInState(service string, state swarm.TaskState, message string) func(*check.C) (interface{}, check.CommentInterface) { 20 return func(c *check.C) (interface{}, check.CommentInterface) { 21 tasks := d.GetServiceTasks(c, service) 22 var count int 23 for _, task := range tasks { 24 if task.Status.State == state { 25 if message == "" || strings.Contains(task.Status.Message, message) { 26 count++ 27 } 28 } 29 } 30 return count, nil 31 } 32 } 33 34 // CheckServiceTasksInStateWithError returns the number of tasks with a matching state, 35 // and optional message substring. 36 func (d *Daemon) CheckServiceTasksInStateWithError(service string, state swarm.TaskState, errorMessage string) func(*check.C) (interface{}, check.CommentInterface) { 37 return func(c *check.C) (interface{}, check.CommentInterface) { 38 tasks := d.GetServiceTasks(c, service) 39 var count int 40 for _, task := range tasks { 41 if task.Status.State == state { 42 if errorMessage == "" || strings.Contains(task.Status.Err, errorMessage) { 43 count++ 44 } 45 } 46 } 47 return count, nil 48 } 49 } 50 51 // CheckServiceRunningTasks returns the number of running tasks for the specified service 52 func (d *Daemon) CheckServiceRunningTasks(service string) func(*check.C) (interface{}, check.CommentInterface) { 53 return d.CheckServiceTasksInState(service, swarm.TaskStateRunning, "") 54 } 55 56 // CheckServiceUpdateState returns the current update state for the specified service 57 func (d *Daemon) CheckServiceUpdateState(service string) func(*check.C) (interface{}, check.CommentInterface) { 58 return func(c *check.C) (interface{}, check.CommentInterface) { 59 service := d.GetService(c, service) 60 if service.UpdateStatus == nil { 61 return "", nil 62 } 63 return service.UpdateStatus.State, nil 64 } 65 } 66 67 // CheckPluginRunning returns the runtime state of the plugin 68 func (d *Daemon) CheckPluginRunning(plugin string) func(c *check.C) (interface{}, check.CommentInterface) { 69 return func(c *check.C) (interface{}, check.CommentInterface) { 70 apiclient, err := d.NewClient() 71 assert.NilError(c, err) 72 resp, _, err := apiclient.PluginInspectWithRaw(context.Background(), plugin) 73 if client.IsErrNotFound(err) { 74 return false, check.Commentf("%v", err) 75 } 76 assert.NilError(c, err) 77 return resp.Enabled, check.Commentf("%+v", resp) 78 } 79 } 80 81 // CheckPluginImage returns the runtime state of the plugin 82 func (d *Daemon) CheckPluginImage(plugin string) func(c *check.C) (interface{}, check.CommentInterface) { 83 return func(c *check.C) (interface{}, check.CommentInterface) { 84 apiclient, err := d.NewClient() 85 assert.NilError(c, err) 86 resp, _, err := apiclient.PluginInspectWithRaw(context.Background(), plugin) 87 if client.IsErrNotFound(err) { 88 return false, check.Commentf("%v", err) 89 } 90 assert.NilError(c, err) 91 return resp.PluginReference, check.Commentf("%+v", resp) 92 } 93 } 94 95 // CheckServiceTasks returns the number of tasks for the specified service 96 func (d *Daemon) CheckServiceTasks(service string) func(*check.C) (interface{}, check.CommentInterface) { 97 return func(c *check.C) (interface{}, check.CommentInterface) { 98 tasks := d.GetServiceTasks(c, service) 99 return len(tasks), nil 100 } 101 } 102 103 // CheckRunningTaskNetworks returns the number of times each network is referenced from a task. 104 func (d *Daemon) CheckRunningTaskNetworks(c *check.C) (interface{}, check.CommentInterface) { 105 cli, err := d.NewClient() 106 c.Assert(err, checker.IsNil) 107 defer cli.Close() 108 109 filterArgs := filters.NewArgs() 110 filterArgs.Add("desired-state", "running") 111 112 options := types.TaskListOptions{ 113 Filters: filterArgs, 114 } 115 116 tasks, err := cli.TaskList(context.Background(), options) 117 c.Assert(err, checker.IsNil) 118 119 result := make(map[string]int) 120 for _, task := range tasks { 121 for _, network := range task.Spec.Networks { 122 result[network.Target]++ 123 } 124 } 125 return result, nil 126 } 127 128 // CheckRunningTaskImages returns the times each image is running as a task. 129 func (d *Daemon) CheckRunningTaskImages(c *check.C) (interface{}, check.CommentInterface) { 130 cli, err := d.NewClient() 131 c.Assert(err, checker.IsNil) 132 defer cli.Close() 133 134 filterArgs := filters.NewArgs() 135 filterArgs.Add("desired-state", "running") 136 137 options := types.TaskListOptions{ 138 Filters: filterArgs, 139 } 140 141 tasks, err := cli.TaskList(context.Background(), options) 142 c.Assert(err, checker.IsNil) 143 144 result := make(map[string]int) 145 for _, task := range tasks { 146 if task.Status.State == swarm.TaskStateRunning && task.Spec.ContainerSpec != nil { 147 result[task.Spec.ContainerSpec.Image]++ 148 } 149 } 150 return result, nil 151 } 152 153 // CheckNodeReadyCount returns the number of ready node on the swarm 154 func (d *Daemon) CheckNodeReadyCount(c *check.C) (interface{}, check.CommentInterface) { 155 nodes := d.ListNodes(c) 156 var readyCount int 157 for _, node := range nodes { 158 if node.Status.State == swarm.NodeStateReady { 159 readyCount++ 160 } 161 } 162 return readyCount, nil 163 } 164 165 // CheckLocalNodeState returns the current swarm node state 166 func (d *Daemon) CheckLocalNodeState(c *check.C) (interface{}, check.CommentInterface) { 167 info := d.SwarmInfo(c) 168 return info.LocalNodeState, nil 169 } 170 171 // CheckControlAvailable returns the current swarm control available 172 func (d *Daemon) CheckControlAvailable(c *check.C) (interface{}, check.CommentInterface) { 173 info := d.SwarmInfo(c) 174 c.Assert(info.LocalNodeState, checker.Equals, swarm.LocalNodeStateActive) 175 return info.ControlAvailable, nil 176 } 177 178 // CheckLeader returns whether there is a leader on the swarm or not 179 func (d *Daemon) CheckLeader(c *check.C) (interface{}, check.CommentInterface) { 180 cli, err := d.NewClient() 181 c.Assert(err, checker.IsNil) 182 defer cli.Close() 183 184 errList := check.Commentf("could not get node list") 185 186 ls, err := cli.NodeList(context.Background(), types.NodeListOptions{}) 187 if err != nil { 188 return err, errList 189 } 190 191 for _, node := range ls { 192 if node.ManagerStatus != nil && node.ManagerStatus.Leader { 193 return nil, nil 194 } 195 } 196 return fmt.Errorf("no leader"), check.Commentf("could not find leader") 197 }