Exemplo n.º 1
0
func TestUpdateServices(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())
	newService := cfg.Service{
		Name:  "newService",
		Type:  "mockService",
		Image: "noImage",
	}
	newServices := []cfg.Service{newService}

	cfg.SetServices(newServices)
	assert.Len(t, cfg.GetServices(), 1, "services should have lenght = 1 after the update")
	assert.Contains(t, List(), "newService", "services should contain service 'newService' after the update")
}
Exemplo n.º 2
0
func TestAddServiceInstance(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())
	name := "service1"
	service, _ := GetServiceByName(name)
	instance_p := "instance1_100"
	instance_ps := "instance1_101"
	instance_s := "instance1_102"
	var err error
	var status enum.Status

	status = enum.PENDING
	err = AddServiceInstance(name, instance_p, status)
	assert.NoError(t, err)
	assert.Contains(t, service.Instances.All, instance_p)
	assert.Contains(t, service.Instances.Pending, instance_p)

	status = enum.PAUSED
	err = AddServiceInstance(name, instance_ps, status)
	assert.NoError(t, err)
	assert.Contains(t, service.Instances.All, instance_ps)
	assert.Contains(t, service.Instances.Paused, instance_ps)

	status = enum.STOPPED
	err = AddServiceInstance(name, instance_s, status)
	assert.NoError(t, err)
	assert.Contains(t, service.Instances.All, instance_s)
	assert.Contains(t, service.Instances.Stopped, instance_s)

	status = enum.PENDING
	err = AddServiceInstance("pippo", "pippo", status)
	assert.Error(t, err)
}
Exemplo n.º 3
0
func TestGetServiceInstanceStatus(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())
	name := "service1"
	instance_p := "instance1_3"
	instance_r := "instance1_1"
	instance_s := "instance1_0"
	instance_ps := "instance1_5"
	var status enum.Status

	status = GetServiceInstanceStatus(name, instance_p)
	assert.Equal(t, enum.PENDING, status)

	status = GetServiceInstanceStatus(name, instance_r)
	assert.Equal(t, enum.RUNNING, status)

	status = GetServiceInstanceStatus(name, instance_s)
	assert.Equal(t, enum.STOPPED, status)

	status = GetServiceInstanceStatus(name, instance_ps)
	assert.Equal(t, enum.PAUSED, status)

	status = GetServiceInstanceStatus(name, "pippo")
	assert.Equal(t, enum.UNKNOWN, status)

	status = GetServiceInstanceStatus("pippo", "pippo")
	assert.Equal(t, enum.UNKNOWN, status)
}
Exemplo n.º 4
0
func TestRemoveServices(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())
	rmService := "service2"
	rmServices := []string{rmService}

	cfg.RemoveServices(rmServices)
	assert.NotContains(t, List(), rmService, "services should not contain removed service 'service2'")
}
Exemplo n.º 5
0
func TestGetServiceByName(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())

	s1, err := GetServiceByName("service2")
	assert.Equal(t, "service2", s1.Name, "service name should be service1")

	_, err = GetServiceByName("pippo")
	assert.Error(t, err, "There should be no service with name 'pippo'")
}
Exemplo n.º 6
0
func TestGetServiceByImage(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())

	img1, err := GetServiceByImage("test/mysql")
	assert.Equal(t, "test/mysql", img1.Image, "service image should be test/tomcat")

	_, err = GetServiceByImage("test/pippo")
	assert.Error(t, err, "There should be no image 'test/pippo'")
}
Exemplo n.º 7
0
func TestBuildConfig(t *testing.T) {
	defer cfg.CleanServices()
	services := service.CreateMockServices()
	cfg.SetServices(services)
	resources.CreateMockResources(2, "1G", 0, "0G")
	ports1 := map[string]string{
		"50100": "50100",
	}
	resources.InitializeServiceAvailablePorts("service1", ports1)
	service1, _ := service.GetServiceByName("service1")

	config := buildConfig(service1, enum.START)
	assert.Equal(t, "0", config.HostConfig.CpusetCpus)
}
Exemplo n.º 8
0
func TestAddServices(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())

	newService := cfg.Service{
		Name:  "newService",
		Type:  "mockService",
		Image: "noImage",
	}
	newServices := []cfg.Service{newService}

	cfg.AddServices(newServices)
	assert.Contains(t, cfg.GetServices(), newService, "services should contain the added service")
}
Exemplo n.º 9
0
func TestGetTargetService(t *testing.T) {
	defer cfg.CleanServices()
	services := service.CreateMockServices()
	cfg.SetServices(services)

	var srv *cfg.Service
	srv = getTargetService("service1")
	assert.Equal(t, "service1", srv.Name)

	srv = getTargetService("noservice")
	assert.Equal(t, "noservice", srv.Name)

	srv = getTargetService("pippo")
	assert.Equal(t, "noservice", srv.Name)
}
Exemplo n.º 10
0
func TestChangeServiceInstanceStatus(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())
	name := "service1"
	service, _ := GetServiceByName(name)
	instance := "instance1_3"
	var prev enum.Status
	var upd enum.Status
	var err error

	prev = enum.PENDING
	upd = enum.RUNNING
	err = ChangeServiceInstanceStatus(name, instance, prev, upd)
	assert.NotContains(t, service.Instances.Pending, instance)
	assert.Contains(t, service.Instances.Running, instance)
	assert.NoError(t, err)

	prev = enum.RUNNING
	upd = enum.PAUSED
	err = ChangeServiceInstanceStatus(name, instance, prev, upd)
	assert.NotContains(t, service.Instances.Running, instance)
	assert.Contains(t, service.Instances.Paused, instance)
	assert.NoError(t, err)

	prev = enum.PAUSED
	upd = enum.STOPPED
	err = ChangeServiceInstanceStatus(name, instance, prev, upd)
	assert.NotContains(t, service.Instances.Paused, instance)
	assert.Contains(t, service.Instances.Stopped, instance)
	assert.NoError(t, err)

	prev = enum.STOPPED
	upd = enum.PENDING
	err = ChangeServiceInstanceStatus(name, instance, prev, upd)
	assert.NotContains(t, service.Instances.Stopped, instance)
	assert.Contains(t, service.Instances.Pending, instance)
	assert.NoError(t, err)

	prev = enum.PENDING
	upd = enum.RUNNING
	err = ChangeServiceInstanceStatus("pippo", instance, prev, upd)
	assert.Error(t, err)

	prev = enum.PENDING
	upd = enum.RUNNING
	err = ChangeServiceInstanceStatus(name, "pippo", prev, upd)
	assert.Error(t, err)
}
Exemplo n.º 11
0
func TestGetServiceById(t *testing.T) {
	defer cfg.CleanServices()

	var err error
	cfg.SetServices(CreateMockServices())

	srv1, err := GetServiceById("instance1_0")
	assert.NoError(t, err)
	assert.Equal(t, "service1", srv1.Name)

	srv2, err := GetServiceById("instance2_1")
	assert.NoError(t, err)
	assert.Equal(t, "service2", srv2.Name)

	_, err = GetServiceById("pippo")
	assert.Error(t, err)
}
Exemplo n.º 12
0
func TestCreateHostConfig(t *testing.T) {
	defer cfg.CleanServices()
	services := service.CreateMockServices()
	cfg.SetServices(services)
	resources.CreateMockResources(2, "1G", 0, "0G")
	ports1 := map[string]string{
		"50100": "50100",
	}
	resources.InitializeServiceAvailablePorts("service1", ports1)
	service1, _ := service.GetServiceByName("service1")

	hostConfigStop := createHostConfig(service1, enum.STOP)
	assert.Equal(t, hostConfigStop.CpusetCpus, "")

	hostConfigStart := createHostConfig(service1, enum.START)
	assert.Equal(t, hostConfigStart.CpusetCpus, "0")
	assert.Len(t, hostConfigStart.PortBindings, 1)
}
Exemplo n.º 13
0
func TestGetServiceByType(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())

	ws := GetServiceByType("webserver")
	if assert.Len(t, ws, 2, "there should be 2 services with type webserver") {
		assert.Equal(t, "webserver", ws[0].Type, "service type should be webserver")
		img := [2]string{ws[0].Image, ws[1].Image}
		assert.Contains(t, img, "test/tomcat", "images should contain test/tomcat")
	}

	db := GetServiceByType("database")
	if assert.Len(t, db, 1, "there should be 1 services with type database") {
		assert.Equal(t, "database", db[0].Type, "service type should be database")
	}

	ap := GetServiceByType("application")
	assert.Len(t, ap, 0, "there should be 0 services with type application")
}
Exemplo n.º 14
0
func TestUpdateMetrics(t *testing.T) {
	cfg.SetServices(service.CreateMockServices())

	updateMetrics()
	assert.Equal(t, 0.0, Metrics().Service["service1"].Stats.BaseMetrics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, 0.0, Metrics().Service["service2"].Analytics.BaseAnalytics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, "noaction", Metrics().Policy.Name)

	data.SaveMockStats()
	data.SaveMockAnalytics()
	data.SaveSharedCluster(data.CreateMockShared())
	plc := data.CreateMockPolicy("policy", 1.0, []string{"pippo"}, map[string][]enum.Action{})
	data.SavePolicy(plc)
	updateMetrics()
	assert.Equal(t, 0.6, Metrics().Service["service1"].Stats.BaseMetrics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, 0.1, Metrics().Service["service2"].Analytics.BaseAnalytics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, 0.6, metrics.Service["service1"].Shared.BaseShared[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, "policy", Metrics().Policy.Name)
}
Exemplo n.º 15
0
func TestRemoveServiceIntance(t *testing.T) {
	defer cfg.CleanServices()
	cfg.SetServices(CreateMockServices())
	name := "service1"
	service, _ := GetServiceByName(name)
	instance := "instance1_0"
	var err error

	err = RemoveServiceInstance(name, instance)
	assert.NotContains(t, service.Instances.Stopped, instance)
	assert.NotContains(t, service.Instances.All, instance)
	assert.NoError(t, err)

	err = RemoveServiceInstance("pippo", instance)
	assert.Error(t, err)

	err = RemoveServiceInstance(name, "pippo")
	assert.Error(t, err)

}
Exemplo n.º 16
0
func TestCreateContainerConfig(t *testing.T) {
	defer cfg.CleanServices()
	services := service.CreateMockServices()
	cfg.SetServices(services)
	resources.CreateMockResources(2, "1G", 0, "0G")
	service1 := "service1"
	id1 := "pippo"
	availablePorts1 := map[string]string{
		"50100": "50100",
	}
	ports1 := map[string][]string{
		"50100": []string{"50100"},
	}
	resources.InitializeServiceAvailablePorts(service1, availablePorts1)
	resources.AssignSpecifiPortsToService(service1, id1, ports1)
	srv1, _ := service.GetServiceByName(service1)

	containerConfigStop := createContainerConfig(srv1, enum.STOP)
	assert.Empty(t, containerConfigStop.ExposedPorts)
	containerConfigStart := createContainerConfig(srv1, enum.START)
	assert.NotEmpty(t, containerConfigStart.ExposedPorts)
}
Exemplo n.º 17
0
func CreateMockMetrics() GruMetric {
	cfg.SetServices(service.CreateMockServices())
	mockMetrics := newMetrics()
	return mockMetrics
}
Exemplo n.º 18
0
func initializeServices(clusterName string) {
	remote := c_GRU_REMOTE + clusterName + "/" + c_SERVICES_REMOTE
	services := cfg.ReadServices(remote)
	cfg.SetServices(services)
}
Exemplo n.º 19
0
func updateServices(cluster string) {
	remote := c_GRU_REMOTE + cluster + "/" + c_SERVICES_REMOTE
	services := cfg.ReadServices(remote)
	cfg.SetServices(services)
	log.WithField("services", services).Debugln("Services updated from remote")
}
Exemplo n.º 20
0
func resetMockServices() {
	mockServices := srv.CreateMockServices()
	cfg.SetServices(mockServices)
}
Exemplo n.º 21
0
func TestAvailableResourcesService(t *testing.T) {
	defer CleanResources()

	name := "test"
	s_over := createService(name, 8, "16G")
	s_bigger := createService(name, 6, "8G")
	s_big := createService(name, 4, "4G")
	s_medium := createService(name, 2, "4G")
	s_low := createService(name, 2, "2G")
	s_lower := createService(name, 1, "1G")
	s_error := createService(name, 1, "error")

	setResources(6, "8G", 6, "8G")
	cfg.SetServices([]cfg.Service{s_over})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_bigger})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_big})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_medium})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_low})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_lower})
	assert.Equal(t, 0.0, AvailableResourcesService(name))

	setResources(6, "8G", 4, "4G")
	cfg.SetServices([]cfg.Service{s_over})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_bigger})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_big})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_medium})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_low})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_lower})
	assert.Equal(t, 1.0, AvailableResourcesService(name))

	setResources(6, "8G", 2, "2G")
	cfg.SetServices([]cfg.Service{s_over})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_bigger})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_big})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_medium})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_low})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_lower})
	assert.Equal(t, 1.0, AvailableResourcesService(name))

	setResources(6, "8G", 0, "0G")
	cfg.SetServices([]cfg.Service{s_error})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_over})
	assert.Equal(t, 0.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_bigger})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_big})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_medium})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_low})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
	cfg.SetServices([]cfg.Service{s_lower})
	assert.Equal(t, 1.0, AvailableResourcesService(name))
}