Skip to content

Commit

Permalink
Merge pull request #12168 from mtrmac/socket-collision
Browse files Browse the repository at this point in the history
Avoid RemoteSocket collisions in e2e tests
  • Loading branch information
openshift-merge-robot authored Jan 5, 2022
2 parents cbb2b68 + f6a3edd commit 2157414
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
28 changes: 23 additions & 5 deletions test/e2e/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,32 @@ func PodmanTestCreateUtil(tempDir string, remote bool) *PodmanTestIntegration {
}

if remote {
uuid := stringid.GenerateNonCryptoID()
var pathPrefix string
if !rootless.IsRootless() {
p.RemoteSocket = fmt.Sprintf("unix:/run/podman/podman-%s.sock", uuid)
pathPrefix = "/run/podman/podman"
} else {
runtimeDir := os.Getenv("XDG_RUNTIME_DIR")
socket := fmt.Sprintf("podman-%s.sock", uuid)
fqpath := filepath.Join(runtimeDir, socket)
p.RemoteSocket = fmt.Sprintf("unix:%s", fqpath)
pathPrefix = filepath.Join(runtimeDir, "podman")
}
// We want to avoid collisions in socket paths, but using the
// socket directly for a collision check doesn’t work; bind(2) on AF_UNIX
// creates the file, and we need to pass a unique path now before the bind(2)
// happens. So, use a podman-%s.sock-lock empty file as a marker.
tries := 0
for {
uuid := stringid.GenerateNonCryptoID()
lockPath := fmt.Sprintf("%s-%s.sock-lock", pathPrefix, uuid)
lockFile, err := os.OpenFile(lockPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0700)
if err == nil {
lockFile.Close()
p.RemoteSocketLock = lockPath
p.RemoteSocket = fmt.Sprintf("unix:%s-%s.sock", pathPrefix, uuid)
break
}
tries++
if tries >= 1000 {
panic("Too many RemoteSocket collisions")
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/e2e/libpod_suite_remote_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build remote
// +build remote

package integration
Expand Down Expand Up @@ -143,6 +144,11 @@ func (p *PodmanTestIntegration) StopRemoteService() {
if err := os.Remove(socket); err != nil {
fmt.Println(err)
}
if p.RemoteSocketLock != "" {
if err := os.Remove(p.RemoteSocketLock); err != nil {
fmt.Println(err)
}
}
}

//MakeOptions assembles all the podman main options
Expand Down
5 changes: 1 addition & 4 deletions test/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type PodmanTest struct {
RemotePodmanBinary string
RemoteSession *os.Process
RemoteSocket string
RemoteSocketLock string // If not "", should be removed _after_ RemoteSocket is removed
RemoteCommand *exec.Cmd
ImageCacheDir string
ImageCacheFS string
Expand Down Expand Up @@ -469,10 +470,6 @@ func Containerized() bool {
return strings.Contains(string(b), "docker")
}

func init() {
rand.Seed(GinkgoRandomSeed())
}

var randomLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

// RandomString returns a string of given length composed of random characters
Expand Down

0 comments on commit 2157414

Please sign in to comment.