func TestPasswords(t *testing.T) { // Add password and ensure it gets reset defer resetPassword(redisHost, password, "") err := addPassword(redisHost, password) if err != nil { t.Fatal("adding password", err) } // Test Fetch metrics with missing password f := mbtest.NewEventFetcher(t, getConfig("")) _, err = f.Fetch() if assert.Error(t, err, "missing password") { assert.Contains(t, err, "NOAUTH Authentication required.") } // Config redis and metricset with an invalid password f = mbtest.NewEventFetcher(t, getConfig("blah")) _, err = f.Fetch() if assert.Error(t, err, "invalid password") { assert.Contains(t, err, "ERR invalid password") } // Config redis and metricset with a valid password f = mbtest.NewEventFetcher(t, getConfig(password)) _, err = f.Fetch() assert.NoError(t, err, "valid password") }
// TestFetchTimeout verifies that the HTTP request times out and an error is // returned. func TestFetchTimeout(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Header().Set("Content-Type", "text/plain; charset=ISO-8859-1") w.Write([]byte(response)) time.Sleep(100 * time.Millisecond) })) defer server.Close() config := map[string]interface{}{ "module": "apache", "metricsets": []string{"status"}, "hosts": []string{server.URL}, "timeout": "50ms", } f := mbtest.NewEventFetcher(t, config) start := time.Now() _, err := f.Fetch() elapsed := time.Since(start) if assert.Error(t, err) { assert.Contains(t, err.Error(), "request canceled (Client.Timeout exceeded") } // Elapsed should be ~50ms, sometimes it can be up to 1s assert.True(t, elapsed < 5*time.Second, "elapsed time: %s", elapsed.String()) }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) assert.Contains(t, event, "checkpoints") assert.Contains(t, event, "buffers") assert.Contains(t, event, "stats_reset") checkpoints := event["checkpoints"].(common.MapStr) assert.Contains(t, checkpoints, "scheduled") assert.Contains(t, checkpoints, "requested") assert.Contains(t, checkpoints, "times") buffers := event["buffers"].(common.MapStr) assert.Contains(t, buffers, "checkpoints") assert.Contains(t, buffers, "clean") assert.Contains(t, buffers, "clean_full") assert.Contains(t, buffers, "backend") assert.Contains(t, buffers, "backend_fsync") assert.Contains(t, buffers, "allocated") }
func TestData(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) err := mbtest.WriteEvent(f, t) if err != nil { t.Fatal("write", err) } }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check number of fields. assert.Equal(t, 12, len(event)) }
func TestData(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) // Do a first fetch to have percentages f.Fetch() time.Sleep(1 * time.Second) err := mbtest.WriteEvent(f, t) if err != nil { t.Fatal("write", err) } }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check event fields openFiles := event["harvesters"].(common.MapStr)["files"].(common.MapStr)["open"].(int64) assert.True(t, openFiles >= 0) }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig("")) event, err := f.Fetch() if err != nil { t.Fatal("fetch", err) } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check fields assert.Equal(t, 8, len(event)) server := event["server"].(common.MapStr) assert.Equal(t, "standalone", server["mode"]) }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check number of fields. if len(event) < 11 { t.Fatal("Too few top-level elements in the event") } }
func TestKeyspace(t *testing.T) { // Write to DB to enable Keyspace stats err := writeToRedis(redisHost) if err != nil { t.Fatal("write to host", err) } // Fetch metrics f := mbtest.NewEventFetcher(t, getConfig("")) event, err := f.Fetch() if err != nil { t.Fatal("fetch", err) } keyspace := event["keyspace"].(map[string]common.MapStr) keyCount := keyspace["db0"]["keys"].(int) assert.True(t, (keyCount > 0)) }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check event fields current := event["connections"].(common.MapStr)["current"].(int64) assert.True(t, current >= 0) available := event["connections"].(common.MapStr)["available"].(int64) assert.True(t, available > 0) commits := event["journaling"].(common.MapStr)["commits"].(int64) assert.True(t, commits >= 0) }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check event fields connections := event["connections"].(int64) open := event["open"].(common.MapStr) openTables := open["tables"].(int64) openFiles := open["files"].(int64) openStreams := open["streams"].(int64) assert.True(t, connections > 0) assert.True(t, openTables > 0) assert.True(t, openFiles >= 0) assert.True(t, openStreams == 0) }
// TestMultipleFetches verifies that the server connection is reused when HTTP // keep-alive is supported by the server. func TestMultipleFetches(t *testing.T) { server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Header().Set("Content-Type", "text/plain; charset=ISO-8859-1") w.Write([]byte(response)) })) connLock := sync.Mutex{} conns := map[string]struct{}{} server.Config.ConnState = func(conn net.Conn, state http.ConnState) { connLock.Lock() conns[conn.RemoteAddr().String()] = struct{}{} connLock.Unlock() } server.Start() defer server.Close() config := map[string]interface{}{ "module": "apache", "metricsets": []string{"status"}, "hosts": []string{server.URL}, } f := mbtest.NewEventFetcher(t, config) for i := 0; i < 20; i++ { _, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } } connLock.Lock() assert.Len(t, conns, 1, "only a single connection should exist because of keep-alives") connLock.Unlock() }
func TestFetchRaw(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig(true)) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check event fields cachedThreads := event["threads"].(common.MapStr)["cached"].(int64) assert.True(t, cachedThreads >= 0) rawData := event["raw"].(common.MapStr) // Make sure field was removed from raw fields as in schema _, exists := rawData["Threads_cached"] assert.False(t, exists) // Check a raw field if it is available _, exists = rawData["Slow_launch_threads"] assert.True(t, exists) }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check values version := event["zk_version"].(string) avgLatency := event["zk_avg_latency"].(int) maxLatency := event["zk_max_latency"].(int) numAliveConnections := event["zk_num_alive_connections"].(int) assert.Equal(t, version, "3.4.8--1, built on 02/06/2016 03:18 GMT") assert.True(t, avgLatency >= 0) assert.True(t, maxLatency >= 0) assert.True(t, numAliveConnections > 0) // Check fields assert.Equal(t, 18, len(event)) }
func TestFetch(t *testing.T) { f := mbtest.NewEventFetcher(t, getConfig()) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event) // Check values version := event["version"].(string) avgLatency := event["latency"].(common.MapStr)["avg"].(int64) maxLatency := event["latency"].(common.MapStr)["max"].(int64) numAliveConnections := event["num_alive_connections"].(int64) assert.Equal(t, version, "3.4.8--1, built on 02/06/2016 03:18 GMT") assert.True(t, avgLatency >= 0) assert.True(t, maxLatency >= 0) assert.True(t, numAliveConnections > 0) // Check number of fields. At least 10, depending on environment assert.True(t, len(event) >= 10) }
func TestFetchEventContents(t *testing.T) { absPath, err := filepath.Abs("./testdata/") // response is a raw response from a couchbase response, err := ioutil.ReadFile(absPath + "/sample_response.json") server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Header().Set("Content-Type", "appication/json;") w.Write([]byte(response)) })) defer server.Close() config := map[string]interface{}{ "module": "couchbase", "metricsets": []string{"cluster"}, "hosts": []string{server.URL}, } f := mbtest.NewEventFetcher(t, config) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event.StringToPrint()) hdd := event["hdd"].(common.MapStr) hdd_free := hdd["free"].(common.MapStr) assert.EqualValues(t, 46902679716, hdd_free["bytes"]) hdd_total := hdd["total"].(common.MapStr) assert.EqualValues(t, 63381999616, hdd_total["bytes"]) hdd_used := hdd["used"].(common.MapStr) hdd_used_value := hdd_used["value"].(common.MapStr) assert.EqualValues(t, 16479319900, hdd_used_value["bytes"]) hdd_used_by_data := hdd_used["by_data"].(common.MapStr) assert.EqualValues(t, 16369010, hdd_used_by_data["bytes"]) hdd_quota := hdd["quota"].(common.MapStr) hdd_quota_total := hdd_quota["total"].(common.MapStr) assert.EqualValues(t, 63381999616, hdd_quota_total["bytes"]) assert.EqualValues(t, 10, event["max_bucket_count"]) quota := event["quota"].(common.MapStr) quota_index_memory := quota["index_memory"].(common.MapStr) assert.EqualValues(t, 300, quota_index_memory["mb"]) quota_memory := quota["memory"].(common.MapStr) assert.EqualValues(t, 300, quota_memory["mb"]) ram := event["ram"].(common.MapStr) ram_quota := ram["quota"].(common.MapStr) ram_quota_total := ram_quota["total"].(common.MapStr) ram_quota_total_value := ram_quota_total["value"].(common.MapStr) assert.EqualValues(t, 314572800, ram_quota_total_value["bytes"]) ram_quota_total_per_node := ram_quota_total["per_node"].(common.MapStr) assert.EqualValues(t, 314572800, ram_quota_total_per_node["bytes"]) ram_quota_used := ram_quota["used"].(common.MapStr) ram_quota_used_value := ram_quota_used["value"].(common.MapStr) assert.EqualValues(t, 104857600, ram_quota_used_value["bytes"]) ram_quota_used_per_node := ram_quota_used["per_node"].(common.MapStr) assert.EqualValues(t, 104857600, ram_quota_used_per_node["bytes"]) ram_total := ram["total"].(common.MapStr) assert.EqualValues(t, 8359174144, ram_total["bytes"]) ram_used := ram["used"].(common.MapStr) ram_used_value := ram_used["value"].(common.MapStr) assert.EqualValues(t, 8004751360, ram_used_value["bytes"]) ram_used_by_data := ram_used["by_data"].(common.MapStr) assert.EqualValues(t, 53962016, ram_used_by_data["bytes"]) }
// TestFetchEventContents verifies the contents of the returned event against // the raw Apache response. func TestFetchEventContents(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Header().Set("Content-Type", "text/plain; charset=ISO-8859-1") w.Write([]byte(response)) })) defer server.Close() config := map[string]interface{}{ "module": "apache", "metricsets": []string{"status"}, "hosts": []string{server.URL}, } f := mbtest.NewEventFetcher(t, config) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event.StringToPrint()) assert.Equal(t, 386.299, event["bytes_per_request"]) assert.Equal(t, .0628293, event["bytes_per_sec"]) workers := event["workers"].(common.MapStr) assert.EqualValues(t, 1, workers["busy"]) assert.EqualValues(t, 99, workers["idle"]) connections := event["connections"].(common.MapStr) async := connections["async"].(common.MapStr) assert.EqualValues(t, 3, async["closing"]) assert.EqualValues(t, 2, async["keep_alive"]) assert.EqualValues(t, 1, async["writing"]) assert.EqualValues(t, 6, connections["total"]) cpu := event["cpu"].(common.MapStr) assert.Equal(t, 11.2, cpu["children_system"]) assert.Equal(t, 10.1, cpu["children_user"]) assert.Equal(t, 2.02841, cpu["load"]) assert.Equal(t, 6750.8, cpu["system"]) assert.Equal(t, 14076.6, cpu["user"]) assert.Equal(t, server.URL[7:], event["hostname"]) load := event["load"].(common.MapStr) assert.Equal(t, .02, load["1"]) assert.Equal(t, .05, load["15"]) assert.Equal(t, .01, load["5"]) assert.Equal(t, .000162644, event["requests_per_sec"]) scoreboard := event["scoreboard"].(common.MapStr) assert.Equal(t, 0, scoreboard["closing_connection"]) assert.Equal(t, 0, scoreboard["dns_lookup"]) assert.Equal(t, 0, scoreboard["gracefully_finishing"]) assert.Equal(t, 0, scoreboard["idle_cleanup"]) assert.Equal(t, 0, scoreboard["keepalive"]) assert.Equal(t, 0, scoreboard["logging"]) assert.Equal(t, 300, scoreboard["open_slot"]) // Number of '.' assert.Equal(t, 0, scoreboard["reading_request"]) assert.Equal(t, 1, scoreboard["sending_reply"]) // Number of 'W' assert.Equal(t, 400, scoreboard["total"]) // Number of scorecard chars. assert.Equal(t, 99, scoreboard["waiting_for_connection"]) // Number of '_' assert.EqualValues(t, 167, event["total_accesses"]) assert.EqualValues(t, 63, event["total_kbytes"]) uptime := event["uptime"].(common.MapStr) assert.EqualValues(t, 1026782, uptime["server_uptime"]) assert.EqualValues(t, 1026782, uptime["uptime"]) }
// TestFetchEventContents verifies the contents of the returned event against // the raw Apache response. func TestFetchEventContents(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) w.Header().Set("Content-Type", "text/plain; charset=ISO-8859-1") w.Write([]byte(response)) })) defer server.Close() config := map[string]interface{}{ "module": "apache", "metricsets": []string{"status"}, "hosts": []string{server.URL}, } f := mbtest.NewEventFetcher(t, config) event, err := f.Fetch() if !assert.NoError(t, err) { t.FailNow() } t.Logf("%s/%s event: %+v", f.Module().Name(), f.Name(), event.StringToPrint()) assert.Equal(t, 1, event["busyWorkers"]) assert.Equal(t, 386.299, event["bytesPerReq"]) assert.Equal(t, .0628293, event["bytesPerSec"]) connections := event["connections"].(common.MapStr) assert.Equal(t, 3, connections["connsAsyncClosing"]) assert.Equal(t, 2, connections["connsAsyncKeepAlive"]) assert.Equal(t, 1, connections["connsAsyncWriting"]) assert.Equal(t, 6, connections["connsTotal"]) cpu := event["cpu"].(common.MapStr) assert.Equal(t, 11.2, cpu["cpuChildrenSystem"]) assert.Equal(t, 10.1, cpu["cpuChildrenUser"]) assert.Equal(t, 2.02841, cpu["cpuLoad"]) assert.Equal(t, 6750.8, cpu["cpuSystem"]) assert.Equal(t, 14076.6, cpu["cpuUser"]) assert.Equal(t, server.URL, event["hostname"]) assert.Equal(t, 99, event["idleWorkers"]) load := event["load"].(common.MapStr) assert.Equal(t, .02, load["load1"]) assert.Equal(t, .05, load["load15"]) assert.Equal(t, .01, load["load5"]) assert.Equal(t, .000162644, event["reqPerSec"]) scoreboard := event["scoreboard"].(common.MapStr) assert.Equal(t, 0, scoreboard["closingConnection"]) assert.Equal(t, 0, scoreboard["dnsLookup"]) assert.Equal(t, 0, scoreboard["gracefullyFinishing"]) assert.Equal(t, 0, scoreboard["idleCleanup"]) assert.Equal(t, 0, scoreboard["keepalive"]) assert.Equal(t, 0, scoreboard["logging"]) assert.Equal(t, 300, scoreboard["openSlot"]) // Number of '.' assert.Equal(t, 0, scoreboard["readingRequest"]) assert.Equal(t, 1, scoreboard["sendingReply"]) // Number of 'W' assert.Equal(t, 400, scoreboard["total"]) // Number of scorecard chars. assert.Equal(t, 99, scoreboard["waitingForConnection"]) // Number of '_' assert.Equal(t, 167, event["totalAccesses"]) assert.Equal(t, 63, event["totalKBytes"]) uptime := event["uptime"].(common.MapStr) assert.Equal(t, 1026782, uptime["serverUptimeSeconds"]) assert.Equal(t, 1026782, uptime["uptime"]) }