|
|
@ -3,8 +3,10 @@ package topology |
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"math/rand" |
|
|
|
"pkg/storage" |
|
|
|
"testing" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
var topologyLayout = ` |
|
|
@ -77,40 +79,41 @@ func setup() *Topology { |
|
|
|
fmt.Println("data:", data) |
|
|
|
printMap(data) |
|
|
|
|
|
|
|
//need to connect all nodes first before server adding volumes
|
|
|
|
//need to connect all nodes first before server adding volumes
|
|
|
|
topo := NewTopology(NodeId("mynetwork")) |
|
|
|
mTopology := data.(map[string]interface{}) |
|
|
|
for dcKey, dcValue := range mTopology { |
|
|
|
dc := NewDataCenter(NodeId(dcKey)) |
|
|
|
dc.Node.parent = &topo.Node |
|
|
|
dcMap := dcValue.(map[string]interface{}) |
|
|
|
topo.Node.AddNode(&dc.Node) |
|
|
|
topo.Node.AddNode(&dc.Node) |
|
|
|
for rackKey, rackValue := range dcMap { |
|
|
|
rack := NewRack(NodeId(rackKey)) |
|
|
|
rack.Node.parent = &dc.Node |
|
|
|
rack.Node.parent = &dc.Node |
|
|
|
rackMap := rackValue.(map[string]interface{}) |
|
|
|
dc.Node.AddNode(&rack.Node) |
|
|
|
dc.Node.AddNode(&rack.Node) |
|
|
|
for serverKey, serverValue := range rackMap { |
|
|
|
server := NewServer(NodeId(serverKey)) |
|
|
|
server.Node.parent = &rack.Node |
|
|
|
server.Node.parent = &rack.Node |
|
|
|
serverMap := serverValue.(map[string]interface{}) |
|
|
|
rack.Node.AddNode(&server.Node) |
|
|
|
rack.Node.AddNode(&server.Node) |
|
|
|
for _, v := range serverMap["volumes"].([]interface{}) { |
|
|
|
m := v.(map[string]interface{}) |
|
|
|
m := v.(map[string]interface{}) |
|
|
|
vi := &storage.VolumeInfo{Id: storage.VolumeId(int64(m["id"].(float64))), Size: int64(m["size"].(float64))} |
|
|
|
server.AddVolume(vi) |
|
|
|
} |
|
|
|
server.Node.AddMaxVolumeCount(int(serverMap["limit"].(float64))) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
fmt.Println("topology:", *topo) |
|
|
|
|
|
|
|
bytes, err := json.Marshal(topo.Node.children) |
|
|
|
if err!=nil{ |
|
|
|
fmt.Println("json error:", err) |
|
|
|
} |
|
|
|
fmt.Println("json topo:", string(bytes)) |
|
|
|
bytes, err := json.Marshal(topo.Node.children) |
|
|
|
if err != nil { |
|
|
|
fmt.Println("json error:", err) |
|
|
|
} |
|
|
|
fmt.Println("json topo:", string(bytes)) |
|
|
|
|
|
|
|
return topo |
|
|
|
} |
|
|
@ -143,34 +146,26 @@ func TestAddVolume(t *testing.T) { |
|
|
|
topo.AddVolume(v) |
|
|
|
} |
|
|
|
|
|
|
|
func TestAddServer(t *testing.T) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestAddRack(t *testing.T) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestAddDataCenter(t *testing.T) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestReserveVolume(t *testing.T) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestRemoveVolume(t *testing.T) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestRemoveServer(t *testing.T) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestRemoveRack(t *testing.T) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func TestRemoveDataCenter(t *testing.T) { |
|
|
|
topo := setup() |
|
|
|
topo.RemoveNode(NodeId("dc2")) |
|
|
|
if topo.activeVolumeCount != 15 { |
|
|
|
t.Fail() |
|
|
|
} |
|
|
|
topo.RemoveNode(NodeId("dc3")) |
|
|
|
if topo.activeVolumeCount != 12 { |
|
|
|
t.Fail() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func TestReserveOneVolume(t *testing.T) { |
|
|
|
topo := setup() |
|
|
|
rand.Seed(time.Now().UnixNano()) |
|
|
|
ret, vid := topo.RandomlyReserveOneVolume() |
|
|
|
fmt.Println("topology:", topo.Node) |
|
|
|
fmt.Println("assigned :", ret) |
|
|
|
fmt.Println("assigned volume id:", vid) |
|
|
|
if topo.reservedVolumeCount != 1 { |
|
|
|
t.Fail() |
|
|
|
} |
|
|
|
} |