-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
213 lines (156 loc) · 4.61 KB
/
util.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package tests
import (
"context"
"io/ioutil"
"log"
"os"
"strconv"
"sync"
"testing"
"time"
"github.com/Jeffail/gabs"
"github.com/alejandro-carstens/golastic"
"github.com/alejandro-carstens/scrubber/actions"
"github.com/alejandro-carstens/scrubber/actions/contexts"
"github.com/alejandro-carstens/scrubber/logger"
"github.com/alejandro-carstens/scrubber/notifications"
"github.com/alejandro-carstens/scrubber/ymlparser"
"github.com/stretchr/testify/assert"
)
const SEED_INDEX_COUNT int = 4
const ELASTICSEARCH_BULK_INSERT_LIMIT int = 10000
func createTestIndex(filePath string) (actions.Actionable, error) {
config, err := config(filePath)
if err != nil {
return nil, err
}
action, err := getAction(config)
if err != nil {
return nil, err
}
return action.Perform(), nil
}
func createTestIndexAsync(filePath string, waitGroup *sync.WaitGroup) {
log.Println("Creating " + filePath)
if _, err := createTestIndex(filePath); err != nil {
log.Println(err)
}
time.Sleep(time.Duration(int64(2)) * time.Second)
defer waitGroup.Done()
}
func config(path string) (*gabs.Container, error) {
currentPath, err := os.Getwd()
if err != nil {
return nil, err
}
return ymlparser.Parse(currentPath + path)
}
func getAction(config *gabs.Container) (actions.Actionable, error) {
ctx, err := contexts.New(config)
if err != nil {
return nil, err
}
logger := logger.NewLogger("", true, true, true, true)
return actions.Create(ctx, logger, connection(), queue(logger), context.Background())
}
func takeAction(path string, t *testing.T) actions.Actionable {
config, err := config(path)
if err != nil {
t.Error(err)
}
action, err := getAction(config)
if err != nil {
t.Error(err)
}
assert.False(t, action.Perform().HasErrors())
time.Sleep(time.Duration(int64(2)) * time.Second)
assert.Nil(t, action.Notify())
return action
}
func snapshotCleanup(repository, snapshot, index string, c *golastic.Connection) error {
var conn *golastic.Connection
if c == nil {
conn = connection()
} else {
conn = c
}
if len(snapshot) > 0 {
if _, err := conn.Indexer(nil).DeleteSnapshot(repository, snapshot); err != nil {
return err
}
}
if _, err := conn.Indexer(nil).DeleteRepositories(repository); err != nil {
return err
}
return conn.Indexer(nil).DeleteIndex(index)
}
func takeActionAsync(path string, t *testing.T, waitGroup *sync.WaitGroup) {
defer waitGroup.Done()
takeAction(path, t)
}
func connection() *golastic.Connection {
connection := golastic.NewConnection(&golastic.ConnectionContext{
Urls: []string{os.Getenv("ELASTICSEARCH_URI")},
Password: os.Getenv("ELASTICSEARCH_PASSWORD"),
Username: os.Getenv("ELASTICSEARCH_USERNAME"),
HealthCheckInterval: 30,
Context: context.Background(),
})
if err := connection.Connect(); err != nil {
panic(err)
}
return connection
}
func queue(logger *logger.Logger) *notifications.Queue {
queue, err := notifications.NewQueue(10, logger)
if err != nil {
panic(err)
}
return queue
}
func seedIndexAsync(index string, count int, connection *golastic.Connection, waitGroup *sync.WaitGroup, useConstantTime bool) {
defer waitGroup.Done()
inserts := []interface{}{}
for i := 0; i < count; i++ {
value := map[string]interface{}{}
value["id"] = strconv.Itoa(i + 1 + 1000000000)
value["exception"] = "Exception exception exception exception exception exception exception exception exception"
value["request"] = "Request request request request request request request request request"
value["message"] = "Message message message message message message message message message"
value["bytes"] = int64(i)
value["number"] = float64(i)
if useConstantTime {
constantTime, err := time.Parse(time.RFC3339, "2017-11-12T11:45:26.371Z")
if err != nil {
panic(err)
}
value["created_at"] = constantTime
} else {
value["created_at"] = time.Now().Add(time.Duration(int64(-1*(i+1))) * time.Hour)
}
inserts = append(inserts, value)
if count >= ELASTICSEARCH_BULK_INSERT_LIMIT && (i+1)%ELASTICSEARCH_BULK_INSERT_LIMIT == 0 {
if _, err := connection.Builder(index).Insert(inserts...); err != nil {
panic(err)
}
inserts = []interface{}{}
}
}
if count < ELASTICSEARCH_BULK_INSERT_LIMIT {
if _, err := connection.Builder(index).Insert(inserts...); err != nil {
panic(err)
}
}
time.Sleep(time.Duration(int64(2)) * time.Second)
}
func compareFiles(f1, f2 string) (bool, error) {
c1, err := ioutil.ReadFile(f1)
if err != nil {
return false, err
}
c2, err := ioutil.ReadFile(f2)
if err != nil {
return false, err
}
return string(c1) == string(c2), nil
}