github.com/coreos/mantle@v0.13.0/kola/tests/misc/nfs.go (about)

     1  // Copyright 2015 CoreOS, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package misc
    16  
    17  import (
    18  	"fmt"
    19  	"path"
    20  	"time"
    21  
    22  	"github.com/coreos/mantle/kola/cluster"
    23  	"github.com/coreos/mantle/kola/register"
    24  	"github.com/coreos/mantle/platform/conf"
    25  	"github.com/coreos/mantle/util"
    26  )
    27  
    28  var (
    29  	nfsserverconf = conf.ContainerLinuxConfig(`storage:
    30    files:
    31      - filesystem: "root"
    32        path: "/etc/hostname"
    33        contents:
    34          inline: "nfs1"
    35        mode: 0644
    36      - filesystem: "root"
    37        path: "/etc/exports"
    38        contents:
    39          inline: "/tmp  *(ro,insecure,all_squash,no_subtree_check,fsid=0)"
    40        mode: 0644
    41      - filesystem: "root"
    42        path: "/var/lib/nfs/etab"
    43        mode: 0644
    44  systemd:
    45    units:
    46      - name: "nfs-server.service"
    47        enabled: true`)
    48  )
    49  
    50  func init() {
    51  	register.Register(&register.Test{
    52  		Run:         NFSv3,
    53  		ClusterSize: 0,
    54  		Name:        "linux.nfs.v3",
    55  		Distros:     []string{"cl"},
    56  
    57  		// Disabled on Azure because setting hostname
    58  		// is required at the instance creation level
    59  		// qemu-unpriv machines cannot communicate
    60  		ExcludePlatforms: []string{"azure", "qemu-unpriv"},
    61  	})
    62  	// TODO: enable FCOS when FCCT exists
    63  	register.Register(&register.Test{
    64  		Run:            NFSv4,
    65  		ClusterSize:    0,
    66  		Name:           "linux.nfs.v4",
    67  		ExcludeDistros: []string{"fcos"},
    68  
    69  		// Disabled on Azure because setting hostname
    70  		// is required at the instance creation level
    71  		// qemu-unpriv machines cannot communicate
    72  		ExcludePlatforms: []string{"azure", "qemu-unpriv"},
    73  	})
    74  }
    75  
    76  func testNFS(c cluster.TestCluster, nfsversion int, remotePath string) {
    77  	m1, err := c.NewMachine(nfsserverconf)
    78  	if err != nil {
    79  		c.Fatalf("Cluster.NewMachine: %s", err)
    80  	}
    81  
    82  	defer m1.Destroy()
    83  
    84  	c.Log("NFS server booted.")
    85  
    86  	/* poke a file in /tmp */
    87  	tmp := c.MustSSH(m1, "mktemp")
    88  
    89  	c.Logf("Test file %q created on server.", tmp)
    90  
    91  	nfstype := "nfs"
    92  	if nfsversion == 4 {
    93  		nfstype = "nfs4"
    94  	}
    95  
    96  	c2 := conf.ContainerLinuxConfig(fmt.Sprintf(`storage:
    97    files:
    98      - filesystem: "root"
    99        path: "/etc/hostname"
   100        contents:
   101          inline: "nfs2"
   102        mode: 0644
   103  systemd:
   104    units:
   105      - name: "var-mnt.mount"
   106        enabled: true
   107        contents: |-
   108          [Unit]
   109          Description=NFS Client
   110          After=network-online.target
   111          Requires=network-online.target
   112          After=rpc-statd.service
   113          Requires=rpc-statd.service
   114  
   115          [Mount]
   116          What=%s:%s
   117          Where=/var/mnt
   118          Type=%s
   119          Options=defaults,noexec,nfsvers=%d
   120  
   121          [Install]
   122          WantedBy=multi-user.target`, m1.PrivateIP(), remotePath, nfstype, nfsversion))
   123  
   124  	m2, err := c.NewMachine(c2)
   125  	if err != nil {
   126  		c.Fatalf("Cluster.NewMachine: %s", err)
   127  	}
   128  
   129  	defer m2.Destroy()
   130  
   131  	c.Log("NFS client booted.")
   132  
   133  	checkmount := func() error {
   134  		status, err := c.SSH(m2, "systemctl is-active var-mnt.mount")
   135  		if err != nil || string(status) != "active" {
   136  			return fmt.Errorf("var-mnt.mount status is %q: %v", status, err)
   137  		}
   138  
   139  		c.Log("Got NFS mount.")
   140  		return nil
   141  	}
   142  
   143  	if err = util.Retry(10, 3*time.Second, checkmount); err != nil {
   144  		c.Fatal(err)
   145  	}
   146  
   147  	c.MustSSH(m2, fmt.Sprintf("stat /var/mnt/%s", path.Base(string(tmp))))
   148  }
   149  
   150  // Test that the kernel NFS server and client work within CoreOS.
   151  func NFSv3(c cluster.TestCluster) {
   152  	testNFS(c, 3, "/tmp")
   153  }
   154  
   155  // Test that NFSv4 without security works.
   156  func NFSv4(c cluster.TestCluster) {
   157  	testNFS(c, 4, "/")
   158  }