Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
Removed godotenv, made it use the podman version in path instead of hard-coded path, and disabled a test when not on linux
  • Loading branch information
jaredoconnell committed Nov 17, 2023
1 parent 8e70ddf commit cad84b3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
50 changes: 34 additions & 16 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"os"
"os/exec"
"runtime"
"strings"
"sync"
"testing"
Expand All @@ -29,13 +30,17 @@ func getConnector(t *testing.T, configJSON string) (deployer.Connector, *Config)
assert.NoError(t, err)
connector, err := factory.Create(unserializedConfig, log.NewTestLogger(t))
assert.NoError(t, err)
unserializedConfig.Podman.Path, err = binaryCheck(unserializedConfig.Podman.Path)
if err != nil {
t.Fatalf("Error checking Podman path (%s)", err)
}
return connector, unserializedConfig
}

var inOutConfig = `
{
"podman":{
"path":"/usr/bin/podman"
"path":"podman"
}
}
`
Expand Down Expand Up @@ -74,15 +79,14 @@ var envConfig = `
{
"deployment":{
"container":{
"NetworkDisabled":true,
"Env":[
"DEPLOYER_PODMAN_TEST_1=TEST1",
"DEPLOYER_PODMAN_TEST_2=TEST2"
]
}
},
"podman":{
"path":"/usr/bin/podman"
"path":"podman"
}
}
`
Expand Down Expand Up @@ -114,7 +118,7 @@ var volumeConfig = `
}
},
"podman":{
"path":"/usr/bin/podman"
"path":"podman"
}
}
`
Expand All @@ -131,7 +135,7 @@ func TestSimpleVolume(t *testing.T) {
cmd := exec.Command("chcon", "-Rt", "svirt_sandbox_file_t", fmt.Sprintf("%s/tests/volume", cwd)) //nolint:gosec
err = cmd.Run()
if err != nil {
logger.Warningf("failed to set SELinux permissions on folder, chcon error: %s, this may cause test failure, let's see...", err.Error())
logger.Warningf("failed to set SELinux permissions on folder, chcon error: %s, this may cause test failure if SELinux is enabled.", err.Error())
}

container, err := connector.Deploy(
Expand All @@ -142,9 +146,8 @@ func TestSimpleVolume(t *testing.T) {
var containerInput = []byte("volume\n")
_, err = container.Write(containerInput)
assert.NoError(t, err)

readBuffer := readOutputUntil(t, container, string(fileContent))
assert.Equals(t, len(readBuffer) > 0, true)
assert.GreaterThan(t, len(readBuffer), 0)

t.Cleanup(func() {
assert.NoError(t, container.Close())
Expand All @@ -154,7 +157,7 @@ func TestSimpleVolume(t *testing.T) {
var nameTemplate = `
{
"podman":{
"path":"/usr/bin/podman",
"path":"podman",
"containerNamePrefix":"%s"
}
}
Expand Down Expand Up @@ -207,9 +210,13 @@ func TestContainerName(t *testing.T) {
var cgroupTemplate = `
{
"podman":{
"path":"/usr/bin/podman",
"containerNamePrefix":"%s",
"cgroupNs":"%s"
"path":"podman",
"containerNamePrefix":"%s"
},
"deployment":{
"host":{
"CgroupnsMode":"%s"
}
}
}
`
Expand Down Expand Up @@ -271,7 +278,8 @@ func TestPrivateCgroupNs(t *testing.T) {
logger := log.NewTestLogger(t)

var wg sync.WaitGroup
userCgroupNs := tests.GetCommmandCgroupNs(logger, "/usr/bin/sleep", []string{"3"})
// Assume sleep is in the path. Because it's not in the same location for every user.
userCgroupNs := tests.GetCommmandCgroupNs(logger, "sleep", []string{"3"})
assert.NotNil(t, userCgroupNs)
logger.Debugf("Detected cgroup namespace for user: %s", userCgroupNs)

Expand Down Expand Up @@ -304,10 +312,15 @@ func TestPrivateCgroupNs(t *testing.T) {
}

func TestHostCgroupNs(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skipf("Not running on Linux. Skipping cgroup test.")
return
}
logger := log.NewTestLogger(t)
var wg sync.WaitGroup

userCgroupNs := tests.GetCommmandCgroupNs(logger, "/usr/bin/sleep", []string{"3"})
// Assume sleep is in the path. Because it's not in the same location for every user.
userCgroupNs := tests.GetCommmandCgroupNs(logger, "sleep", []string{"3"})
assert.NotNil(t, userCgroupNs)

logger.Debugf("Detected cgroup namespace for user: %s", userCgroupNs)
Expand All @@ -334,7 +347,7 @@ func TestHostCgroupNs(t *testing.T) {
assert.NotNil(t, podmanCgroupNs)
wg.Wait()

assert.Equals(t, userCgroupNs == podmanCgroupNs, true)
assert.Equals(t, userCgroupNs, podmanCgroupNs)

t.Cleanup(func() {
assert.NoError(t, container.Close())
Expand Down Expand Up @@ -400,8 +413,12 @@ var networkTemplate = `
{
"podman":{
"containerNamePrefix":"%s",
"path":"/usr/bin/podman",
"networkMode":"%s"
"path":"podman"
},
"deployment":{
"host":{
"NetworkMode":"%s"
}
}
}
`
Expand Down Expand Up @@ -502,6 +519,7 @@ func readOutputUntil(t *testing.T, plugin io.Reader, lookForOutput string) []byt
if err != io.EOF {
t.Fatalf("error while reading stdout: %s", err.Error())
} else {
t.Errorf("Hit EOF.")
return readBuffer[:n]
}
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.18
require (
github.com/docker/docker v24.0.7+incompatible
github.com/docker/go-connections v0.4.0
github.com/joho/godotenv v1.5.1
go.arcalot.io/assert v1.6.0
go.arcalot.io/lang v1.0.0
go.flow.arcalot.io/deployer v0.4.0
Expand Down
8 changes: 2 additions & 6 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ var Schema = schema.NewTypedScopeSchema[*Config](
nil,
),
"NetworkMode": schema.NewPropertySchema(
schema.NewStringSchema(nil, nil, regexp.MustCompile("^(none|bridge|host|container:[a-zA-Z0-9][a-zA-Z0-9_.-]+|[a-zA-Z0-9][a-zA-Z0-9_.-]+)$")),
schema.NewStringSchema(nil, nil, regexp.MustCompile("^(none|private|bridge(:.+)?|host|container:[a-zA-Z0-9][a-zA-Z0-9_.-]+|[a-zA-Z0-9][a-zA-Z0-9_.-]+|pasta:.+|slirp4netns:.+|ns:.+)$")),
schema.NewDisplayValue(schema.PointerTo("Network mode"), schema.PointerTo("Specifies either the network mode, the container network to attach to, or a name of a Docker network to use."), nil),
false,
nil,
Expand Down Expand Up @@ -271,11 +271,7 @@ var Schema = schema.NewTypedScopeSchema[*Config](
nil,
).Disable(notImplemented),
"CgroupnsMode": schema.NewPropertySchema(
schema.NewStringEnumSchema(map[string]*schema.DisplayValue{
"private": {NameValue: schema.PointerTo("Private")},
"host": {NameValue: schema.PointerTo("Host")},
"": {NameValue: schema.PointerTo("Empty")},
}),
schema.NewStringSchema(nil, nil, regexp.MustCompile("host|private|ns:.+|container:.+")),
schema.NewDisplayValue(schema.PointerTo("CGroup namespace mode"), schema.PointerTo("CGroup namespace mode to use for the container."), nil),
false,
nil,
Expand Down
8 changes: 4 additions & 4 deletions tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/joho/godotenv"
log "go.arcalot.io/log/v2"
"os"
"os/exec"
Expand All @@ -27,10 +26,11 @@ type BasicInspection struct {
}

func GetPodmanPath() string {
if err := godotenv.Load("../../tests/env/test.env"); err != nil {
panic(err)
envPath := os.Getenv("PODMAN_PATH")
if len(envPath) > 0 {
return envPath
}
return os.Getenv("PODMAN_PATH")
return "podman"
}

func RemoveImage(logger log.Logger, image string) {
Expand Down
1 change: 0 additions & 1 deletion tests/env/test.env

This file was deleted.

0 comments on commit cad84b3

Please sign in to comment.