go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/mount/mount_test.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package mount_test
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  	"go.mondoo.com/cnquery/providers-sdk/v1/inventory"
    14  	"go.mondoo.com/cnquery/providers/os/connection/mock"
    15  	"go.mondoo.com/cnquery/providers/os/resources/mount"
    16  )
    17  
    18  func TestMountLinuxParser(t *testing.T) {
    19  	mock, err := mock.New("./testdata/debian.toml", &inventory.Asset{
    20  		Platform: &inventory.Platform{Family: []string{"linux"}},
    21  	})
    22  	require.NoError(t, err)
    23  
    24  	f, err := mock.RunCommand("mount")
    25  	require.NoError(t, err)
    26  
    27  	entries := mount.ParseLinuxMountCmd(f.Stdout)
    28  	assert.Equal(t, 25, len(entries))
    29  
    30  	// /dev/sda1 on / type ext4 (rw,relatime,data=ordered)
    31  	expected := &mount.MountPoint{
    32  		Device:     "/dev/sda1",
    33  		MountPoint: "/",
    34  		FSType:     "ext4",
    35  		Options: map[string]string{
    36  			"rw":       "",
    37  			"relatime": "",
    38  			"data":     "ordered",
    39  		},
    40  	}
    41  	found := findMountpoint(entries, "/")
    42  	assert.True(t, cmp.Equal(expected, found), cmp.Diff(expected, found))
    43  }
    44  
    45  func TestMountMacosParser(t *testing.T) {
    46  	mock, err := mock.New("./testdata/osx.toml", &inventory.Asset{
    47  		Platform: &inventory.Platform{Family: []string{"unix"}},
    48  	})
    49  	require.NoError(t, err)
    50  
    51  	f, err := mock.RunCommand("mount")
    52  	require.NoError(t, err)
    53  
    54  	entries := mount.ParseUnixMountCmd(f.Stdout)
    55  	// NOTE: we do not handle `map auto_home` yet
    56  	assert.Equal(t, 4, len(entries))
    57  
    58  	expected := &mount.MountPoint{
    59  		Device:     "/dev/disk1s5",
    60  		MountPoint: "/",
    61  		FSType:     "apfs",
    62  		Options: map[string]string{
    63  			"apfs":      "",
    64  			"local":     "",
    65  			"read-only": "",
    66  			"journaled": "",
    67  		},
    68  	}
    69  	found := findMountpoint(entries, "/")
    70  	assert.True(t, cmp.Equal(expected, found), cmp.Diff(expected, found))
    71  }
    72  
    73  func TestMountFreeBsdParser(t *testing.T) {
    74  	mock, err := mock.New("./testdata/freebsd12.toml", &inventory.Asset{
    75  		Platform: &inventory.Platform{Family: []string{"unix"}},
    76  	})
    77  	require.NoError(t, err)
    78  
    79  	f, err := mock.RunCommand("mount")
    80  	require.NoError(t, err)
    81  
    82  	entries := mount.ParseUnixMountCmd(f.Stdout)
    83  	assert.Equal(t, 2, len(entries))
    84  
    85  	expected := &mount.MountPoint{
    86  		Device:     "/dev/gpt/rootfs",
    87  		MountPoint: "/",
    88  		FSType:     "ufs",
    89  		Options: map[string]string{
    90  			"ufs":          "",
    91  			"local":        "",
    92  			"soft-updates": "",
    93  		},
    94  	}
    95  	found := findMountpoint(entries, "/")
    96  	assert.True(t, cmp.Equal(expected, found), cmp.Diff(expected, found))
    97  }
    98  
    99  func TestProcModulesParser(t *testing.T) {
   100  	mock, err := mock.New("./testdata/debian.toml", &inventory.Asset{
   101  		Platform: &inventory.Platform{Family: []string{"linux"}},
   102  	})
   103  	require.NoError(t, err)
   104  
   105  	f, err := mock.FileSystem().Open("/proc/mounts")
   106  	require.NoError(t, err)
   107  	defer f.Close()
   108  
   109  	entries := mount.ParseLinuxProcMount(f)
   110  	assert.Equal(t, 25, len(entries))
   111  
   112  	// /dev/sda1 on / type ext4 (rw,relatime,data=ordered)
   113  	expected := &mount.MountPoint{
   114  		Device:     "/dev/sda1",
   115  		MountPoint: "/",
   116  		FSType:     "ext4",
   117  		Options: map[string]string{
   118  			"rw":       "",
   119  			"relatime": "",
   120  			"data":     "ordered",
   121  		},
   122  	}
   123  	found := findMountpoint(entries, "/")
   124  	assert.True(t, cmp.Equal(expected, found), cmp.Diff(expected, found))
   125  }
   126  
   127  func findMountpoint(mounts []mount.MountPoint, name string) *mount.MountPoint {
   128  	for i := range mounts {
   129  		if mounts[i].MountPoint == name {
   130  			return &mounts[i]
   131  		}
   132  	}
   133  	return nil
   134  }
   135  
   136  var fstabExample = `
   137  # 
   138  # /etc/fstab: static file system information
   139  #
   140  # <file system>	<dir>	<type>	<options>	<dump>	<pass>
   141  # /dev/sdc2
   142  UUID=6c44ec5a-4727-47d4-b485-81cff72b207e	/         	ext4      	rw,relatime,data=ordered	0 1
   143  
   144  # /dev/sdc1
   145  UUID=0EC7-F4C1      	/boot     	vfat      	rw,relatime,fmask=0022,dmask=0022,iocharset=iso8859-1	0 2
   146  
   147  UUID=6060df9a-7e53-439c-9189-ba9657161fd4       /data           btrfs           rw,nofail              0 2
   148  `
   149  
   150  func TestFstab(t *testing.T) {
   151  	r := strings.NewReader(fstabExample)
   152  
   153  	entries, err := mount.ParseFstab(r)
   154  	require.NoError(t, err)
   155  
   156  	// /dev/sda1 on / type ext4 (rw,relatime,data=ordered)
   157  	expected := []mount.MountPoint{
   158  		{
   159  			Device:     "UUID=6c44ec5a-4727-47d4-b485-81cff72b207e",
   160  			MountPoint: "/",
   161  			FSType:     "ext4",
   162  			Options: map[string]string{
   163  				"rw":       "",
   164  				"relatime": "",
   165  				"data":     "ordered",
   166  			},
   167  		},
   168  		{
   169  			Device:     "UUID=0EC7-F4C1",
   170  			MountPoint: "/boot",
   171  			FSType:     "vfat",
   172  			Options: map[string]string{
   173  				"rw":        "",
   174  				"relatime":  "",
   175  				"fmask":     "0022",
   176  				"dmask":     "0022",
   177  				"iocharset": "iso8859-1",
   178  			},
   179  		},
   180  		{
   181  			Device:     "UUID=6060df9a-7e53-439c-9189-ba9657161fd4",
   182  			MountPoint: "/data",
   183  			FSType:     "btrfs",
   184  			Options: map[string]string{
   185  				"rw":     "",
   186  				"nofail": "",
   187  			},
   188  		},
   189  	}
   190  
   191  	assert.Equal(t, expected, entries)
   192  }