Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ci): fixing health endpoint issue #9116

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 13 additions & 7 deletions dgraphtest/dgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (z *zero) healthURL(c *LocalCluster) (string, error) {
if err != nil {
return "", err
}
return "http://localhost:" + publicPort + "/health", nil
return "http://0.0.0.0:" + publicPort + "/health", nil
}

func (z *zero) changeStatus(isRunning bool) {
Expand All @@ -184,7 +184,7 @@ func (z *zero) assignURL(c *LocalCluster) (string, error) {
if err != nil {
return "", err
}
return "http://localhost:" + publicPort + "/assign", nil
return "http://0.0.0.0:" + publicPort + "/assign", nil
}

func (z *zero) alphaURL(c *LocalCluster) (string, error) {
Expand All @@ -196,7 +196,7 @@ func (z *zero) zeroURL(c *LocalCluster) (string, error) {
if err != nil {
return "", err
}
return "localhost:" + publicPort + "", nil
return "0.0.0.0:" + publicPort + "", nil
}

type alpha struct {
Expand Down Expand Up @@ -351,7 +351,7 @@ func (a *alpha) healthURL(c *LocalCluster) (string, error) {
if err != nil {
return "", err
}
return "http://localhost:" + publicPort + "/health", nil
return "http://0.0.0.0:" + publicPort + "/health", nil
}

func (a *alpha) assignURL(c *LocalCluster) (string, error) {
Expand All @@ -363,7 +363,7 @@ func (a *alpha) alphaURL(c *LocalCluster) (string, error) {
if err != nil {
return "", err
}
return "localhost:" + publicPort + "", nil
return "0.0.0.0:" + publicPort + "", nil
}

func (a *alpha) changeStatus(isRunning bool) {
Expand All @@ -388,8 +388,14 @@ func publicPort(dcli *docker.Client, dc dnode, privatePort string) (string, erro
if len(bindings) == 0 {
continue
}
if port.Port() == privatePort {
return bindings[0].HostPort, nil
if port.Port() != privatePort {
continue
}

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

Expand Down
86 changes: 85 additions & 1 deletion dgraphtest/local_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dgraphtest

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -281,6 +282,87 @@ func (c *LocalCluster) destroyContainers() error {
return nil
}

// CheckRunningServices checks open ports using lsof and returns the output as a string
func CheckRunningServices() (string, error) {
lsofCmd := exec.Command("lsof", "-i", "-n")
output, err := runCommand(lsofCmd)
if err != nil {
return "", fmt.Errorf("error running lsof command: %v", err)
}
return output, nil
}

// ListRunningContainers lists running Docker containers using the Docker Go client
func (c *LocalCluster) listRunningContainers() (string, error) {
containers, err := c.dcli.ContainerList(context.Background(), types.ContainerListOptions{})
if err != nil {
return "", fmt.Errorf("error listing Docker containers: %v", err)
}

var result bytes.Buffer
for _, container := range containers {
result.WriteString(fmt.Sprintf("ID: %s, Image: %s, Command: %s, Status: %s\n",
container.ID[:10], container.Image, container.Command, container.Status))

result.WriteString("Port Mappings:\n")
for _, port := range container.Ports {
result.WriteString(fmt.Sprintf(" %s:%d -> %d\n", port.IP, port.PublicPort, port.PrivatePort))
}
result.WriteString("\n")

result.WriteString("Port Mappings:\n")
info, err := c.dcli.ContainerInspect(context.Background(), container.ID)
if err != nil {
return "", errors.Wrap(err, "error inspecting container")
}

for port, bindings := range info.NetworkSettings.Ports {
if len(bindings) == 0 {
continue
}
result.WriteString(fmt.Sprintf(" %s:%s\n", port.Port(), bindings))
}
result.WriteString("\n")
}

return result.String(), nil
}

// runCommand executes a command and returns its output or an error
func runCommand(cmd *exec.Cmd) (string, error) {
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return "", fmt.Errorf("%v: %v", err, stderr.String())
}
return out.String(), nil
}

func (c *LocalCluster) printNetworkStuff() {
log.Printf("Checking running services and ports using lsof, netstat, and Docker...\n")

// Check running services using lsof
lsofOutput, err := CheckRunningServices()
if err != nil {
fmt.Printf("Error checking running services: %v\n", err)
} else {
log.Printf("Output of lsof -i:")
log.Println(lsofOutput)
}

// List running Docker containers
dockerOutput, err := c.listRunningContainers()
if err != nil {
fmt.Printf("Error listing Docker containers: %v\n", err)
} else {
log.Printf("Running Docker containers:")
log.Println(dockerOutput)
}
}

func (c *LocalCluster) Cleanup(verbose bool) {
if c == nil {
return
Expand Down Expand Up @@ -489,6 +571,7 @@ func (c *LocalCluster) containerHealthCheck(url func(c *LocalCluster) (string, e
if err != nil {
return errors.Wrap(err, "error getting health URL")
}

for i := 0; i < 60; i++ {
time.Sleep(waitDurBeforeRetry)

Expand Down Expand Up @@ -530,6 +613,7 @@ func (c *LocalCluster) containerHealthCheck(url func(c *LocalCluster) (string, e
return nil
}

c.printNetworkStuff()
return fmt.Errorf("health failed, cluster took too long to come up [%v]", endpoint)
}

Expand Down Expand Up @@ -773,7 +857,7 @@ func (c *LocalCluster) serverURL(server, endpoint string) (string, error) {
if err != nil {
return "", err
}
url := "localhost:" + pubPort + endpoint
url := "0.0.0.0:" + pubPort + endpoint
return url, nil
}

Expand Down