Repository: dgraph-io/dgraph
File: dgraphtest/dgraph.go (Go)

Code before:
		for _, binding := range bindings {
			if binding.HostIP == "0.0.0.0" {
				return binding.HostPort, nil
			}
		}
	}

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

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,
	// we have observed "exec format error" when we don't do this. Our suspicion is that this is related
	// to the fact that we mount same binary inside multiple docker containers. We noticed a similar
	// issue with this PR when we mount same ACL secret file inside multiple containers.
	if err := c.setupBinary(); err != nil {
		return mount.Mount{}, err
	}

	return mount.Mount{
		Type:     mount.TypeBind,
		Source:   c.tempBinDir,
		Target:   goBinMountPath,
		ReadOnly: true,
	}, nil
}

// ShouldSkipTest skips a given test if clusterVersion < minVersion
func ShouldSkipTest(t *testing.T, minVersion, clusterVersion string) {
	supported, err := IsHigherVersion(clusterVersion, minVersion)
	if err != nil {
		t.Fatal(err)
	}
	if !supported {
		t.Skipf("test is valid for commits greater than [%v]", minVersion)
	}
}


Review comment (suggestion):
The function strings.SplitSeq is not a standard library function. Consider changing it to strings.Split to iterate over the lines of the output.
```suggestion
	for _, line := range strings.Split(string(out), "\n") {
```

Code after:
		if len(bindings) == 0 {
			continue
		}
		if port.Port() != privatePort {
			continue
		}

		for _, binding := range bindings {
			if binding.HostIP == "0.0.0.0" {
				return binding.HostPort, nil
			}
		}
	}

	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,
	// we have observed "exec format error" when we don't do this. Our suspicion is that this is related
	// to the fact that we mount same binary inside multiple docker containers. We noticed a similar
	// issue with this PR when we mount same ACL secret file inside multiple containers.
	if err := c.setupBinary(); err != nil {
		return mount.Mount{}, err
	}