From 820527be7e766240c1efeb87a1f7de4b5a2c06e7 Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Fri, 3 Apr 2015 14:39:55 -0700 Subject: [PATCH] Sort docker info by name Signed-off-by: Victor Vieux --- cluster/fakenode_test.go | 20 ++++++++++++++++++++ cluster/node.go | 20 ++++++++++++++++++++ cluster/node_test.go | 18 ++++++++++++++++++ cluster/swarm/cluster.go | 6 +++++- 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 cluster/fakenode_test.go create mode 100644 cluster/node_test.go diff --git a/cluster/fakenode_test.go b/cluster/fakenode_test.go new file mode 100644 index 0000000000..1098753f23 --- /dev/null +++ b/cluster/fakenode_test.go @@ -0,0 +1,20 @@ +package cluster + +type FakeNode struct { + name string +} + +func (fn *FakeNode) ID() string { return "" } +func (fn *FakeNode) Name() string { return fn.name } +func (fn *FakeNode) IP() string { return "" } +func (fn *FakeNode) Addr() string { return "" } +func (fn *FakeNode) Images() []*Image { return nil } +func (fn *FakeNode) Image(_ string) *Image { return nil } +func (fn *FakeNode) Containers() []*Container { return nil } +func (fn *FakeNode) Container(_ string) *Container { return nil } +func (fn *FakeNode) TotalCpus() int64 { return 0 } +func (fn *FakeNode) UsedCpus() int64 { return 0 } +func (fn *FakeNode) TotalMemory() int64 { return 0 } +func (fn *FakeNode) UsedMemory() int64 { return 0 } +func (fn *FakeNode) Labels() map[string]string { return nil } +func (fn *FakeNode) IsHealthy() bool { return true } diff --git a/cluster/node.go b/cluster/node.go index 1c596d12e9..a5c44b14f6 100644 --- a/cluster/node.go +++ b/cluster/node.go @@ -33,3 +33,23 @@ func SerializeNode(node Node) string { "Addr", node.Addr(), "Ip", node.IP()) } + +// NodeSorter implements the Sort interface to sort Cluster.Node. +// It is not guaranteed to be a stable sort. +type NodeSorter []Node + +// Len returns the number of nodes to be sorted. +func (s NodeSorter) Len() int { + return len(s) +} + +// Swap exchanges the node elements with indices i and j. +func (s NodeSorter) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +// Less reports whether the node with index i should sort before the node with index j. +// Nodes are sorted chronologically by name. +func (s NodeSorter) Less(i, j int) bool { + return s[i].Name() < s[j].Name() +} diff --git a/cluster/node_test.go b/cluster/node_test.go new file mode 100644 index 0000000000..d92c62d17b --- /dev/null +++ b/cluster/node_test.go @@ -0,0 +1,18 @@ +package cluster + +import ( + "sort" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNodeSorter(t *testing.T) { + nodes := []Node{&FakeNode{"name1"}, &FakeNode{"name3"}, &FakeNode{"name2"}} + + sort.Sort(NodeSorter(nodes)) + + assert.Equal(t, nodes[0].Name(), "name1") + assert.Equal(t, nodes[1].Name(), "name2") + assert.Equal(t, nodes[2].Name(), "name3") +} diff --git a/cluster/swarm/cluster.go b/cluster/swarm/cluster.go index 5d04edf045..b9f32d121a 100644 --- a/cluster/swarm/cluster.go +++ b/cluster/swarm/cluster.go @@ -2,6 +2,7 @@ package swarm import ( "fmt" + "sort" "sync" log "github.com/Sirupsen/logrus" @@ -274,7 +275,10 @@ func (c *Cluster) Info() [][2]string { {"\bNodes", fmt.Sprintf("%d", len(c.nodes))}, } - for _, node := range c.nodes { + nodes := c.listNodes() + sort.Sort(cluster.NodeSorter(nodes)) + + for _, node := range nodes { info = append(info, [2]string{node.Name(), node.Addr()}) info = append(info, [2]string{" └ Containers", fmt.Sprintf("%d", len(node.Containers()))}) info = append(info, [2]string{" └ Reserved CPUs", fmt.Sprintf("%d / %d", node.UsedCpus(), node.TotalCpus())})