Пример #1
0
func TestDao_StartService(t *testing.T) {
	controlPlaneDao.RemoveService("0", &unused)
	controlPlaneDao.RemoveService("01", &unused)
	controlPlaneDao.RemoveService("011", &unused)
	controlPlaneDao.RemoveService("02", &unused)

	s0, _ := dao.NewService()
	s0.Id = "0"
	s0.DesiredState = dao.SVC_STOP

	s01, _ := dao.NewService()
	s01.Id = "01"
	s01.ParentServiceId = "0"
	s01.DesiredState = dao.SVC_STOP

	s011, _ := dao.NewService()
	s011.Id = "011"
	s011.ParentServiceId = "01"
	s011.DesiredState = dao.SVC_STOP

	s02, _ := dao.NewService()
	s02.Id = "02"
	s02.ParentServiceId = "0"
	s02.DesiredState = dao.SVC_STOP

	controlPlaneDao.AddService(*s0, &id)
	controlPlaneDao.AddService(*s01, &id)
	controlPlaneDao.AddService(*s011, &id)
	controlPlaneDao.AddService(*s02, &id)

	var unusedString string
	controlPlaneDao.StartService("0", &unusedString)

	service := dao.Service{}
	controlPlaneDao.GetService("0", &service)
	if service.DesiredState != dao.SVC_RUN {
		t.Errorf("Service: 0 not requested to run: %+v", service)
		t.Fail()
	}

	controlPlaneDao.GetService("01", &service)
	if service.DesiredState != dao.SVC_RUN {
		t.Errorf("Service: 01 not requested to run: %+v", service)
		t.Fail()
	}

	controlPlaneDao.GetService("011", &service)
	if service.DesiredState != dao.SVC_RUN {
		t.Errorf("Service: 011 not requested to run: %+v", service)
		t.Fail()
	}

	controlPlaneDao.GetService("02", &service)
	if service.DesiredState != dao.SVC_RUN {
		t.Errorf("Service: 02 not requested to run: %+v", service)
		t.Fail()
	}
}
Пример #2
0
func TestDao_GetTenantId(t *testing.T) {
	controlPlaneDao.RemoveService("0", &unused)
	controlPlaneDao.RemoveService("01", &unused)
	controlPlaneDao.RemoveService("011", &unused)

	var err error
	var tenantId string
	err = controlPlaneDao.GetTenantId("0", &tenantId)
	if err == nil {
		t.Errorf("Expected failure for getting tenantId for 0")
		t.Fail()
	}

	s0, _ := dao.NewService()
	s0.Id = "0"

	s01, _ := dao.NewService()
	s01.Id = "01"
	s01.ParentServiceId = "0"

	s011, _ := dao.NewService()
	s011.Id = "011"
	s011.ParentServiceId = "01"

	controlPlaneDao.AddService(*s0, &id)
	controlPlaneDao.AddService(*s01, &id)
	controlPlaneDao.AddService(*s011, &id)

	tenantId = ""
	err = controlPlaneDao.GetTenantId("0", &tenantId)
	if err != nil || tenantId != "0" {
		t.Errorf("Failure getting tenantId for 0, err=%s, tenantId=%s", err, tenantId)
		t.Fail()
	}

	tenantId = ""
	err = controlPlaneDao.GetTenantId("01", &tenantId)
	if err != nil || tenantId != "0" {
		t.Errorf("Failure getting tenantId for 0, err=%s, tenantId=%s", err, tenantId)
		t.Fail()
	}

	tenantId = ""
	err = controlPlaneDao.GetTenantId("011", &tenantId)
	if err != nil || tenantId != "0" {
		t.Errorf("Failure getting tenantId for 0, err=%s, tenantId=%s", err, tenantId)
		t.Fail()
	}
}
Пример #3
0
func TestDao_GetServices(t *testing.T) {
	controlPlaneDao.RemoveService("0", &unused)
	controlPlaneDao.RemoveService("1", &unused)
	controlPlaneDao.RemoveService("2", &unused)
	controlPlaneDao.RemoveService("3", &unused)
	controlPlaneDao.RemoveService("4", &unused)
	controlPlaneDao.RemoveService("01", &unused)
	controlPlaneDao.RemoveService("011", &unused)
	controlPlaneDao.RemoveService("02", &unused)
	controlPlaneDao.RemoveService("default", &unused)

	service, _ := dao.NewService()
	service.Id = "default"
	service.Name = "name"
	service.Description = "description"
	service.Instances = 0
	controlPlaneDao.AddService(*service, &id)

	var result []*dao.Service
	err := controlPlaneDao.GetServices(new(dao.EntityRequest), &result)
	if err == nil && len(result) == 1 {
		//XXX the time.Time types fail comparison despite being equal...
		//    as far as I can tell this is a limitation with Go
		result[0].UpdatedAt = service.UpdatedAt
		result[0].CreatedAt = service.CreatedAt
		if !result[0].Equals(service) {
			t.Errorf("expected [%+v] actual=%+v", *service, result)
			t.Fail()
		}
	} else {
		t.Errorf("Error Retrieving Services: err=%s, result=%s", err, result)
		t.Fail()
	}
}
Пример #4
0
func TestDao_UpdateService(t *testing.T) {
	controlPlaneDao.RemoveService("default", &unused)

	service, _ := dao.NewService()
	service.Id = "default"
	controlPlaneDao.AddService(*service, &id)

	service.Name = "name"
	err := controlPlaneDao.UpdateService(*service, &unused)
	if err != nil {
		t.Errorf("Failure updating service %-v with error: %s", service, err)
		t.Fail()
	}

	result := dao.Service{}
	controlPlaneDao.GetService("default", &result)
	//XXX the time.Time types fail comparison despite being equal...
	//    as far as I can tell this is a limitation with Go
	result.UpdatedAt = service.UpdatedAt
	result.CreatedAt = service.CreatedAt
	if !service.Equals(&result) {
		t.Errorf("Expected Service %+v, Actual Service %+v", result, *service)
		t.Fail()
	}
}
Пример #5
0
func TestDao_GetService(t *testing.T) {
	controlPlaneDao.RemoveService("default", &unused)

	service, _ := dao.NewService()
	service.Id = "default"
	controlPlaneDao.AddService(*service, &id)

	var result dao.Service
	err := controlPlaneDao.GetService("default", &result)
	//XXX the time.Time types fail comparison despite being equal...
	//    as far as I can tell this is a limitation with Go
	result.UpdatedAt = service.UpdatedAt
	result.CreatedAt = service.CreatedAt
	if err == nil {
		if !service.Equals(&result) {
			t.Errorf("GetService Failed: expected=%+v, actual=%+v", service, result)
		}
	} else {
		t.Errorf("Unexpected Error Retrieving Service: err=%s", err)
	}
}
Пример #6
0
func RestAddService(w *rest.ResponseWriter, r *rest.Request, client *serviced.ControlClient) {
	var payload dao.Service
	var serviceId string
	err := r.DecodeJsonPayload(&payload)
	if err != nil {
		glog.V(1).Info("Could not decode service payload: ", err)
		RestBadRequest(w)
		return
	}
	service, err := dao.NewService()
	if err != nil {
		glog.Errorf("Could not create service: %v", err)
		RestServerError(w)
		return
	}
	now := time.Now()
	service.Name = payload.Name
	service.Description = payload.Description
	service.Context = payload.Context
	service.Tags = payload.Tags
	service.PoolId = payload.PoolId
	service.ImageId = payload.ImageId
	service.Startup = payload.Startup
	service.Instances = payload.Instances
	service.ParentServiceId = payload.ParentServiceId
	service.DesiredState = payload.DesiredState
	service.Launch = payload.Launch
	service.Endpoints = payload.Endpoints
	service.ConfigFiles = payload.ConfigFiles
	service.Volumes = payload.Volumes
	service.CreatedAt = now
	service.UpdatedAt = now

	//for each endpoint, evaluate it's Application
	if err = service.EvaluateEndpointTemplates(client); err != nil {
		glog.Errorf("Unable to evaluate service endpoints: %v", err)
		RestServerError(w)
		return
	}

	//add the service to the data store
	err = client.AddService(*service, &serviceId)
	if err != nil {
		glog.Errorf("Unable to add service: %v", err)
		RestServerError(w)
		return
	}

	//deploy the service, in other words start it
	var unused int
	sduuid, _ := dao.NewUuid()
	deployment := dao.ServiceDeployment{sduuid, "", service.Id, now}
	err = client.AddServiceDeployment(deployment, &unused)
	if err != nil {
		glog.Errorf("Unable to add service deployment: %v", err)
		RestServerError(w)
		return
	}

	glog.V(0).Info("Added service ", serviceId)
	w.WriteJson(&SimpleResponse{"Added service", serviceLinks(serviceId)})
}