Skip to content

Commit

Permalink
Linter-driven feature-creep
Browse files Browse the repository at this point in the history
  • Loading branch information
webbnh committed Mar 28, 2024
1 parent 1d9c536 commit abb5fcc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
11 changes: 4 additions & 7 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 @@ -124,13 +123,12 @@ func bindMountHelper(t *testing.T, options string) {

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

//goland:noinspection GoBoolExpressions // The linter cannot tell that this expression is not constant.
if runtime.GOOS == "linux" && options == "" {
if tests.IsRunningOnGithub() && 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") //nolint:gosec
cmd := exec.Command("chcon", "-Rt", "svirt_sandbox_file_t", "./tests/volume")
assert.NoError(t, cmd.Run())
}

Expand All @@ -156,7 +154,7 @@ func TestBindMount(t *testing.T) {
"No options": "",
}
//goland:noinspection GoBoolExpressions // The linter cannot tell that this expression is not constant.
if runtime.GOOS == "linux" {
if tests.IsRunningOnGithub() {
// The SELinux options seem to cause problems on Mac OS X, so only test
// them on Linux.
scenarios["Private"] = ":Z"
Expand Down Expand Up @@ -321,9 +319,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.IsRunningOnGithub() {
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(
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())
}
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 abb5fcc

Please sign in to comment.