Skip to content

Commit

Permalink
Review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
webbnh committed Mar 28, 2024
1 parent d6353ac commit 53cfa45
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
35 changes: 26 additions & 9 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"os"
"os/exec"
"runtime"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -118,12 +117,26 @@ var volumeConfig = `
`

func bindMountHelper(t *testing.T, options string) {
t.Helper()
fileContent, err := os.ReadFile("./tests/volume/test_file.txt")
assert.NoError(t, err)

connector, _ := getConnector(t, fmt.Sprintf(volumeConfig, options))

if tests.IsRunningOnLinux() && options == "" {
// On Linux, when SELinux is enabled, then bind mounts without
// relabeling options will fail. So, to test this case, disable
// SELinux on the test folder in order to make the file readable
// from within the container.
cmd := exec.Command("chcon", "-Rt", "svirt_sandbox_file_t", "./tests/volume")
var stderr bytes.Buffer
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
t.Logf("chcon error: %s: %s", err.Error(), stderr.String())
t.Fail()
}
}

container, err := connector.Deploy(
context.Background(),
"quay.io/arcalot/podman-deployer-test-helper:0.1.0")
Expand All @@ -136,16 +149,21 @@ func bindMountHelper(t *testing.T, options string) {
// Note: If it ends up with length zero buffer, restarting the VM may help:
// https://stackoverflow.com/questions/71977532/podman-mount-host-volume-return-error-statfs-no-such-file-or-directory-in-ma
readBuffer := readOutputUntil(t, container, string(fileContent))
assert.GreaterThan(t, len(readBuffer), 0)
assert.Contains(t, string(readBuffer), string(fileContent))
}

func TestBindMount(t *testing.T) {
scenarios := map[string]string{
"No options": "",
"Private": ":Z",
"Shared": ":z",
"ReadOnly": ":ro",
"Multiple": ":z,ro,noexec",
"Multiple": ":ro,noexec",
"No options": "",
}
//goland:noinspection GoBoolExpressions // The linter cannot tell that this expression is not constant.
if tests.IsRunningOnLinux() {
// The SELinux options seem to cause problems on Mac OS X, so only test
// them on Linux.
scenarios["Private"] = ":Z"
scenarios["Shared"] = ":z"
}
for name, s := range scenarios {
options := s
Expand Down Expand Up @@ -306,9 +324,8 @@ func TestPrivateCgroupNs(t *testing.T) {

func TestHostCgroupNs(t *testing.T) {
//goland:noinspection GoBoolExpressions // The linter cannot tell that this expression is not constant.
if runtime.GOOS != "linux" {
if !tests.IsRunningOnLinux() {
t.Skipf("Not running on Linux. Skipping cgroup test.")
return
}
logger := log.NewTestLogger(t)

Expand Down
35 changes: 17 additions & 18 deletions tests/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -84,7 +84,7 @@ func GetCommmandCgroupNs(logger log.Logger, command string, args []string) strin
}
// parse output from command
stdoutStr := stdout.String()
regex := regexp.MustCompile(`.*cgroup:\[(\d+)\]`)
regex := regexp.MustCompile(`.*cgroup:\[(\d+)]`)
userCgroupNs = regex.ReplaceAllString(stdoutStr, "$1")
userCgroupNs = strings.TrimSuffix(userCgroupNs, "\n")

Expand All @@ -97,22 +97,16 @@ func GetCommmandCgroupNs(logger log.Logger, command string, args []string) strin

// GetPodmanCgroupNs detects the running container cgroup namespace
func GetPodmanCgroupNs(logger log.Logger, podmanPath string, containerName string) string {
var wg sync.WaitGroup
wg.Add(1)
var podmanCgroupNs string
go func() {
defer wg.Done()
var stdout bytes.Buffer
cmd := exec.Command(podmanPath, "ps", "--ns", "--filter", fmt.Sprintf("name=%s", containerName), "--format", "{{.CGROUPNS}}") //nolint:gosec
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
logger.Errorf(err.Error())
}
podmanCgroupNs = stdout.String()
}()
wg.Wait()
podmanCgroupNs = strings.TrimSuffix(podmanCgroupNs, "\n")
return podmanCgroupNs
var stdout bytes.Buffer
cmd := exec.Command( //nolint:gosec
podmanPath, "ps", "--ns", "--filter",
fmt.Sprintf("name=%s", containerName),
"--format", "{{.CGROUPNS}}")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
logger.Errorf(err.Error())
}
return strings.TrimSuffix(stdout.String(), "\n")
}

func IsContainerRunning(logger log.Logger, podmanPath string, containerName string) bool {
Expand Down Expand Up @@ -140,3 +134,8 @@ func IsRunningOnGithub() bool {
githubEnv := os.Getenv("GITHUB_ACTION")
return githubEnv != ""
}

func IsRunningOnLinux() bool {
//goland:noinspection GoBoolExpressions // The linter cannot tell that this expression is not constant.
return runtime.GOOS == "linux"
}

0 comments on commit 53cfa45

Please sign in to comment.