github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/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="" 41 KNAME="sda1" SIZE="254803968" LABEL="" UUID="7a62bd85-a350-4c09-8944-5b99bf2080c6" 42 KNAME="sda2" SIZE="1024" LABEL="boot" UUID="" 43 KNAME="sdb" SIZE="32017047552" LABEL="" UUID="" 44 KNAME="sdb1" SIZE="32015122432" LABEL="media" UUID="2c1c701d-f2ce-43a4-b345-33e2e39f9503" FSTYPE="ext4" 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 }, { 57 DeviceName: "sda2", 58 Size: 0, // truncated 59 Label: "boot", 60 }, { 61 DeviceName: "sdb", 62 Size: 30533, 63 InUse: true, 64 }, { 65 DeviceName: "sdb1", 66 Size: 30532, 67 Label: "media", 68 UUID: "2c1c701d-f2ce-43a4-b345-33e2e39f9503", 69 FilesystemType: "ext4", 70 }}) 71 } 72 73 func (s *ListBlockDevicesSuite) TestListBlockDevicesLsblkError(c *gc.C) { 74 testing.PatchExecutableThrowError(c, s, "lsblk", 123) 75 devices, err := diskmanager.ListBlockDevices() 76 c.Assert(err, gc.ErrorMatches, "cannot list block devices: lsblk failed: exit status 123") 77 c.Assert(devices, gc.IsNil) 78 } 79 80 func (s *ListBlockDevicesSuite) TestListBlockDevicesBlockDeviceInUseError(c *gc.C) { 81 s.PatchValue(diskmanager.BlockDeviceInUse, func(dev storage.BlockDevice) (bool, error) { 82 return false, errors.New("badness") 83 }) 84 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 85 cat <<EOF 86 KNAME="sda" SIZE="240057409536" LABEL="" UUID="" 87 EOF`) 88 89 // If the in-use check errors, the block device will be marked "in use" 90 // to prevent it from being used, but no error will be returned. 91 devices, err := diskmanager.ListBlockDevices() 92 c.Assert(err, jc.ErrorIsNil) 93 c.Assert(devices, jc.SameContents, []storage.BlockDevice{{ 94 DeviceName: "sda", 95 Size: 228936, 96 InUse: true, 97 }}) 98 } 99 100 func (s *ListBlockDevicesSuite) TestListBlockDevicesLsblkBadOutput(c *gc.C) { 101 // Extra key/value pairs should be ignored; invalid sizes should 102 // be logged and ignored (Size will be set to zero). 103 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 104 cat <<EOF 105 KNAME="sda" SIZE="eleventy" LABEL="" UUID="" 106 KNAME="sdb" SIZE="1048576" LABEL="" UUID="" BOB="DOBBS" 107 EOF`) 108 109 devices, err := diskmanager.ListBlockDevices() 110 c.Assert(err, jc.ErrorIsNil) 111 c.Assert(devices, jc.SameContents, []storage.BlockDevice{{ 112 DeviceName: "sda", 113 Size: 0, 114 }, { 115 DeviceName: "sdb", 116 Size: 1, 117 }}) 118 } 119 120 func (s *ListBlockDevicesSuite) TestListBlockDevicesDeviceNotExist(c *gc.C) { 121 s.PatchValue(diskmanager.BlockDeviceInUse, func(dev storage.BlockDevice) (bool, error) { 122 return false, os.ErrNotExist 123 }) 124 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 125 cat <<EOF 126 KNAME="sda" SIZE="240057409536" LABEL="" UUID="" 127 KNAME="sdb" SIZE="32017047552" LABEL="" UUID="" 128 EOF`) 129 130 devices, err := diskmanager.ListBlockDevices() 131 c.Assert(err, jc.ErrorIsNil) 132 c.Assert(devices, gc.HasLen, 0) 133 } 134 135 func (s *ListBlockDevicesSuite) TestListBlockDevicesDevicePartitions(c *gc.C) { 136 testing.PatchExecutable(c, s, "lsblk", `#!/bin/bash --norc 137 cat <<EOF 138 KNAME="sda" SIZE="240057409536" LABEL="" UUID="" TYPE="disk" 139 KNAME="sda1" SIZE="254803968" LABEL="" UUID="" TYPE="part" 140 EOF`) 141 142 devices, err := diskmanager.ListBlockDevices() 143 c.Assert(err, gc.IsNil) 144 c.Assert(devices, gc.DeepEquals, []storage.BlockDevice{{ 145 DeviceName: "sda", 146 Size: 228936, 147 }}) 148 }