github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/worker/diskmanager/diskmanager_test.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package diskmanager_test 5 6 import ( 7 "time" 8 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/juju/storage" 13 coretesting "github.com/juju/juju/testing" 14 "github.com/juju/juju/worker/diskmanager" 15 ) 16 17 var _ = gc.Suite(&DiskManagerWorkerSuite{}) 18 19 type DiskManagerWorkerSuite struct { 20 coretesting.BaseSuite 21 } 22 23 func (s *DiskManagerWorkerSuite) SetUpTest(c *gc.C) { 24 s.BaseSuite.SetUpTest(c) 25 s.PatchValue(diskmanager.BlockDeviceInUse, func(storage.BlockDevice) (bool, error) { 26 return false, nil 27 }) 28 } 29 30 func (s *DiskManagerWorkerSuite) TestWorker(c *gc.C) { 31 done := make(chan struct{}) 32 var setDevices BlockDeviceSetterFunc = func(devices []storage.BlockDevice) error { 33 close(done) 34 return nil 35 } 36 37 var listDevices diskmanager.ListBlockDevicesFunc = func() ([]storage.BlockDevice, error) { 38 return []storage.BlockDevice{{DeviceName: "whatever"}}, nil 39 } 40 41 w := diskmanager.NewWorker(listDevices, setDevices) 42 defer w.Wait() 43 defer w.Kill() 44 45 select { 46 case <-done: 47 case <-time.After(coretesting.LongWait): 48 c.Fatalf("timed out waiting for diskmanager to update") 49 } 50 } 51 52 func (s *DiskManagerWorkerSuite) TestBlockDeviceChanges(c *gc.C) { 53 var oldDevices []storage.BlockDevice 54 var devicesSet [][]storage.BlockDevice 55 var setDevices BlockDeviceSetterFunc = func(devices []storage.BlockDevice) error { 56 devicesSet = append(devicesSet, append([]storage.BlockDevice{}, devices...)) 57 return nil 58 } 59 60 device := storage.BlockDevice{DeviceName: "sda", DeviceLinks: []string{"a", "b"}} 61 var listDevices diskmanager.ListBlockDevicesFunc = func() ([]storage.BlockDevice, error) { 62 return []storage.BlockDevice{device}, nil 63 } 64 65 err := diskmanager.DoWork(listDevices, setDevices, &oldDevices) 66 c.Assert(err, jc.ErrorIsNil) 67 c.Assert(devicesSet, gc.HasLen, 1) 68 69 // diskmanager only calls the BlockDeviceSetter when it sees a 70 // change in disks. Order of DeviceLinks should not matter. 71 device.DeviceLinks = []string{"b", "a"} 72 err = diskmanager.DoWork(listDevices, setDevices, &oldDevices) 73 c.Assert(err, jc.ErrorIsNil) 74 c.Assert(devicesSet, gc.HasLen, 1) 75 76 device.DeviceName = "sdb" 77 err = diskmanager.DoWork(listDevices, setDevices, &oldDevices) 78 c.Assert(err, jc.ErrorIsNil) 79 c.Assert(devicesSet, gc.HasLen, 2) 80 81 c.Assert(devicesSet[0], gc.DeepEquals, []storage.BlockDevice{{ 82 DeviceName: "sda", DeviceLinks: []string{"a", "b"}, 83 }}) 84 c.Assert(devicesSet[1], gc.DeepEquals, []storage.BlockDevice{{ 85 DeviceName: "sdb", DeviceLinks: []string{"a", "b"}, 86 }}) 87 } 88 89 func (s *DiskManagerWorkerSuite) TestBlockDevicesSorted(c *gc.C) { 90 var devicesSet [][]storage.BlockDevice 91 var setDevices BlockDeviceSetterFunc = func(devices []storage.BlockDevice) error { 92 devicesSet = append(devicesSet, devices) 93 return nil 94 } 95 96 var listDevices diskmanager.ListBlockDevicesFunc = func() ([]storage.BlockDevice, error) { 97 return []storage.BlockDevice{{ 98 DeviceName: "sdb", 99 }, { 100 DeviceName: "sda", 101 }, { 102 DeviceName: "sdc", 103 }}, nil 104 } 105 err := diskmanager.DoWork(listDevices, setDevices, new([]storage.BlockDevice)) 106 c.Assert(err, jc.ErrorIsNil) 107 108 // The block Devices should be sorted when passed to the block 109 // device setter. 110 c.Assert(devicesSet, gc.DeepEquals, [][]storage.BlockDevice{{{ 111 DeviceName: "sda", 112 }, { 113 DeviceName: "sdb", 114 }, { 115 DeviceName: "sdc", 116 }}}) 117 } 118 119 type BlockDeviceSetterFunc func([]storage.BlockDevice) error 120 121 func (f BlockDeviceSetterFunc) SetMachineBlockDevices(devices []storage.BlockDevice) error { 122 return f(devices) 123 }