disktool_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 disktool
  15. import "testing"
  16. // TODO: use mock ssh server backend test disktool
  17. /*
  18. import (
  19. "testing"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/util/stringutils"
  22. "yunion.io/x/onecloud/pkg/compute/baremetal"
  23. "yunion.io/x/onecloud/pkg/util/ssh"
  24. )
  25. var (
  26. term *ssh.Client
  27. )
  28. func init() {
  29. var err error
  30. term, err = ssh.NewClient("192.168.0.254", 22, "root", "rMw2qrm6Lb5NVpe0", "")
  31. if err != nil {
  32. log.Fatalf("Failed to init ssh client: %v", err)
  33. }
  34. }
  35. func TestSSHCreate(t *testing.T) {
  36. tool := NewSSHPartitionTool(term)
  37. err := tool.FetchDiskConfs([]baremetal.DiskConfiguration{
  38. {
  39. Adapter: 0,
  40. Driver: baremetal.DISK_DRIVER_LINUX,
  41. },
  42. }).RetrieveDiskInfo()
  43. if err != nil {
  44. t.Errorf("Failed to RetrieveDiskInfo: %v", err)
  45. }
  46. err = tool.RetrievePartitionInfo()
  47. if err != nil {
  48. t.Errorf("Failed to RetrievePartitionInfo: %v", err)
  49. }
  50. log.Infof("%s", tool.DebugString())
  51. uuid := stringutils.UUID4
  52. tool.CreatePartition(-1, 32, "swap", true, baremetal.DISK_DRIVER_LINUX, uuid())
  53. tool.CreatePartition(-1, 1024, "ext4", true, baremetal.DISK_DRIVER_LINUX, uuid())
  54. err = tool.CreatePartition(-1, -1, "xfs", true, baremetal.DISK_DRIVER_LINUX, uuid())
  55. //err = tool.ResizePartition(0, 110*1024)
  56. if err != nil {
  57. t.Errorf("Failed to resize fs: %v", err)
  58. }
  59. }*/
  60. func Test_getPCIPathPrefix(t *testing.T) {
  61. tests := []struct {
  62. input string
  63. want string
  64. }{
  65. {
  66. input: "/sys/devices/pci0000:00/0000:00:02.2/0000:02:00.0/host0/target0:1:0/0:1:0:0/block/sda",
  67. want: "/sys/devices/pci0000:00/0000:00:02.2/0000:02:00.0/host0/target0:1:0/0:1:0:0",
  68. },
  69. {
  70. input: "/sys/devices/pci0000:00/0000:00:06.0/0000:02:00.0/nvme/nvme0/nvme0n1",
  71. want: "/sys/devices/pci0000:00/0000:00:06.0/0000:02:00.0/nvme",
  72. },
  73. }
  74. for _, tt := range tests {
  75. t.Run(tt.input, func(t *testing.T) {
  76. if got := getPCIPathPrefix(tt.input); got != tt.want {
  77. t.Errorf("getPCIPathPrefix() = %v, want %v", got, tt.want)
  78. }
  79. })
  80. }
  81. }