Beispiel #1
0
func TestPushValues(t *testing.T) {
	values := []float64{1.0, 2.0, 3.0, 4.0, 5.0, 6.0}
	var b Buffer
	var res []float64

	b = BuildBuffer(10)
	res = b.PushValues(values)
	assert.Nil(t, res)
	assert.Len(t, b.values, len(values))

	b = BuildBuffer(6)
	res = b.PushValues(values)
	assert.NotNil(t, res)
	assert.Len(t, res, len(values))
	assert.Empty(t, b.values)

	b = BuildBuffer(2)
	res = b.PushValues(values)
	assert.NotNil(t, res)
	assert.Len(t, res, len(values))
	assert.Empty(t, b.values)

	b = BuildBuffer(4)
	res = b.PushValues(values)
	assert.NotNil(t, res)
	assert.Len(t, res, b.capacity)
	assert.Len(t, b.values, len(values)-b.capacity)
}
Beispiel #2
0
func TestCreatePortBindings(t *testing.T) {
	service1 := "service1"
	ports1 := map[string]string{
		"50100": "50100",
	}
	service2 := "service2"
	ports2 := map[string]string{
		"50200": "50200",
	}
	service3 := "service3"
	resources.InitializeServiceAvailablePorts(service1, ports1)
	resources.InitializeServiceAvailablePorts(service2, ports2)

	bindings1 := createPortBindings(service1)
	assert.Len(t, bindings1, 1)
	assert.NotEmpty(t, bindings1["50100/tcp"])
	assert.Equal(t, "50100", bindings1["50100/tcp"][0].HostPort)

	bindings2 := createPortBindings(service2)
	assert.Len(t, bindings2, 1)
	assert.NotEmpty(t, bindings2["50200/tcp"])
	assert.Equal(t, "50200", bindings2["50200/tcp"][0].HostPort)

	bindings3 := createPortBindings(service3)
	assert.Empty(t, bindings3)
}
Beispiel #3
0
func TestGetCompleteRange(t *testing.T) {
	var err error
	var result []string

	invalid := "pippo"
	invalidLimit := "pippo-10"
	onveValue := "5"
	valid := "0-10"

	_, err = GetCompleteRange(invalid)
	assert.Error(t, err)

	_, err = GetCompleteRange(invalidLimit)
	assert.Error(t, err)

	result, err = GetCompleteRange(onveValue)
	assert.NoError(t, err)
	assert.Len(t, result, 1)
	assert.Contains(t, result, "5")

	result, err = GetCompleteRange(valid)
	assert.NoError(t, err)
	assert.Len(t, result, 11)
	assert.Contains(t, result, "0")
	assert.Contains(t, result, "10")
	assert.NotContains(t, result, "11")

}
Beispiel #4
0
func TestRemoveValues(t *testing.T) {
	b := BuildBuffer(10)
	values := []float64{1, 2, 3}
	b.values = append(b.values, values...)
	res := b.RemoveValues()
	assert.Len(t, res, len(values))
	assert.Empty(t, b.values)
}
Beispiel #5
0
func TestCheckAndAppend(t *testing.T) {
	list0 := []string{}
	list1 := []string{"pippo", "topolino"}
	list2 := []string{"paperino"}
	list3 := []string{"topolino", "paperino", "paperone"}

	list0 = checkAndAppend(list0, list1)
	assert.Len(t, list0, 2)
	assert.Contains(t, list0, "pippo")
	assert.Contains(t, list0, "topolino")

	list0 = checkAndAppend(list0, list2)
	assert.Len(t, list0, 3)
	assert.Contains(t, list0, "paperino")

	list0 = checkAndAppend(list0, list3)
	assert.Len(t, list0, 4)
	assert.Contains(t, list0, "paperone")
}
Beispiel #6
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")
}
Beispiel #7
0
func TestUpdateSystemInstances(t *testing.T) {
	defer resetMockServices()
	list := srv.List()

	updateSystemInstances(list)

	srv1, _ := srv.GetServiceByName("service1")
	srv2, _ := srv.GetServiceByName("service2")
	tot_all := len(srv1.Instances.All) + len(srv2.Instances.All)
	tot_pen := len(srv1.Instances.Pending) + len(srv2.Instances.Pending)
	tot_run := len(srv1.Instances.Running) + len(srv2.Instances.Running)
	tot_stop := len(srv1.Instances.Stopped) + len(srv2.Instances.Stopped)
	tot_pause := len(srv1.Instances.Paused) + len(srv2.Instances.Paused)

	instances := cfg.GetNodeInstances()
	assert.Len(t, instances.All, tot_all)
	assert.Len(t, instances.Pending, tot_pen)
	assert.Len(t, instances.Running, tot_run)
	assert.Len(t, instances.Stopped, tot_stop)
	assert.Len(t, instances.Paused, tot_pause)
}
Beispiel #8
0
func TestRequestPortsForService(t *testing.T) {
	defer clearServicePorts()
	var err error
	createServicePorts()
	service := "pippo"
	port1 := "50100"
	port2 := "50200"

	result, err := RequestPortsForService(service)
	assert.NoError(t, err)
	assert.Len(t, result, 2)
	assert.Len(t, resources.Network.ServicePorts[service].LastRequested, 2)
	available1 := resources.Network.ServicePorts[service].Status[port1].Available
	occupied1 := resources.Network.ServicePorts[service].Status[port1].Occupied
	assert.Len(t, available1, 4)
	assert.Len(t, occupied1, 0)
	available2 := resources.Network.ServicePorts[service].Status[port2].Available
	occupied2 := resources.Network.ServicePorts[service].Status[port2].Occupied
	assert.Len(t, available2, 2)
	assert.Len(t, occupied2, 0)

	ports := resources.Network.ServicePorts[service]
	status := ports.Status[port1]
	status.Available = []string{}
	ports.Status[port1] = status
	resources.Network.ServicePorts[service] = ports
	_, err = RequestPortsForService(service)
	assert.Error(t, err)
	assert.Empty(t, resources.Network.ServicePorts[service].LastRequested)
}
Beispiel #9
0
func TestResetMetrics(t *testing.T) {
	id := "id1"
	defer delete(instancesMetrics, id)

	instancesMetrics[id] = Metric{
		BaseMetrics: make(map[string][]float64),
	}
	instancesMetrics[id].BaseMetrics[enum.METRIC_CPU_INST.ToString()] = []float64{10000, 20000, 30000, 40000, 50000, 60000}
	instancesMetrics[id].BaseMetrics[enum.METRIC_CPU_SYS.ToString()] = []float64{1000000, 1100000, 1200000, 1300000, 1400000, 1500000}
	servicesMetrics["service1"].UserMetrics["response_time"] = []float64{1000, 2000, 3000, 4000, 5000}
	servicesMetrics["service2"].UserMetrics["response_time"] = []float64{1000, 2000, 3000, 4000, 5000}

	resetMetrics()
	assert.Len(t, servicesMetrics, len(srv.List()))
	assert.Empty(t, servicesMetrics["service1"].UserMetrics["response_time"])
	assert.Empty(t, servicesMetrics["service2"].UserMetrics["response_time"])
	assert.Empty(t, servicesMetrics["service3"].UserMetrics["response_time"])
	assert.Len(t, instancesMetrics, 1)
	assert.Empty(t, instancesMetrics[id].BaseMetrics[enum.METRIC_CPU_INST.ToString()])
	assert.Empty(t, instancesMetrics[id].BaseMetrics[enum.METRIC_CPU_SYS.ToString()])

}
Beispiel #10
0
func TestInitializeServiceAvailablePorts(t *testing.T) {
	defer clearServicePorts()

	service1 := "service1"
	ports1 := map[string]string{
		"50100": "50100-50103",
	}
	InitializeServiceAvailablePorts(service1, ports1)
	servicePorts1 := resources.Network.ServicePorts[service1]
	assert.NotNil(t, servicePorts1)
	status1 := servicePorts1.Status["50100"]
	assert.NotNil(t, status1)
	assert.Empty(t, status1.Occupied)
	assert.Len(t, status1.Available, 4)

	service2 := "service2"
	ports2 := map[string]string{
		"50200": "50200",
	}
	InitializeServiceAvailablePorts(service2, ports2)
	servicePorts2 := resources.Network.ServicePorts[service2]
	assert.NotNil(t, servicePorts2)
	status2 := servicePorts2.Status["50200"]
	assert.NotNil(t, status2)
	assert.Empty(t, status2.Occupied)
	assert.Len(t, status2.Available, 1)

	service3 := "service3"
	ports3 := map[string]string{
		"50300": "pippo",
	}
	InitializeServiceAvailablePorts(service3, ports3)
	servicePorts3 := resources.Network.ServicePorts[service3]
	assert.NotNil(t, servicePorts3)
	status3 := servicePorts3.Status["50300"]
	assert.NotNil(t, status3)
	assert.Empty(t, status3.Occupied)
	assert.Empty(t, status3.Available)
}
Beispiel #11
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")
}
Beispiel #12
0
func TestCreateExposedPorts(t *testing.T) {
	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)

	exposed := createExposedPorts(service1)
	assert.Len(t, exposed, 1)
	assert.Equal(t, exposed["50100/tcp"], struct{}{})
}
Beispiel #13
0
func TestPushValue(t *testing.T) {
	value := 1.0
	var b Buffer
	var res []float64

	b = BuildBuffer(10)
	res = b.PushValue(value)
	res = b.PushValue(value)
	res = b.PushValue(value)
	assert.Nil(t, res)
	assert.Len(t, b.values, 3)

	b = BuildBuffer(1)
	res = b.PushValue(value)
	assert.NotNil(t, res)
	assert.Empty(t, b.values)
}
Beispiel #14
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)
}
Beispiel #15
0
func TestFreePortsFromService(t *testing.T) {
	defer clearServicePorts()
	createServicePorts()
	service := "pippo"
	id := "123456789"
	port1 := "50100"
	assignPort(service, id, port1, []string{"50100"})
	available1 := resources.Network.ServicePorts[service].Status[port1].Available
	occupied1 := resources.Network.ServicePorts[service].Status[port1].Occupied
	assert.Len(t, available1, 3)
	assert.Len(t, occupied1, 1)

	FreePortsFromService(service, id)
	available1 = resources.Network.ServicePorts[service].Status[port1].Available
	occupied1 = resources.Network.ServicePorts[service].Status[port1].Occupied
	assert.Len(t, available1, 4)
	assert.Len(t, occupied1, 0)

	FreePortsFromService(service, id)
	assert.Len(t, available1, 4)
	assert.Len(t, occupied1, 0)

}
Beispiel #16
0
func TestAssignSpecificPortsToService(t *testing.T) {
	defer clearServicePorts()
	var err error
	createServicePorts()
	service := "pippo"
	id := "123456789"
	port1 := "50100"
	port2 := "50200"
	bindings := map[string][]string{
		port1: []string{"50100"},
		port2: []string{"50200"},
	}

	err = AssignSpecifiPortsToService(service, id, bindings)
	assert.NoError(t, err)
	available1 := resources.Network.ServicePorts[service].Status[port1].Available
	occupied1 := resources.Network.ServicePorts[service].Status[port1].Occupied
	assert.Len(t, available1, 3)
	assert.Len(t, occupied1, 1)
	available2 := resources.Network.ServicePorts[service].Status[port2].Available
	occupied2 := resources.Network.ServicePorts[service].Status[port2].Occupied
	assert.Len(t, available2, 1)
	assert.Len(t, occupied2, 1)
	assert.NotEmpty(t, instanceBindings)

	err = AssignSpecifiPortsToService(service, id, bindings)
	assert.Error(t, err)
	available1 = resources.Network.ServicePorts[service].Status[port1].Available
	occupied1 = resources.Network.ServicePorts[service].Status[port1].Occupied
	assert.Len(t, available1, 3)
	assert.Len(t, occupied1, 1)
	available2 = resources.Network.ServicePorts[service].Status[port2].Available
	occupied2 = resources.Network.ServicePorts[service].Status[port2].Occupied
	assert.Len(t, available2, 1)
	assert.Len(t, occupied2, 1)

	bindings[port1] = []string{"50000"}
	bindings[port2] = []string{"50201"}
	err = AssignSpecifiPortsToService(service, id, bindings)
	assert.NoError(t, err)
	available1 = resources.Network.ServicePorts[service].Status[port1].Available
	occupied1 = resources.Network.ServicePorts[service].Status[port1].Occupied
	assert.Len(t, available1, 3)
	assert.Len(t, occupied1, 2)
	available2 = resources.Network.ServicePorts[service].Status[port2].Available
	occupied2 = resources.Network.ServicePorts[service].Status[port2].Occupied
	assert.Len(t, available2, 0)
	assert.Len(t, occupied2, 2)

}
Beispiel #17
0
func TestGenerateUUID(t *testing.T) {
	uuid, err := GenerateUUID()
	assert.NoError(t, err, "UUID generation should produce no errors")
	assert.Len(t, uuid, uuidLen, "generated UUID should be 32 characters")
}