Skip to content

Commit

Permalink
feat: docker host mismatch event (#2288)
Browse files Browse the repository at this point in the history
* emit event in case of host mismatch when running it on docker

* check if running on k8s
  • Loading branch information
mathnogueira committed Mar 31, 2023
1 parent 80b7e40 commit baf4b14
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions server/executor/runner.go
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"net/url"
"os"
"strings"

"github.com/kubeshop/tracetest/server/executor/trigger"
Expand Down Expand Up @@ -233,6 +235,10 @@ func (r persistentRunner) processExecQueue(job execReq) {
if err != nil {
if isConnectionError(err) {
r.emitUnreachableEndpointEvent(job, err)

if isTargetLocalhost(job, err) && isServerRunningInsideContainer() {
r.emitMismatchEndpointEvent(job, err)
}
}

emitErr := r.eventEmitter.Emit(job.ctx, events.TriggerExecutionError(job.run.TestID, job.run.ID, err))
Expand Down Expand Up @@ -287,6 +293,13 @@ func (r persistentRunner) emitUnreachableEndpointEvent(job execReq, err error) {
}
}

func (r persistentRunner) emitMismatchEndpointEvent(job execReq, err error) {
emitErr := r.eventEmitter.Emit(job.ctx, events.TriggerDockerComposeHostMismatchError(job.run.TestID, job.run.ID))
if emitErr != nil {
r.handleError(job.run, emitErr)
}
}

func isConnectionError(err error) bool {
for err != nil {
// a dial error means we couldn't open a TCP connection (either host is not available or DNS doesn't exist)
Expand All @@ -304,3 +317,42 @@ func isConnectionError(err error) bool {

return false
}

func isTargetLocalhost(job execReq, err error) bool {
var endpoint string
switch job.test.ServiceUnderTest.Type {
case model.TriggerTypeHTTP:
endpoint = job.test.ServiceUnderTest.HTTP.URL
case model.TriggerTypeGRPC:
endpoint = job.test.ServiceUnderTest.GRPC.Address
}

url, err := url.Parse(endpoint)
if err != nil {
return false
}

// removes port
host := url.Host
colonPosition := strings.Index(url.Host, ":")
if colonPosition >= 0 {
host = host[0:colonPosition]
}

return host == "localhost" || host == "127.0.0.1"
}

func isServerRunningInsideContainer() bool {
// Check if running on Docker
// Reference: https://paulbradley.org/indocker/
if _, err := os.Stat("/.dockerenv"); err == nil {
return true
}

// Check if running on k8s
if os.Getenv("KUBERNETES_SERVICE_HOST") != "" {
return true
}

return false
}

0 comments on commit baf4b14

Please sign in to comment.