fork/gdu
git clone https://git.julia.blue/fork/gdu
81b1d22
ignore
1//go:build linux 2 3package device 4 5import ( 6"strings" 7"testing" 8 9"github.com/stretchr/testify/assert" 10) 11 12func TestGetDevicesInfo (t * testing.T ) { 13getter := LinuxDevicesInfoGetter {MountsPath :"/proc/mounts" } 14devices ,_ := getter .GetDevicesInfo () 15assert .IsType (t ,Devices {},devices ) 16} 17 18func TestGetDevicesInfoFail (t * testing.T ) { 19getter := LinuxDevicesInfoGetter {MountsPath :"/xxxyyy" } 20_ ,err := getter .GetDevicesInfo () 21assert .Equal (t ,"open /xxxyyy: no such file or directory" ,err .Error ()) 22} 23 24func TestSnapMountsNotShown (t * testing.T ) { 25mounts ,_ := readMountsFile (strings .NewReader (`/dev/loop4 /var/lib/snapd/snap/core18/1944 squashfs ro,nodev,relatime 0 0 26/dev/loop3 /var/lib/snapd/snap/core20/904 squashfs ro,nodev,relatime 0 0 27/dev/nvme0n1p1 /boot vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 0` )) 28 29devices ,err := processMounts (mounts ,true ) 30assert .Len (t ,devices ,1 ) 31assert .Nil (t ,err ) 32} 33 34func TestZfsMountsShown (t * testing.T ) { 35mounts ,_ := readMountsFile (strings .NewReader (`rootpool/opt /opt zfs rw,nodev,relatime,xattr,posixacl 0 0 36rootpool/usr/local /usr/local zfs rw,nodev,relatime,xattr,posixacl 0 0 37rootpool/home/root /root zfs rw,nodev,relatime,xattr,posixacl 0 0 38rootpool/usr/games /usr/games zfs rw,nodev,relatime,xattr,posixacl 0 0 39rootpool/home /home zfs rw,nodev,relatime,xattr,posixacl 0 0 40/dev/loop4 /var/lib/snapd/snap/core18/1944 squashfs ro,nodev,relatime 0 0 41/dev/loop3 /var/lib/snapd/snap/core20/904 squashfs ro,nodev,relatime 0 0 42/dev/nvme0n1p1 /boot vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro 0 0` )) 43 44devices ,err := processMounts (mounts ,true ) 45assert .Len (t ,devices ,6 ) 46assert .Nil (t ,err ) 47} 48 49func TestNfsMountsShown (t * testing.T ) { 50// nolint: lll // Why: Test data 51mounts ,_ := readMountsFile (strings .NewReader (`host1:/dir1/ /mnt/dir1 nfs4 rw,nosuid,nodev,noatime,nodiratime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.1,fsc,local_lock=none,addr=192.168.1.2 0 0 52host2:/dir2/ /mnt/dir2 nfs rw,relatime,vers=3,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.3,mountvers=3,mountport=38081,mountproto=udp,fsc,local_lock=none,addr=192.168.1.4 0 0` )) 53 54devices ,err := processMounts (mounts ,true ) 55assert .Len (t ,devices ,2 ) 56assert .Equal (t ,"host1:/dir1/" ,devices [0 ].Name ) 57assert .Equal (t ,"/mnt/dir1" ,devices [0 ].MountPoint ) 58assert .Nil (t ,err ) 59} 60 61func TestMalformedMountsLinesAreSkipped (t * testing.T ) { 62mounts ,err := readMountsFile (strings .NewReader (` 63/dev/nvme0n1p1 64/dev/nvme0n1p2 /boot 65/dev/nvme0n1p3 /home ext4 rw,relatime 0 0` )) 66 67assert .Nil (t ,err ) 68assert .Len (t ,mounts ,1 ) 69assert .Equal (t ,"/dev/nvme0n1p3" ,mounts [0 ].Name ) 70assert .Equal (t ,"/home" ,mounts [0 ].MountPoint ) 71assert .Equal (t ,"ext4" ,mounts [0 ].Fstype ) 72} 73 74func TestMountsWithSpaces (t * testing.T ) { 75// nolint: lll // Why: Test data 76mounts ,_ := readMountsFile (strings .NewReader (`host1:/dir1/ /mnt/dir\040with\040spaces nfs4 rw,nosuid,nodev,noatime,nodiratime,vers=4.2,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.1,fsc,local_lock=none,addr=192.168.1.2 0 0 77host2:/dir2/ /mnt/dir2 nfs rw,relatime,vers=3,rsize=524288,wsize=524288,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=192.168.1.3,mountvers=3,mountport=38081,mountproto=udp,fsc,local_lock=none,addr=192.168.1.4 0 0` )) 78 79devices ,err := processMounts (mounts ,true ) 80assert .Len (t ,devices ,2 ) 81assert .Equal (t ,"host1:/dir1/" ,devices [0 ].Name ) 82assert .Equal (t ,"/mnt/dir with spaces" ,devices [0 ].MountPoint ) 83assert .Nil (t ,err ) 84}