-
Notifications
You must be signed in to change notification settings - Fork 384
/
docker_compose.go
118 lines (102 loc) · 3.18 KB
/
docker_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
package setup
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"gopkg.in/yaml.v3"
"github.com/alibaba/ilogtail/pkg/protocol"
"github.com/alibaba/ilogtail/test/config"
"github.com/alibaba/ilogtail/test/engine/setup/controller"
)
const dependencyHome = "test_cases"
type DockerComposeEnv struct {
BootController *controller.BootController
}
func StartDockerComposeEnv(ctx context.Context, dependencyName string) (context.Context, error) {
if dockerComposeEnv, ok := Env.(*DockerComposeEnv); ok {
path := dependencyHome + "/" + dependencyName
err := config.Load(path, config.TestConfig.Profile)
if err != nil {
return ctx, err
}
dockerComposeEnv.BootController = new(controller.BootController)
if err = dockerComposeEnv.BootController.Init(); err != nil {
return ctx, err
}
startTime := time.Now().Unix()
if err = dockerComposeEnv.BootController.Start(ctx); err != nil {
return ctx, err
}
return context.WithValue(ctx, config.StartTimeContextKey, int32(startTime)), nil
}
return ctx, fmt.Errorf("env is not docker-compose")
}
func SetDockerComposeDependOn(ctx context.Context, dependOnContainers string) (context.Context, error) {
if _, ok := Env.(*DockerComposeEnv); ok {
containers := make([]string, 0)
err := yaml.Unmarshal([]byte(dependOnContainers), &containers)
if err != nil {
return ctx, err
}
ctx = context.WithValue(ctx, config.DependOnContainerKey, containers)
} else {
return ctx, fmt.Errorf("env is not docker-compose")
}
return ctx, nil
}
func MountVolume(ctx context.Context, source, target string) (context.Context, error) {
if _, ok := Env.(*DockerComposeEnv); ok {
var existVolume []string
var ok bool
if existVolume, ok = ctx.Value(config.MountVolumeKey).([]string); ok {
existVolume = append(existVolume, source+":"+target)
} else {
existVolume = []string{source + ":" + target}
}
ctx = context.WithValue(ctx, config.MountVolumeKey, existVolume)
} else {
return ctx, fmt.Errorf("env is not docker-compose")
}
return ctx, nil
}
func ExposePort(ctx context.Context, source, target string) (context.Context, error) {
if _, ok := Env.(*DockerComposeEnv); ok {
var existPort []string
var ok bool
if existPort, ok = ctx.Value(config.ExposePortKey).([]string); ok {
existPort = append(existPort, source+":"+target)
} else {
existPort = []string{source + ":" + target}
}
ctx = context.WithValue(ctx, config.ExposePortKey, existPort)
} else {
return ctx, fmt.Errorf("env is not docker-compose")
}
return ctx, nil
}
func NewDockerComposeEnv() *DockerComposeEnv {
env := &DockerComposeEnv{}
root, _ := filepath.Abs(".")
reportDir := root + "/report/"
_ = os.Mkdir(reportDir, 0750)
config.ConfigDir = reportDir + "config"
return env
}
func (d *DockerComposeEnv) GetType() string {
return "docker-compose"
}
func (d *DockerComposeEnv) GetData() (*protocol.LogGroup, error) {
return nil, fmt.Errorf("not implemented")
}
func (d *DockerComposeEnv) Clean() error {
d.BootController.Clean()
return nil
}
func (d *DockerComposeEnv) ExecOnLogtail(command string) error {
return fmt.Errorf("not implemented")
}
func (d *DockerComposeEnv) ExecOnSource(command string) error {
return fmt.Errorf("not implemented")
}