Skip to content

Commit

Permalink
chmod is not working inside a Docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
pastuxso committed Aug 7, 2024
1 parent bef639d commit 81da0e8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
48 changes: 48 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"bufio"
"bytes"
"os"
"os/exec"
Expand All @@ -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,
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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 .

Expand Down

0 comments on commit 81da0e8

Please sign in to comment.