Skip to content

Commit

Permalink
Add another test for elasticsearch output
Browse files Browse the repository at this point in the history
- requested by elastic#7
  • Loading branch information
monicasarbu committed Apr 28, 2015
1 parent ad82a83 commit 46b4721
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion outputs/elasticsearch/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestTopologyInES(t *testing.T) {
}
}

func TestEvents(t *testing.T) {
func TestOneEvent(t *testing.T) {
if testing.Short() {
t.Skip("Skipping events publish in short mode, because they require Elasticsearch")
}
Expand Down Expand Up @@ -144,3 +144,74 @@ func TestEvents(t *testing.T) {
t.Errorf("Too many results")
}
}

func TestEvents(t *testing.T) {
if testing.Short() {
t.Skip("Skipping events publish in short mode, because they require Elasticsearch")
}
ts := time.Now()

elasticsearchOutput := createElasticsearchConnection()

event := common.MapStr{}
event["type"] = "redis"
event["status"] = "OK"
event["responsetime"] = 34
event["dst_ip"] = "192.168.21.1"
event["dst_port"] = 6379
event["src_ip"] = "192.168.22.2"
event["src_port"] = 6378
event["agent"] = "appserver1"
r := common.MapStr{}
r["request"] = "MGET key1"
r["response"] = "value1"
event["redis"] = r

index := fmt.Sprintf("%s-%d.%02d.%02d", elasticsearchOutput.Index, ts.Year(), ts.Month(), ts.Day())

es := NewElasticsearch("http://localhost:9200")

if es == nil {
t.Errorf("Failed to create Elasticsearch connection")
}
_, err := es.DeleteIndex(index)
if err != nil {
t.Errorf("Failed to delete index: %s", err)
}

err = elasticsearchOutput.PublishEvent(ts, event)
if err != nil {
t.Errorf("Failed to publish the event: %s", err)
}

r = common.MapStr{}
r["request"] = "MSET key1 value1"
r["response"] = 0
event["redis"] = r

err = elasticsearchOutput.PublishEvent(ts, event)
if err != nil {
t.Errorf("Failed to publish the event: %s", err)
}

es.Refresh(index)

resp, err := es.Search(index, "?search?q=agent:appserver1", "{}")

if err != nil {
t.Errorf("Failed to query elasticsearch: %s", err)
}
defer resp.Body.Close()
objresp, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("Failed to read body from response")
}
var search_res ESSearchResults
err = json.Unmarshal(objresp, &search_res)
if err != nil {
t.Errorf("Failed to unmarshal response: %s", err)
}
if search_res.Hits.Total != 2 {
t.Errorf("Too many results")
}
}

0 comments on commit 46b4721

Please sign in to comment.