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))
	}

}
func testDaoHostExists(t *testing.T) {
	found, err := hostExists("blam")
	if found || err != nil {
		t.Errorf("Found %v; error: %v", found, err)
		t.FailNow()
	}

	host := dao.Host{}
	host.Id = "existsTest"
	err = controlPlaneDao.AddHost(host, &id)
	defer controlPlaneDao.RemoveHost("existsTest", &unused)

	found, err = hostExists(id)
	if !found || err != nil {
		t.Errorf("Found %v; error: %v", found, err)
	}

}
func TestDao_GetHost(t *testing.T) {
	controlPlaneDao.RemoveHost("default", &unused)

	host := dao.NewHost()
	host.Id = "default"
	controlPlaneDao.AddHost(*host, &id)

	var result = dao.Host{}
	err := controlPlaneDao.GetHost("default", &result)
	result.CreatedAt = host.CreatedAt
	result.UpdatedAt = host.UpdatedAt
	if err == nil {
		if *host != result {
			t.Errorf("Unexpected Host: expected=%+v, actual=%+v", host, result)
		}
	} else {
		t.Errorf("Unexpected Error Retrieving Host: err=%s", err)
	}
}
func TestDaoGetHostIPsNotFound(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
	}

	err = controlPlaneDao.GetHostIPs(HOSTID, &hostIPs)
	if err != nil {
		t.Errorf("Unexpected error: %v", err)
	}
	if hostIPs.Id != "" {
		t.Errorf("Expected uninitialzed id, got %v", hostIPs.Id)
	}
}
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)
	}

}
func TestDao_AddHost(t *testing.T) {
	host := dao.Host{}
	controlPlaneDao.RemoveHost("default", &unused)
	err := controlPlaneDao.AddHost(host, &id)
	if err == nil {
		t.Errorf("Expected failure to create host %-v", host)
		t.Fail()
	}

	host.Id = "default"
	err = controlPlaneDao.AddHost(host, &id)
	if err != nil {
		t.Errorf("Failure creating host %-v with error: %s", host, err)
		t.Fail()
	}

	err = controlPlaneDao.AddHost(host, &id)
	if err == nil {
		t.Errorf("Expected error creating redundant host %-v", host)
		t.Fail()
	}
}
Example #7
0
func RestAddHost(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var payload dao.Host
	var hostId string
	err := r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Infof("Could not decode host payload: %v", err)
		RestBadRequest(w)
		return
	}
	// Save the pool ID and IP address for later. GetInfo wipes these
	pool := payload.PoolId
	ipAddr := payload.IpAddr
	remoteClient, err := serviced.NewAgentClient(payload.IpAddr)
	if err != nil {
		glog.Errorf("Could not create connection to host %s: %v", payload.IpAddr, err)
		RestServerError(w)
		return
	}

	err = remoteClient.GetInfo(0, &payload)
	if err != nil {
		glog.Errorf("Unable to get remote host info: %v", err)
		RestBadRequest(w)
		return
	}
	// Reset the pool ID and IP address
	payload.PoolId = pool
	parts := strings.Split(ipAddr, ":")
	payload.IpAddr = parts[0]

	err = client.AddHost(payload, &hostId)
	if err != nil {
		glog.Errorf("Unable to add host: %v", err)
		RestServerError(w)
		return
	}
	glog.V(0).Info("Added host ", hostId)
	w.WriteJson(&SimpleResponse{"Added host", hostLinks(hostId)})
}
func TestDao_UpdateHost(t *testing.T) {
	controlPlaneDao.RemoveHost("default", &unused)

	host := dao.NewHost()
	host.Id = "default"
	controlPlaneDao.AddHost(*host, &id)

	host.Name = "hostname"
	host.IpAddr = "127.0.0.1"
	err := controlPlaneDao.UpdateHost(*host, &unused)
	if err != nil {
		t.Errorf("Failure updating host %-v with error: %s", host, err)
		t.Fail()
	}

	var result = dao.Host{}
	controlPlaneDao.GetHost("default", &result)
	result.CreatedAt = host.CreatedAt
	result.UpdatedAt = host.UpdatedAt
	if *host != result {
		t.Errorf("%+v != %+v", result, host)
		t.Fail()
	}
}
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)
			}
		}

	}
}