-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
docker.go
200 lines (179 loc) · 6.53 KB
/
docker.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
// Copyright 2017 Authors of Cilium
//
// 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 helpers
import (
"fmt"
"strings"
"github.com/cilium/cilium/test/helpers/constants"
"github.com/onsi/ginkgo"
)
// ContainerExec executes cmd in the container with the provided name along with
// any other additional arguments needed.
func (s *SSHMeta) ContainerExec(name string, cmd string, optionalArgs ...string) *CmdRes {
optionalArgsCoalesced := ""
if len(optionalArgs) > 0 {
optionalArgsCoalesced = strings.Join(optionalArgs, " ")
}
dockerCmd := fmt.Sprintf("docker exec -i %s %s %s", optionalArgsCoalesced, name, cmd)
return s.Exec(dockerCmd)
}
// ContainerRun is a wrapper to a one execution docker run container. It runs
// an instance of the specific Docker image with the provided network, name and
// options.
func (s *SSHMeta) ContainerRun(name, image, net, options string, cmdParams ...string) *CmdRes {
cmdOnStart := ""
if len(cmdParams) > 0 {
cmdOnStart = strings.Join(cmdParams, " ")
}
cmd := fmt.Sprintf(
"docker run --name %s --net %s %s %s %s", name, net, options, image, cmdOnStart)
return s.ExecWithSudo(cmd)
}
// ContainerCreate is a wrapper for `docker run`. It runs an instance of the
// specified Docker image with the provided network, name, options and container
// startup commands.
func (s *SSHMeta) ContainerCreate(name, image, net, options string, cmdParams ...string) *CmdRes {
cmdOnStart := ""
if len(cmdParams) > 0 {
cmdOnStart = strings.Join(cmdParams, " ")
}
if _, ok := constants.AllImages[image]; !ok {
ginkgo.Fail(fmt.Sprintf("Image %s is not in the set of pre-pulled Docker images; add this image to `AllImages` in `test/helpers/constants/images.go` and / or update the VM image to pull this image if necessary", image), 1)
}
cmd := fmt.Sprintf(
"docker run -d --name %s --net %s %s %s %s", name, net, options, image, cmdOnStart)
log.Debugf("spinning up container with command '%v'", cmd)
return s.ExecWithSudo(cmd)
}
// ContainerRm is a wrapper around `docker rm -f`. It forcibly removes the
// Docker container of the provided name.
func (s *SSHMeta) ContainerRm(name string) *CmdRes {
cmd := fmt.Sprintf("docker rm -f %s", name)
return s.ExecWithSudo(cmd)
}
// ContainerInspect runs `docker inspect` for the container with the provided
// name.
func (s *SSHMeta) ContainerInspect(name string) *CmdRes {
return s.ExecWithSudo(fmt.Sprintf("docker inspect %s", name))
}
// ContainerInspectNet returns a map of Docker networking information fields and
// their associated values for the container of the provided name. An error
// is returned if the networking information could not be retrieved.
func (s *SSHMeta) ContainerInspectNet(name string) (map[string]string, error) {
res := s.ContainerInspect(name)
properties := map[string]string{
"EndpointID": "EndpointID",
"GlobalIPv6Address": IPv6,
"IPAddress": IPv4,
"NetworkID": "NetworkID",
"IPv6Gateway": "IPv6Gateway",
}
if !res.WasSuccessful() {
return nil, fmt.Errorf("could not inspect container %s", name)
}
filter := fmt.Sprintf(`{ [0].NetworkSettings.Networks.%s }`, CiliumDockerNetwork)
result := map[string]string{
Name: name,
}
data, err := res.FindResults(filter)
if err != nil {
return nil, err
}
for _, val := range data {
iface := val.Interface()
for k, v := range iface.(map[string]interface{}) {
if key, ok := properties[k]; ok {
result[key] = fmt.Sprintf("%s", v)
}
}
}
return result, nil
}
// NetworkCreate creates a Docker network of the provided name with the
// specified subnet. It is a wrapper around `docker network create`.
func (s *SSHMeta) NetworkCreate(name string, subnet string) *CmdRes {
if subnet == "" {
subnet = "::1/112"
}
cmd := fmt.Sprintf(
"docker network create --ipv6 --subnet %s --driver cilium --ipam-driver cilium %s",
subnet, name)
res := s.ExecWithSudo(cmd)
if !res.WasSuccessful() {
s.logger.Warningf("Unable to create docker network %s: %s", name, res.CombineOutput().String())
}
return res
}
// NetworkDelete deletes the Docker network of the provided name. It is a wrapper
// around `docker network rm`.
func (s *SSHMeta) NetworkDelete(name string) *CmdRes {
return s.ExecWithSudo(fmt.Sprintf("docker network rm %s", name))
}
// NetworkGet returns all of the Docker network configuration for the provided
// network. It is a wrapper around `docker network inspect`.
func (s *SSHMeta) NetworkGet(name string) *CmdRes {
return s.ExecWithSudo(fmt.Sprintf("docker network inspect %s", name))
}
func (s *SSHMeta) execCmd(cmd string) *CmdRes {
return s.ExecWithSudo(cmd)
}
// SampleContainersActions creates or deletes various containers used for
// testing Cilium and adds said containers to the provided Docker network.
func (s *SSHMeta) SampleContainersActions(mode string, networkName string, createOptions ...string) {
createOptionsString := ""
for _, opt := range createOptions {
createOptionsString = fmt.Sprintf("%s %s", createOptionsString, opt)
}
images := map[string]string{
Httpd1: constants.HttpdImage,
Httpd2: constants.HttpdImage,
Httpd3: constants.HttpdImage,
App1: constants.NetperfImage,
App2: constants.NetperfImage,
App3: constants.NetperfImage,
}
switch mode {
case Create:
for k, v := range images {
s.ContainerCreate(k, v, networkName, fmt.Sprintf("-l id.%s %s", k, createOptionsString))
}
case Delete:
for k := range images {
s.ContainerRm(k)
}
}
}
// GatherDockerLogs dumps docker containers logs output to the directory
// testResultsPath
func (s *SSHMeta) GatherDockerLogs() {
res := s.Exec("docker ps -a --format {{.Names}}")
if !res.WasSuccessful() {
log.WithField("error", res.CombineOutput()).Errorf("cannot get docker logs")
return
}
commands := map[string]string{}
for _, k := range res.ByLines() {
if k != "" {
key := fmt.Sprintf("docker logs %s", k)
commands[key] = fmt.Sprintf("container_%s.log", k)
}
}
testPath, err := CreateReportDirectory()
if err != nil {
s.logger.WithError(err).Errorf(
"cannot create test results path '%s'", testPath)
return
}
reportMap(testPath, commands, s)
}