github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/nfs_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 nfsSuite struct{}
    29  
    30  var _ = Suite(&nfsSuite{})
    31  
    32  func (s *nfsSuite) TestIsHomeUsingNFS(c *C) {
    33  	cases := []struct {
    34  		mountinfo, fstab string
    35  		nfs              bool
    36  		errorPattern     string
    37  	}{{
    38  		// Errors from parsing mountinfo and fstab are propagated.
    39  		mountinfo:    "bad syntax",
    40  		errorPattern: "cannot parse mountinfo:.*, .*",
    41  	}, {
    42  		fstab:        "bad syntax",
    43  		errorPattern: "cannot parse .*/fstab.*, .*",
    44  	}, {
    45  		// NFSv3 {tcp,udp} and NFSv4 currently mounted at /home/zyga/nfs are recognized.
    46  		mountinfo: "1074 28 0:59 / /home/zyga/nfs rw,relatime shared:342 - nfs localhost:/srv/nfs rw,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=127.0.0.1,mountvers=3,mountport=54125,mountproto=tcp,local_lock=none,addr=127.0.0.1",
    47  		nfs:       true,
    48  	}, {
    49  		mountinfo: "1074 28 0:59 / /home/zyga/nfs rw,relatime shared:342 - nfs localhost:/srv/nfs rw,vers=3,rsize=32768,wsize=32768,namlen=255,hard,proto=udp,timeo=11,retrans=3,sec=sys,mountaddr=127.0.0.1,mountvers=3,mountport=47875,mountproto=udp,local_lock=none,addr=127.0.0.1",
    50  		nfs:       true,
    51  	}, {
    52  		mountinfo: "680 27 0:59 / /home/zyga/nfs rw,relatime shared:478 - nfs4 localhost:/srv/nfs rw,vers=4.2,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=127.0.0.1,local_lock=none,addr=127.0.0.1",
    53  		nfs:       true,
    54  	}, {
    55  		// NFSv3 {tcp,udp} and NFSv4 currently mounted at /home/zyga/nfs are ignored (not in $HOME).
    56  		mountinfo: "1074 28 0:59 / /mnt/nfs rw,relatime shared:342 - nfs localhost:/srv/nfs rw,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=127.0.0.1,mountvers=3,mountport=54125,mountproto=tcp,local_lock=none,addr=127.0.0.1",
    57  	}, {
    58  		mountinfo: "1074 28 0:59 / /mnt/nfs rw,relatime shared:342 - nfs localhost:/srv/nfs rw,vers=3,rsize=32768,wsize=32768,namlen=255,hard,proto=udp,timeo=11,retrans=3,sec=sys,mountaddr=127.0.0.1,mountvers=3,mountport=47875,mountproto=udp,local_lock=none,addr=127.0.0.1",
    59  	}, {
    60  		mountinfo: "680 27 0:59 / /mnt/nfs rw,relatime shared:478 - nfs4 localhost:/srv/nfs rw,vers=4.2,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=127.0.0.1,local_lock=none,addr=127.0.0.1",
    61  	}, {
    62  		// NFS that may be mounted at /home and /home/zyga/nfs is recognized.
    63  		// Two spellings are possible, "nfs" and "nfs4" (they are equivalent
    64  		// nowadays).
    65  		fstab: "localhost:/srv/nfs /home nfs defaults 0 0",
    66  		nfs:   true,
    67  	}, {
    68  		fstab: "localhost:/srv/nfs /home nfs4 defaults 0 0",
    69  		nfs:   true,
    70  	}, {
    71  		fstab: "localhost:/srv/nfs /home/zyga/nfs nfs defaults 0 0",
    72  		nfs:   true,
    73  	}, {
    74  		fstab: "localhost:/srv/nfs /home/zyga/nfs nfs4 defaults 0 0",
    75  		nfs:   true,
    76  	}, {
    77  		// NFS that may be mounted at /mnt/nfs is ignored (not in $HOME).
    78  		fstab: "localhost:/srv/nfs /mnt/nfs nfs defaults 0 0",
    79  	}, {
    80  		// autofs that is mounted at /home.
    81  		mountinfo: "137 29 0:50 / /home rw,relatime shared:87 - autofs /etc/auto.master.d/home rw,fd=7,pgrp=22588,timeout=300,minproto=5,maxproto=5,indirect,pipe_ino=173399",
    82  		nfs:       true,
    83  	}}
    84  	for _, tc := range cases {
    85  		restore := osutil.MockMountInfo(tc.mountinfo)
    86  		defer restore()
    87  		restore = osutil.MockEtcFstab(tc.fstab)
    88  		defer restore()
    89  
    90  		nfs, err := osutil.IsHomeUsingNFS()
    91  		if tc.errorPattern != "" {
    92  			c.Assert(err, ErrorMatches, tc.errorPattern, Commentf("test case %#v", tc))
    93  		} else {
    94  			c.Assert(err, IsNil)
    95  		}
    96  		c.Assert(nfs, Equals, tc.nfs)
    97  	}
    98  }