github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/pkg/mount/mount_linux_test.go (about)

     1  // Copyright 2019 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package mount
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/u-root/u-root/pkg/testutil"
    16  )
    17  
    18  // Assumptions:
    19  //
    20  //   /dev/sda is ./testdata/1MB.ext4_vfat
    21  //	/dev/sda1 is ext4
    22  //	/dev/sda2 is vfat
    23  //
    24  //   /dev/sdb is ./testdata/12Kzeros
    25  //	/dev/sdb1 exists, but is not formatted.
    26  
    27  func TestTryMount(t *testing.T) {
    28  	testutil.SkipIfNotRoot(t)
    29  
    30  	d, err := ioutil.TempDir("", "test-")
    31  	if err != nil {
    32  		t.Fatal(err)
    33  	}
    34  	defer os.RemoveAll(d)
    35  
    36  	sda1 := filepath.Join(d, "sda1")
    37  	if mp, err := TryMount("/dev/sda1", sda1, ReadOnly); err != nil {
    38  		t.Errorf("TryMount(/dev/sda1) = %v, want nil", err)
    39  	} else {
    40  		want := &MountPoint{
    41  			Path:   sda1,
    42  			Device: "/dev/sda1",
    43  			FSType: "ext4",
    44  			Flags:  ReadOnly,
    45  		}
    46  		if !reflect.DeepEqual(mp, want) {
    47  			t.Errorf("TryMount(/dev/sda1) = %v, want %v", mp, want)
    48  		}
    49  
    50  		if err := mp.Unmount(0); err != nil {
    51  			t.Errorf("Unmount(%q) = %v, want nil", sda1, err)
    52  		}
    53  	}
    54  
    55  	sda2 := filepath.Join(d, "sda2")
    56  	if mp, err := TryMount("/dev/sda2", sda2, ReadOnly); err != nil {
    57  		t.Errorf("TryMount(/dev/sda2) = %v, want nil", err)
    58  	} else {
    59  		want := &MountPoint{
    60  			Path:   sda2,
    61  			Device: "/dev/sda2",
    62  			FSType: "vfat",
    63  			Flags:  ReadOnly,
    64  		}
    65  		if !reflect.DeepEqual(mp, want) {
    66  			t.Errorf("TryMount(/dev/sda2) = %v, want %v", mp, want)
    67  		}
    68  
    69  		if err := mp.Unmount(0); err != nil {
    70  			t.Errorf("Unmount(%q) = %v, want nil", sda1, err)
    71  		}
    72  	}
    73  
    74  	sdb1 := filepath.Join(d, "sdb1")
    75  	if _, err := TryMount("/dev/sdb1", sdb1, ReadOnly); !strings.Contains(err.Error(), "no suitable filesystem") {
    76  		t.Errorf("TryMount(/dev/sdb1) = %v, want an error containing 'no suitable filesystem'", err)
    77  	}
    78  
    79  	sdc1 := filepath.Join(d, "sdc1")
    80  	if _, err := TryMount("/dev/sdc1", sdc1, ReadOnly); !os.IsNotExist(err) {
    81  		t.Errorf("TryMount(/dev/sdc1) = %v, want an error equivalent to Does Not Exist", err)
    82  	}
    83  }