/
compose.go
342 lines (323 loc) · 10.4 KB
/
compose.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2021 iLogtail Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package boot
import (
"context"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"github.com/docker/docker/api/types"
dockertypes "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"gopkg.in/yaml.v3"
"github.com/alibaba/ilogtail/pkg/helper"
"github.com/alibaba/ilogtail/pkg/logger"
"github.com/alibaba/ilogtail/test/config"
)
const (
composeCategory = "docker-compose"
finalFileName = "testcase-compose.yaml"
identifier = "e2e"
template = `version: '3.8'
services:
goc:
image: goc-server:latest
hostname: goc
ports:
- 7777:7777
volumes:
- %s:/coverage.log
healthcheck:
test: [ "CMD-SHELL", "curl -O /dev/null http://localhost:7777 || exit 1" ]
timeout: 5s
interval: 1s
retries: 10
ilogtailC:
image: aliyun/ilogtail:2.0.0
hostname: ilogtail
privileged: true
pid: host
volumes:
- %s:/ilogtail/default_flusher.json
- %s:/ilogtail/config/local
- /:/logtail_host
- /var/run/docker.sock:/var/run/docker.sock
- /sys/:/sys/
ports:
- 18689:18689
environment:
- LOGTAIL_FORCE_COLLECT_SELF_TELEMETRY=true
- LOGTAIL_DEBUG_FLAG=true
- LOGTAIL_AUTO_PROF=false
- LOGTAIL_HTTP_LOAD_CONFIG=true
- ALICLOUD_LOG_DOCKER_ENV_CONFIG=true
- ALICLOUD_LOG_PLUGIN_ENV_CONFIG=false
- ALIYUN_LOGTAIL_USER_DEFINED_ID=1111
`
)
// ComposeBooter control docker-compose to start or stop containers.
type ComposeBooter struct {
cfg *config.Case
cli *client.Client
gocID string
logtailID string
}
// StrategyWrapper caches the real wait.StrategyTarget to get the metadata of the running containers.
type StrategyWrapper struct {
original wait.Strategy
target wait.StrategyTarget
service string
virtualPort nat.Port
}
// NewComposeBooter create a new compose booter.
func NewComposeBooter(c *config.Case) *ComposeBooter {
return &ComposeBooter{cfg: c}
}
func (c *ComposeBooter) Start() error {
if err := c.createComposeFile(); err != nil {
return err
}
compose := testcontainers.NewLocalDockerCompose([]string{config.CaseHome + finalFileName}, identifier).WithCommand([]string{"up", "-d", "--build"})
strategyWrappers := withExposedService(compose)
execError := compose.Invoke()
if execError.Error != nil {
logger.Error(context.Background(), "START_DOCKER_COMPOSE_ERROR",
"stdout", execError.Error.Error())
return execError.Error
}
cli, err := helper.CreateDockerClient()
if err != nil {
return err
}
c.cli = cli
list, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
Filters: filters.NewArgs(filters.Arg("name", "e2e-ilogtailC")),
})
if len(list) != 1 {
logger.Errorf(context.Background(), "LOGTAIL_COMPOSE_ALARM", "logtail container size is not equal 1, got %d count", len(list))
return err
}
c.logtailID = list[0].ID
gocList, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
Filters: filters.NewArgs(filters.Arg("name", "goc")),
})
if len(gocList) != 1 {
logger.Errorf(context.Background(), "LOGTAIL_COMPOSE_ALARM", "goc container size is not equal 1, got %d count", len(list))
return err
}
c.gocID = gocList[0].ID
// the docker engine cannot access host on the linux platform, more details please see: https://github.com/docker/for-linux/issues/264
cmd := []string{
"sh",
"-c",
"env |grep HOST_OS|grep Linux && ip -4 route list match 0/0|awk '{print $3\" host.docker.internal\"}' >> /etc/hosts",
}
if err = c.exec(c.logtailID, cmd); err != nil {
return err
}
err = registerDockerNetMapping(strategyWrappers)
logger.Debugf(context.Background(), "registered net mapping: %v", networkMapping)
return err
}
func (c *ComposeBooter) Stop() error {
// fetch logtail code coverage
if c.gocID != "" {
f := strings.Join(c.cfg.CoveragePackages, "|")
cmd := "goc profile -o /coverage-raw.log"
if f == "" {
cmd += " && cat /coverage-raw.log >> /coverage.log"
} else {
cmd += " && head -n 1 /coverage-raw.log >> /coverage.log && cat /coverage-raw.log|grep -E \"" + f + "\" >> /coverage.log"
}
execCmd := []string{"sh", "-c", cmd}
if err := c.exec(c.gocID, execCmd); err != nil {
logger.Error(context.Background(), "FETCH_COVERAGE_ALARM", "err", err)
}
}
execError := testcontainers.NewLocalDockerCompose([]string{config.CaseHome + finalFileName}, identifier).Down()
if execError.Error != nil {
logger.Error(context.Background(), "STOP_DOCKER_COMPOSE_ERROR",
"stdout", execError.Stdout.Error(), "stderr", execError.Stderr.Error())
return execError.Error
}
_ = os.Remove(config.CaseHome + finalFileName)
return nil
}
func (c *ComposeBooter) exec(id string, cmd []string) error {
cfg := dockertypes.ExecConfig{
User: "root",
Cmd: cmd,
}
resp, err := c.cli.ContainerExecCreate(context.Background(), id, cfg)
if err != nil {
logger.Errorf(context.Background(), "DOCKER_EXEC_ALARM", "cannot create exec config: %v", err)
return err
}
err = c.cli.ContainerExecStart(context.Background(), resp.ID, dockertypes.ExecStartCheck{
Detach: false,
Tty: false,
})
if err != nil {
logger.Errorf(context.Background(), "DOCKER_EXEC_ALARM", "cannot start exec config: %v", err)
return err
}
return nil
}
func (c *ComposeBooter) CopyCoreLogs() {
if c.logtailID != "" {
_ = os.Remove(config.LogDir)
_ = os.Mkdir(config.LogDir, 0750)
cmd := exec.Command("docker", "cp", c.logtailID+":/ilogtail/ilogtail.LOG", config.LogDir)
output, err := cmd.CombinedOutput()
logger.Debugf(context.Background(), "\n%s", string(output))
if err != nil {
logger.Error(context.Background(), "COPY_LOG_ALARM", "type", "main", "err", err)
}
cmd = exec.Command("docker", "cp", c.logtailID+":/ilogtail/logtail_plugin.LOG", config.LogDir)
output, err = cmd.CombinedOutput()
logger.Debugf(context.Background(), "\n%s", string(output))
if err != nil {
logger.Error(context.Background(), "COPY_LOG_ALARM", "type", "plugin", "err", err)
}
}
}
func (s *StrategyWrapper) WaitUntilReady(ctx context.Context, target wait.StrategyTarget) error {
s.target = target
return s.original.WaitUntilReady(ctx, target)
}
func (c *ComposeBooter) createComposeFile() error {
// read the case docker compose file.
_, err := os.Stat(config.CaseHome + config.DockerComposeFileName)
var bytes []byte
if err != nil {
if !os.IsNotExist(err) {
return err
}
} else {
if bytes, err = os.ReadFile(config.CaseHome + config.DockerComposeFileName); err != nil {
return err
}
}
cfg := c.getLogtailpluginConfig()
// merge docker compose file.
if len(bytes) > 0 {
caseCfg := make(map[string]interface{})
if err = yaml.Unmarshal(bytes, &caseCfg); err != nil {
return err
}
if caseServices, ok := caseCfg["services"].(map[string]interface{}); ok {
services := cfg["services"].(map[string]interface{})
for name, spec := range caseServices {
services[name] = spec
}
}
}
yml, err := yaml.Marshal(cfg)
if err != nil {
return err
}
return os.WriteFile(config.CaseHome+finalFileName, yml, 0600)
}
// getLogtailpluginConfig find the docker compose configuration of the ilogtail.
func (c *ComposeBooter) getLogtailpluginConfig() map[string]interface{} {
var envs []string
for k, v := range c.cfg.Ilogtail.ENV {
envs = append(envs, k+"="+v)
}
cfg := make(map[string]interface{})
f, _ := os.Create(config.CoverageFile)
_ = f.Close()
str := fmt.Sprintf(template, config.CoverageFile, config.FlusherFile, config.ConfigDir)
if err := yaml.Unmarshal([]byte(str), &cfg); err != nil {
panic(err)
}
services := cfg["services"].(map[string]interface{})
ilogtail := services["ilogtailC"].(map[string]interface{})
if len(envs) > 0 {
ilogtail["environment"] = envs
}
if c.cfg.Ilogtail.DependsOn == nil {
c.cfg.Ilogtail.DependsOn = make(map[string]interface{})
}
c.cfg.Ilogtail.DependsOn["goc"] = map[string]string{
"condition": "service_healthy",
}
ilogtail["depends_on"] = c.cfg.Ilogtail.DependsOn
for _, m := range c.cfg.Ilogtail.MountFiles {
ilogtail["volumes"] = append(ilogtail["volumes"].([]interface{}), m)
}
for _, port := range c.cfg.Ilogtail.Ports {
ilogtail["ports"] = append(ilogtail["ports"].([]interface{}), port)
}
bytes, _ := yaml.Marshal(cfg)
println(string(bytes))
return cfg
}
// registerDockerNetMapping
func registerDockerNetMapping(wrappers []*StrategyWrapper) error {
for _, wrapper := range wrappers {
host, err := wrapper.target.Host(context.Background())
if err != nil {
return err
}
physicalPort, err := wrapper.target.MappedPort(context.Background(), wrapper.virtualPort)
if err != nil {
return err
}
physicalPort.Int()
registerNetMapping(wrapper.service+":"+wrapper.virtualPort.Port(), host+":"+physicalPort.Port())
}
return nil
}
// withExposedService add wait.Strategy to the docker compose.
func withExposedService(compose testcontainers.DockerCompose) (wrappers []*StrategyWrapper) {
localCompose := compose.(*testcontainers.LocalDockerCompose)
for serv, rawCfg := range localCompose.Services {
cfg := rawCfg.(map[string]interface{})
rawPorts, ok := cfg["ports"]
if !ok {
continue
}
ports := rawPorts.([]interface{})
for i := 0; i < len(ports); i++ {
var wrapper StrategyWrapper
var portNum int
var np nat.Port
switch p := ports[i].(type) {
case int:
portNum = p
np = nat.Port(fmt.Sprintf("%d/tcp", p))
case string:
relations := strings.Split(p, ":")
proto, port := nat.SplitProtoPort(relations[len(relations)-1])
np = nat.Port(fmt.Sprintf("%s/%s", port, proto))
portNum, _ = strconv.Atoi(port)
}
wrapper = StrategyWrapper{
original: wait.ForListeningPort(np),
service: serv,
virtualPort: np,
}
localCompose.WithExposedService(serv, portNum, &wrapper)
wrappers = append(wrappers, &wrapper)
}
}
return
}