Exemplo n.º 1
0
func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {

	var buffer bytes.Buffer
	var fields Fields

	logger := New()
	logger.Out = &buffer
	logger.Formatter = new(JSONFormatter)

	llog := logger.WithField("context", "eating raw fish")

	llog.Info("looks delicious")

	err := json.Unmarshal(buffer.Bytes(), &fields)
	assert.NoError(t, err, "should have decoded first message")
	assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields")
	assert.Equal(t, fields["msg"], "looks delicious")
	assert.Equal(t, fields["context"], "eating raw fish")

	buffer.Reset()

	llog.Warn("omg it is!")

	err = json.Unmarshal(buffer.Bytes(), &fields)
	assert.NoError(t, err, "should have decoded second message")
	assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields")
	assert.Equal(t, fields["msg"], "omg it is!")
	assert.Equal(t, fields["context"], "eating raw fish")
	assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")

}
Exemplo n.º 2
0
func TestDeleteData(t *testing.T) {
	intern := 0
	key := "test"
	data := []byte("pippo")
	var value []byte
	var err error

	//INTERNAL
	dataStore = intern
	Initialize()
	StoreData(key, data, enum.STATS)
	StoreData(key, data, enum.ANALYTICS)
	StoreData(key, data, enum.POLICIES)
	StoreData(key, data, enum.SHARED)

	err = DeleteData(key, enum.STATS)
	assert.NoError(t, err)
	value, _ = GetData(key, enum.STATS)
	assert.Nil(t, value)
	err = DeleteData(key, enum.ANALYTICS)
	assert.NoError(t, err)
	value, _ = GetData(key, enum.ANALYTICS)
	assert.Nil(t, value)
	err = DeleteData(key, enum.POLICIES)
	assert.NoError(t, err)
	value, _ = GetData(key, enum.POLICIES)
	assert.Nil(t, value)
	err = DeleteData(key, enum.SHARED)
	assert.NoError(t, err)
	value, _ = GetData(key, enum.SHARED)
	assert.Nil(t, value)
}
Exemplo n.º 3
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.º 4
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")

}
Exemplo n.º 5
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")
}
Exemplo n.º 6
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)

}
Exemplo n.º 7
0
func TestGetAllData(t *testing.T) {
	intern := 0

	//INTERNAL
	dataStore = intern
	Initialize()
	_, err := GetAllData(enum.STATS)
	assert.NoError(t, err)
	_, err = GetAllData(enum.ANALYTICS)
	assert.NoError(t, err)
	_, err = GetAllData(enum.POLICIES)
	assert.NoError(t, err)
	_, err = GetAllData(enum.SHARED)
	assert.NoError(t, err)
}
Exemplo n.º 8
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.º 9
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)
}
Exemplo n.º 10
0
func LogAndAssertText(t *testing.T, log func(*Logger), assertions func(fields map[string]string)) {
	var buffer bytes.Buffer

	logger := New()
	logger.Out = &buffer
	logger.Formatter = &TextFormatter{
		DisableColors: true,
	}

	log(logger)

	fields := make(map[string]string)
	for _, kv := range strings.Split(buffer.String(), " ") {
		if !strings.Contains(kv, "=") {
			continue
		}
		kvArr := strings.Split(kv, "=")
		key := strings.TrimSpace(kvArr[0])
		val := kvArr[1]
		if kvArr[1][0] == '"' {
			var err error
			val, err = strconv.Unquote(val)
			assert.NoError(t, err)
		}
		fields[key] = val
	}
	assertions(fields)
}
Exemplo n.º 11
0
func TestCreateInfluxMetrics(t *testing.T) {
	New("influxdb", createInfluxMockConfig())
	mockMetrics := CreateMockMetrics()
	points, err := createInfluxMetrics(mockMetrics)
	assert.NoError(t, err)
	assert.NotEmpty(t, points)
}
Exemplo n.º 12
0
func TestStoreClusterData(t *testing.T) {
	New("internal")
	var err error
	data := []byte{1, 2, 3, 4, 5}

	err = StoreClusterData(data, enum.POLICIES)
	assert.NoError(t, err)
}
Exemplo n.º 13
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.º 14
0
func TestStoreData(t *testing.T) {
	intern := 0
	key := "test"
	data := []byte("pippo")
	var err error

	//INTERNAL
	dataStore = intern
	Initialize()
	err = StoreData(key, data, enum.STATS)
	assert.NoError(t, err)
	err = StoreData(key, data, enum.ANALYTICS)
	assert.NoError(t, err)
	err = StoreData(key, data, enum.POLICIES)
	assert.NoError(t, err)
	err = StoreData(key, data, enum.SHARED)
	assert.NoError(t, err)
}
Exemplo n.º 15
0
func TestGetClusterData(t *testing.T) {
	data := []byte{1, 2, 3, 4, 5}
	New("intenal")

	StoreData(enum.CLUSTER.ToString(), data, enum.POLICIES)
	test, err := GetClusterData(enum.POLICIES)
	assert.NoError(t, err)
	assert.Equal(t, data, test)
}
Exemplo n.º 16
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())
}
Exemplo n.º 17
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)
}
Exemplo n.º 18
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)
}
Exemplo n.º 19
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)
}
Exemplo n.º 20
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)
}
Exemplo n.º 21
0
func TestClearFriendsData(t *testing.T) {
	var err error
	storage.New("internal")
	data := []byte{}
	storage.StoreLocalData(data, enum.SHARED)
	storage.StoreClusterData(data, enum.SHARED)
	storage.StoreData("node1", data, enum.SHARED)
	storage.StoreData("node2", data, enum.SHARED)
	storage.StoreData("node3", data, enum.SHARED)

	err = clearFriendsData()
	assert.NoError(t, err)
	stored, _ := storage.GetAllData(enum.SHARED)
	assert.Equal(t, 2, len(stored))
}
Exemplo n.º 22
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)
}
Exemplo n.º 23
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))
}
Exemplo n.º 24
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)
}
Exemplo n.º 25
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)
}
Exemplo n.º 26
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)
}
Exemplo n.º 27
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)

}
Exemplo n.º 28
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)
}
Exemplo n.º 29
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())
}
Exemplo n.º 30
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)
}