/
config_utils.go
70 lines (58 loc) · 2.01 KB
/
config_utils.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
package gotestwithdocker
import (
"os"
"strconv"
"strings"
"github.com/olebedev/config"
"github.com/samalba/dockerclient"
)
type DockerTestConfig struct {
config *config.Config
}
func NewConfigContent(configContent string) (*DockerTestConfig, error) {
config, err := config.ParseYaml(configContent)
return &DockerTestConfig{config}, err
}
func NewConfig(configYamlFile string) (*DockerTestConfig, error) {
file, err := os.OpenFile(configYamlFile, os.O_RDONLY, 0666)
if err != nil {
return &DockerTestConfig{}, err
}
config, err := config.ParseYamlFile(file.Name())
return &DockerTestConfig{config}, err
}
func (testconfig *DockerTestConfig) GetContainerName() string {
return testconfig.config.UString("container_name", "test-container-name")
}
func (testconfig *DockerTestConfig) GetImageName() (string, error) {
return testconfig.config.String("image")
}
func (testconfig *DockerTestConfig) GetExposePorts() (map[string][]dockerclient.PortBinding, error) {
var exposePorts = make(map[string][]dockerclient.PortBinding, 0)
ports, err := testconfig.config.List("ports")
for _, v := range ports {
yamlPortValue, _ := v.(string)
if strings.Contains(yamlPortValue, ":") {
portSplit := strings.Split(yamlPortValue, ":")
containerPort := portSplit[0] + "/tcp"
hostPort := portSplit[1]
exposePorts[containerPort] = []dockerclient.PortBinding{{HostPort: hostPort}}
} else {
exposePorts[strconv.Itoa(v.(int))+"/tcp"] = []dockerclient.PortBinding{{HostIp: "0.0.0.0"}}
}
}
return exposePorts, err
}
func (testconfig *DockerTestConfig) GetWaitLogMessage() (string, error) {
return testconfig.config.String("wait.log")
}
func (testconfig *DockerTestConfig) GetWaitTimeout() int {
return testconfig.config.UInt("wait.timeout", 180)
}
func (testconfig *DockerTestConfig) GetBuildContextDirectory() (string, error) {
return testconfig.config.String("build.dockerfile_dir")
}
func (testconfig *DockerTestConfig) HasBuildConfig() bool {
_, err := testconfig.config.Get("build.dockerfile_dir")
return err == nil
}