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