-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtesting_container_elasticsearch.go
107 lines (88 loc) · 2.5 KB
/
testing_container_elasticsearch.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
package kibana
import (
"bytes"
"errors"
"fmt"
"log"
"os"
dockertest "github.com/ory/dockertest"
dck "github.com/ory/dockertest/docker"
"github.com/parnurzeal/gorequest"
)
type elasticSearchContainer struct {
Name string
pool *dockertest.Pool
resource *dockertest.Resource
Uri string
}
func newElasticSearchContainer(pool *dockertest.Pool, elasticSearchVersion string) (*elasticSearchContainer, error) {
_, useXpackSecurity := os.LookupEnv("USE_XPACK_SECURITY")
envVars := []string{
"discovery.type=single-node",
}
if useXpackSecurity {
envVars = append(envVars,
"ELASTIC_PASSWORD=changeme",
"xpack.security.enabled=true",
)
}
options := &dockertest.RunOptions{
Name: "elasticsearch",
Hostname: "elasticsearch",
Repository: "elastic-local",
Tag: elasticSearchVersion,
Env: envVars,
}
resource, err := pool.RunWithOptions(options)
if err != nil {
return nil, err
}
elasticSearchAddress := fmt.Sprintf("http://localhost:%v", resource.GetPort("9200/tcp"))
if err := pool.Retry(func() error {
container, err := pool.Client.InspectContainer(resource.Container.ID)
if err != nil {
return err
}
if !container.State.Running {
var buf bytes.Buffer
//err = pool.Client.AttachToContainer(docker.AttachToContainerOptions{
options := dck.AttachToContainerOptions{
Container: resource.Container.ID,
OutputStream: &buf,
Logs: true,
Stdout: true,
Stderr: true,
}
err = pool.Client.AttachToContainer(options)
if err != nil {
return errors.New("Container is not running: " + container.State.StateString())
}
return errors.New("Container is not running: " + buf.String())
}
client := gorequest.New()
response, body, errs := client.Get(elasticSearchAddress).SetBasicAuth("elastic", "changeme").End()
if errs != nil {
return errs[0]
}
if response.StatusCode >= 300 {
return errors.New(fmt.Sprintf("Status: %d, %s", response.StatusCode, body))
}
return nil
}); err != nil {
log.Fatalf("Could not connect to elastic search: %s", err)
}
if err != nil {
log.Fatalf("Could not connect to elastic search: %s", err)
}
name := getContainerName(resource)
log.Printf("Elastic %s search (%v): up", elasticSearchVersion, name)
return &elasticSearchContainer{
Name: name,
pool: pool,
resource: resource,
Uri: elasticSearchAddress,
}, nil
}
func (elasticSearch *elasticSearchContainer) Stop() error {
return elasticSearch.pool.Purge(elasticSearch.resource)
}