command_volume_list_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package shell
  2. import (
  3. _ "embed"
  4. "github.com/chrislusf/seaweedfs/weed/storage/types"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/stretchr/testify/assert"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. )
  12. func TestParsing(t *testing.T) {
  13. topo := parseOutput(topoData)
  14. assert.Equal(t, 5, len(topo.DataCenterInfos))
  15. }
  16. func parseOutput(output string) *master_pb.TopologyInfo {
  17. lines := strings.Split(output, "\n")
  18. var topo *master_pb.TopologyInfo
  19. var dc *master_pb.DataCenterInfo
  20. var rack *master_pb.RackInfo
  21. var dn *master_pb.DataNodeInfo
  22. var disk *master_pb.DiskInfo
  23. for _, line := range lines {
  24. line = strings.TrimSpace(line)
  25. parts := strings.Split(line, " ")
  26. switch parts[0] {
  27. case "Topology":
  28. if topo == nil {
  29. topo = &master_pb.TopologyInfo{}
  30. }
  31. case "DataCenter":
  32. if dc == nil {
  33. dc = &master_pb.DataCenterInfo{
  34. Id: parts[1],
  35. }
  36. topo.DataCenterInfos = append(topo.DataCenterInfos, dc)
  37. } else {
  38. dc = nil
  39. }
  40. case "Rack":
  41. if rack == nil {
  42. rack = &master_pb.RackInfo{
  43. Id: parts[1],
  44. }
  45. dc.RackInfos = append(dc.RackInfos, rack)
  46. } else {
  47. rack = nil
  48. }
  49. case "DataNode":
  50. if dn == nil {
  51. dn = &master_pb.DataNodeInfo{
  52. Id: parts[1],
  53. DiskInfos: make(map[string]*master_pb.DiskInfo),
  54. }
  55. rack.DataNodeInfos = append(rack.DataNodeInfos, dn)
  56. } else {
  57. dn = nil
  58. }
  59. case "Disk":
  60. if disk == nil {
  61. diskType := parts[1][:strings.Index(parts[1], "(")]
  62. volumeCountStr := parts[1][strings.Index(parts[1], ":")+1 : strings.Index(parts[1], "/")]
  63. maxVolumeCountStr := parts[1][strings.Index(parts[1], "/")+1:]
  64. maxVolumeCount, _ := strconv.Atoi(maxVolumeCountStr)
  65. volumeCount, _ := strconv.Atoi(volumeCountStr)
  66. disk = &master_pb.DiskInfo{
  67. Type: diskType,
  68. MaxVolumeCount: int64(maxVolumeCount),
  69. VolumeCount: int64(volumeCount),
  70. }
  71. dn.DiskInfos[types.ToDiskType(diskType).String()] = disk
  72. } else {
  73. disk = nil
  74. }
  75. case "volume":
  76. volumeLine := line[len("volume "):]
  77. volume := &master_pb.VolumeInformationMessage{}
  78. proto.UnmarshalText(volumeLine, volume)
  79. disk.VolumeInfos = append(disk.VolumeInfos, volume)
  80. }
  81. }
  82. return topo
  83. }
  84. //go:embed sample.topo.txt
  85. var topoData string