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.

124 lines
2.6 KiB

13 years ago
13 years ago
12 years ago
13 years ago
  1. package topology
  2. import (
  3. "code.google.com/p/weed-fs/go/sequence"
  4. "code.google.com/p/weed-fs/go/storage"
  5. "encoding/json"
  6. "fmt"
  7. "testing"
  8. )
  9. var topologyLayout = `
  10. {
  11. "dc1":{
  12. "rack1":{
  13. "server1":{
  14. "volumes":[
  15. {"id":1, "size":12312},
  16. {"id":2, "size":12312},
  17. {"id":3, "size":12312}
  18. ],
  19. "limit":3
  20. },
  21. "server2":{
  22. "volumes":[
  23. {"id":4, "size":12312},
  24. {"id":5, "size":12312},
  25. {"id":6, "size":12312}
  26. ],
  27. "limit":10
  28. }
  29. },
  30. "rack2":{
  31. "server1":{
  32. "volumes":[
  33. {"id":4, "size":12312},
  34. {"id":5, "size":12312},
  35. {"id":6, "size":12312}
  36. ],
  37. "limit":4
  38. },
  39. "server2":{
  40. "volumes":[],
  41. "limit":4
  42. },
  43. "server3":{
  44. "volumes":[
  45. {"id":2, "size":12312},
  46. {"id":3, "size":12312},
  47. {"id":4, "size":12312}
  48. ],
  49. "limit":2
  50. }
  51. }
  52. },
  53. "dc2":{
  54. },
  55. "dc3":{
  56. "rack2":{
  57. "server1":{
  58. "volumes":[
  59. {"id":1, "size":12312},
  60. {"id":3, "size":12312},
  61. {"id":5, "size":12312}
  62. ],
  63. "limit":4
  64. }
  65. }
  66. }
  67. }
  68. `
  69. func setup(topologyLayout string) *Topology {
  70. var data interface{}
  71. err := json.Unmarshal([]byte(topologyLayout), &data)
  72. if err != nil {
  73. fmt.Println("error:", err)
  74. }
  75. //need to connect all nodes first before server adding volumes
  76. topo, err := NewTopology("mynetwork", "/etc/weed.conf", sequence.NewMemorySequencer(), 234, 5)
  77. if err != nil {
  78. fmt.Println("error:", err)
  79. }
  80. mTopology := data.(map[string]interface{})
  81. for dcKey, dcValue := range mTopology {
  82. dc := NewDataCenter(dcKey)
  83. dcMap := dcValue.(map[string]interface{})
  84. topo.LinkChildNode(dc)
  85. for rackKey, rackValue := range dcMap {
  86. rack := NewRack(rackKey)
  87. rackMap := rackValue.(map[string]interface{})
  88. dc.LinkChildNode(rack)
  89. for serverKey, serverValue := range rackMap {
  90. server := NewDataNode(serverKey)
  91. serverMap := serverValue.(map[string]interface{})
  92. rack.LinkChildNode(server)
  93. for _, v := range serverMap["volumes"].([]interface{}) {
  94. m := v.(map[string]interface{})
  95. vi := storage.VolumeInfo{
  96. Id: storage.VolumeId(int64(m["id"].(float64))),
  97. Size: uint64(m["size"].(float64)),
  98. Collection: "testingCollection",
  99. Version: storage.CurrentVersion}
  100. server.AddOrUpdateVolume(vi)
  101. }
  102. server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
  103. }
  104. }
  105. }
  106. return topo
  107. }
  108. func TestRemoveDataCenter(t *testing.T) {
  109. topo := setup(topologyLayout)
  110. topo.UnlinkChildNode(NodeId("dc2"))
  111. if topo.GetActiveVolumeCount() != 15 {
  112. t.Fail()
  113. }
  114. topo.UnlinkChildNode(NodeId("dc3"))
  115. if topo.GetActiveVolumeCount() != 12 {
  116. t.Fail()
  117. }
  118. }