From 81da0e8d95aa9efdddaf46b80069ace5e6f41fe8 Mon Sep 17 00:00:00 2001 From: Cristian Cepeda <43882+pastuxso@users.noreply.github.com> Date: Wed, 7 Aug 2024 18:16:24 -0500 Subject: [PATCH] chmod is not working inside a Docker container --- main_test.go | 48 +++++++++++++++++++ .../basic.txtar} | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) rename testdata/{script/permissions.txtar => permissions/basic.txtar} (92%) diff --git a/main_test.go b/main_test.go index 1bfa8089d..a9eb8866e 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,7 @@ package main import ( + "bufio" "bytes" "os" "os/exec" @@ -16,6 +17,42 @@ import ( "github.com/stretchr/testify/require" ) +func isDocker() bool { + if _, err := os.Stat("/.dockerenv"); err == nil { + return true + } + + paths := []string{"/proc/1/cgroup", "/proc/self/cgroup"} + for _, path := range paths { + file, err := os.Open(path) + if err != nil { + continue + } + + scanner := bufio.NewScanner(file) + isDocker := false + for scanner.Scan() { + if strings.Contains(scanner.Text(), "docker") || strings.Contains(scanner.Text(), "kubepods") { + isDocker = true + break + } + } + + if err := scanner.Err(); err != nil { + _ = file.Close() + return false + } + + _ = file.Close() + + if isDocker { + return true + } + } + + return false +} + func TestMain(m *testing.M) { os.Exit(testscript.RunMain(m, map[string]func() int{ "runme": root, @@ -33,6 +70,17 @@ func TestRunme(t *testing.T) { }) } +func TestRunmeFilePermissions(t *testing.T) { + if isDocker() { + return + } + + testscript.Run(t, testscript.Params{ + Dir: "testdata/permissions", + ContinueOnError: true, + }) +} + func TestRunmeFlags(t *testing.T) { testscript.Run(t, testscript.Params{ Dir: "testdata/flags", diff --git a/testdata/script/permissions.txtar b/testdata/permissions/basic.txtar similarity index 92% rename from testdata/script/permissions.txtar rename to testdata/permissions/basic.txtar index c53aaaa6d..0af3313b1 100644 --- a/testdata/script/permissions.txtar +++ b/testdata/permissions/basic.txtar @@ -1,6 +1,6 @@ exec chmod 000 secret exec chmod 000 CONTRIBUTING.md -exec runme ls --ignore-pattern=secret +exec runme ls cmp stdout output.txt ! stderr .