func (dt *DaoTest) TestDao_GetTenantId(t *C) {
	var err error
	var tenantId string
	err = dt.Dao.GetTenantId("0", &tenantId)
	if err == nil {
		t.Errorf("Expected failure for getting tenantId for 0")
		t.Fail()
	}

	s0, _ := service.NewService()
	s0.Name = "name0"
	s0.PoolID = "default"
	s0.Launch = "auto"
	s0.ID = "0"
	s0.DeploymentID = "deployment_id"

	s01, _ := service.NewService()
	s01.ID = "01"
	s01.ParentServiceID = "0"
	s01.Name = "name1"
	s01.PoolID = "default"
	s01.Launch = "auto"
	s01.DeploymentID = "deployment_id"

	s011, _ := service.NewService()
	s011.ID = "011"
	s011.ParentServiceID = "01"
	s011.Name = "name2"
	s011.PoolID = "default"
	s011.Launch = "auto"
	s011.DeploymentID = "deployment_id"

	err = dt.Dao.AddService(*s0, &id)
	t.Assert(err, IsNil)
	err = dt.Dao.AddService(*s01, &id)
	t.Assert(err, IsNil)
	err = dt.Dao.AddService(*s011, &id)
	t.Assert(err, IsNil)

	tenantId = ""
	err = dt.Dao.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 = dt.Dao.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 = dt.Dao.GetTenantId("011", &tenantId)
	if err != nil || tenantId != "0" {
		t.Errorf("Failure getting tenantId for 0, err=%s, tenantId=%s", err, tenantId)
		t.Fail()
	}
}
func (dt *DaoTest) TestDao_UpdateServiceWithConfigFile(t *C) {
	svc, _ := service.NewService()
	svc.ID = "default"
	svc.Name = "default0"
	svc.PoolID = "default"
	svc.Launch = "auto"
	svc.DeploymentID = "deployment_id"

	err := dt.Dao.AddService(*svc, &id)
	t.Assert(err, IsNil)
	//Conf file update shouldn't occur because original service didn't have conf files
	confFile := servicedefinition.ConfigFile{Content: "Test content", Filename: "testname"}
	svc.ConfigFiles = map[string]servicedefinition.ConfigFile{"testname": confFile}
	err = dt.Dao.UpdateService(*svc, &unused)
	t.Assert(err, IsNil)

	result := service.Service{}
	dt.Dao.GetService("default", &result)
	t.Assert(0, Equals, len(result.ConfigFiles))

	//test update conf file works
	svc, _ = service.NewService()
	svc.ID = "default_conf"
	svc.Name = "default1"
	svc.PoolID = "default"
	svc.Launch = "auto"
	svc.DeploymentID = "deployment_id"
	svc.OriginalConfigs = map[string]servicedefinition.ConfigFile{"testname": confFile}

	err = dt.Dao.AddService(*svc, &id)
	t.Assert(err, IsNil)
	dt.Dao.GetService("default_conf", &result)
	t.Assert(1, Equals, len(result.ConfigFiles))
	t.Assert(1, Equals, len(result.OriginalConfigs))
	t.Assert(result.ConfigFiles, DeepEquals, svc.OriginalConfigs)
	t.Assert(result.ConfigFiles, DeepEquals, result.OriginalConfigs)

	confFile2 := servicedefinition.ConfigFile{Content: "Test content 2", Filename: "testname"}
	svc.ConfigFiles = map[string]servicedefinition.ConfigFile{"testname": confFile2}
	err = dt.Dao.UpdateService(*svc, &unused)
	t.Assert(err, IsNil)
	dt.Dao.GetService("default_conf", &result)
	t.Assert(1, Equals, len(result.ConfigFiles))
	t.Assert(result.ConfigFiles["testname"], DeepEquals, confFile2)
	t.Assert(result.ConfigFiles, Not(DeepEquals), result.OriginalConfigs)

	//now delete service and re-add, it should have previous modified config file
	err = dt.Dao.RemoveService(svc.ID, &unused)
	t.Assert(err, IsNil)
	err = dt.Dao.AddService(*svc, &id)
	t.Assert(err, IsNil)
	dt.Dao.GetService("default_conf", &result)
	t.Assert(1, Equals, len(result.ConfigFiles))
	t.Assert(result.ConfigFiles["testname"], DeepEquals, confFile2)
	t.Assert(result.ConfigFiles, Not(DeepEquals), result.OriginalConfigs)

}
func (dt *DaoTest) TestDao_GetServices(t *C) {
	svc, _ := service.NewService()
	svc.ID = "default"
	svc.Name = "name"
	svc.PoolID = "default"
	svc.DeploymentID = "deployment_id"
	svc.Launch = "auto"
	svc.Description = "description"
	svc.Instances = 0

	err := dt.Dao.AddService(*svc, &id)
	t.Assert(err, IsNil)

	var result []service.Service
	var serviceRequest dao.ServiceRequest
	err = dt.Dao.GetServices(serviceRequest, &result)
	t.Assert(err, IsNil)
	t.Assert(len(result), Equals, 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 = svc.UpdatedAt
	result[0].CreatedAt = svc.CreatedAt
	if !result[0].Equals(svc) {
		t.Errorf("expected [%+v] actual=%+v", *svc, result)
		t.Fail()
	}
}
func (dt *DaoTest) TestDao_UpdateService(t *C) {
	dt.Dao.RemoveService("default", &unused)

	svc, _ := service.NewService()
	svc.ID = "default0"
	svc.Name = "default0"
	svc.PoolID = "default"
	svc.Launch = "auto"
	svc.DeploymentID = "deployment_id"
	err := dt.Dao.AddService(*svc, &id)
	t.Assert(err, IsNil)

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

	result := service.Service{}
	dt.Dao.GetService("default0", &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 = svc.UpdatedAt
	result.CreatedAt = svc.CreatedAt
	if !svc.Equals(&result) {
		t.Errorf("Expected Service %+v, Actual Service %+v", result, *svc)
		t.Fail()
	}

	svc, _ = service.NewService()
	svc.ID = "default1"
	svc.Name = "default1"
	svc.PoolID = "default"
	svc.Launch = "auto"
	svc.DeploymentID = "deployment_id"
	err = dt.Dao.AddService(*svc, &id)
	t.Assert(err, IsNil)

	svc.Name = "name"
	err = dt.Dao.UpdateService(*svc, &unused)
	if err == nil {
		t.Errorf("Expected error updating service with same name and parent", svc)
		t.Fail()
	}
}
func (dt *DaoTest) TestDao_GetService(t *C) {
	svc, _ := service.NewService()
	svc.Name = "testname"
	svc.PoolID = "default"
	svc.Launch = "auto"
	svc.DeploymentID = "deployment_id"
	err := dt.Dao.AddService(*svc, &id)
	t.Assert(err, IsNil)

	var result service.Service
	err = dt.Dao.GetService(svc.ID, &result)
	t.Assert(err, IsNil)
	//XXX the time.Time types fail comparison despite being equal...
	//	  as far as I can tell this is a limitation with Go
	result.UpdatedAt = svc.UpdatedAt
	result.CreatedAt = svc.CreatedAt
	if !svc.Equals(&result) {
		t.Errorf("GetService Failed: expected=%+v, actual=%+v", svc, result)
	}
}
示例#6
0
func (s *FacadeTest) Test_HostRemove(t *C) {
	//create pool for testing
	resoucePool := pool.New("poolid")
	s.Facade.AddResourcePool(s.CTX, resoucePool)
	defer s.Facade.RemoveResourcePool(s.CTX, "poolid")

	//add host1
	h1 := host.Host{
		ID:      "deadb11f",
		PoolID:  "poolid",
		Name:    "h1",
		IPAddr:  "192.168.0.1",
		RPCPort: 65535,
		IPs: []host.HostIPResource{
			host.HostIPResource{
				HostID:    "deadb11f",
				IPAddress: "192.168.0.1",
			},
		},
	}
	err := s.Facade.AddHost(s.CTX, &h1)
	if err != nil {
		t.Fatalf("Failed to add host %+v: %s", h1, err)
	}

	//add host2
	h2 := host.Host{
		ID:      "deadb12f",
		PoolID:  "poolid",
		Name:    "h2",
		IPAddr:  "192.168.0.2",
		RPCPort: 65535,
		IPs: []host.HostIPResource{
			host.HostIPResource{
				HostID:    "deadb12f",
				IPAddress: "192.168.0.2",
			},
		},
	}
	err = s.Facade.AddHost(s.CTX, &h2)
	if err != nil {
		t.Fatalf("Failed to add host %+v: %s", h2, err)
	}
	defer s.Facade.RemoveHost(s.CTX, "host2")

	//add service with address assignment
	s1, _ := service.NewService()
	s1.Name = "name"
	s1.PoolID = "poolid"
	s1.DeploymentID = "deployment_id"
	s1.Launch = "manual"
	s1.Endpoints = []service.ServiceEndpoint{
		service.ServiceEndpoint{},
	}
	s1.Endpoints[0].Name = "name"
	s1.Endpoints[0].AddressConfig = servicedefinition.AddressResourceConfig{Port: 123, Protocol: "tcp"}
	aa := addressassignment.AddressAssignment{ID: "id", HostID: h1.ID}
	s1.Endpoints[0].SetAssignment(aa)
	err = s.Facade.AddService(s.CTX, *s1)
	if err != nil {
		t.Fatalf("Failed to add service %+v: %s", s1, err)
	}
	defer s.Facade.RemoveService(s.CTX, s1.ID)

	request := dao.AssignmentRequest{ServiceID: s1.ID, IPAddress: h1.IPAddr, AutoAssignment: false}
	if err = s.Facade.AssignIPs(s.CTX, request); err != nil {
		t.Fatalf("Failed assigning ip to service: %s", err)
	}

	var serviceRequest dao.ServiceRequest
	services, _ := s.Facade.GetServices(s.CTX, serviceRequest)
	if len(services) <= 0 {
		t.Fatalf("Expected one service in context")
	}
	if len(services[0].Endpoints) <= 0 {
		t.Fatalf("Expected service with one endpoint in context")
	}
	ep := services[0].Endpoints[0]
	if ep.AddressAssignment.IPAddr != h1.IPAddr && ep.AddressAssignment.HostID != h1.ID {
		t.Fatalf("Incorrect IPAddress and HostID before remove host")
	}

	//remove host1
	err = s.Facade.RemoveHost(s.CTX, h1.ID)
	if err != nil {
		t.Fatalf("Failed to remove host: %s", err)
	}
	services, _ = s.Facade.GetServices(s.CTX, serviceRequest)

	if len(services) <= 0 {
		t.Fatalf("Expected one service in context")
	}

	if len(services[0].Endpoints) <= 0 {
		t.Fatalf("Expected service with one endpoint in context")
	}

	ep = services[0].Endpoints[0]
	if ep.AddressAssignment.IPAddr == h2.IPAddr || ep.AddressAssignment.HostID == h2.ID {
		t.Fatalf("Incorrect IPAddress and HostID after remove host")
	}
}
func (dt *DaoTest) TestDao_StartService(t *C) {

	s0, _ := service.NewService()
	s0.ID = "0"
	s0.Name = "name0"
	s0.PoolID = "default"
	s0.Launch = "auto"
	s0.DesiredState = int(service.SVCStop)
	s0.DeploymentID = "deployment_id"

	s01, _ := service.NewService()
	s01.ID = "01"
	s01.Name = "name1"
	s01.PoolID = "default"
	s01.Launch = "auto"
	s01.ParentServiceID = "0"
	s01.DesiredState = int(service.SVCStop)
	s01.DeploymentID = "deployment_id"

	s011, _ := service.NewService()
	s011.ID = "011"
	s011.Name = "name2"
	s011.PoolID = "default"
	s011.Launch = "auto"
	s011.ParentServiceID = "01"
	s011.DesiredState = int(service.SVCStop)
	s011.DeploymentID = "deployment_id"

	s02, _ := service.NewService()
	s02.ID = "02"
	s02.Name = "name3"
	s02.DeploymentID = "deployment_id2"

	s02.PoolID = "default"
	s02.Launch = "auto"
	s02.ParentServiceID = "0"
	s02.DesiredState = int(service.SVCStop)

	err := dt.Dao.AddService(*s0, &id)
	t.Assert(err, IsNil)
	err = dt.Dao.AddService(*s01, &id)
	t.Assert(err, IsNil)
	err = dt.Dao.AddService(*s011, &id)
	t.Assert(err, IsNil)
	err = dt.Dao.AddService(*s02, &id)
	t.Assert(err, IsNil)

	var affected int
	if err := dt.Dao.StartService(dao.ScheduleServiceRequest{"0", true}, &affected); err != nil {
		t.Fatalf("could not start services: %v", err)
	}

	svc := service.Service{}
	dt.Dao.GetService("0", &svc)
	if svc.DesiredState != int(service.SVCRun) {
		t.Errorf("Service: 0 not requested to run: %+v", svc)
		t.Fail()
	}

	dt.Dao.GetService("01", &svc)
	if svc.DesiredState != int(service.SVCRun) {
		t.Errorf("Service: 01 not requested to run: %+v", svc)
		t.Fail()
	}

	dt.Dao.GetService("011", &svc)
	if svc.DesiredState != int(service.SVCRun) {
		t.Errorf("Service: 011 not requested to run: %+v", svc)
		t.Fail()
	}

	dt.Dao.GetService("02", &svc)
	if svc.DesiredState != int(service.SVCRun) {
		t.Errorf("Service: 02 not requested to run: %+v", svc)
		t.Fail()
	}
}