From 60005a7975c0248443ac915d60264e96d86a2c73 Mon Sep 17 00:00:00 2001 From: yeya24 Date: Fri, 24 May 2019 01:30:30 +0800 Subject: [PATCH] enhancement/sort-list Signed-off-by: yeya24 add testcase for volume listing Signed-off-by: yeya24 --- cli/network.go | 5 ++ cli/volume.go | 5 ++ test/cli_network_test.go | 34 ++++++++++++++ test/cli_volume_test.go | 99 ++++++++++++++++++++++++---------------- 4 files changed, 103 insertions(+), 40 deletions(-) diff --git a/cli/network.go b/cli/network.go index c5405a4a7..43e27481f 100644 --- a/cli/network.go +++ b/cli/network.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "sort" "strings" "github.com/alibaba/pouch/apis/types" @@ -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 { diff --git a/cli/volume.go b/cli/volume.go index 914104168..94d2eb083 100644 --- a/cli/volume.go +++ b/cli/volume.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "sort" "strings" "github.com/alibaba/pouch/apis/filters" @@ -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"} diff --git a/test/cli_network_test.go b/test/cli_network_test.go index 9be7699df..fcdc7f5bc 100644 --- a/test/cli_network_test.go +++ b/test/cli_network_test.go @@ -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 +} diff --git a/test/cli_volume_test.go b/test/cli_volume_test.go index 52aca616a..188652eb4 100644 --- a/test/cli_volume_test.go +++ b/test/cli_volume_test.go @@ -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) @@ -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") @@ -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", } @@ -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) } } @@ -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") } } @@ -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 +}