Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions dgraphtest/dgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -369,6 +371,11 @@ func (a *alpha) zeroURL(c *LocalCluster) (string, error) {

func publicPort(dcli *docker.Client, dc dnode, privatePort string) (string, error) {
// TODO(aman): we should cache the port information

if runtime.GOOS == "darwin" {
return getPortMappingsOnMac(dc.cid(), privatePort)
}

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
defer cancel()

Expand All @@ -395,6 +402,31 @@ func publicPort(dcli *docker.Client, dc dnode, privatePort string) (string, erro
return "", fmt.Errorf("no mapping found for private port [%v] for container [%v]", privatePort, dc.cname())
}

// getPortMappingsOnMac parses `docker ps` output to get accurate macOS mappings
// because docker inspect does not know about port mappings at all.
func getPortMappingsOnMac(containerID, privatePort string) (string, error) {
out, err := exec.Command("docker", "ps", "--format", "{{.ID}} {{.Ports}}").Output()
if err != nil {
return "", fmt.Errorf("docker ps failed: %w", err)
}

for line := range strings.SplitSeq(string(out), "\n") {
fields := strings.Fields(line)
if len(fields) < 2 || !strings.HasPrefix(containerID, fields[0]) {
continue
}

// Example: "0.0.0.0:55069->8080/tcp," => "55069"
for _, part := range fields[1:] {
if strings.Contains(part, privatePort+"/tcp") {
return strings.Split(strings.Split(part, ":")[1], "->")[0], nil
}
}
}

return "", fmt.Errorf("no mapping found for private port [%v] for container [%v]", privatePort, containerID)
}

func mountBinary(c *LocalCluster) (mount.Mount, error) {
// We shouldn't need to call setupBinary here, we already call it in LocalCluster.setupBeforeCluster
// function which is called whenever the dgraph's cluster version is initialized or upgraded. Though,
Expand Down