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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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  	. "gopkg.in/check.v1"
    24  
    25  	"github.com/snapcore/snapd/osutil"
    26  )
    27  
    28  type mountSuite struct{}
    29  
    30  var _ = Suite(&mountSuite{})
    31  
    32  func (s *mountSuite) TestIsMountedHappyish(c *C) {
    33  	// note the different optional fields
    34  	const content = "" +
    35  		"44 24 7:1 / /snap/ubuntu-core/855 rw,relatime shared:27 - squashfs /dev/loop1 ro\n" +
    36  		"44 24 7:1 / /snap/something/123 rw,relatime - squashfs /dev/loop2 ro\n" +
    37  		"44 24 7:1 / /snap/random/456 rw,relatime opt:1 shared:27 - squashfs /dev/loop1 ro\n"
    38  	restore := osutil.MockMountInfo(content)
    39  	defer restore()
    40  
    41  	mounted, err := osutil.IsMounted("/snap/ubuntu-core/855")
    42  	c.Check(err, IsNil)
    43  	c.Check(mounted, Equals, true)
    44  
    45  	mounted, err = osutil.IsMounted("/snap/something/123")
    46  	c.Check(err, IsNil)
    47  	c.Check(mounted, Equals, true)
    48  
    49  	mounted, err = osutil.IsMounted("/snap/random/456")
    50  	c.Check(err, IsNil)
    51  	c.Check(mounted, Equals, true)
    52  
    53  	mounted, err = osutil.IsMounted("/random/made/up/name")
    54  	c.Check(err, IsNil)
    55  	c.Check(mounted, Equals, false)
    56  }
    57  
    58  func (s *mountSuite) TestIsMountedBroken(c *C) {
    59  	restore := osutil.MockMountInfo("44 24 7:1 ...truncated-stuff")
    60  	defer restore()
    61  
    62  	mounted, err := osutil.IsMounted("/snap/ubuntu-core/855")
    63  	c.Check(err, ErrorMatches, "incorrect number of fields, .*")
    64  	c.Check(mounted, Equals, false)
    65  }