Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: sort objects to solve out-of-order output #2862

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cli/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"sort"
"strings"

"github.com/alibaba/pouch/apis/types"
Expand Down Expand Up @@ -354,6 +355,10 @@ func (n *NetworkListCommand) runNetworkList(args []string) error {
return err
}

sort.Slice(respNetworkResource, func(i, j int) bool {
return respNetworkResource[i].Name < respNetworkResource[j].Name
})

display := n.cli.NewTableDisplay()
display.AddRow([]string{"NETWORK ID", "NAME", "DRIVER", "SCOPE"})
for _, network := range respNetworkResource {
Expand Down
5 changes: 5 additions & 0 deletions cli/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"sort"
"strings"

"github.com/alibaba/pouch/apis/filters"
Expand Down Expand Up @@ -336,6 +337,10 @@ func (v *VolumeListCommand) runVolumeList(args []string) error {
return fmt.Errorf("Conflicting options: --size (or --mountpoint) and -q")
}

sort.Slice(volumeList.Volumes, func(i, j int) bool {
return volumeList.Volumes[i].Name < volumeList.Volumes[j].Name
})

display := v.cli.NewTableDisplay()
displayHead := []string{"VOLUME NAME"}

Expand Down
34 changes: 34 additions & 0 deletions test/cli_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,3 +605,37 @@ func (suite *PouchNetworkSuite) TestNetworkConnectWithRestart(c *check.C) {

c.Assert(found, check.Equals, false)
}

func (suite *PouchNetworkSuite) TestNetworkList(c *check.C) {
// start the test pouch daemon
dcfg, err := StartDefaultDaemonDebug()
if err != nil {
c.Skip("daemon start failed.")
}
defer dcfg.KillDaemon()

// list pouch network
ret := RunWithSpecifiedDaemon(dcfg, "network", "ls").Assert(c, icmd.Success)
expect := []string{"bridge", "host", "none"}

actual := networkNamesToSlice(ret.Stdout())
for i := 0; i < len(actual); i++ {
c.Assert(actual[i], check.Equals, expect[i])
}
}

// networkNamesToSlice parses networks' name to slice.
func networkNamesToSlice(volumes string) []string {
lines := strings.Split(volumes, "\n")[1:]

res := make([]string, 0)
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}

items := strings.Fields(line)
res = append(res, items[1])
}
return res
}
99 changes: 59 additions & 40 deletions test/cli_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,17 @@ func (suite *PouchVolumeSuite) TestVolumeBindReplaceMode(c *check.C) {

// TestVolumeList tests the volume list.
func (suite *PouchVolumeSuite) TestVolumeList(c *check.C) {
funcname := "TestVolumeList"

volumeName := "volume_" + funcname
volumeName1 := volumeName + "_1"
command.PouchRun("volume", "create", "--name", volumeName1, "-o", "opt.size=1g").Assert(c, icmd.Success)
defer command.PouchRun("volume", "rm", volumeName1)

volumeName2 := volumeName + "_2"
command.PouchRun("volume", "create", "--name", volumeName2, "-o", "opt.size=2g").Assert(c, icmd.Success)
defer command.PouchRun("volume", "rm", volumeName2)

volumeName3 := volumeName + "_3"
command.PouchRun("volume", "create", "--name", volumeName3, "-o", "opt.size=3g").Assert(c, icmd.Success)
defer command.PouchRun("volume", "rm", volumeName3)
var volumeName string
expectedVolumeNames := make([]string, 0)

for i := 1; i <= 3; i++ {
volumeName = fmt.Sprintf("volume_TestVolumeList_%d", i)
expectedVolumeNames = append(expectedVolumeNames, volumeName)
command.PouchRun("volume", "create", "--name", volumeName, "-o", fmt.Sprintf("opt.size=%dg", i)).Assert(c, icmd.Success)
defer func(volumeName string) {
command.PouchRun("volume", "rm", volumeName)
}(volumeName)
}

ret := command.PouchRun("volume", "list")
ret.Assert(c, icmd.Success)
Expand All @@ -226,24 +223,26 @@ func (suite *PouchVolumeSuite) TestVolumeList(c *check.C) {
for _, line := range lines {
c.Assert(line[0], check.Equals, "local")
}

names := volumeNamesToSlice(ret.Stdout())
for i, name := range names {
c.Assert(name, check.Equals, expectedVolumeNames[i])
}
}

// TestVolumeListOptions tests the volume list with options: size, mountpoint, quiet.
func (suite *PouchVolumeSuite) TestVolumeListOptions(c *check.C) {
funcname := "TestVolumeListOptions"

volumeName := "volume_" + funcname
volumeName1 := volumeName + "_1"
command.PouchRun("volume", "create", "--name", volumeName1, "-o", "opt.size=1g").Assert(c, icmd.Success)
defer command.PouchRun("volume", "rm", volumeName1)

volumeName2 := volumeName + "_2"
command.PouchRun("volume", "create", "--name", volumeName2, "-o", "opt.size=2g").Assert(c, icmd.Success)
defer command.PouchRun("volume", "rm", volumeName2)

volumeName3 := volumeName + "_3"
command.PouchRun("volume", "create", "--name", volumeName3, "-o", "opt.size=3g").Assert(c, icmd.Success)
defer command.PouchRun("volume", "rm", volumeName3)
var volumeName string
expectedVolumeNames := make([]string, 0)

for i := 1; i <= 3; i++ {
volumeName = fmt.Sprintf("volume_TestVolumeListOptions_%d", i)
expectedVolumeNames = append(expectedVolumeNames, volumeName)
command.PouchRun("volume", "create", "--name", volumeName, "-o", fmt.Sprintf("opt.size=%dg", i)).Assert(c, icmd.Success)
defer func(volumeName string) {
command.PouchRun("volume", "rm", volumeName)
}(volumeName)
}

// test --size and --mountpoint options
ret := command.PouchRun("volume", "list", "--size", "--mountpoint")
Expand All @@ -264,22 +263,21 @@ func (suite *PouchVolumeSuite) TestVolumeListOptions(c *check.C) {
lines = volumesToKV(ret.Stdout())
for _, line := range lines {
c.Assert(len(line), check.Equals, 1)
if !strings.EqualFold(line[0], volumeName1) &&
!strings.EqualFold(line[0], volumeName2) &&
!strings.EqualFold(line[0], volumeName3) {
c.Errorf("list volume doesn't match any existing volume name, line: %s", line)
break
}
}

names := volumeNamesToSlice(ret.Stdout())
for i, name := range names {
c.Assert(name, check.Equals, expectedVolumeNames[i])
}

// test filter options
volumeName4 := "volume_" + funcname + "4"
command.PouchRun("volume", "create", "--name", volumeName4, "--driver", "tmpfs", "--label", "test=foo").Assert(c, icmd.Success)
defer command.PouchRun("volume", "rm", volumeName4)
volumeName = "volume_TestVolumeListOptions_4"
command.PouchRun("volume", "create", "--name", volumeName, "--driver", "tmpfs", "--label", "test=foo").Assert(c, icmd.Success)
defer command.PouchRun("volume", "rm", volumeName)

// test name, label, driver filter separately
filterArgs := []string{
"name=" + volumeName4,
"name=" + volumeName,
"label=test",
"driver=tmpfs",
}
Expand All @@ -290,7 +288,7 @@ func (suite *PouchVolumeSuite) TestVolumeListOptions(c *check.C) {

lines := volumesToKV(res.Stdout())
c.Assert(len(lines), check.Equals, 1)
if _, exist := lines[volumeName4]; !exist {
if _, exist := lines[volumeName]; !exist {
c.Errorf("volume filter options doesn't work, filter : ", args)
}
}
Expand All @@ -301,7 +299,7 @@ func (suite *PouchVolumeSuite) TestVolumeListOptions(c *check.C) {

lines = volumesToKV(res.Stdout())
c.Assert(len(lines), check.Equals, 1)
if _, exist := lines[volumeName4]; !exist {
if _, exist := lines[volumeName]; !exist {
c.Error("volume filter options doesn't work, with all filters")
}
}
Expand All @@ -327,3 +325,24 @@ func volumesToKV(volumes string) map[string][]string {
}
return res
}

// volumeNamesToSlice parses volumes' name to slice.
func volumeNamesToSlice(volumes string) []string {
lines := strings.Split(volumes, "\n")[1:]

res := make([]string, 0)
for _, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}

items := strings.Fields(line)
if len(items) > 1 {
res = append(res, items[1])
} else {
// --quiet case
res = append(res, items[0])
}
}
return res
}