partition_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutils2
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. var (
  20. sdaOut = []string{
  21. "Model: QEMU QEMU HARDDISK (scsi)\n",
  22. "Disk /dev/sda: 204800000s\n",
  23. "Sector size (logical/physical): 512B/512B\n",
  24. "Partition Table: msdos\n",
  25. "Disk Flags:",
  26. "",
  27. "Number Start End Size File system Name Flags\n",
  28. " 1 63s 204796619s 204796557s primary\n",
  29. }
  30. sdbOut = []string{
  31. "Model: ATA PH5-SE128G+ (scsi)\n",
  32. "Disk /dev/sdb: 250069680s\n",
  33. "Sector size (logical/physical): 512B/512B\n",
  34. "Partition Table: gpt\n",
  35. "Disk Flags:",
  36. "",
  37. "Number Start End Size File system Name Flags\n",
  38. " 1 2048s 1023999s 1021952s ntfs Basic data partition hidden, diag\n",
  39. " 2 1024000s 1226751s 202752s fat32 EFI boot\n",
  40. " 3 1226752s 1259519s 32768s Microsoft reserved partition msftres\n",
  41. " 4 1259520s 105734143s 104474624s ntfs Basic data partition msftdata\n",
  42. " 5 105734144s 250069646s 144335503s xfs Linux filesystem\n",
  43. }
  44. )
  45. func TestParseDiskPartitions(t *testing.T) {
  46. type args struct {
  47. dev string
  48. lines []string
  49. }
  50. tests := []struct {
  51. name string
  52. args args
  53. want []Partition
  54. want1 string
  55. }{
  56. {
  57. name: "sdaInput",
  58. args: args{
  59. dev: "/dev/sda",
  60. lines: sdaOut,
  61. },
  62. want: []Partition{
  63. NewPartition(1, false, 63, 204796619, 204796557, "primary", "", "/dev/sda1"),
  64. },
  65. want1: "msdos",
  66. },
  67. {
  68. name: "sdbInput",
  69. args: args{
  70. dev: "/dev/sdb",
  71. lines: sdbOut,
  72. },
  73. want: []Partition{
  74. NewPartition(1, false, 2048, 1023999, 1021952, "Basic", "ntfs", "/dev/sdb1"),
  75. NewPartition(2, true, 1024000, 1226751, 202752, "EFI", "fat32", "/dev/sdb2"),
  76. NewPartition(3, false, 1226752, 1259519, 32768, "Microsoft", "", "/dev/sdb3"),
  77. NewPartition(4, false, 1259520, 105734143, 104474624, "Basic", "ntfs", "/dev/sdb4"),
  78. NewPartition(5, false, 105734144, 250069646, 144335503, "Linux", "xfs", "/dev/sdb5"),
  79. },
  80. want1: "gpt",
  81. },
  82. }
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. got, got1 := ParseDiskPartitions(tt.args.dev, tt.args.lines)
  86. for i, gotD := range got {
  87. if !reflect.DeepEqual(gotD, tt.want[i]) {
  88. t.Errorf("ParseDiskPartitions() got[%d] = %v, want %v", i, gotD, tt.want[i])
  89. }
  90. }
  91. if got1 != tt.want1 {
  92. t.Errorf("ParseDiskPartitions() got1 = %v, want %v", got1, tt.want1)
  93. }
  94. })
  95. }
  96. }