seaweedfs/go/topology/topo_test.go

125 lines
2.6 KiB
Go
Raw Normal View History

2012-08-29 16:42:24 +08:00
package topology
import (
"code.google.com/p/weed-fs/go/sequence"
2013-02-27 14:54:22 +08:00
"code.google.com/p/weed-fs/go/storage"
2012-08-31 16:35:11 +08:00
"encoding/json"
"fmt"
"testing"
2012-08-29 16:42:24 +08:00
)
2012-08-31 16:35:11 +08:00
var topologyLayout = `
{
2013-02-27 14:54:22 +08:00
"dc1":{
"rack1":{
"server1":{
"volumes":[
{"id":1, "size":12312},
{"id":2, "size":12312},
{"id":3, "size":12312}
],
"limit":3
},
"server2":{
"volumes":[
{"id":4, "size":12312},
{"id":5, "size":12312},
{"id":6, "size":12312}
],
"limit":10
}
},
"rack2":{
"server1":{
"volumes":[
{"id":4, "size":12312},
{"id":5, "size":12312},
{"id":6, "size":12312}
],
"limit":4
},
"server2":{
"volumes":[],
"limit":4
},
"server3":{
"volumes":[
{"id":2, "size":12312},
{"id":3, "size":12312},
{"id":4, "size":12312}
],
"limit":2
}
}
},
"dc2":{
},
"dc3":{
"rack2":{
"server1":{
"volumes":[
{"id":1, "size":12312},
{"id":3, "size":12312},
{"id":5, "size":12312}
],
"limit":4
}
}
}
2012-08-31 16:35:11 +08:00
}
`
func setup(topologyLayout string) *Topology {
2012-08-31 16:35:11 +08:00
var data interface{}
err := json.Unmarshal([]byte(topologyLayout), &data)
if err != nil {
fmt.Println("error:", err)
}
2012-09-01 17:20:59 +08:00
//need to connect all nodes first before server adding volumes
topo, err := NewTopology("mynetwork", "/etc/weed.conf", sequence.NewMemorySequencer(), 234, 5)
2013-02-27 14:54:22 +08:00
if err != nil {
fmt.Println("error:", err)
}
2012-08-31 16:35:11 +08:00
mTopology := data.(map[string]interface{})
for dcKey, dcValue := range mTopology {
dc := NewDataCenter(dcKey)
2012-08-31 16:35:11 +08:00
dcMap := dcValue.(map[string]interface{})
topo.LinkChildNode(dc)
2012-08-31 16:35:11 +08:00
for rackKey, rackValue := range dcMap {
rack := NewRack(rackKey)
2012-08-31 16:35:11 +08:00
rackMap := rackValue.(map[string]interface{})
dc.LinkChildNode(rack)
2012-08-31 16:35:11 +08:00
for serverKey, serverValue := range rackMap {
2012-09-09 07:25:44 +08:00
server := NewDataNode(serverKey)
2012-08-31 16:35:11 +08:00
serverMap := serverValue.(map[string]interface{})
rack.LinkChildNode(server)
2012-08-31 16:35:11 +08:00
for _, v := range serverMap["volumes"].([]interface{}) {
2012-09-01 17:20:59 +08:00
m := v.(map[string]interface{})
2013-02-27 14:54:22 +08:00
vi := storage.VolumeInfo{
2013-11-12 18:21:22 +08:00
Id: storage.VolumeId(int64(m["id"].(float64))),
Size: uint64(m["size"].(float64)),
Collection: "testingCollection",
Version: storage.CurrentVersion}
2012-09-21 16:31:52 +08:00
server.AddOrUpdateVolume(vi)
2012-08-31 16:35:11 +08:00
}
server.UpAdjustMaxVolumeCountDelta(int(serverMap["limit"].(float64)))
2012-08-31 16:35:11 +08:00
}
}
}
return topo
}
2012-08-29 16:42:24 +08:00
func TestRemoveDataCenter(t *testing.T) {
topo := setup(topologyLayout)
topo.UnlinkChildNode(NodeId("dc2"))
if topo.GetActiveVolumeCount() != 15 {
2012-09-01 17:20:59 +08:00
t.Fail()
}
topo.UnlinkChildNode(NodeId("dc3"))
if topo.GetActiveVolumeCount() != 12 {
2012-09-01 17:20:59 +08:00
t.Fail()
}
}