summaryrefslogtreecommitdiff
path: root/test/endpoint/setup.go
blob: c05cc0cbb4cfe6e49768ddc6c689d6315e9619da (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
218
219
220
221
// +build varlink

package endpoint

import (
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"strings"

	"github.com/containers/libpod/pkg/rootless"
	iopodman "github.com/containers/libpod/pkg/varlink"
	"github.com/containers/storage/pkg/stringid"
	"github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/sirupsen/logrus"
)

func Setup(tempDir string) *EndpointTestIntegration {
	var (
		endpoint string
	)
	cwd, _ := os.Getwd()
	INTEGRATION_ROOT = filepath.Join(cwd, "../../")

	podmanBinary := filepath.Join(cwd, "../../bin/podman")
	if os.Getenv("PODMAN_BINARY") != "" {
		podmanBinary = os.Getenv("PODMAN_BINARY")
	}
	conmonBinary := filepath.Join("/usr/libexec/podman/conmon")
	altConmonBinary := "/usr/bin/conmon"
	if _, err := os.Stat(conmonBinary); os.IsNotExist(err) {
		conmonBinary = altConmonBinary
	}
	if os.Getenv("CONMON_BINARY") != "" {
		conmonBinary = os.Getenv("CONMON_BINARY")
	}
	storageOptions := STORAGE_OPTIONS
	if os.Getenv("STORAGE_OPTIONS") != "" {
		storageOptions = os.Getenv("STORAGE_OPTIONS")
	}
	cgroupManager := CGROUP_MANAGER
	if rootless.IsRootless() {
		cgroupManager = "cgroupfs"
	}
	if os.Getenv("CGROUP_MANAGER") != "" {
		cgroupManager = os.Getenv("CGROUP_MANAGER")
	}

	ociRuntime := os.Getenv("OCI_RUNTIME")
	if ociRuntime == "" {
		var err error
		ociRuntime, err = exec.LookPath("runc")
		// If we cannot find the runc binary, setting to something static as we have no way
		// to return an error.  The tests will fail and point out that the runc binary could
		// not be found nicely.
		if err != nil {
			ociRuntime = "/usr/bin/runc"
		}
	}
	os.Setenv("DISABLE_HC_SYSTEMD", "true")
	CNIConfigDir := "/etc/cni/net.d"

	storageFs := STORAGE_FS
	if rootless.IsRootless() {
		storageFs = ROOTLESS_STORAGE_FS
	}

	uuid := stringid.GenerateNonCryptoID()
	if !rootless.IsRootless() {
		endpoint = fmt.Sprintf("unix:/run/podman/io.podman-%s", uuid)
	} else {
		runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
		socket := fmt.Sprintf("io.podman-%s", uuid)
		fqpath := filepath.Join(runtimeDir, socket)
		endpoint = fmt.Sprintf("unix:%s", fqpath)
	}

	eti := EndpointTestIntegration{
		ArtifactPath:        ARTIFACT_DIR,
		CNIConfigDir:        CNIConfigDir,
		CgroupManager:       cgroupManager,
		ConmonBinary:        conmonBinary,
		CrioRoot:            filepath.Join(tempDir, "crio"),
		ImageCacheDir:       ImageCacheDir,
		ImageCacheFS:        storageFs,
		OCIRuntime:          ociRuntime,
		PodmanBinary:        podmanBinary,
		RunRoot:             filepath.Join(tempDir, "crio-run"),
		SignaturePolicyPath: filepath.Join(INTEGRATION_ROOT, "test/policy.json"),
		StorageOptions:      storageOptions,
		TmpDir:              tempDir,
		// Timings:             nil,
		VarlinkBinary:   VarlinkBinary,
		VarlinkCommand:  nil,
		VarlinkEndpoint: endpoint,
		VarlinkSession:  nil,
	}
	return &eti
}

func (p *EndpointTestIntegration) Cleanup() {
	// Remove all containers
	// TODO Make methods to do all this?

	p.stopAllContainers()

	// TODO need to make stop all pods

	p.StopVarlink()
	// Nuke tempdir
	if err := os.RemoveAll(p.TmpDir); err != nil {
		fmt.Printf("%q\n", err)
	}

	// Clean up the registries configuration file ENV variable set in Create
	resetRegistriesConfigEnv()
}

func (p *EndpointTestIntegration) listContainers() []iopodman.Container {
	containers := p.Varlink("ListContainers", "", false)
	var varlinkContainers map[string][]iopodman.Container
	if err := json.Unmarshal(containers.OutputToBytes(), &varlinkContainers); err != nil {
		logrus.Error("failed to unmarshal containers")
	}
	return varlinkContainers["containers"]
}

func (p *EndpointTestIntegration) stopAllContainers() {
	containers := p.listContainers()
	for _, container := range containers {
		p.stopContainer(container.Id)
	}
}

func (p *EndpointTestIntegration) stopContainer(cid string) {
	p.Varlink("StopContainer", fmt.Sprintf("{\"name\":\"%s\", \"timeout\":0}", cid), false)
}

func resetRegistriesConfigEnv() {
	os.Setenv("REGISTRIES_CONFIG_PATH", "")
}

func (p *EndpointTestIntegration) createArtifact(image string) {
	if os.Getenv("NO_TEST_CACHE") != "" {
		return
	}
	dest := strings.Split(image, "/")
	destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1))
	fmt.Printf("Caching %s at %s...", image, destName)
	if _, err := os.Stat(destName); os.IsNotExist(err) {
		pull := p.Varlink("PullImage", fmt.Sprintf("{\"name\":\"%s\"}", image), false)
		Expect(pull.ExitCode()).To(Equal(0))

		imageSave := iopodman.ImageSaveOptions{
			// Name:image,
			// Output: destName,
			// Format: "oci-archive",
		}
		imageSave.Name = image
		imageSave.Output = destName
		imageSave.Format = "oci-archive"
		foo := make(map[string]iopodman.ImageSaveOptions)
		foo["options"] = imageSave
		f, _ := json.Marshal(foo)
		save := p.Varlink("ImageSave", string(f), false)
		result := save.OutputToMoreResponse()
		Expect(save.ExitCode()).To(Equal(0))
		Expect(os.Rename(result.Id, destName)).To(BeNil())
		fmt.Printf("\n")
	} else {
		fmt.Printf(" already exists.\n")
	}
}

func populateCache(p *EndpointTestIntegration) {
	p.CrioRoot = p.ImageCacheDir
	p.StartVarlink()
	for _, image := range CACHE_IMAGES {
		p.RestoreArtifactToCache(image)
	}
	p.StopVarlink()
}

func (p *EndpointTestIntegration) RestoreArtifactToCache(image string) error {
	fmt.Printf("Restoring %s...\n", image)
	dest := strings.Split(image, "/")
	destName := fmt.Sprintf("/tmp/%s.tar", strings.Replace(strings.Join(strings.Split(dest[len(dest)-1], "/"), ""), ":", "-", -1))
	// fmt.Println(destName, p.ImageCacheDir)
	load := p.Varlink("LoadImage", fmt.Sprintf("{\"name\": \"%s\", \"inputFile\": \"%s\"}", image, destName), false)
	Expect(load.ExitCode()).To(BeZero())
	return nil
}

func (p *EndpointTestIntegration) startTopContainer(name string) string {
	t := true
	args := iopodman.Create{
		Args:   []string{"docker.io/library/alpine:latest", "top"},
		Tty:    &t,
		Detach: &t,
	}
	if len(name) > 0 {
		args.Name = &name
	}
	b, err := json.Marshal(args)
	if err != nil {
		ginkgo.Fail("failed to marshal data for top container")
	}
	input := fmt.Sprintf("{\"create\":%s}", string(b))
	top := p.Varlink("CreateContainer", input, false)
	if top.ExitCode() != 0 {
		ginkgo.Fail("failed to start top container")
	}
	start := p.Varlink("StartContainer", fmt.Sprintf("{\"name\":\"%s\"}", name), false)
	if start.ExitCode() != 0 {
		ginkgo.Fail("failed to start top container")
	}
	return start.OutputToString()
}