Exemple #1
0
func TestGetIndexStandard(t *testing.T) {

	time := time.Now().UTC()
	extension := fmt.Sprintf("%d.%02d.%02d", time.Year(), time.Month(), time.Day())

	event := common.MapStr{
		"@timestamp": common.Time(time),
		"field":      1,
	}

	pattern := "beatname-%{+yyyy.MM.dd}"
	fmtstr := fmtstr.MustCompileEvent(pattern)
	indexSel := outil.MakeSelector(outil.FmtSelectorExpr(fmtstr, ""))

	index := getIndex(event, indexSel)
	assert.Equal(t, index, "beatname-"+extension)
}
func esConnect(t *testing.T, index string) *esConnection {
	ts := time.Now().UTC()

	host := getElasticsearchHost()
	indexFmt := fmtstr.MustCompileEvent(fmt.Sprintf("%s-%%{+yyyy.MM.dd}", index))
	indexSel := outil.MakeSelector(outil.FmtSelectorExpr(indexFmt, ""))
	index, _ = indexSel.Select(common.MapStr{
		"@timestamp": common.Time(ts),
	})

	username := os.Getenv("ES_USER")
	password := os.Getenv("ES_PASS")
	client, err := elasticsearch.NewClient(elasticsearch.ClientSettings{
		URL:      host,
		Index:    indexSel,
		Username: username,
		Password: password,
		Timeout:  60 * time.Second,
	}, nil)
	if err != nil {
		t.Fatal(err)
	}

	// try to drop old index if left over from failed test
	_, _, _ = client.Delete(index, "", "", nil) // ignore error

	_, _, err = client.CreateIndex(index, common.MapStr{
		"settings": common.MapStr{
			"number_of_shards":   1,
			"number_of_replicas": 0,
		},
	})
	if err != nil {
		t.Fatalf("failed to create test index: %s", err)
	}

	es := &esConnection{}
	es.t = t
	es.Client = client
	es.index = index
	return es
}