github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-elasticsearch/lib/elasticsearch_test.go (about)

     1  package mpelasticsearch
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  var testHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    14  	json, err := os.ReadFile("./stat.json")
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  
    19  	fmt.Fprint(w, string(json))
    20  })
    21  
    22  func TestGraphDefinition(t *testing.T) {
    23  	elasticsearch := ElasticsearchPlugin{
    24  		Prefix:      "elasticsearch",
    25  		LabelPrefix: "Elasticsearch",
    26  	}
    27  	graphdef := elasticsearch.GraphDefinition()
    28  
    29  	assert.EqualValues(t, "Elasticsearch HTTP", graphdef["elasticsearch.http"].Label)
    30  	assert.EqualValues(t, "Elasticsearch Thread-Pool Threads", graphdef["elasticsearch.thread_pool.threads"].Label)
    31  	assert.EqualValues(t, "threads_fetch_shard_started", graphdef["elasticsearch.thread_pool.threads"].Metrics[16].Name)
    32  	assert.EqualValues(t, "threads_fetch_shard_store", graphdef["elasticsearch.thread_pool.threads"].Metrics[17].Name)
    33  	assert.EqualValues(t, "threads_listener", graphdef["elasticsearch.thread_pool.threads"].Metrics[18].Name)
    34  	assert.EqualValues(t, "compilation_limit_triggered", graphdef["elasticsearch.script"].Metrics[2].Name)
    35  }
    36  
    37  func TestFetchMetrics(t *testing.T) {
    38  	ts := httptest.NewServer(testHandler)
    39  	defer ts.Close()
    40  
    41  	var elasticsearch ElasticsearchPlugin
    42  	elasticsearch.URI = ts.URL
    43  	stat, err := elasticsearch.FetchMetrics()
    44  	if err != nil {
    45  		t.Fatal(err)
    46  	}
    47  
    48  	assert.EqualValues(t, 37, stat["http_opened"])
    49  	assert.EqualValues(t, 8, stat["threads_generic"])
    50  	assert.EqualValues(t, 13, stat["threads_search"])
    51  	assert.EqualValues(t, 0, stat["threads_fetch_shard_started"])
    52  	assert.EqualValues(t, 0, stat["threads_fetch_shard_store"])
    53  	assert.EqualValues(t, 331, stat["open_file_descriptors"])
    54  	assert.EqualValues(t, 1, stat["compilations"])
    55  }