github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/overlord/snapstate/backend/snapdata_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2018 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 backend_test 21 22 import ( 23 "os" 24 "path/filepath" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/dirs" 29 "github.com/snapcore/snapd/osutil" 30 "github.com/snapcore/snapd/snap" 31 "github.com/snapcore/snapd/snap/snaptest" 32 33 "github.com/snapcore/snapd/overlord/snapstate/backend" 34 ) 35 36 type snapdataSuite struct { 37 be backend.Backend 38 tempdir string 39 } 40 41 var _ = Suite(&snapdataSuite{}) 42 43 func (s *snapdataSuite) SetUpTest(c *C) { 44 s.tempdir = c.MkDir() 45 dirs.SetRootDir(s.tempdir) 46 } 47 48 func (s *snapdataSuite) TearDownTest(c *C) { 49 dirs.SetRootDir("") 50 } 51 52 func (s *snapdataSuite) TestRemoveSnapData(c *C) { 53 homedir := filepath.Join(s.tempdir, "home", "user1", "snap") 54 homeData := filepath.Join(homedir, "hello/10") 55 err := os.MkdirAll(homeData, 0755) 56 c.Assert(err, IsNil) 57 varData := filepath.Join(dirs.SnapDataDir, "hello/10") 58 err = os.MkdirAll(varData, 0755) 59 c.Assert(err, IsNil) 60 61 info := snaptest.MockSnap(c, helloYaml1, &snap.SideInfo{Revision: snap.R(10)}) 62 err = s.be.RemoveSnapData(info) 63 64 c.Assert(err, IsNil) 65 c.Assert(osutil.FileExists(homeData), Equals, false) 66 c.Assert(osutil.FileExists(filepath.Dir(homeData)), Equals, true) 67 c.Assert(osutil.FileExists(varData), Equals, false) 68 c.Assert(osutil.FileExists(filepath.Dir(varData)), Equals, true) 69 } 70 71 func (s *snapdataSuite) TestRemoveSnapCommonData(c *C) { 72 homedir := filepath.Join(s.tempdir, "home", "user1", "snap") 73 homeCommonData := filepath.Join(homedir, "hello/common") 74 err := os.MkdirAll(homeCommonData, 0755) 75 c.Assert(err, IsNil) 76 varCommonData := filepath.Join(dirs.SnapDataDir, "hello/common") 77 err = os.MkdirAll(varCommonData, 0755) 78 c.Assert(err, IsNil) 79 80 info := snaptest.MockSnap(c, helloYaml1, &snap.SideInfo{Revision: snap.R(10)}) 81 82 err = s.be.RemoveSnapCommonData(info) 83 c.Assert(err, IsNil) 84 c.Assert(osutil.FileExists(homeCommonData), Equals, false) 85 c.Assert(osutil.FileExists(filepath.Dir(homeCommonData)), Equals, true) 86 c.Assert(osutil.FileExists(varCommonData), Equals, false) 87 c.Assert(osutil.FileExists(filepath.Dir(varCommonData)), Equals, true) 88 } 89 90 func (s *snapdataSuite) TestRemoveSnapDataDir(c *C) { 91 varBaseData := filepath.Join(dirs.SnapDataDir, "hello") 92 err := os.MkdirAll(varBaseData, 0755) 93 c.Assert(err, IsNil) 94 varBaseDataInstance := filepath.Join(dirs.SnapDataDir, "hello_instance") 95 err = os.MkdirAll(varBaseDataInstance, 0755) 96 c.Assert(err, IsNil) 97 98 info := snaptest.MockSnap(c, helloYaml1, &snap.SideInfo{Revision: snap.R(10)}) 99 100 err = s.be.RemoveSnapDataDir(info, true) 101 c.Assert(err, IsNil) 102 c.Assert(osutil.FileExists(varBaseData), Equals, true) 103 c.Assert(osutil.FileExists(varBaseDataInstance), Equals, true) 104 105 // now with instance key 106 info.InstanceKey = "instance" 107 err = s.be.RemoveSnapDataDir(info, true) 108 c.Assert(err, IsNil) 109 // instance directory is gone 110 c.Assert(osutil.FileExists(varBaseDataInstance), Equals, false) 111 // but the snap-name one is still around 112 c.Assert(osutil.FileExists(varBaseData), Equals, true) 113 114 // back to no instance key 115 info.InstanceKey = "" 116 err = s.be.RemoveSnapDataDir(info, false) 117 c.Assert(err, IsNil) 118 // the snap-name directory is gone now too 119 c.Assert(osutil.FileExists(varBaseData), Equals, false) 120 }