示例#1
0
文件: mocks.go 项目: yangzhares/core
// mockAgent creates the agent with the configuration
// needed for tests without the need to go through
// configuration files.
func mockAgent() Agent {

	host0 := common.HostMessage{Ip: "172.17.0.1", RomanaIp: "127.0.0.1/8"}

	romanaIP, romanaNet, _ := net.ParseCIDR(host0.RomanaIp)
	networkConfig := &NetworkConfig{}
	networkConfig.currentHostIP = net.ParseIP(host0.Ip)
	networkConfig.currentHostGW = romanaIP
	networkConfig.currentHostGWNet = *romanaNet
	networkConfig.currentHostGWNetSize, _ = romanaNet.Mask.Size()
	networkConfig.currentHostIndex = 0

	host1 := common.HostMessage{Ip: "192.168.0.12", RomanaIp: "10.65.0.0/16"}
	networkConfig.hosts = []common.HostMessage{host0, host1}

	dc := common.Datacenter{}
	dc.Cidr = "10.0.0.0/8"
	dc.PortBits = 8
	dc.TenantBits = 4
	dc.SegmentBits = 4
	dc.EndpointSpaceBits = 0
	dc.EndpointBits = 8

	networkConfig.dc = dc

	agent := &Agent{networkConfig: networkConfig}
	helper := NewAgentHelper(agent)
	agent.Helper = &helper

	return *agent
}
示例#2
0
文件: topology.go 项目: romana/core
// SetConfig implements SetConfig function of the Service interface.
// Returns an error if cannot connect to the data store
func (topology *TopologySvc) SetConfig(config common.ServiceConfig) error {
	topology.config = config
	dcMap := config.ServiceSpecific["datacenter"].(map[string]interface{})
	dc := common.Datacenter{}
	dc.IpVersion = uint(dcMap["ip_version"].(float64))
	if dc.IpVersion != 4 {
		return common.NewError("Only IPv4 is currently supported.")
	}
	dc.Cidr = dcMap["cidr"].(string)
	_, ipNet, err := net.ParseCIDR(dc.Cidr)
	if err != nil {
		return err
	}
	prefixBits, _ := ipNet.Mask.Size()
	dc.PrefixBits = uint(prefixBits)

	dc.PortBits = uint(dcMap["host_bits"].(float64))
	dc.TenantBits = uint(dcMap["tenant_bits"].(float64))
	dc.SegmentBits = uint(dcMap["segment_bits"].(float64))
	dc.EndpointBits = uint(dcMap["endpoint_bits"].(float64))
	dc.EndpointSpaceBits = uint(dcMap["endpoint_space_bits"].(float64))
	if dc.EndpointBits == 0 {
		return common.NewError("Endpoint bits may not be 0")
	}
	bitSum := dc.PrefixBits + dc.PortBits + dc.TenantBits + dc.SegmentBits + dc.EndpointBits + dc.EndpointSpaceBits
	if bitSum != 32 {
		bitSumStr := fmt.Sprintf("%s+%d+%d+%d+%d+%d", dc.Cidr, dc.PortBits, dc.TenantBits, dc.SegmentBits, dc.EndpointBits, dc.EndpointSpaceBits)
		return common.NewError("Sum of prefix, port, tenant, segment, endpoint and endpoint space bits must be exactly 32, but it is %s=%d", bitSumStr, bitSum)
	}

	// TODO this should have worked but it doesn't...
	//	err := mapstructure.Decode(dcMap, &dc)
	//	if err != nil {
	//		return err
	//	}
	log.Printf("Datacenter information: was %s, decoded to %+v\n", dcMap, dc)
	topology.datacenter = &dc
	storeConfig := config.ServiceSpecific["store"].(map[string]interface{})
	topology.store = topoStore{}
	topology.store.ServiceStore = &topology.store
	return topology.store.SetConfig(storeConfig)
}
示例#3
0
// SetConfig implements SetConfig function of the Service interface.
// Returns an error if cannot connect to the data store
func (topology *TopologySvc) SetConfig(config common.ServiceConfig) error {
	log.Println(config)
	topology.config = config
	dcMap := config.ServiceSpecific["datacenter"].(map[string]interface{})
	dc := common.Datacenter{}
	dc.IpVersion = uint(dcMap["ip_version"].(float64))
	dc.Cidr = dcMap["cidr"].(string)
	_, ipNet, err := net.ParseCIDR(dc.Cidr)
	if err != nil {
		return err
	}
	prefixBits, _ := ipNet.Mask.Size()
	dc.PrefixBits = uint(prefixBits)

	dc.PortBits = uint(dcMap["host_bits"].(float64))
	dc.TenantBits = uint(dcMap["tenant_bits"].(float64))
	dc.SegmentBits = uint(dcMap["segment_bits"].(float64))
	dc.EndpointBits = uint(dcMap["endpoint_bits"].(float64))
	dc.EndpointSpaceBits = uint(dcMap["endpoint_space_bits"].(float64))
	// TODO this should have worked but it doesn't...
	//	err := mapstructure.Decode(dcMap, &dc)
	//	if err != nil {
	//		return err
	//	}
	log.Printf("Datacenter information: was %s, decoded to %s\n", dcMap, dc)
	topology.datacenter = &dc
	storeConfig := config.ServiceSpecific["store"].(map[string]interface{})
	storeType := strings.ToLower(storeConfig["type"].(string))
	switch storeType {
	case "mysql":
		topology.store = &mysqlStore{}

	case "mock":
		topology.store = &mockStore{}

	default:
		return errors.New("Unknown store type: " + storeType)
	}
	return topology.store.setConfig(storeConfig)
}
示例#4
0
文件: mocks.go 项目: romana/core
// mockAgent creates the agent with the configuration
// needed for tests without the need to go through
// configuration files.
func mockAgent() Agent {

	host0 := common.Host{Ip: "172.17.0.1", RomanaIp: "127.0.0.1/8"}

	// romanaIP, romanaNet, _ := net.ParseCIDR(host0.RomanaIp)

	networkConfig := &NetworkConfig{}
	networkConfig.romanaGW = net.ParseIP(host0.Ip)

	host1 := common.Host{Ip: "192.168.0.12", RomanaIp: "10.65.0.0/16"}
	networkConfig.otherHosts = []common.Host{host1}

	dc := common.Datacenter{}
	dc.Cidr = "10.0.0.0/8"
	dc.PortBits = 8
	dc.TenantBits = 4
	dc.SegmentBits = 4
	dc.EndpointSpaceBits = 0
	dc.EndpointBits = 8

	networkConfig.dc = dc
	agent := &Agent{networkConfig: networkConfig}
	helper := NewAgentHelper(agent)
	agent.Helper = &helper

	storeConfig := common.ServiceConfig{ServiceSpecific: map[string]interface{}{
		"type":     "sqlite3",
		"database": "/tmp/agent.db"}}
	agent.store = agentStore{}
	agent.store.ServiceStore = &agent.store
	agent.store.SetConfig(storeConfig.ServiceSpecific)

	agent.CreateSchema(true) // overwrite

	return *agent
}
示例#5
0
文件: ipam_test.go 项目: romana/core
func (s *MySuite) TestStore(c *check.C) {
	var err error

	store := ipamStore{}
	store.ServiceStore = &store

	storeConfig := make(map[string]interface{})
	storeConfig["type"] = "sqlite3"
	storeConfig["database"] = s.RomanaTestSuite.GetMockSqliteFile("ipam")
	err = store.SetConfig(storeConfig)
	c.Assert(err, check.IsNil)
	cidr := "10.0.0.0/8"
	ip, _, _ := net.ParseCIDR(cidr)

	dc := common.Datacenter{Cidr: cidr, IpVersion: 4, Prefix: common.IPv4ToInt(ip), PrefixBits: 8, PortBits: 8, TenantBits: 4, SegmentBits: 4}

	_, network, _ := net.ParseCIDR("10.1.0.0/16")
	hostIpInt := common.IPv4ToInt(network.IP)

	segmentBitShift := uint64(8)
	tenantBitShift := uint64(segmentBitShift + 4)
	upToEndpointIpInt := hostIpInt | (1 << tenantBitShift) | (1 << segmentBitShift)
	// 253
	// 127
	// 63
	// 31
	for _, stride := range []uint{0, 1, 2, 4} {
		//	for _, stride := range []uint{0} {
		err = store.CreateSchema(true)
		if err != nil {
			panic(err)
		}
		err = store.Connect()
		if err != nil {
			panic(err)
		}
		dc.EndpointSpaceBits = stride
		dc.EndpointBits = 8 - stride
		endpoint := &Endpoint{Id: 0, EffectiveNetworkID: 0, HostId: "X", SegmentID: "X", TenantID: "X"}
		i := uint(1)
		firstIp := ""
		var upperBound uint
		switch stride {
		case 0:
			upperBound = 253
		case 1:
			upperBound = 127
		case 2:
			upperBound = 64
		case 4:
			upperBound = 16
		}
		log.Printf("For %d/%d go until 1= %d", dc.EndpointBits, stride, upperBound)
		for i = 1; i <= uint(upperBound); i++ {
			endpoint.Id = 0
			msg := fmt.Sprintf("For stride %d, endpoint bits %d, try %d\n", stride, dc.EndpointBits, i)
			log.Println(msg)
			err = store.addEndpoint(endpoint, upToEndpointIpInt, dc)
			if err != nil {
				c.Error(fmt.Sprintf("Unexpected error on try %d: %v", i, err))
				c.FailNow()
			}
			log.Printf("%s: Got IP: %s (effective network ID %d)", msg, endpoint.Ip, endpoint.EffectiveNetworkID)
			if firstIp == "" {
				firstIp = endpoint.Ip
			}

		}
		// Here we have reached the end...
		endpoint.Id = 0
		err = store.addEndpoint(endpoint, upToEndpointIpInt, dc)
		if err == nil {
			c.Error(fmt.Sprintf("Expected error, but got %+v", endpoint))
			c.FailNow()
		}

		endpoint.Id = 0
		_, err = store.deleteEndpoint(firstIp)
		if err != nil {
			c.Error(fmt.Sprintf("Unexpected error on try %d: %v", i, err))
			c.FailNow()
		}
		endpoint.Id = 0
		err = store.addEndpoint(endpoint, upToEndpointIpInt, dc)
		if err != nil {
			c.Error(fmt.Sprintf("Unexpected error on try %d: %v", i, err))
			c.Fail()
		}
		c.Assert(endpoint.Ip, check.Equals, firstIp)
		if c.Failed() {
			return
		}

		endpoint.Id = 0
		err = store.addEndpoint(endpoint, upToEndpointIpInt, dc)
		if err == nil {
			c.Error(fmt.Sprintf("Expected error, but got %+v", endpoint))
			c.FailNow()
		}

	}
}