device_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package sys
  14. import (
  15. "fmt"
  16. "testing"
  17. exectest "github.com/rook/rook/pkg/util/exec/test"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. const (
  21. udevOutput = `DEVLINKS=/dev/disk/by-id/scsi-36001405d27e5d898829468b90ce4ef8c /dev/disk/by-id/wwn-0x6001405d27e5d898829468b90ce4ef8c /dev/disk/by-path/ip-127.0.0.1:3260-iscsi-iqn.2016-06.world.srv:storage.target01-lun-0 /dev/disk/by-uuid/f2d38cba-37da-411d-b7ba-9a6696c58174
  22. DEVNAME=/dev/sdk
  23. DEVPATH=/devices/platform/host6/session2/target6:0:0/6:0:0:0/block/sdk
  24. DEVTYPE=disk
  25. ID_BUS=scsi
  26. ID_FS_TYPE=ext2
  27. ID_FS_USAGE=filesystem
  28. ID_FS_UUID=f2d38cba-37da-411d-b7ba-9a6696c58174
  29. ID_FS_UUID_ENC=f2d38cba-37da-411d-b7ba-9a6696c58174
  30. ID_FS_VERSION=1.0
  31. ID_MODEL=disk01
  32. ID_MODEL_ENC=disk01\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
  33. ID_PATH=ip-127.0.0.1:3260-iscsi-iqn.2016-06.world.srv:storage.target01-lun-0
  34. ID_PATH_TAG=ip-127_0_0_1_3260-iscsi-iqn_2016-06_world_srv_storage_target01-lun-0
  35. ID_REVISION=4.0
  36. ID_SCSI=1
  37. ID_SCSI_SERIAL=d27e5d89-8829-468b-90ce-4ef8c02f07fe
  38. ID_SERIAL=36001405d27e5d898829468b90ce4ef8c
  39. ID_SERIAL_SHORT=6001405d27e5d898829468b90ce4ef8c
  40. ID_TARGET_PORT=0
  41. ID_TYPE=disk
  42. ID_VENDOR=LIO-ORG
  43. ID_VENDOR_ENC=LIO-ORG\x20
  44. ID_WWN=0x6001405d27e5d898
  45. ID_WWN_VENDOR_EXTENSION=0x829468b90ce4ef8c
  46. ID_WWN_WITH_EXTENSION=0x6001405d27e5d898829468b90ce4ef8c
  47. MAJOR=8
  48. MINOR=160
  49. SUBSYSTEM=block
  50. TAGS=:systemd:
  51. USEC_INITIALIZED=15981915740802
  52. `
  53. udevPartOutput = `ID_PART_ENTRY_DISK=8:32
  54. ID_PART_ENTRY_NAME=%s
  55. ID_PART_ENTRY_NUMBER=3
  56. ID_PART_ENTRY_OFFSET=3278848
  57. ID_PART_ENTRY_SCHEME=gpt
  58. ID_PART_ENTRY_SIZE=7206879
  59. ID_PART_ENTRY_TYPE=0fc63daf-8483-4772-8e79-3d69d8477de4
  60. ID_PART_ENTRY_UUID=2089640e-bdeb-4fb4-aaec-88e165780b88
  61. ID_PART_TABLE_TYPE=gpt
  62. ID_PART_TABLE_UUID=46242f96-6cf7-4e5d-b4bd-9d046e6ad920
  63. ID_REVISION=4.0
  64. ID_SCSI=1
  65. ID_SCSI_SERIAL=68c0bd28-d4ee-4376-9387-c9f02c53b3f2
  66. ID_SERIAL=3600140568c0bd28d4ee43769387c9f02
  67. ID_SERIAL_SHORT=600140568c0bd28d4ee43769387c9f02
  68. ID_TARGET_PORT=0
  69. ID_TYPE=disk
  70. ID_VENDOR=LIO-ORG
  71. ID_VENDOR_ENC=LIO-ORG\x20
  72. ID_WWN=0x600140568c0bd28d
  73. ID_WWN_VENDOR_EXTENSION=0x4ee43769387c9f02
  74. ID_WWN_WITH_EXTENSION=0x600140568c0bd28d4ee43769387c9f02
  75. MAJOR=8
  76. MINOR=35
  77. PARTN=3
  78. PARTNAME=Linux filesystem
  79. SUBSYSTEM=block
  80. `
  81. )
  82. var (
  83. lsblkChildOutput = `NAME="ceph--cec981b8--2eca--45cd--bf91--a4472779f2a9-osd--data--428984b7--f94d--40cd--9cb7--1458e1613eab" MAJ:MIN="252:0" RM="0" SIZE="29G" RO="0" TYPE="lvm" MOUNTPOINT=""
  84. NAME="vdb" MAJ:MIN="253:16" RM="0" SIZE="30G" RO="0" TYPE="disk" MOUNTPOINT=""
  85. NAME="vdb1" MAJ:MIN="253:17" RM="0" SIZE="30G" RO="0" TYPE="part" MOUNTPOINT=""`
  86. )
  87. func TestFindUUID(t *testing.T) {
  88. output := `Disk /dev/sdb: 10485760 sectors, 5.0 GiB
  89. Logical sector size: 512 bytes
  90. Disk identifier (GUID): 31273B25-7B2E-4D31-BAC9-EE77E62EAC71
  91. Partition table holds up to 128 entries
  92. First usable sector is 34, last usable sector is 10485726
  93. Partitions will be aligned on 2048-sector boundaries
  94. Total free space is 20971453 sectors (10.0 GiB)
  95. `
  96. uuid, err := parseUUID("sdb", output)
  97. assert.Nil(t, err)
  98. assert.Equal(t, "31273b25-7b2e-4d31-bac9-ee77e62eac71", uuid)
  99. }
  100. func TestParseFileSystem(t *testing.T) {
  101. output := udevOutput
  102. result := parseFS(output)
  103. assert.Equal(t, "ext2", result)
  104. }
  105. func TestGetPartitions(t *testing.T) {
  106. run := 0
  107. executor := &exectest.MockExecutor{
  108. MockExecuteCommandWithOutput: func(command string, arg ...string) (string, error) {
  109. run++
  110. logger.Infof("run %d command %s", run, command)
  111. switch {
  112. case run == 1:
  113. return `NAME="sdc" SIZE="100000" TYPE="disk" PKNAME=""`, nil
  114. case run == 2:
  115. return `NAME="sdb" SIZE="65" TYPE="disk" PKNAME=""
  116. NAME="sdb2" SIZE="10" TYPE="part" PKNAME="sdb"
  117. NAME="sdb3" SIZE="20" TYPE="part" PKNAME="sdb"
  118. NAME="sdb1" SIZE="30" TYPE="part" PKNAME="sdb"`, nil
  119. case run == 3:
  120. return fmt.Sprintf(udevPartOutput, "ROOK-OSD0-DB"), nil
  121. case run == 4:
  122. return fmt.Sprintf(udevPartOutput, "ROOK-OSD0-BLOCK"), nil
  123. case run == 5:
  124. return fmt.Sprintf(udevPartOutput, "ROOK-OSD0-WAL"), nil
  125. case run == 6:
  126. return `NAME="sda" SIZE="19818086400" TYPE="disk" PKNAME=""
  127. NAME="sda4" SIZE="1073741824" TYPE="part" PKNAME="sda"
  128. NAME="sda2" SIZE="2097152" TYPE="part" PKNAME="sda"
  129. NAME="sda9" SIZE="17328766976" TYPE="part" PKNAME="sda"
  130. NAME="sda7" SIZE="67108864" TYPE="part" PKNAME="sda"
  131. NAME="sda3" SIZE="1073741824" TYPE="part" PKNAME="sda"
  132. NAME="usr" SIZE="1065345024" TYPE="crypt" PKNAME="sda3"
  133. NAME="sda1" SIZE="134217728" TYPE="part" PKNAME="sda"
  134. NAME="sda6" SIZE="134217728" TYPE="part" PKNAME="sda"`, nil
  135. case run == 14:
  136. return `NAME="dm-0" SIZE="100000" TYPE="lvm" PKNAME=""
  137. NAME="ceph--89fa04fa--b93a--4874--9364--c95be3ec01c6-osd--data--70847bdb--2ec1--4874--98ba--d87d4860a70d" SIZE="31138512896" TYPE="lvm" PKNAME=""`, nil
  138. }
  139. return "", nil
  140. },
  141. }
  142. partitions, unused, err := GetDevicePartitions("sdc", executor)
  143. assert.Nil(t, err)
  144. assert.Equal(t, uint64(100000), unused)
  145. assert.Equal(t, 0, len(partitions))
  146. partitions, unused, err = GetDevicePartitions("sdb", executor)
  147. assert.Nil(t, err)
  148. assert.Equal(t, uint64(5), unused)
  149. assert.Equal(t, 3, len(partitions))
  150. assert.Equal(t, uint64(10), partitions[0].Size)
  151. assert.Equal(t, "ROOK-OSD0-DB", partitions[0].Label)
  152. assert.Equal(t, "sdb2", partitions[0].Name)
  153. partitions, unused, err = GetDevicePartitions("sda", executor)
  154. assert.Nil(t, err)
  155. assert.Equal(t, uint64(0x400000), unused)
  156. assert.Equal(t, 7, len(partitions))
  157. partitions, _, err = GetDevicePartitions("dm-0", executor)
  158. assert.Nil(t, err)
  159. assert.Equal(t, 1, len(partitions))
  160. partitions, _, err = GetDevicePartitions("sdx", executor)
  161. assert.Nil(t, err)
  162. assert.Equal(t, 0, len(partitions))
  163. }
  164. func TestParseUdevInfo(t *testing.T) {
  165. m := parseUdevInfo(udevOutput)
  166. assert.Equal(t, m["ID_FS_TYPE"], "ext2")
  167. }
  168. func TestListDevicesChildListDevicesChild(t *testing.T) {
  169. executor := &exectest.MockExecutor{
  170. MockExecuteCommandWithOutput: func(command string, arg ...string) (string, error) {
  171. logger.Infof("command %s", command)
  172. return lsblkChildOutput, nil
  173. },
  174. }
  175. device := "/dev/vdb"
  176. child, err := ListDevicesChild(executor, device)
  177. assert.NoError(t, err)
  178. assert.Equal(t, 3, len(child))
  179. }
  180. func TestGetDiskDeviceClass(t *testing.T) {
  181. d := &LocalDisk{}
  182. assert.Equal(t, "ssd", GetDiskDeviceClass(d))
  183. d.Rotational = true
  184. assert.Equal(t, "hdd", GetDiskDeviceClass(d))
  185. d.Rotational = false
  186. d.RealPath = "nvme"
  187. assert.Equal(t, "nvme", GetDiskDeviceClass(d))
  188. }