github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/worker/diskmanager/lsblk_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // +build linux 5 6 package diskmanager_test 7 8 import ( 9 "errors" 10 "os" 11 12 "github.com/juju/testing" 13 jc "github.com/juju/testing/checkers" 14 gc "gopkg.in/check.v1" 15 16 "github.com/juju/juju/storage" 17 coretesting "github.com/juju/juju/testing" 18 "github.com/juju/juju/worker/diskmanager" 19 ) 20 21 var _ = gc.Suite(&ListBlockDevicesSuite{}) 22 23 type ListBlockDevicesSuite struct { 24 coretesting.BaseSuite 25 } 26 27 func (s *ListBlockDevicesSuite) SetUpTest(c *gc.C) { 28 s.BaseSuite.SetUpTest(c) 29 s.PatchValue(diskmanager.BlockDeviceInUse, func(storage.BlockDevice) (bool, error) { 30 return false, nil 31 }) 32 } 33 34 func (s *ListBlockDevicesSuite) TestListBlockDevices(c *gc.C) { 35 s.PatchValue(diskmanager.BlockDeviceInUse, func(dev storage.BlockDevice) (bool, error) { 36 return dev.DeviceName == "sdb", nil 37 }) 38 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 39 cat <<EOF 40 KNAME="sda" SIZE="240057409536" LABEL="" UUID="" TYPE="disk" 41 KNAME="sda1" SIZE="254803968" LABEL="" UUID="7a62bd85-a350-4c09-8944-5b99bf2080c6" MOUNTPOINT="/tmp" TYPE="disk" 42 KNAME="sda2" SIZE="1024" LABEL="boot" UUID="" TYPE="disk" 43 KNAME="sdb" SIZE="32017047552" LABEL="" UUID="" TYPE="disk" 44 KNAME="sdb1" SIZE="32015122432" LABEL="media" UUID="2c1c701d-f2ce-43a4-b345-33e2e39f9503" FSTYPE="ext4" TYPE="disk" 45 EOF`) 46 47 devices, err := diskmanager.ListBlockDevices() 48 c.Assert(err, jc.ErrorIsNil) 49 c.Assert(devices, jc.SameContents, []storage.BlockDevice{{ 50 DeviceName: "sda", 51 Size: 228936, 52 }, { 53 DeviceName: "sda1", 54 Size: 243, 55 UUID: "7a62bd85-a350-4c09-8944-5b99bf2080c6", 56 MountPoint: "/tmp", 57 }, { 58 DeviceName: "sda2", 59 Size: 0, // truncated 60 Label: "boot", 61 }, { 62 DeviceName: "sdb", 63 Size: 30533, 64 InUse: true, 65 }, { 66 DeviceName: "sdb1", 67 Size: 30532, 68 Label: "media", 69 UUID: "2c1c701d-f2ce-43a4-b345-33e2e39f9503", 70 FilesystemType: "ext4", 71 }}) 72 } 73 74 func (s *ListBlockDevicesSuite) TestListBlockDevicesLsblkError(c *gc.C) { 75 testing.PatchExecutableThrowError(c, s, "lsblk", 123) 76 devices, err := diskmanager.ListBlockDevices() 77 c.Assert(err, gc.ErrorMatches, "cannot list block devices: lsblk failed: exit status 123") 78 c.Assert(devices, gc.IsNil) 79 } 80 81 func (s *ListBlockDevicesSuite) TestListBlockDevicesBlockDeviceInUseError(c *gc.C) { 82 s.PatchValue(diskmanager.BlockDeviceInUse, func(dev storage.BlockDevice) (bool, error) { 83 return false, errors.New("badness") 84 }) 85 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 86 cat <<EOF 87 KNAME="sda" SIZE="240057409536" LABEL="" UUID="" TYPE="disk" 88 EOF`) 89 90 // If the in-use check errors, the block device will be marked "in use" 91 // to prevent it from being used, but no error will be returned. 92 devices, err := diskmanager.ListBlockDevices() 93 c.Assert(err, jc.ErrorIsNil) 94 c.Assert(devices, jc.SameContents, []storage.BlockDevice{{ 95 DeviceName: "sda", 96 Size: 228936, 97 InUse: true, 98 }}) 99 } 100 101 func (s *ListBlockDevicesSuite) TestListBlockDevicesLsblkBadOutput(c *gc.C) { 102 // Extra key/value pairs should be ignored; invalid sizes should 103 // be logged and ignored (Size will be set to zero). 104 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 105 cat <<EOF 106 KNAME="sda" SIZE="eleventy" LABEL="" UUID="" TYPE="disk" 107 KNAME="sdb" SIZE="1048576" LABEL="" UUID="" BOB="DOBBS" TYPE="disk" 108 EOF`) 109 110 devices, err := diskmanager.ListBlockDevices() 111 c.Assert(err, jc.ErrorIsNil) 112 c.Assert(devices, jc.SameContents, []storage.BlockDevice{{ 113 DeviceName: "sda", 114 Size: 0, 115 }, { 116 DeviceName: "sdb", 117 Size: 1, 118 }}) 119 } 120 121 func (s *ListBlockDevicesSuite) TestListBlockDevicesDeviceNotExist(c *gc.C) { 122 s.PatchValue(diskmanager.BlockDeviceInUse, func(dev storage.BlockDevice) (bool, error) { 123 return false, os.ErrNotExist 124 }) 125 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 126 cat <<EOF 127 KNAME="sda" SIZE="240057409536" LABEL="" UUID="" TYPE="disk" 128 KNAME="sdb" SIZE="32017047552" LABEL="" UUID="" TYPE="disk" 129 EOF`) 130 131 devices, err := diskmanager.ListBlockDevices() 132 c.Assert(err, jc.ErrorIsNil) 133 c.Assert(devices, gc.HasLen, 0) 134 } 135 136 func (s *ListBlockDevicesSuite) TestListBlockDevicesDeviceFiltering(c *gc.C) { 137 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 138 cat <<EOF 139 KNAME="sda" SIZE="240057409536" LABEL="" UUID="" TYPE="disk" 140 KNAME="sda1" SIZE="254803968" LABEL="" UUID="" TYPE="part" 141 KNAME="loop0" SIZE="254803968" LABEL="" UUID="" TYPE="loop" 142 KNAME="sr0" SIZE="254803968" LABEL="" UUID="" TYPE="rom" 143 KNAME="whatever" SIZE="254803968" LABEL="" UUID="" TYPE="lvm" 144 EOF`) 145 146 devices, err := diskmanager.ListBlockDevices() 147 c.Assert(err, gc.IsNil) 148 c.Assert(devices, jc.SameContents, []storage.BlockDevice{{ 149 DeviceName: "sda", 150 Size: 228936, 151 }, { 152 DeviceName: "loop0", 153 Size: 243, 154 }}) 155 }