github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/osutil/disk_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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 osutil_test
    21  
    22  import (
    23  	"os"
    24  	"syscall"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/osutil"
    29  )
    30  
    31  type diskSuite struct{}
    32  
    33  var _ = Suite(&diskSuite{})
    34  
    35  func (s *diskSuite) TestCheckFreeSpaceHappy(c *C) {
    36  	var called bool
    37  	restore := osutil.MockSyscallStatfs(func(path string, st *syscall.Statfs_t) error {
    38  		c.Assert(path, Equals, "/path")
    39  		st.Bsize = 4096
    40  		st.Bavail = 2
    41  		called = true
    42  		return nil
    43  	})
    44  	defer restore()
    45  
    46  	c.Assert(osutil.CheckFreeSpace("/path", 8191), IsNil)
    47  	c.Assert(called, Equals, true)
    48  }
    49  
    50  func (s *diskSuite) TestCheckFreeSpaceUnhappy(c *C) {
    51  	restore := osutil.MockSyscallStatfs(func(path string, st *syscall.Statfs_t) error {
    52  		c.Assert(path, Equals, "/path")
    53  		st.Bsize = 4096
    54  		st.Bavail = 2
    55  		return nil
    56  	})
    57  	defer restore()
    58  
    59  	err := osutil.CheckFreeSpace("/path", 8193)
    60  	c.Assert(err, ErrorMatches, `insufficient space in "/path", at least 1B more is required`)
    61  	diskSpaceErr, ok := err.(*osutil.NotEnoughDiskSpaceError)
    62  	c.Assert(ok, Equals, true)
    63  	c.Check(diskSpaceErr.Path, Equals, "/path")
    64  	c.Check(diskSpaceErr.Delta, Equals, int64(1))
    65  }
    66  
    67  func (s *diskSuite) TestCheckFreeSpacePathError(c *C) {
    68  	err := osutil.CheckFreeSpace("/does/not/exist/path", 8193)
    69  	c.Assert(os.IsNotExist(err), Equals, true)
    70  }