Example #1
0
func TestTestCollect(t *testing.T) {
	config := make(map[string]interface{})

	testChannel := make(chan metric.Metric)
	testLogger := test_utils.BuildLogger()

	// conforms to the valueGenerator interface in the collector
	mockGen := func() float64 {
		return 4.0
	}

	test := NewTest(testChannel, 123, testLogger)
	test.Configure(config)
	test.generator = mockGen

	go test.Collect()

	select {
	case m := <-test.Channel():
		assert.Equal(t, 4.0, m.Value)
		return
	case <-time.After(2 * time.Second):
		t.Fail()
	}
}
Example #2
0
func TestDiamondCollect(t *testing.T) {
	config := make(map[string]interface{})
	config["port"] = "0"

	testChannel := make(chan metric.Metric)
	testLog := test_utils.BuildLogger()

	d := newDiamond(testChannel, 123, testLog).(*Diamond)
	d.Configure(config)

	// start collecting Diamond metrics
	go d.Collect()

	conn, err := connectToDiamondCollector(d)
	require.Nil(t, err, "should connect")
	require.NotNil(t, conn, "should connect")

	emitTestMetric(conn)

	select {
	case m := <-d.Channel():
		assert.Equal(t, m.Name, "test")
	case <-time.After(1 * time.Second):
		t.Fail()
	}
}
func TestProcStatusCollect(t *testing.T) {
	config := make(map[string]interface{})
	config["interval"] = 9999

	dims := map[string]string{
		"module": "(.*)",
	}

	config["generatedDimensions"] = dims

	channel := make(chan metric.Metric)

	testLog := test_utils.BuildLogger()
	ps := NewProcStatus(channel, 12, testLog)
	ps.Configure(config)

	go ps.Collect()

	select {
	case <-ps.Channel():
		return
	case <-time.After(2 * time.Second):
		t.Fail()
	}
}
func TestProcStatusMetrics(t *testing.T) {
	testLog := test_utils.BuildLogger()

	config := make(map[string]interface{})

	dims := map[string]string{
		"seven":  "(.......)",
		"eleven": "(...........)",
	}
	config["generatedDimensions"] = dims

	ps := NewProcStatus(nil, 12, testLog)
	ps.Configure(config)

	count := 0
	for _, m := range ps.procStatusMetrics() {
		mDims := m.Dimensions
		_, existsSeven := mDims["seven"]
		_, existsEleven := mDims["eleven"]
		if existsSeven == false || existsEleven == false {
			continue
		}
		count++
	}
	if count == 0 {
		t.Fail()
	}
}
Example #5
0
func TestCollectorLogsErrors(t *testing.T) {
	testLogger := test_utils.BuildLogger()
	testLogger = testLogger.WithField("collector", "Test")

	channel := make(chan metric.Metric)
	config := make(map[string]interface{})

	testCol := collector.NewTest(channel, 123, testLogger)
	testCol.Configure(config)

	timeout := time.Duration(5 * time.Second)
	h := handler.NewTest(channel, 10, 10, timeout, testLogger)

	hook := NewLogErrorHook([]handler.Handler{h})
	testLogger.Logger.Hooks.Add(hook)

	go testCol.Collect()
	testLogger.Error("testing Error log")

	select {
	case m := <-h.Channel():
		assert.Equal(t, "fullerite.collector_errors", m.Name)
		assert.Equal(t, 1.0, m.Value)
		assert.Equal(t, "Test", m.Dimensions["collector"])
		return
	case <-time.After(1 * time.Second):
		t.Fail()
	}
}
func TestProcStatusCollectMetricTypes(t *testing.T) {
	config := make(map[string]interface{})
	config["interval"] = 9999

	dims := map[string]string{
		"module": "(.*)",
	}

	config["generatedDimensions"] = dims

	channel := make(chan metric.Metric)

	testLog := test_utils.BuildLogger()
	ps := NewProcStatus(channel, 12, testLog)
	ps.Configure(config)

	go ps.Collect()

	select {
	case <-ps.Channel():
		for _, m := range ps.procStatusMetrics() {
			if m.Name == "CPUTime" {
				assert.Equal(t, m.MetricType, metric.CumulativeCounter, "CPUTime is a CumulativeCounter")
			} else {
				assert.Equal(t, m.MetricType, metric.Gauge, "All others are a Gauge")
			}
		}
	}
}
Example #7
0
func TestFulleriteCollect(t *testing.T) {
	config := make(map[string]interface{})

	testChannel := make(chan metric.Metric)
	testLog := test_utils.BuildLogger()

	f := NewFullerite(testChannel, 123, testLog)
	f.Configure(config)

	go f.Collect()

	select {
	case <-f.Channel():
		return
	case <-time.After(2 * time.Second):
		t.Fail()
	}
}
Example #8
0
func TestTestConfigureMetricName(t *testing.T) {
	config := make(map[string]interface{})
	config["metricName"] = "lala"

	testChannel := make(chan metric.Metric)
	testLogger := test_utils.BuildLogger()

	test := NewTest(testChannel, 123, testLogger)
	test.Configure(config)

	go test.Collect()

	select {
	case m := <-test.Channel():
		// don't test for the value - only metric name
		assert.Equal(t, m.Name, "lala")
	case <-time.After(1 * time.Second):
		t.Fail()
	}
}
Example #9
0
func TestCpuInfoCollect(t *testing.T) {
	config := make(map[string]interface{})
	config["procPath"] = path.Join(test_utils.DirectoryOfCurrentFile(), "/../../fixtures/proc/cpuinfo")
	testChannel := make(chan metric.Metric)
	testLogger := test_utils.BuildLogger()

	cpuInfo := newCPUInfo(testChannel, 100, testLogger)
	cpuInfo.Configure(config)

	go cpuInfo.Collect()

	select {
	case m := <-cpuInfo.Channel():
		assert.Equal(t, 2.0, m.Value)
		assert.Equal(t, "Xeon(R) CPU E5-2630 0 @ 2.30GHz", m.Dimensions["model"])
		return
	case <-time.After(2 * time.Second):
		t.Fail()
	}
}
Example #10
0
func TestProcStatusExtractDimensions(t *testing.T) {
	testLog := test_utils.BuildLogger()

	config := make(map[string]interface{})

	dims := map[string]string{
		"module": "^python.*?test.*?\\.([^\\.]*)?\\-\\[\\d+\\]$",
		"order":  "^python.*?test.*?\\.[^\\.]*?\\-\\[(\\d+)\\]$",
	}
	config["generatedDimensions"] = dims

	ps := NewProcStatus(nil, 12, testLog)
	ps.Configure(config)

	dim := map[string]string{
		"module": "bond",
		"order":  "007",
	}

	extracted := ps.extractDimensions("python -m test.my.function.bond-[007]")
	assert.Equal(t, dim, extracted)
}
Example #11
0
func TestProcStatusMatches(t *testing.T) {
	assert := assert.New(t)
	testLog := test_utils.BuildLogger()
	ps := NewProcStatus(nil, 12, testLog)
	config := make(map[string]interface{})

	commGenerator := func(comm string, err error) func() (string, error) {
		return func() (string, error) {
			return comm, err
		}
	}

	config["pattern"] = ".*"
	config["matchCommandLine"] = true
	ps.Configure(config)

	match := ps.matches([]string{"proc", "status"}, commGenerator("proc", nil))
	assert.True(match)
	match = ps.matches([]string{"proc", "status"}, commGenerator("proc", errors.New("")))
	assert.True(match)

	config["pattern"] = ".*"
	config["matchCommandLine"] = false
	ps.Configure(config)

	match = ps.matches([]string{"proc", "status"}, commGenerator("proc", nil))
	assert.True(match)
	match = ps.matches([]string{"proc", "status"}, commGenerator("proc", errors.New("")))
	assert.False(match)

	config["pattern"] = "sta"
	config["matchCommandLine"] = true
	ps.Configure(config)

	match = ps.matches([]string{"proc", "status"}, commGenerator("proc", nil))
	assert.True(match)
	match = ps.matches([]string{"proc", "butter"}, commGenerator("proc", nil))
	assert.False(match)
	match = ps.matches([]string{"proc", "butter"}, commGenerator("status", nil))
	assert.False(match)
	match = ps.matches([]string{"proc", "status"}, commGenerator("proc", errors.New("")))
	assert.True(match)

	config["pattern"] = "pro"
	config["matchCommandLine"] = false
	ps.Configure(config)

	match = ps.matches([]string{"proc", "status"}, commGenerator("proc", nil))
	assert.True(match)
	match = ps.matches([]string{"peanut", "status"}, commGenerator("peanut", nil))
	assert.False(match)
	match = ps.matches([]string{"proc", "status"}, commGenerator("peanut", nil))
	assert.False(match)
	match = ps.matches([]string{"proc", "status"}, commGenerator("proc", errors.New("")))
	assert.False(match)

	config["pattern"] = "oc sta"
	config["matchCommandLine"] = true
	ps.Configure(config)

	match = ps.matches([]string{"proc", "status"}, commGenerator("proc", nil))
	assert.True(match)
}