github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap-update-ns/sorting_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package main 21 22 import ( 23 "sort" 24 25 . "gopkg.in/check.v1" 26 27 "github.com/snapcore/snapd/osutil" 28 ) 29 30 type sortSuite struct{} 31 32 var _ = Suite(&sortSuite{}) 33 34 func (s *sortSuite) TestTrailingSlashesComparison(c *C) { 35 // Naively sorted entries. 36 entries := []osutil.MountEntry{ 37 {Dir: "/a/b"}, 38 {Dir: "/a/b-1"}, 39 {Dir: "/a/b-1/3"}, 40 {Dir: "/a/b/c"}, 41 } 42 sort.Sort(byOriginAndMagicDir(entries)) 43 // Entries sorted as if they had a trailing slash. 44 c.Assert(entries, DeepEquals, []osutil.MountEntry{ 45 {Dir: "/a/b-1"}, 46 {Dir: "/a/b-1/3"}, 47 {Dir: "/a/b"}, 48 {Dir: "/a/b/c"}, 49 }) 50 } 51 52 func (s *sortSuite) TestParallelInstancesAndSimple(c *C) { 53 // Naively sorted entries. 54 entries := []osutil.MountEntry{ 55 {Dir: "/a/b"}, 56 {Dir: "/a/b-1"}, 57 {Dir: "/snap/bar", Options: []string{osutil.XSnapdOriginOvername()}}, 58 {Dir: "/a/b-1/3"}, 59 {Dir: "/foo/bar", Options: []string{osutil.XSnapdOriginOvername()}}, 60 {Dir: "/a/b/c"}, 61 } 62 sort.Sort(byOriginAndMagicDir(entries)) 63 // Entries sorted as if they had a trailing slash. 64 c.Assert(entries, DeepEquals, []osutil.MountEntry{ 65 {Dir: "/foo/bar", Options: []string{osutil.XSnapdOriginOvername()}}, 66 {Dir: "/snap/bar", Options: []string{osutil.XSnapdOriginOvername()}}, 67 {Dir: "/a/b-1"}, 68 {Dir: "/a/b-1/3"}, 69 {Dir: "/a/b"}, 70 {Dir: "/a/b/c"}, 71 }) 72 }