コード例 #1
0
ファイル: haproxy_test.go プロジェクト: toorop/telegraf
func TestHaproxyGeneratesMetricsWithoutAuthentication(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, csvOutputSample)
	}))
	defer ts.Close()

	r := &haproxy{
		Servers: []string{ts.URL},
	}

	var acc testutil.Accumulator

	err := r.Gather(&acc)
	require.NoError(t, err)

	tags := map[string]string{
		"proxy": "be_app",
		"host":  ts.Listener.Addr().String(),
		"sv":    "host0",
	}

	assert.NoError(t, acc.ValidateTaggedValue("stot", uint64(171014), tags))
	assert.NoError(t, acc.ValidateTaggedValue("scur", uint64(1), tags))
	assert.NoError(t, acc.ValidateTaggedValue("rate", uint64(3), tags))
	assert.Equal(t, true, acc.CheckValue("bin", uint64(5557055817)))
}
コード例 #2
0
ファイル: exec_test.go プロジェクト: toorop/telegraf
func TestExec(t *testing.T) {
	runner := newRunnerMock([]byte(validJson), nil)
	command := Command{Command: "testcommand arg1", Name: "mycollector"}
	e := &Exec{runner: runner, Commands: []*Command{&command}}

	var acc testutil.Accumulator
	err := e.Gather(&acc)
	require.NoError(t, err)

	checkFloat := []struct {
		name  string
		value float64
	}{
		{"mycollector_num_processes", 82},
		{"mycollector_cpu_used", 8234},
		{"mycollector_cpu_free", 32},
		{"mycollector_percent", 0.81},
	}

	for _, c := range checkFloat {
		assert.True(t, acc.CheckValue(c.name, c.value))
	}

	assert.Equal(t, len(acc.Points), 4, "non-numeric measurements should be ignored")
}
コード例 #3
0
ファイル: haproxy_test.go プロジェクト: toorop/telegraf
func TestHaproxyGeneratesMetricsWithAuthentication(t *testing.T) {
	//We create a fake server to return test data
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		username, password, ok := r.BasicAuth()
		if !ok {
			w.WriteHeader(http.StatusNotFound)
			fmt.Fprint(w, "Unauthorized")
			return
		}

		if username == "user" && password == "password" {
			fmt.Fprint(w, csvOutputSample)
		} else {
			w.WriteHeader(http.StatusNotFound)
			fmt.Fprint(w, "Unauthorized")
		}
	}))
	defer ts.Close()

	//Now we tested again above server, with our authentication data
	r := &haproxy{
		Servers: []string{strings.Replace(ts.URL, "http://", "http://*****:*****@", 1)},
	}

	var acc testutil.Accumulator

	err := r.Gather(&acc)
	require.NoError(t, err)

	tags := map[string]string{
		"host":  ts.Listener.Addr().String(),
		"proxy": "be_app",
		"sv":    "host0",
	}

	assert.NoError(t, acc.ValidateTaggedValue("stot", uint64(171014), tags))

	checkInt := []struct {
		name  string
		value uint64
	}{
		{"bin", 5557055817},
		{"scur", 288},
		{"qmax", 81},
		{"http_response.1xx", 0},
		{"http_response.2xx", 1314093},
		{"http_response.3xx", 537036},
		{"http_response.4xx", 123452},
		{"dreq", 1102},
		{"dresp", 80},
		{"wretr", 17},
		{"wredis", 19},
		{"ereq", 95740},
		{"econ", 0},
		{"eresp", 0},
		{"req_rate", 35},
		{"req_rate_max", 140},
		{"req_tot", 1987928},
		{"bin", 5557055817},
		{"bout", 24096715169},
		{"rate", 18},
		{"rate_max", 102},

		{"throttle", 13},
		{"lbtot", 114},
	}

	for _, c := range checkInt {
		assert.Equal(t, true, acc.CheckValue(c.name, c.value))
	}

	//Here, we should get error because we don't pass authentication data
	r = &haproxy{
		Servers: []string{ts.URL},
	}

	err = r.Gather(&acc)
	require.Error(t, err)
}
コード例 #4
0
ファイル: redis_test.go プロジェクト: toorop/telegraf
func TestRedisGeneratesMetrics(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	l, err := net.Listen("tcp", ":0")
	require.NoError(t, err)

	defer l.Close()

	go func() {
		c, err := l.Accept()
		if err != nil {
			return
		}

		buf := bufio.NewReader(c)

		for {
			line, err := buf.ReadString('\n')
			if err != nil {
				return
			}

			if line != "info\r\n" {
				return
			}

			fmt.Fprintf(c, "$%d\n", len(testOutput))
			c.Write([]byte(testOutput))
		}
	}()

	addr := fmt.Sprintf("redis://%s", l.Addr().String())

	r := &Redis{
		Servers: []string{addr},
	}

	var acc testutil.Accumulator

	err = r.Gather(&acc)
	require.NoError(t, err)

	checkInt := []struct {
		name  string
		value uint64
	}{
		{"uptime", 238},
		{"clients", 1},
		{"used_memory", 1003936},
		{"used_memory_rss", 811008},
		{"used_memory_peak", 1003936},
		{"used_memory_lua", 33792},
		{"rdb_changes_since_last_save", 0},
		{"total_connections_received", 2},
		{"total_commands_processed", 1},
		{"instantaneous_ops_per_sec", 0},
		{"sync_full", 0},
		{"sync_partial_ok", 0},
		{"sync_partial_err", 0},
		{"expired_keys", 0},
		{"evicted_keys", 0},
		{"keyspace_hits", 0},
		{"keyspace_misses", 0},
		{"pubsub_channels", 0},
		{"pubsub_patterns", 0},
		{"latest_fork_usec", 0},
		{"connected_slaves", 0},
		{"master_repl_offset", 0},
		{"repl_backlog_active", 0},
		{"repl_backlog_size", 1048576},
		{"repl_backlog_histlen", 0},
	}

	for _, c := range checkInt {
		assert.True(t, acc.CheckValue(c.name, c.value))
	}

	checkFloat := []struct {
		name  string
		value float64
	}{
		{"mem_fragmentation_ratio", 0.81},
		{"used_cpu_sys", 0.14},
		{"used_cpu_user", 0.05},
		{"used_cpu_sys_children", 0.00},
		{"used_cpu_user_children", 0.00},
	}

	for _, c := range checkFloat {
		assert.True(t, acc.CheckValue(c.name, c.value))
	}
}
コード例 #5
0
ファイル: disque_test.go プロジェクト: toorop/telegraf
func TestDisqueCanPullStatsFromMultipleServers(t *testing.T) {
	if testing.Short() {
		t.Skip("Skipping integration test in short mode")
	}

	l, err := net.Listen("tcp", ":0")
	require.NoError(t, err)

	defer l.Close()

	go func() {
		c, err := l.Accept()
		if err != nil {
			return
		}

		buf := bufio.NewReader(c)

		for {
			line, err := buf.ReadString('\n')
			if err != nil {
				return
			}

			if line != "info\r\n" {
				return
			}

			fmt.Fprintf(c, "$%d\n", len(testOutput))
			c.Write([]byte(testOutput))
		}
	}()

	addr := fmt.Sprintf("disque://%s", l.Addr().String())

	r := &Disque{
		Servers: []string{addr},
	}

	var acc testutil.Accumulator

	err = r.Gather(&acc)
	require.NoError(t, err)

	checkInt := []struct {
		name  string
		value uint64
	}{
		{"uptime", 1452705},
		{"clients", 31},
		{"blocked_clients", 13},
		{"used_memory", 1840104},
		{"used_memory_rss", 3227648},
		{"used_memory_peak", 89603656},
		{"total_connections_received", 5062777},
		{"total_commands_processed", 12308396},
		{"instantaneous_ops_per_sec", 18},
		{"latest_fork_usec", 1644},
		{"registered_jobs", 360},
		{"registered_queues", 12},
	}

	for _, c := range checkInt {
		assert.True(t, acc.CheckValue(c.name, c.value))
	}

	checkFloat := []struct {
		name  string
		value float64
	}{
		{"mem_fragmentation_ratio", 1.75},
		{"used_cpu_sys", 19585.73},
		{"used_cpu_user", 11255.96},
		{"used_cpu_sys_children", 1.75},
		{"used_cpu_user_children", 1.91},
	}

	for _, c := range checkFloat {
		assert.True(t, acc.CheckValue(c.name, c.value))
	}
}