[lxc-devel] [lxd/master] Fix crash when sorting images with custom columns
albertodonato on Github
lxc-bot at linuxcontainers.org
Thu Jun 15 08:07:05 UTC 2017
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 445 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170615/a8abc93b/attachment.bin>
-------------- next part --------------
From 4080e481f45ec14ec1b339664145f50af680ecb6 Mon Sep 17 00:00:00 2001
From: Alberto Donato <alberto.donato at canonical.com>
Date: Thu, 15 Jun 2017 10:05:17 +0200
Subject: [PATCH] Fix crash when sorting images with custom columns.
Signed-off-by: Alberto Donato <alberto.donato at canonical.com>
---
lxc/config.go | 2 +-
lxc/image.go | 4 ++--
lxc/utils.go | 28 +++++++++++-----------------
lxc/utils_test.go | 37 +++++++++++++++++++++++++++++++++++++
4 files changed, 51 insertions(+), 20 deletions(-)
create mode 100644 lxc/utils_test.go
diff --git a/lxc/config.go b/lxc/config.go
index 7be59aca8..884736828 100644
--- a/lxc/config.go
+++ b/lxc/config.go
@@ -310,7 +310,7 @@ func (c *configCmd) run(config *lxd.Config, args []string) error {
i18n.G("COMMON NAME"),
i18n.G("ISSUE DATE"),
i18n.G("EXPIRY DATE")})
- sort.Sort(SortImage(data))
+ sort.Sort(StringList(data))
table.AppendBulk(data)
table.Render()
diff --git a/lxc/image.go b/lxc/image.go
index 377cfaf63..bc2526124 100644
--- a/lxc/image.go
+++ b/lxc/image.go
@@ -744,7 +744,7 @@ func (c *imageCmd) showImages(images []api.Image, filters []string, columns []im
data = append(data, row)
}
- sort.Sort(SortImage(data))
+ sort.Sort(StringList(data))
return data
}
@@ -814,7 +814,7 @@ func (c *imageCmd) showAliases(aliases []api.ImageAliasesEntry, filters []string
i18n.G("ALIAS"),
i18n.G("FINGERPRINT"),
i18n.G("DESCRIPTION")})
- sort.Sort(SortImage(data))
+ sort.Sort(StringList(data))
table.AppendBulk(data)
table.Render()
diff --git a/lxc/utils.go b/lxc/utils.go
index 96d74d7a3..24e64a0ce 100644
--- a/lxc/utils.go
+++ b/lxc/utils.go
@@ -74,39 +74,33 @@ func (p *ProgressRenderer) UpdateOp(op api.Operation) {
}
}
-// Image fingerprint and alias sorting
-type SortImage [][]string
+type StringList [][]string
-func (a SortImage) Len() int {
+func (a StringList) Len() int {
return len(a)
}
-func (a SortImage) Swap(i, j int) {
+func (a StringList) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
-func (a SortImage) Less(i, j int) bool {
- if a[i][0] == a[j][0] {
- if a[i][3] == "" {
- return false
- }
-
- if a[j][3] == "" {
- return true
+func (a StringList) Less(i, j int) bool {
+ x := 0
+ for x, _ = range a[i] {
+ if a[i][x] != a[j][x] {
+ break
}
-
- return a[i][3] < a[j][3]
}
- if a[i][0] == "" {
+ if a[i][x] == "" {
return false
}
- if a[j][0] == "" {
+ if a[j][x] == "" {
return true
}
- return a[i][0] < a[j][0]
+ return a[i][x] < a[j][x]
}
// Container name sorting
diff --git a/lxc/utils_test.go b/lxc/utils_test.go
new file mode 100644
index 000000000..cd11ad829
--- /dev/null
+++ b/lxc/utils_test.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+ "sort"
+ "testing"
+
+ "github.com/stretchr/testify/suite"
+)
+
+type utilsTestSuite struct {
+ suite.Suite
+}
+
+func TestUtilsTestSuite(t *testing.T) {
+ suite.Run(t, new(utilsTestSuite))
+}
+
+// StringList can be used to sort a list of strings.
+func (s *utilsTestSuite) Test_StringList() {
+ data := [][]string{{"foo", "bar"}, {"baz", "bza"}}
+ sort.Sort(StringList(data))
+ s.Equal([][]string{{"baz", "bza"}, {"foo", "bar"}}, data)
+}
+
+// The first different string is used in sorting.
+func (s *utilsTestSuite) Test_StringList_sort_by_column() {
+ data := [][]string{{"foo", "baz"}, {"foo", "bar"}}
+ sort.Sort(StringList(data))
+ s.Equal([][]string{{"foo", "bar"}, {"foo", "baz"}}, data)
+}
+
+// Empty strings are sorted last.
+func (s *utilsTestSuite) Test_StringList_empty_strings() {
+ data := [][]string{{"", "bar"}, {"foo", "baz"}}
+ sort.Sort(StringList(data))
+ s.Equal([][]string{{"foo", "baz"}, {"", "bar"}}, data)
+}
More information about the lxc-devel
mailing list