Пример #1
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)
}
Пример #2
0
func TestComputeSysMetrics(t *testing.T) {
	defer resetMetrics()

	instMetrics := make(map[string]data.MetricData)
	instMetrics["instance1_1"] = data.MetricData{
		BaseMetrics: map[string]float64{
			enum.METRIC_CPU_AVG.ToString(): 0.4,
			enum.METRIC_MEM_AVG.ToString(): 0.4,
		},
	}
	instMetrics["instance1_2"] = data.MetricData{
		BaseMetrics: map[string]float64{
			enum.METRIC_CPU_AVG.ToString(): 0.6,
			enum.METRIC_MEM_AVG.ToString(): 0.6,
		},
	}
	instMetrics["instance2_1"] = data.MetricData{
		BaseMetrics: map[string]float64{
			enum.METRIC_CPU_AVG.ToString(): 0.8,
			enum.METRIC_MEM_AVG.ToString(): 0.8,
		},
	}

	res.CreateMockResources(4, "1G", 0, "0G")
	sysMet := computeSysMetrics(instMetrics)
	assert.NotEmpty(t, sysMet)
	assert.InEpsilon(t, 0.65, sysMet.BaseMetrics[enum.METRIC_CPU_AVG.ToString()], 0.0001)
}
Пример #3
0
func TestComputeServicesMetrics(t *testing.T) {
	defer resetMetrics()

	instMetrics := make(map[string]data.MetricData)
	instMetrics["instance1_1"] = data.MetricData{
		BaseMetrics: map[string]float64{
			enum.METRIC_CPU_AVG.ToString(): 0.4,
			enum.METRIC_MEM_AVG.ToString(): 0.4,
		},
	}
	instMetrics["instance1_2"] = data.MetricData{
		BaseMetrics: map[string]float64{
			enum.METRIC_CPU_AVG.ToString(): 0.6,
			enum.METRIC_MEM_AVG.ToString(): 0.6,
		},
	}
	instMetrics["instance2_1"] = data.MetricData{
		BaseMetrics: map[string]float64{
			enum.METRIC_CPU_AVG.ToString(): 0.5,
			enum.METRIC_MEM_AVG.ToString(): 0.5,
		},
	}

	servicesMetrics["service1"].UserMetrics["response_time"] = []float64{1000, 2000, 3000, 4000, 5000}
	servicesMetrics["service2"].UserMetrics["response_time"] = []float64{1000, 2000, 3000, 4000, 5000}

	serviceMet := computeServicesMetrics(instMetrics)
	assert.NotEmpty(t, serviceMet)
	assert.InEpsilon(t, 0.5, serviceMet["service1"].BaseMetrics[enum.METRIC_CPU_AVG.ToString()], 0.0001)
	assert.InEpsilon(t, 0.5, serviceMet["service2"].BaseMetrics[enum.METRIC_CPU_AVG.ToString()], 0.0001)
	assert.Equal(t, 0.0, serviceMet["service3"].BaseMetrics[enum.METRIC_CPU_AVG.ToString()])
	assert.Equal(t, 0.0, serviceMet["service3"].BaseMetrics[enum.METRIC_MEM_AVG.ToString()])
	assert.Equal(t, 3000.0, serviceMet["service1"].UserMetrics["response_time"])
	assert.Equal(t, 3000.0, serviceMet["service2"].UserMetrics["response_time"])
}
Пример #4
0
func TestCreateInfluxMetrics(t *testing.T) {
	New("influxdb", createInfluxMockConfig())
	mockMetrics := CreateMockMetrics()
	points, err := createInfluxMetrics(mockMetrics)
	assert.NoError(t, err)
	assert.NotEmpty(t, points)
}
Пример #5
0
func TestGetValues(t *testing.T) {
	b := BuildBuffer(10)
	values := []float64{1, 2, 3}
	b.values = append(b.values, values...)
	res := b.GetValues()
	assert.Len(t, res, len(values))
	assert.NotEmpty(t, b.values)
}
Пример #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)

}
Пример #7
0
func TestLogstashFormatter(t *testing.T) {
	assert := assert.New(t)

	lf := LogstashFormatter{Type: "abc"}

	fields := logrus.Fields{
		"message": "def",
		"level":   "ijk",
		"type":    "lmn",
		"one":     1,
		"pi":      3.14,
		"bool":    true,
	}

	entry := logrus.WithFields(fields)
	entry.Message = "msg"
	entry.Level = logrus.InfoLevel

	b, _ := lf.Format(entry)

	var data map[string]interface{}
	dec := json.NewDecoder(bytes.NewReader(b))
	dec.UseNumber()
	dec.Decode(&data)

	// base fields
	assert.Equal(json.Number("1"), data["@version"])
	assert.NotEmpty(data["@timestamp"])
	assert.Equal("abc", data["type"])
	assert.Equal("msg", data["message"])
	assert.Equal("info", data["level"])

	// substituted fields
	assert.Equal("def", data["fields.message"])
	assert.Equal("ijk", data["fields.level"])
	assert.Equal("lmn", data["fields.type"])

	// formats
	assert.Equal(json.Number("1"), data["one"])
	assert.Equal(json.Number("3.14"), data["pi"])
	assert.Equal(true, data["bool"])
}
Пример #8
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)
}