Skip to content

Commit

Permalink
topology: add testcases and todos to handle a specific condition later.
Browse files Browse the repository at this point in the history
  • Loading branch information
pritesh committed Sep 20, 2016
1 parent b8d34b1 commit 2d4ed2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
12 changes: 11 additions & 1 deletion topology/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,18 @@ func (topoStore *topoStore) addHost(dc *common.Datacenter, host *common.Host) er
tx := topoStore.DbStore.Db.Begin()

var allHostsID []uint64
if err := tx.Select("id").Find(&allHostsID).Error; err != nil {
if err := tx.Table("hosts").Pluck("id", &allHostsID).Error; err != nil {
tx.Rollback()
return err
}

id := findFirstAvaiableID(allHostsID)
host.RomanaIp, err = getNetworkFromID(id, dc.PortBits, dc.Cidr)
// TODO: auto generation of romana cidr doesn't handle previously
// allocated cidrs currently, thus it needs to be handled
// here so that no 2 hosts get same or overlapping cidrs.
// here check needs to be in place to detect all manually
// inserted romana cidrs for overlap.
if err != nil {
tx.Rollback()
return err
Expand All @@ -193,6 +198,11 @@ func (topoStore *topoStore) addHost(dc *common.Datacenter, host *common.Host) er
}
tx.Commit()
} else {
// TODO: auto generation of romana cidr doesn't handle previously
// allocated cidrs currently, thus it needs to be handled
// here so that no 2 hosts get same or overlapping cidrs.
// here check needs to be in place that auto generated cidrs
// overlap with this manually assigned one or not.
topoStore.DbStore.Db.NewRecord(*host)
db := topoStore.DbStore.Db.Create(host)
if err := common.GetDbErrors(db); err != nil {
Expand Down
25 changes: 23 additions & 2 deletions topology/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,30 @@ func (s *MySuite) TestTopology(c *check.C) {
c.Assert(newHostResp.Ip, check.Equals, "10.10.10.11")
c.Assert(newHostResp.ID, check.Equals, uint64(2))

newHostReqWithoutRomanaIP := common.Host{Ip: "10.10.10.12", AgentPort: 9999, Name: "host12"}
newHostRespWithoutRomanaIP := common.Host{}
client.Post(hostsRelURL, newHostReqWithoutRomanaIP, &newHostRespWithoutRomanaIP)
myLog(c, "Response: ", newHostRespWithoutRomanaIP)

c.Assert(newHostRespWithoutRomanaIP.Ip, check.Equals, "10.10.10.12")
c.Assert(newHostRespWithoutRomanaIP.RomanaIp, check.Equals, "10.2.0.0/16")
c.Assert(newHostRespWithoutRomanaIP.ID, check.Equals, uint64(3))

newHostReqWithoutRomanaIP = common.Host{Ip: "10.10.10.13", AgentPort: 9999, Name: "host13"}
newHostRespWithoutRomanaIP = common.Host{}
client.Post(hostsRelURL, newHostReqWithoutRomanaIP, &newHostRespWithoutRomanaIP)
myLog(c, "Response: ", newHostRespWithoutRomanaIP)

c.Assert(newHostRespWithoutRomanaIP.Ip, check.Equals, "10.10.10.13")
c.Assert(newHostRespWithoutRomanaIP.RomanaIp, check.Equals, "10.3.0.0/16")
c.Assert(newHostRespWithoutRomanaIP.ID, check.Equals, uint64(4))

// TODO: auto generation of romana cidr currently don't
// handle manually assigned one gracefully, thus tests
// to be added here once that support is added.

var hostList2 []common.Host
client.Get(hostsRelURL, &hostList2)
myLog(c, "Host list: ", hostList2)
c.Assert(len(hostList2), check.Equals, 2)

c.Assert(len(hostList2), check.Equals, 4)
}

0 comments on commit 2d4ed2d

Please sign in to comment.