コード例 #1
0
ファイル: agent.go プロジェクト: jhuie-zenoss/serviced
/*
registerIPs does the actual work of determining the IPs on the host. Parameters are the hostId for this host
and the function used to send the found IPs
*/
func registerIPs(hostId string, sendFn SendHostIPs) error {

	interfaces, err := net.Interfaces()
	if err != nil {
		glog.Error("Problem reading interfaces: ", err)
		return err
	}
	hostIPResources := make([]dao.HostIPResource, 0, len(interfaces))
	for _, iface := range interfaces {
		addrs, err := iface.Addrs()
		if err != nil {
			glog.Errorf("Problem reading address for interface %s: %s", iface.Name, err)
			return err
		}
		for _, addr := range addrs {
			//send address to Master
			hostIp := dao.HostIPResource{}
			hostIp.IPAddress = addr.String()
			hostIp.InterfaceName = iface.Name
			hostIPResources = append(hostIPResources, hostIp)
		}
	}
	var unused int
	glog.V(4).Infof("Agent registering IPs %v", hostIPResources)
	hostIps := dao.HostIPs{}
	hostIps.HostId = hostId
	hostIps.IPs = hostIPResources
	if err := sendFn(hostIps, &unused); err != nil {
		glog.Errorf("Error registering IPs %v", err)
	}
	return nil
}
コード例 #2
0
func TestDaoRegisterHostIPsNoHost(t *testing.T) {
	ips := dao.HostIPs{}
	ips.HostId = HOSTID
	ips.IPs = []dao.HostIPResource{dao.HostIPResource{"valid", "testip", "ifname", []dao.AssignedPort{}}}

	err := controlPlaneDao.RegisterHostIPs(ips, &unused)
	if !strings.HasPrefix(err.Error(), "Host not found for id "+HOSTID) {
		t.Errorf("Unexpected error: %v", err)
	}
}
コード例 #3
0
func TestDaoGetHostIPsWithIPs(t *testing.T) {
	hostIPs := dao.HostIPs{}
	//Add host to test scenario where host exists but no IP resource registered
	host := dao.Host{}
	host.Id = HOSTID
	err = controlPlaneDao.AddHost(host, &id)
	defer controlPlaneDao.RemoveHost(HOSTID, &unused)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
		return
	}

	//store a HostIPs object
	hostIPs.Id = HOSTIPSID
	hostIPs.HostId = HOSTID
	hostIPs.IPs = []dao.HostIPResource{dao.HostIPResource{"valid", "testip", "ifname", []dao.AssignedPort{}}}
	_, err = newHostIPs(hostIPs.Id, hostIPs)
	defer deleteHostIPs(HOSTIPSID)
	if err != nil {
		t.Errorf("Error creating newHostIPs")
		return
	}

	err = controlPlaneDao.GetHostIPs(HOSTID, &hostIPs)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
		return
	}
	if hostIPs.Id != HOSTIPSID {
		t.Errorf("Expected host ips id %v , got %v", HOSTIPSID, hostIPs.Id)
	}
	if hostIPs.HostId != HOSTID {
		t.Errorf("Expected host id %v, got %v", HOSTID, hostIPs.Id)
	}
	if hostIPs.PoolId != "" {
		t.Errorf("Expected uninitialzed poolid, got %v", hostIPs.PoolId)
	}
	if hostIPs.PoolId != "" {
		t.Errorf("Expected uninitialzed id, got %v", hostIPs.Id)
	}
	if len(hostIPs.IPs) != 1 {
		t.Errorf("Expected %v IPs, got %v", 1, len(hostIPs.IPs))
	}

}
コード例 #4
0
func TestDaoRegisterHostIPs(t *testing.T) {
	//Add host to test scenario where host exists but no IP resource registered
	host := dao.Host{}
	host.Id = HOSTID
	err = controlPlaneDao.AddHost(host, &id)
	defer controlPlaneDao.RemoveHost(HOSTID, &unused)

	ips := dao.HostIPs{}
	ips.HostId = HOSTID
	ips.IPs = []dao.HostIPResource{dao.HostIPResource{"valid", "testip", "ifname", []dao.AssignedPort{}}}

	err := controlPlaneDao.RegisterHostIPs(ips, &unused)
	//remove any registered
	defer removeHostIPs(HOSTID)

	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	//verify registration
	ips = dao.HostIPs{}
	controlPlaneDao.GetHostIPs(HOSTID, &ips)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
	if len(ips.IPs) != 1 {
		t.Errorf("Expected 1 HostIPResource, found: %v", len(ips.IPs))
	}
	ipResource := ips.IPs[0]
	if ipResource.State != "valid" {
		t.Errorf("Unexpected state: %v", ipResource.State)
	}
	if ipResource.IPAddress != "testip" {
		t.Errorf("Unexpected IPAddress: %v", ipResource.IPAddress)
	}
	if ipResource.InterfaceName != "ifname" {
		t.Errorf("Unexpected InterfaceName: %v", ipResource.InterfaceName)
	}

}
コード例 #5
0
func TestDaoRegisterHostIPsMerge(t *testing.T) {
	//Add host to test scenario where host exists but no IP resource registered
	host := dao.Host{}
	host.Id = HOSTID
	err = controlPlaneDao.AddHost(host, &id)
	defer controlPlaneDao.RemoveHost(HOSTID, &unused)

	ips := dao.HostIPs{}
	ips.HostId = HOSTID
	ips.IPs = []dao.HostIPResource{
		dao.HostIPResource{"valid", "testip", "ifname", []dao.AssignedPort{}},
		dao.HostIPResource{"deleted", "testip1", "ifname", []dao.AssignedPort{}},
	}

	err := controlPlaneDao.RegisterHostIPs(ips, &unused)
	//remove any registered
	defer removeHostIPs(HOSTID)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	ips.IPs = []dao.HostIPResource{
		dao.HostIPResource{"valid", "testip2", "ifname2", []dao.AssignedPort{}},
		dao.HostIPResource{"valid", "testip1", "ifname", []dao.AssignedPort{}},
	}
	err = controlPlaneDao.RegisterHostIPs(ips, &unused)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}

	//verify registration
	ips = dao.HostIPs{}
	controlPlaneDao.GetHostIPs(HOSTID, &ips)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
	if len(ips.IPs) != 3 {
		t.Errorf("Expected 3 HostIPResources, found: %v", len(ips.IPs))
	}
	for _, ipResource := range ips.IPs {
		addr := ipResource.IPAddress
		switch addr {
		case "testip":
			{
				if ipResource.State != "deleted" {
					t.Errorf("Unexpected state: %v", ipResource.State)
				}

				if ipResource.InterfaceName != "ifname" {
					t.Errorf("Unexpected InterfaceName: %v", ipResource.InterfaceName)
				}
			}
		case "testip1":
			{
				if ipResource.State != "valid" {
					t.Errorf("Unexpected state: %v", ipResource.State)
				}

				if ipResource.InterfaceName != "ifname" {
					t.Errorf("Unexpected InterfaceName: %v", ipResource.InterfaceName)
				}
			}
		case "testip2":
			{
				if ipResource.State != "valid" {
					t.Errorf("Unexpected state: %v", ipResource.State)
				}

				if ipResource.InterfaceName != "ifname2" {
					t.Errorf("Unexpected InterfaceName: %v", ipResource.InterfaceName)
				}
			}
		default:
			{
				t.Errorf("Unexpected IP resource %v", ipResource)
			}
		}

	}
}