192 lines
4.5 KiB
Go
192 lines
4.5 KiB
Go
package driver
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const TEST_IMAGE_NAME = "busybox:stable"
|
|
|
|
type PodmanMountDetails struct {
|
|
Name string `json:"Name"`
|
|
Source string `json:"Source"`
|
|
ReadWrite bool `json:"RW"`
|
|
Destination string `json:"Destination"`
|
|
}
|
|
|
|
type PodmanInspect struct {
|
|
Name string `json:"Name"`
|
|
ImageName string `json:"ImageName"`
|
|
Mounts []PodmanMountDetails `json:"Mounts"`
|
|
}
|
|
|
|
func MustPodman(t *testing.T, testCase func()) {
|
|
cmd := exec.Command("podman", "--version")
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
t.Error("Podman is required for Podman driver tests.")
|
|
}
|
|
|
|
if os.Getenv("PODMAN_TESTS") != "1" {
|
|
t.Log("Skipping because PODMAN_TESTS is not 1.")
|
|
return
|
|
}
|
|
|
|
testCase()
|
|
}
|
|
|
|
func CleanupContainer(name string) {
|
|
exec.Command("podman", "rm", "-f", "-t", "1", name).Run()
|
|
}
|
|
|
|
func CleanupVolume(name string) {
|
|
exec.Command("podman", "volume", "rm", "-f", name).Run()
|
|
}
|
|
|
|
func CleanAll(name string) {
|
|
CleanupContainer(name)
|
|
CleanupVolume(fmt.Sprintf("%s-workspace", name))
|
|
}
|
|
|
|
func ContainerExists(name string) bool {
|
|
exists := exec.Command("podman", "container", "exists", name)
|
|
|
|
exists.Run()
|
|
|
|
exitCode := exists.ProcessState.ExitCode()
|
|
|
|
return exitCode == 0
|
|
}
|
|
|
|
func VolumeExists(name string) bool {
|
|
exists := exec.Command("podman", "volume", "exists", name)
|
|
|
|
exists.Run()
|
|
|
|
exitCode := exists.ProcessState.ExitCode()
|
|
|
|
return exitCode == 0
|
|
}
|
|
|
|
func InspectContainer(name string) PodmanInspect {
|
|
inspect := exec.Command("podman", "inspect", name)
|
|
|
|
out, _ := inspect.Output()
|
|
|
|
var inspectOut []PodmanInspect
|
|
|
|
json.Unmarshal(out, &inspectOut)
|
|
|
|
return inspectOut[0]
|
|
}
|
|
|
|
func TestStartCreatesAContainerWithTheDesiredUriAndName(t *testing.T) {
|
|
MustPodman(t, func() {
|
|
driver := PodmanDriver{}
|
|
containerName := "test-container"
|
|
|
|
defer CleanAll(containerName)
|
|
|
|
driver.Start(TEST_IMAGE_NAME, containerName, t.TempDir())
|
|
|
|
inspectOut := InspectContainer(containerName)
|
|
|
|
if inspectOut.Name != containerName {
|
|
t.Errorf("Expected container name to be %s, got %s instead.", containerName, inspectOut.Name)
|
|
}
|
|
|
|
if !strings.HasSuffix(inspectOut.ImageName, TEST_IMAGE_NAME) {
|
|
t.Errorf("Expected image name to be %s, got %s instead.", TEST_IMAGE_NAME, inspectOut.ImageName)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestStartCreatesAContainerWithAnAttachedVolume(t *testing.T) {
|
|
MustPodman(t, func() {
|
|
driver := PodmanDriver{}
|
|
containerName := "test-container"
|
|
|
|
defer CleanAll(containerName)
|
|
|
|
driver.Start(TEST_IMAGE_NAME, containerName, t.TempDir())
|
|
|
|
inspectOut := InspectContainer(containerName)
|
|
|
|
if len(inspectOut.Mounts) == 0 {
|
|
t.Error("Expected a mount.")
|
|
}
|
|
|
|
expectedVolumeName := fmt.Sprintf("%s-workspace", containerName)
|
|
if !VolumeExists(expectedVolumeName) {
|
|
t.Error("Expected volume.")
|
|
}
|
|
|
|
if inspectOut.Mounts[0].Name != expectedVolumeName {
|
|
t.Errorf("Expected volume name to be %s, got %s instead.", expectedVolumeName, inspectOut.Mounts[0].Name)
|
|
}
|
|
|
|
if inspectOut.Mounts[0].Destination != "/workspace" {
|
|
t.Errorf("Expected volume mountpoint to be /workspace, got %s instead.", inspectOut.Mounts[0].Destination)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestStartCreatesAContainerWithAReadonlyCacheVolume(t *testing.T) {
|
|
MustPodman(t, func() {
|
|
driver := PodmanDriver{}
|
|
containerName := "test-container"
|
|
|
|
defer CleanAll(containerName)
|
|
cacheRoot := t.TempDir()
|
|
driver.Start(TEST_IMAGE_NAME, containerName, cacheRoot)
|
|
|
|
inspectOut := InspectContainer(containerName)
|
|
|
|
cacheVolume := inspectOut.Mounts[1]
|
|
|
|
if cacheVolume.ReadWrite {
|
|
t.Error("Expected cache volume to be read-only.")
|
|
}
|
|
|
|
if cacheVolume.Destination != "/cache" || cacheVolume.Source != cacheRoot {
|
|
t.Error("Expected cache volume to map from provided cache root to /cache")
|
|
}
|
|
})
|
|
}
|
|
func TestStopStopsAContainerByName(t *testing.T) {
|
|
MustPodman(t, func() {
|
|
driver := PodmanDriver{}
|
|
containerName := "test-container"
|
|
|
|
defer CleanAll(containerName)
|
|
|
|
driver.Start(TEST_IMAGE_NAME, containerName, t.TempDir())
|
|
driver.Stop(containerName)
|
|
|
|
if ContainerExists(containerName) {
|
|
t.Error("Expected container to be cleaned up, still exists.")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestStopCleansUpWorkspaceVolume(t *testing.T) {
|
|
MustPodman(t, func() {
|
|
driver := PodmanDriver{}
|
|
containerName := "test-container"
|
|
|
|
defer CleanAll(containerName)
|
|
|
|
driver.Start(TEST_IMAGE_NAME, containerName, t.TempDir())
|
|
driver.Stop(containerName)
|
|
|
|
if VolumeExists(fmt.Sprintf("%s-workspace", containerName)) {
|
|
t.Error("Expected volume to be cleaned up, still exists.")
|
|
}
|
|
})
|
|
}
|