Chris Lu
12 years ago
3 changed files with 76 additions and 33 deletions
-
34weed-fs/src/pkg/topology/configuration.go
-
42weed-fs/src/pkg/topology/configuration_test.go
-
33weed-fs/src/pkg/topology/topo_test.go
@ -0,0 +1,34 @@ |
|||
package topology |
|||
|
|||
import ( |
|||
"encoding/xml" |
|||
) |
|||
|
|||
type rack struct { |
|||
Name string `xml:"name,attr"` |
|||
Ips []string `xml:"Ip"` |
|||
} |
|||
type dataCenter struct { |
|||
Name string `xml:"name,attr"` |
|||
Racks []rack `xml:"Rack"` |
|||
} |
|||
type topology struct { |
|||
DataCenters []dataCenter `xml:"DataCenter"` |
|||
} |
|||
type configuration struct { |
|||
XMLName xml.Name `xml:"Configuration"` |
|||
Topo topology `xml:"Topology"` |
|||
} |
|||
|
|||
func NewConfiguration(b []byte) (*configuration, error){ |
|||
c := &configuration{} |
|||
err := xml.Unmarshal(b, c) |
|||
return c, err |
|||
} |
|||
|
|||
func (c *configuration) String() string{ |
|||
if b, e := xml.MarshalIndent(c, " ", " "); e==nil { |
|||
return string(b) |
|||
} |
|||
return "" |
|||
} |
@ -0,0 +1,42 @@ |
|||
package topology |
|||
|
|||
import ( |
|||
"fmt" |
|||
"testing" |
|||
) |
|||
|
|||
func TestLoadConfiguration(t *testing.T) { |
|||
|
|||
confContent := ` |
|||
|
|||
<?xml version="1.0" encoding="UTF-8" ?> |
|||
<Configuration> |
|||
<Topology> |
|||
<DataCenter name="dc1"> |
|||
<Rack name="rack1"> |
|||
<Ip>192.168.1.1</Ip> |
|||
</Rack> |
|||
</DataCenter> |
|||
<DataCenter name="dc2"> |
|||
<Rack name="rack1"> |
|||
<Ip>192.168.1.2</Ip> |
|||
</Rack> |
|||
<Rack name="rack2"> |
|||
<Ip>192.168.1.3</Ip> |
|||
<Ip>192.168.1.4</Ip> |
|||
</Rack> |
|||
</DataCenter> |
|||
</Topology> |
|||
</Configuration> |
|||
` |
|||
c, err := NewConfiguration([]byte(confContent)) |
|||
|
|||
fmt.Printf("%s\n", c) |
|||
if err!=nil{ |
|||
t.Fatalf("unmarshal error:%s",err.Error()) |
|||
} |
|||
|
|||
if len(c.Topo.DataCenters) <= 0 || c.Topo.DataCenters[0].Name != "dc1" { |
|||
t.Fatalf("unmarshal error:%s",c) |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue