-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathelasticDb_integration_test.go
More file actions
159 lines (137 loc) · 3.83 KB
/
elasticDb_integration_test.go
File metadata and controls
159 lines (137 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// +build integration
package main
import (
"context"
"encoding/json"
"os"
"testing"
"time"
"github.com/bradleyjkemp/cupaloy/v2"
"github.com/olivere/elastic"
)
func TestNewElasticDb_AddLog(t *testing.T) {
testindex := "integrationtestunit"
db, err := NewElasticDb(getElasticUrl(), "", "", "")
if err != nil {
t.Fatal("Error by connect to db" + err.Error())
}
type Testdata struct {
Message string
}
countBefore, err := getCountOfDocumentsAtIndex(db.ctx, db.dbClient, testindex)
if err != nil {
countBefore = 0
}
db.AddLog(LogData{
Timestamp: time.Now(),
Type: string(MessageTypeLog),
Logs: Testdata{
Message: "Test",
},
Attributes: Attributes{
Host: "Integrationtest",
},
}, testindex)
countAfter, err := getCountOfDocumentsAtIndex(db.ctx, db.dbClient, testindex)
if err != nil {
t.Fatal("Error by get response" + err.Error())
}
if countBefore+1 != countAfter {
t.Errorf("Added Log but counts of document is not one more want %v got %v", countBefore+1, countAfter)
}
}
func TestNewElasticDb_AddStats(t *testing.T) {
testindex := "integrationtestunit_stats"
db, err := NewElasticDb(getElasticUrl(), "", "", "")
if err != nil {
t.Fatal("Error by connect to db" + err.Error())
}
type Testdata struct {
Message string
}
countBefore, err := getCountOfDocumentsAtIndex(db.ctx, db.dbClient, testindex)
if err != nil {
countBefore = 0
}
db.AddStats(StatsData{
Timestamp: time.Now(),
Type: string(MessageTypeLog),
Stats: Testdata{
Message: "Test",
},
Attributes: Attributes{
Host: "Integrationtest",
},
}, testindex)
countAfter, err := getCountOfDocumentsAtIndex(db.ctx, db.dbClient, testindex)
if err != nil {
t.Fatal("Error by get response" + err.Error())
}
if countBefore+1 != countAfter {
t.Errorf("Added Log but counts of document is not one more want %v got %v", countBefore+1, countAfter)
}
}
func TestNewElasticDb_SetPolicyTemplate(t *testing.T) {
db, err := NewElasticDb(getElasticUrl(), "", "", "")
if err != nil {
t.Fatal("Error by connect to db" + err.Error())
}
err = db.SetPolicyTemplate()
if err != nil {
t.Fatal("Error by SetPolicyTemplate " + err.Error())
}
ts := elastic.NewIndicesGetTemplateService(db.dbClient)
resp, err := ts.Name("funk_template").Do(db.ctx)
if err != nil {
t.Fatal("Error by get response" + err.Error())
}
cupaloy.SnapshotT(t, resp)
}
func TestNewElasticDb_SetIlmPolicy(t *testing.T) {
db, err := NewElasticDb(getElasticUrl(), "", "", "")
if err != nil {
t.Fatal("Error by connect to db" + err.Error())
}
db.SetIlmPolicy(DataRolloverPattern("monthly"))
ls := elastic.NewXPackIlmGetLifecycleService(db.dbClient)
res, err := ls.Policy("funk_policy").Do(db.ctx)
if err != nil {
t.Fatal("Error by get response" + err.Error())
}
phases := res["funk_policy"].Policy["phases"]
jsonphases, err := json.Marshal(phases)
cupaloy.SnapshotT(t, jsonphases)
if err != nil {
t.Fatal("Error by json.Marshal" + err.Error())
}
}
func TestNewElasticDb_SetFunkLogsDynamicTemplate(t *testing.T) {
db, err := NewElasticDb(getElasticUrl(), "", "", "")
if err != nil {
t.Fatal("Error by connect to db" + err.Error())
}
err = db.SetFunkLogsDynamicTemplate()
if err != nil {
t.Errorf("Error by create dynamic Template %v", err)
}
res, err := db.dbClient.IndexGetTemplate("funklog_dynamic_template").Do(db.ctx)
if err != nil {
t.Errorf("Error by Search for Template funklog_dynamic_template %v", err)
}
cupaloy.SnapshotT(t, res)
}
func getCountOfDocumentsAtIndex(ctx context.Context, db *elastic.Client, index string) (int64, error) {
is := elastic.NewIndicesStatsService(db)
resp, err := is.Index(index).Do(ctx)
if err != nil {
return 0, err
}
return resp.All.Primaries.Indexing.IndexTotal, nil
}
func getElasticUrl() string {
res := os.Getenv("ELASTICSEARCH")
if res == "" {
return "http://127.0.0.1:9200"
}
return res
}