-
Notifications
You must be signed in to change notification settings - Fork 169
/
podman.go
300 lines (258 loc) · 8.84 KB
/
podman.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
// Copyright 2018 Red Hat, Inc.
//
// 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 podman
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/coreos/coreos-assembler/mantle/kola"
"github.com/coreos/coreos-assembler/mantle/kola/cluster"
"github.com/coreos/coreos-assembler/mantle/kola/register"
tutil "github.com/coreos/coreos-assembler/mantle/kola/tests/util"
"github.com/coreos/coreos-assembler/mantle/platform"
"github.com/coreos/coreos-assembler/mantle/util"
)
// init runs when the package is imported and takes care of registering tests
func init() {
register.RegisterTest(®ister.Test{
Run: podmanBaseTest,
ClusterSize: 1,
Name: `podman.base`,
Description: "Verify podman info and running with various options work.",
})
// These remaining tests use networking, and hence don't work reliably on RHCOS
// right now due to due to https://bugzilla.redhat.com/show_bug.cgi?id=1757572
register.RegisterTest(®ister.Test{
Run: podmanWorkflow,
ClusterSize: 1,
Name: `podman.workflow`,
Description: "Verify container can run with volume mount and port forwarding.",
Tags: []string{kola.NeedsInternetTag}, // For pulling nginx
Distros: []string{"fcos"},
FailFast: true,
})
register.RegisterTest(®ister.Test{
Run: podmanNetworksReliably,
ClusterSize: 1,
Name: `podman.network-single`,
Description: "Verify basic container network connectivity.",
// Not really but podman blows up if there's no /etc/resolv.conf
Tags: []string{kola.NeedsInternetTag},
Distros: []string{"fcos"},
Timeout: 20 * time.Minute,
})
// https://github.com/coreos/mantle/pull/1080
// register.RegisterTest(®ister.Test{
// Run: podmanNetworkTest,
// ClusterSize: 2,
// Name: `podman.network-multi`,
// Distros: []string{"fcos"},
// })
}
// simplifiedContainerPsInfo represents a container entry in podman ps -a
type simplifiedContainerPsInfo struct {
ID string `json:"id"`
Image string `json:"image"`
Status string `json:"status"`
}
// simplifiedPsInfo represents the results of podman ps -a
type simplifiedPsInfo struct {
containers []simplifiedContainerPsInfo
}
// simplifiedPodmanInfo represents the results of podman info
type simplifiedPodmanInfo struct {
Store struct {
GraphDriverName string `json:"GraphDriverName"`
GraphRoot string `json:"GraphRoot"`
}
}
func getSimplifiedPsInfo(c cluster.TestCluster, m platform.Machine) (simplifiedPsInfo, error) {
target := simplifiedPsInfo{}
psJSON, err := c.SSH(m, `sudo podman ps -a --format json`)
if err != nil {
return target, fmt.Errorf("could not get info: %v", err)
}
err = json.Unmarshal(psJSON, &target.containers)
if err != nil {
return target, fmt.Errorf("could not unmarshal info %q into known json: %v", string(psJSON), err)
}
return target, nil
}
// Returns the result of podman info as a simplifiedPodmanInfo
func getPodmanInfo(c cluster.TestCluster, m platform.Machine) (simplifiedPodmanInfo, error) {
target := simplifiedPodmanInfo{}
pInfoJSON, err := c.SSH(m, `sudo podman info --format json`)
if err != nil {
return target, fmt.Errorf("Could not get info: %v", err)
}
err = json.Unmarshal(pInfoJSON, &target)
if err != nil {
return target, fmt.Errorf("Could not unmarshal info %q into known JSON: %v", string(pInfoJSON), err)
}
return target, nil
}
func podmanBaseTest(c cluster.TestCluster) {
c.Run("info", podmanInfo)
c.Run("resources", podmanResources)
}
// Test: Run basic podman commands
func podmanWorkflow(c cluster.TestCluster) {
m := c.Machines()[0]
// Test: Verify container can run with volume mount and port forwarding
image := "quay.io/fedora/fedora"
wwwRoot := "/var/html"
var id string
c.Run("run", func(c cluster.TestCluster) {
dir := c.MustSSH(m, `mktemp -d`)
cmd := fmt.Sprintf("echo TEST PAGE > %s/index.html", string(dir))
c.RunCmdSync(m, cmd)
webServerCmd := fmt.Sprintf("sh -c 'dnf install -y python3 && python3 -m http.server -d %s 80'", wwwRoot)
cmd = fmt.Sprintf("sudo podman run -d -p 80:80 -v %s/index.html:%s/index.html:z %s %s", string(dir), wwwRoot, image, webServerCmd)
out := c.MustSSH(m, cmd)
id = string(out)[0:64]
podIsRunning := func() error {
b, err := c.SSH(m, `curl -f http://localhost 2>/dev/null`)
if err != nil {
return err
}
if !bytes.Contains(b, []byte("TEST PAGE")) {
return fmt.Errorf("Fedora container is not running %s", b)
}
return nil
}
if err := util.RetryUntilTimeout(15*time.Minute, 5*time.Minute, podIsRunning); err != nil {
c.Fatal("Pod is not running")
}
})
// Test: Execute command in container
c.Run("exec", func(c cluster.TestCluster) {
cmd := fmt.Sprintf("sudo podman exec %s echo hello", id)
c.AssertCmdOutputContains(m, cmd, "hello")
})
// Test: Stop container
c.Run("stop", func(c cluster.TestCluster) {
cmd := fmt.Sprintf("sudo podman stop %s", id)
c.RunCmdSync(m, cmd)
psInfo, err := getSimplifiedPsInfo(c, m)
if err != nil {
c.Fatal(err)
}
found := false
for _, container := range psInfo.containers {
// Sometime between podman 1.x and 2.x podman started putting
// full 64 character IDs into the json output. Dynamically detect
// the length of the ID and compare that number of characters.
if container.ID == id[0:len(container.ID)] {
found = true
if !strings.Contains(strings.ToLower(container.Status), "exited") {
c.Fatalf("Container %s was not stopped. Current status: %s", id, container.Status)
}
}
}
if !found {
c.Fatalf("Unable to find container %s in podman ps -a output", id)
}
})
// Test: Remove container
c.Run("remove", func(c cluster.TestCluster) {
cmd := fmt.Sprintf("sudo podman rm %s", id)
c.RunCmdSync(m, cmd)
psInfo, err := getSimplifiedPsInfo(c, m)
if err != nil {
c.Fatal(err)
}
found := false
for _, container := range psInfo.containers {
if container.ID == id {
found = true
}
}
if found {
c.Fatalf("Container %s should be removed. %v", id, psInfo.containers)
}
})
// Test: Delete container
c.Run("delete", func(c cluster.TestCluster) {
cmd := fmt.Sprintf("sudo podman rmi %s", image)
out := c.MustSSH(m, cmd)
imageID := string(out)
cmd = fmt.Sprintf("sudo podman images | grep %s", imageID)
out, err := c.SSH(m, cmd)
if err == nil {
c.Fatalf("Image should be deleted but found %s", string(out))
}
})
}
// Test: Verify basic podman info information
func podmanInfo(c cluster.TestCluster) {
m := c.Machines()[0]
info, err := getPodmanInfo(c, m)
if err != nil {
c.Fatal(err)
}
// test for known settings
expectedGraphDriver := "overlay"
if info.Store.GraphDriverName != expectedGraphDriver {
c.Errorf("Unexpected driver: %v != %v", expectedGraphDriver, info.Store.GraphDriverName)
}
expectedGraphRoot := "/var/lib/containers/storage"
if info.Store.GraphRoot != expectedGraphRoot {
c.Errorf("Unexected graph root: %v != %v", expectedGraphRoot, info.Store.GraphRoot)
}
}
// Test: Run podman with various options
func podmanResources(c cluster.TestCluster) {
m := c.Machines()[0]
tutil.GenPodmanScratchContainer(c, m, "echo", []string{"echo"})
podmanFmt := "sudo podman run --net=none --rm %s echo echo 1"
pCmd := func(arg string) string {
return fmt.Sprintf(podmanFmt, arg)
}
for _, podmanCmd := range []string{
// must set memory when setting memory-swap
// See https://github.com/opencontainers/runc/issues/1980 for
// why we use 128m for memory
pCmd("--memory=128m --memory-swap=128m"),
pCmd("--memory-reservation=10m"),
pCmd("--cpu-shares=100"),
pCmd("--cpu-period=1000"),
pCmd("--cpuset-cpus=0"),
pCmd("--cpuset-mems=0"),
pCmd("--cpu-quota=1000"),
pCmd("--blkio-weight=10"),
pCmd("--memory=128m"),
pCmd("--shm-size=1m"),
} {
cmd := podmanCmd
output, err := c.SSH(m, cmd)
if err != nil {
c.Fatalf("Failed to run %q: output: %q status: %q", cmd, output, err)
}
}
}
// Test: Verify basic container network connectivity
func podmanNetworksReliably(c cluster.TestCluster) {
m := c.Machines()[0]
tutil.GenPodmanScratchContainer(c, m, "ping", []string{"sh", "ping"})
output := c.MustSSH(m, `for i in $(seq 1 100); do
echo -n "$i: "
sudo podman run --rm ping sh -c 'ping -i 0.2 10.88.0.1 -w 1 >/dev/null && echo PASS || echo FAIL'
done`)
numPass := strings.Count(string(output), "PASS")
if numPass <= 98 {
c.Fatalf("Expected more than or equal to 98/100 passes, but output was: %s", output)
}
}