You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3 KiB

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