aboutsummaryrefslogtreecommitdiff
path: root/test/system/libpod_suite_test.go
blob: 5de50e4e74c84de6ae8cb92653513dc1c1be7deb (plain)
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
package system

import (
	"fmt"
	"os"
	"strings"
	"testing"

	. "github.com/containers/libpod/test/utils"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var (
	PODMAN_BINARY string
	GLOBALOPTIONS = []string{"--cgroup-manager",
		"--cni-config-dir",
		"--config", "-c",
		"--conmon",
		"--cpu-profile",
		"--log-level",
		"--root",
		"--tmpdir",
		"--runroot",
		"--runtime",
		"--storage-driver",
		"--storage-opt",
		"--syslog",
	}
	PODMAN_SUBCMD = []string{"attach",
		"commit",
		"container",
		"build",
		"create",
		"diff",
		"exec",
		"export",
		"history",
		"image",
		"images",
		"import",
		"info",
		"inspect",
		"kill",
		"load",
		"login",
		"logout",
		"logs",
		"mount",
		"pause",
		"ps",
		"pod",
		"port",
		"pull",
		"push",
		"restart",
		"rm",
		"rmi",
		"run",
		"save",
		"search",
		"start",
		"stats",
		"stop",
		"tag",
		"top",
		"umount",
		"unpause",
		"version",
		"wait",
		"h",
	}
	INTEGRATION_ROOT   string
	ARTIFACT_DIR       = "/tmp/.artifacts"
	ALPINE             = "docker.io/library/alpine:latest"
	BB                 = "docker.io/library/busybox:latest"
	BB_GLIBC           = "docker.io/library/busybox:glibc"
	fedoraMinimal      = "registry.fedoraproject.org/fedora-minimal:latest"
	nginx              = "quay.io/baude/alpine_nginx:latest"
	redis              = "docker.io/library/redis:alpine"
	registry           = "docker.io/library/registry:2"
	infra              = "k8s.gcr.io/pause:3.1"
	defaultWaitTimeout = 90
)

// PodmanTestSystem struct for command line options
type PodmanTestSystem struct {
	PodmanTest
	GlobalOptions    map[string]string
	PodmanCmdOptions map[string][]string
}

// TestLibpod ginkgo master function
func TestLibpod(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Libpod Suite")
}

var _ = BeforeSuite(func() {
})

// PodmanTestCreate creates a PodmanTestSystem instance for the tests
func PodmanTestCreate(tempDir string) *PodmanTestSystem {
	var envKey string
	globalOptions := make(map[string]string)
	podmanCmdOptions := make(map[string][]string)

	for _, n := range GLOBALOPTIONS {
		envKey = strings.Replace(strings.ToUpper(strings.Trim(n, "-")), "-", "_", -1)
		if isEnvSet(envKey) {
			globalOptions[n] = os.Getenv(envKey)
		}
	}

	for _, n := range PODMAN_SUBCMD {
		envKey = strings.Replace("PODMAN_SUBCMD_OPTIONS", "SUBCMD", strings.ToUpper(n), -1)
		if isEnvSet(envKey) {
			podmanCmdOptions[n] = strings.Split(os.Getenv(envKey), " ")
		}
	}

	podmanBinary := "podman"
	if os.Getenv("PODMAN_BINARY") != "" {
		podmanBinary = os.Getenv("PODMAN_BINARY")
	}

	p := &PodmanTestSystem{
		PodmanTest: PodmanTest{
			PodmanBinary: podmanBinary,
			ArtifactPath: ARTIFACT_DIR,
			TempDir:      tempDir,
		},
		GlobalOptions:    globalOptions,
		PodmanCmdOptions: podmanCmdOptions,
	}

	p.PodmanMakeOptions = p.makeOptions

	return p
}

func (p *PodmanTestSystem) Podman(args []string) *PodmanSession {
	return p.PodmanBase(args)
}

//MakeOptions assembles all the podman options
func (p *PodmanTestSystem) makeOptions(args []string) []string {
	var addOptions, subArgs []string
	for _, n := range GLOBALOPTIONS {
		if p.GlobalOptions[n] != "" {
			addOptions = append(addOptions, n, p.GlobalOptions[n])
		}
	}

	if len(args) == 0 {
		return addOptions
	}

	subCmd := args[0]
	addOptions = append(addOptions, subCmd)
	if subCmd == "unmount" {
		subCmd = "umount"
	}
	if subCmd == "help" {
		subCmd = "h"
	}

	if _, ok := p.PodmanCmdOptions[subCmd]; ok {
		m := make(map[string]bool)
		subArgs = p.PodmanCmdOptions[subCmd]
		for i := 0; i < len(subArgs); i++ {
			m[subArgs[i]] = true
		}
		for i := 1; i < len(args); i++ {
			if _, ok := m[args[i]]; !ok {
				subArgs = append(subArgs, args[i])
			}
		}
	} else {
		subArgs = args[1:]
	}

	addOptions = append(addOptions, subArgs...)

	return addOptions
}

// Cleanup cleans up the temporary store
func (p *PodmanTestSystem) Cleanup() {
	// Remove all containers
	stopall := p.Podman([]string{"stop", "-a", "--timeout", "0"})
	stopall.WaitWithDefaultTimeout()

	session := p.Podman([]string{"rm", "-fa"})
	session.Wait(90)
	// Nuke tempdir
	if err := os.RemoveAll(p.TempDir); err != nil {
		fmt.Printf("%q\n", err)
	}
}

// CleanupPod cleans up the temporary store
func (p *PodmanTestSystem) CleanupPod() {
	// Remove all containers
	session := p.Podman([]string{"pod", "rm", "-fa"})
	session.Wait(90)
	// Nuke tempdir
	if err := os.RemoveAll(p.TempDir); err != nil {
		fmt.Printf("%q\n", err)
	}
}

// Check if the key is set in Env
func isEnvSet(key string) bool {
	_, set := os.LookupEnv(key)
	return set
}