示例#1
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")

}
示例#2
0
func TestChooseRandomFriends(t *testing.T) {
	cfg.SetNode(node.CreateMockNode())
	mockPeers := createMockPeers(100)
	nFriends := 10
	test, err := chooseRandomFriends(mockPeers, nFriends)
	assert.NoError(t, err, "(nFrineds > 0) Choose friends should produce no error")

	nFriends = 150
	test, err = chooseRandomFriends(mockPeers, nFriends)
	friendsKeys := make([]string, 0, len(mockPeers)-1)
	for key, _ := range test {
		friendsKeys = append(friendsKeys, key)
	}
	assert.NoError(t, err, "(nFrineds > nPeers) Choose friends should produce no error")

	nFriends = 0
	test, err = chooseRandomFriends(mockPeers, nFriends)
	assert.Error(t, err, "(nFriends < 0) Choose friends should produce an error")

	mockPeers = createMockPeers(0)
	nFriends = 10
	_, err = chooseRandomFriends(mockPeers, nFriends)
	assert.Error(t, err, "(peers = 0) Choose friend should produce an error")

	mockPeers = createMockPeers(1)
	nFriends = 10
	_, err = chooseRandomFriends(mockPeers, nFriends)
	assert.Error(t, err, "(peers = me) Choose friend should produce an error")

	mockPeers = createMockPeers(2)
	nFriends = 10
	test, err = chooseRandomFriends(mockPeers, nFriends)
	assert.NoError(t, err, "(nFrineds == 2) Choose friends should produce no error")
}
示例#3
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)
}
示例#4
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)
}
示例#5
0
func TestNew(t *testing.T) {
	notSupported := "notSupported"

	dscvr, err := New(notSupported, "http://localhost:5000")
	assert.Error(t, err)
	assert.Equal(t, "noservice", dscvr.Name())
}
示例#6
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)
}
示例#7
0
func TestFillStruct(t *testing.T) {
	testMap := map[string]interface{}{
		"Pippo":    5,
		"Topolino": 4.6,
		"Paperino": "ciao",
	}
	noErr := &noError{}
	err1 := &wrongField{}
	err2 := &wrongType{}
	err3 := &cantSet{}

	assert.NoError(t, FillStruct(noErr, testMap))
	assert.Error(t, FillStruct(err1, testMap))
	assert.Error(t, FillStruct(err2, testMap))
	assert.Error(t, FillStruct(err3, testMap))
}
示例#8
0
func TestNew(t *testing.T) {
	noService := "noservice"
	noServiceConf := CreateMetricsMockConfig(noService)
	influxService := "influxdb"
	influxConf := CreateMetricsMockConfig(influxService)

	test, testErr := New(influxService, influxConf)
	assert.NoError(t, testErr)
	assert.Equal(t, test.Name(), influxService)
	influxConf["Username"] = 1
	test, testErr = New(influxService, influxConf)
	assert.Error(t, testErr)
	assert.Equal(t, test.Name(), noService)

	test, testErr = New(noService, noServiceConf)
	assert.Error(t, testErr)
	assert.Equal(t, test.Name(), noService)
}
示例#9
0
func TestGetCoresNumber(t *testing.T) {
	var err error

	cores := "0,1,2,3"
	cores_tooMany := "0,1,2,3,4,5,6"
	cores_wrongNumber := "0,1,85"
	cores_atoi := "1,pippo"

	var n []int
	n, err = getCoresNumber(cores)
	assert.NoError(t, err)
	assert.Equal(t, []int{0, 1, 2, 3}, n)
	n, err = getCoresNumber(cores_tooMany)
	assert.Error(t, err)
	n, err = getCoresNumber(cores_wrongNumber)
	assert.Error(t, err)
	n, err = getCoresNumber(cores_atoi)
	assert.Error(t, err)
}
示例#10
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'")
}
示例#11
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'")
}
示例#12
0
func TestRAMInBytes(t *testing.T) {
	human := [13]string{
		"32",
		"32b",
		"32B",
		"32k",
		"32K",
		"32kb",
		"32Kb",
		"32Mb",
		"32Gb",
		"32Tb",
		"32Pb",
		"32PB",
		"32P",
	}

	values := [13]int64{
		32,
		32,
		32,
		32 * KiB,
		32 * KiB,
		32 * KiB,
		32 * KiB,
		32 * MiB,
		32 * GiB,
		32 * TiB,
		32 * PiB,
		32 * PiB,
		32 * PiB,
	}

	errors := [9]string{
		"",
		"hello",
		"-32",
		"32.3",
		" 32 ",
		"32.3Kb",
		"32 mb",
		"32m b",
		"32bm",
	}

	for i := 0; i < len(human); i++ {
		ram, _ := RAMInBytes(human[i])
		assert.Equal(t, values[i], ram)
	}

	for i := 0; i < len(errors); i++ {
		_, err := RAMInBytes(errors[i])
		assert.Error(t, err)
	}
}
示例#13
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)

}
示例#14
0
func TestGetPolicy(t *testing.T) {
	defer storage.DeleteAllData(enum.POLICIES)
	var err error

	_, err = GetPolicy()
	assert.Error(t, err)

	expected := CreateRandomMockPolicies(1)[0]
	SavePolicy(expected)
	policy, err := GetPolicy()
	assert.NoError(t, err)
	assert.Equal(t, expected, policy)
}
示例#15
0
func TestGetAnalytics(t *testing.T) {
	defer storage.DeleteAllData(enum.ANALYTICS)
	var err error

	_, err = GetAnalytics()
	assert.Error(t, err)

	expected := CreateMockAnalytics()
	SaveMockAnalytics()
	analytics, err := GetAnalytics()
	assert.NoError(t, err)
	assert.Equal(t, expected, analytics)
}
示例#16
0
func TestGetStats(t *testing.T) {
	defer storage.DeleteAllData(enum.STATS)
	var err error

	_, err = GetStats()
	assert.Error(t, err)

	expected := CreateMockStats()
	SaveMockStats()
	stats, err := GetStats()
	assert.NoError(t, err)
	assert.Equal(t, expected, stats)
}
示例#17
0
func TestGeShared(t *testing.T) {
	defer storage.DeleteAllData(enum.SHARED)
	var err error
	expected := CreateMockShared()

	// Local
	_, err = GetSharedLocal()
	assert.Error(t, err)

	SaveSharedLocal(expected)
	info, err := GetSharedLocal()
	assert.NoError(t, err)
	assert.Equal(t, expected, info)

	// Local
	_, err = GetSharedCluster()
	assert.Error(t, err)

	SaveSharedCluster(expected)
	info, err = GetSharedCluster()
	assert.NoError(t, err)
	assert.Equal(t, expected, info)
}
示例#18
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)

}
示例#19
0
func TestCheckAndSetSpecificCores(t *testing.T) {
	defer freeCores()

	req := "0,3"
	req_tooMany := "0,1,2,3,4,5"
	req_wrongNumber := "0,1,85"
	id := "pippo"

	assert.NoError(t, CheckAndSetSpecificCores(req, id))
	assert.Equal(t, c_NCORES-2, getAvailableCores())

	freeCores()
	assert.Error(t, CheckAndSetSpecificCores(req_tooMany, id))
	assert.Equal(t, c_NCORES, getAvailableCores())

	freeCores()
	assert.Error(t, CheckAndSetSpecificCores(req_wrongNumber, id))
	assert.Equal(t, c_NCORES, getAvailableCores())

	freeCores()
	assignSpecificCores([]int{1, 3}, id)
	assert.Error(t, CheckAndSetSpecificCores(req, id))

}
示例#20
0
func TestByteToAnalytics(t *testing.T) {
	var encoded []byte
	var decoded GruAnalytics
	var err error

	analytics := CreateMockAnalytics()
	encoded, _ = json.Marshal(analytics)
	decoded, err = ByteToAnalytics(encoded)
	assert.NoError(t, err)
	assert.Equal(t, analytics, decoded)

	bad := 0
	encoded, _ = json.Marshal(bad)
	decoded, err = ByteToAnalytics(encoded)
	assert.Error(t, err)
}
示例#21
0
func TestByteToPolicy(t *testing.T) {
	var encoded []byte
	var decoded Policy
	var err error

	policy := CreateRandomMockPolicies(1)[0]
	encoded, _ = json.Marshal(policy)
	decoded, err = ByteToPolicy(encoded)
	assert.NoError(t, err)
	assert.Equal(t, policy, decoded)

	bad := 0
	encoded, _ = json.Marshal(bad)
	decoded, err = ByteToPolicy(encoded)
	assert.Error(t, err)
}
示例#22
0
func TestByteToInfo(t *testing.T) {
	var encoded []byte
	var decoded Shared
	var err error

	info := CreateMockShared()
	encoded, _ = json.Marshal(info)
	decoded, err = ByteToShared(encoded)
	assert.NoError(t, err)
	assert.Equal(t, info, decoded)

	bad := 0
	encoded, _ = json.Marshal(bad)
	decoded, err = ByteToShared(encoded)
	assert.Error(t, err)
}
示例#23
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)
}
示例#24
0
func TestByteToStats(t *testing.T) {
	var encoded []byte
	var decoded GruStats
	var err error

	stats := CreateMockStats()
	encoded, _ = json.Marshal(stats)
	decoded, err = ByteToStats(encoded)
	assert.NoError(t, err)
	assert.Equal(t, stats, decoded)

	bad := 0
	encoded, _ = json.Marshal(bad)
	decoded, err = ByteToStats(encoded)
	assert.Error(t, err)

}
示例#25
0
func TestNew(t *testing.T) {
	supported := "internal"
	notSuported := "notSupported"

	_, err := New(supported)
	assert.NoError(t, err)
	assert.Equal(t, "internal", Name())

	test := client()
	assert.Equal(t, "internal", test.Name())

	_, err = New(notSuported)
	assert.Error(t, err)
	assert.Equal(t, "internal", Name())

	test = client()
	assert.Equal(t, "internal", test.Name())
}
示例#26
0
func TestNew(t *testing.T) {
	var err error

	_, err = New("dummy")
	assert.NoError(t, err)
	assert.Equal(t, "dummy", Name())

	_, err = New("probcumulative")
	assert.NoError(t, err)
	assert.Equal(t, "probcumulative", Name())

	_, err = New("probdelta")
	assert.NoError(t, err)
	assert.Equal(t, "probdelta", Name())

	_, err = New("notImplemented")
	assert.Error(t, err)
	assert.Equal(t, "dummy", Name())
}
示例#27
0
func TestMergeInfo(t *testing.T) {
	defer service.ClearMockServices()
	var err error

	service.SetMockServices()
	shared1 := CreateMockShared()
	shared2 := CreateMockShared()
	shared3 := CreateMockShared()

	peers := []Shared{shared1, shared2, shared3}
	merged, err := MergeShared(peers)
	assert.NoError(t, err)
	assert.Equal(t, shared1.Service["service1"].Data.BaseShared[enum.METRIC_CPU_AVG.ToString()],
		merged.Service["service1"].Data.BaseShared[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, shared1.System.ActiveServices, merged.System.ActiveServices)

	empty := []Shared{}
	_, err = MergeShared(empty)
	assert.Error(t, err)

	one := []Shared{shared1}
	_, err = MergeShared(one)
	assert.NoError(t, err)
}