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) }
func TestWithFieldsShouldAllowAssignments(t *testing.T) { var buffer bytes.Buffer var fields Fields logger := New() logger.Out = &buffer logger.Formatter = new(JSONFormatter) localLog := logger.WithFields(Fields{ "key1": "value1", }) localLog.WithField("key2", "value2").Info("test") err := json.Unmarshal(buffer.Bytes(), &fields) assert.Nil(t, err) assert.Equal(t, "value2", fields["key2"]) assert.Equal(t, "value1", fields["key1"]) buffer = bytes.Buffer{} fields = Fields{} localLog.Info("test") err = json.Unmarshal(buffer.Bytes(), &fields) assert.Nil(t, err) _, ok := fields["key2"] assert.Equal(t, false, ok) assert.Equal(t, "value1", fields["key1"]) }
func TestDeltaElement(t *testing.T) { var plc *data.Policy var threshold float64 var policies []data.Policy targets := []string{"pippo"} actions := map[string][]enum.Action{ "pippo": []enum.Action{enum.START}, } policies = []data.Policy{ data.CreateMockPolicy("p1", 0.1, targets, actions), data.CreateMockPolicy("p2", 0.4, targets, actions), data.CreateMockPolicy("p3", 0.6, targets, actions), data.CreateMockPolicy("p4", 0.2, targets, actions), } threshold = 0.4 plc = deltaElement(policies, threshold) assert.Equal(t, "p3", plc.Name) threshold = 0.8 plc = deltaElement(policies, threshold) assert.Equal(t, "p3", plc.Name) }
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"]) }
func TestGetCoresAvailable(t *testing.T) { defer freeCores() var assigned string var ok bool req := 2 id := "pippo" assigned, ok = GetCoresAvailable(req) assert.True(t, ok) assert.Equal(t, "0,1", assigned) freeCores() assignSpecificCores([]int{1, 3}, id) assigned, ok = GetCoresAvailable(req) assert.True(t, ok) assert.Equal(t, "0,2", assigned) freeCores() req_wrong := c_NCORES + 1 assigned, ok = GetCoresAvailable(req_wrong) assert.False(t, ok) assert.Equal(t, "", assigned) }
func TestMean(t *testing.T) { values := []float64{2, 4, 6} valuesEmpty := []float64{} assert.Equal(t, 4.0, Mean(values)) assert.Equal(t, 0.0, Mean(valuesEmpty)) }
func TestWarn(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.Warn("test") }, func(fields Fields) { assert.Equal(t, fields["msg"], "test") assert.Equal(t, fields["level"], "warning") }) }
func TestInfo(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.Info("test") }, func(fields Fields) { assert.Equal(t, fields["msg"], "test") assert.Equal(t, fields["level"], "info") }) }
func TestUserSuppliedLevelFieldHasPrefix(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.WithField("level", 1).Info("test") }, func(fields Fields) { assert.Equal(t, fields["level"], "info") assert.Equal(t, fields["fields.level"], 1.0) // JSON has floats only }) }
func TestUserSuppliedMsgFieldHasPrefix(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.WithField("msg", "hello").Info("test") }, func(fields Fields) { assert.Equal(t, fields["msg"], "test") assert.Equal(t, fields["fields.msg"], "hello") }) }
func TestGetters(t *testing.T) { defer storage.DeleteAllData(enum.ANALYTICS) data.SaveMockAnalytics() analytics := data.CreateMockAnalytics() assert.Equal(t, analytics, GetAnalytics()) assert.Equal(t, analytics.Service["service1"], GetServiceAnalytics("service1")) assert.Equal(t, analytics.Service, GetServicesAnalytics()) assert.Equal(t, analytics.System, GetSystemAnalytics()) }
func TestCreateMemory(t *testing.T) { notSet := "" mem1 := "1G" var memory int64 memory = createMemory(notSet) assert.Equal(t, int64(0), memory) memory = createMemory(mem1) assert.Equal(t, 1024, 1024) }
func TestComputeInstanceCpuPerc(t *testing.T) { mockInstCpus := []float64{10000, 20000, 30000, 40000, 50000, 60000} mockSysCpus := []float64{1000000, 1100000, 1200000, 1300000, 1400000, 1500000} mockPerc := computeInstanceCpuPerc(mockInstCpus, mockSysCpus) assert.Equal(t, 0.1, mockPerc) mockInstCpus = []float64{10000, 10000, 10000, 10000, 10000, 10000} mockPerc = computeInstanceCpuPerc(mockInstCpus, mockSysCpus) assert.Equal(t, 0.0, mockPerc) }
func TestHookFires(t *testing.T) { hook := new(TestHook) LogAndAssertJSON(t, func(log *Logger) { log.Hooks.Add(hook) assert.Equal(t, hook.Fired, false) log.Print("test") }, func(fields Fields) { assert.Equal(t, hook.Fired, true) }) }
func TestGetFreeSlots(t *testing.T) { b := BuildBuffer(10) values := []float64{1, 2, 3} var f int f = b.GetFreeSlots() assert.Equal(t, b.capacity, f) b.values = append(b.values, values...) f = b.GetFreeSlots() assert.Equal(t, b.capacity-len(b.values), f) }
func TestParseLevel(t *testing.T) { l, err := ParseLevel("panic") assert.Nil(t, err) assert.Equal(t, PanicLevel, l) l, err = ParseLevel("fatal") assert.Nil(t, err) assert.Equal(t, FatalLevel, l) l, err = ParseLevel("error") assert.Nil(t, err) assert.Equal(t, ErrorLevel, l) l, err = ParseLevel("warn") assert.Nil(t, err) assert.Equal(t, WarnLevel, l) l, err = ParseLevel("warning") assert.Nil(t, err) assert.Equal(t, WarnLevel, l) l, err = ParseLevel("info") assert.Nil(t, err) assert.Equal(t, InfoLevel, l) l, err = ParseLevel("debug") assert.Nil(t, err) assert.Equal(t, DebugLevel, l) l, err = ParseLevel("invalid") assert.Equal(t, "not a valid logrus Level: \"invalid\"", err.Error()) }
func TestCanFireMultipleHooks(t *testing.T) { hook1 := new(ModifyHook) hook2 := new(TestHook) LogAndAssertJSON(t, func(log *Logger) { log.Hooks.Add(hook1) log.Hooks.Add(hook2) log.WithField("wow", "elephant").Print("test") }, func(fields Fields) { assert.Equal(t, fields["wow"], "whale") assert.Equal(t, hook2.Fired, true) }) }
func TestSetPlannerStrategy(t *testing.T) { probcumulative := "probcumulative" probdelta := "probdelta" notSupported := "notsupported" SetPlannerStrategy(probcumulative) assert.Equal(t, probcumulative, currentStrategy.Name(), "(probcumulative) Current strategy should be probcumulative") SetPlannerStrategy(probdelta) assert.Equal(t, probdelta, currentStrategy.Name(), "(probdelta) Current strategy should be probdelta") SetPlannerStrategy(notSupported) assert.Equal(t, "dummy", currentStrategy.Name(), "(notsupported) Current strategy should be dummy") }
func TestCreateCpusetCpus(t *testing.T) { resources.CreateMockResources(2, "1G", 0, "0G") empty := "" core0 := "0" var cpusetcpus string cpusetcpus = createCpusetCpus(empty, 1) assert.Equal(t, "0", cpusetcpus) cpusetcpus = createCpusetCpus(core0, 1) assert.Equal(t, "0", cpusetcpus) cpusetcpus = createCpusetCpus(empty, 0) assert.Equal(t, "0", cpusetcpus) }
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) }
func TestNew(t *testing.T) { notSupported := "notSupported" dscvr, err := New(notSupported, "http://localhost:5000") assert.Error(t, err) assert.Equal(t, "noservice", dscvr.Name()) }
func TestValue(t *testing.T) { assert.Equal(t, 0.0, data_s.Value()) assert.Equal(t, 1.0, data_a.Value()) assert.Equal(t, 2.0, data_p.Value()) assert.Equal(t, 3.0, data_i.Value()) assert.Equal(t, 0.0, action_no.Value()) assert.Equal(t, 1.0, action_st.Value()) assert.Equal(t, 2.0, action_sp.Value()) assert.Equal(t, 3.0, action_rm.Value()) assert.Equal(t, 0.0, owner_l.Value()) assert.Equal(t, 1.0, owner_c.Value()) }
func TestInfoShouldAddSpacesBetweenTwoNonStrings(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.Infoln(10, 10) }, func(fields Fields) { assert.Equal(t, fields["msg"], "10 10") }) }
func TestInfoShouldNotAddSpacesBetweenStrings(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.Info("test", "test") }, func(fields Fields) { assert.Equal(t, fields["msg"], "testtest") }) }
func TestUserSuppliedFieldDoesNotOverwriteDefaults(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.WithField("msg", "hello").Info("test") }, func(fields Fields) { assert.Equal(t, fields["msg"], "test") }) }
func TestComputeServicesAnalytics(t *testing.T) { stats := data.CreateMockStats() expected := make(map[string]data.AnalyticData) expected["service1"] = data.AnalyticData{ BaseAnalytics: map[string]float64{ enum.METRIC_CPU_AVG.ToString(): 0.6, enum.METRIC_MEM_AVG.ToString(): 0.3, }, UserAnalytics: map[string]float64{ "expr1": 0.5, }, } expected["service2"] = data.AnalyticData{ BaseAnalytics: map[string]float64{ enum.METRIC_CPU_AVG.ToString(): 0.1, enum.METRIC_MEM_AVG.ToString(): 0.1, }, UserAnalytics: map[string]float64{ "expr2": 0.6, }, } expected["service3"] = data.AnalyticData{ BaseAnalytics: map[string]float64{ enum.METRIC_CPU_AVG.ToString(): 0.9, enum.METRIC_MEM_AVG.ToString(): 0.8, }, UserAnalytics: map[string]float64{ "expr3": 0.0, }, } analytics := computeServicesAnalytics(stats.Metrics.Service) assert.Equal(t, expected, analytics) }
func TestInfolnShouldAddSpacesBetweenStringAndNonstring(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.Infoln("test", 10) }, func(fields Fields) { assert.Equal(t, fields["msg"], "test 10") }) }
func TestBuildExpression(t *testing.T) { metrics := map[string]float64{ "M1": 0.5, "M2": 0.8, } constraints := map[string]float64{ "C1": 0.8, "C2": 2.0, } expr1 := cfg.AnalyticExpr{ Expr: "M1 + M2 / C1", Metrics: []string{"M1", "M2"}, Constraints: []string{"C1"}, } expct1 := "0.5 + 0.8 / 0.8" expr2 := cfg.AnalyticExpr{ Expr: "(M1 + C1) * (M2 + C2)", Metrics: []string{"M1", "M2"}, Constraints: []string{"C1", "C2"}, } expct2 := "(0.5 + 0.8) * (0.8 + 2)" expr3 := cfg.AnalyticExpr{ Expr: "M3 + M2 / C1", Metrics: []string{"M3", "M2"}, Constraints: []string{"C1"}, } expct3 := "noexp" expr4 := cfg.AnalyticExpr{ Expr: "M1 + M2 / C3", Metrics: []string{"M1", "M2"}, Constraints: []string{"C3"}, } expct4 := "noexp" toBuild1 := buildExpression(expr1, metrics, constraints) toBuild2 := buildExpression(expr2, metrics, constraints) toBuild3 := buildExpression(expr3, metrics, constraints) toBuild4 := buildExpression(expr4, metrics, constraints) assert.Equal(t, expct1, toBuild1) assert.Equal(t, expct2, toBuild2) assert.Equal(t, expct3, toBuild3) assert.Equal(t, expct4, toBuild4) }
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) }
func TestFreeInstanceCores(t *testing.T) { defer freeCores() var id string id = "pippo" assignCores(2, id) assert.Equal(t, c_NCORES-2, getAvailableCores()) FreeInstanceCores(id) assert.Equal(t, c_NCORES, getAvailableCores()) assignCores(2, id) id = "topolino" FreeInstanceCores(id) assert.Equal(t, c_NCORES-2, getAvailableCores()) }