Skip to content

Commit

Permalink
fix: add back container execution detection
Browse files Browse the repository at this point in the history
  • Loading branch information
mathnogueira committed Sep 5, 2023
1 parent a784025 commit 42f7cbf
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 26 deletions.
70 changes: 70 additions & 0 deletions server/executor/trigger_executer_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package executor

import (
"context"
"errors"
"fmt"
"net/url"
"os"
"strings"

"github.com/kubeshop/tracetest/server/analytics"
triggerer "github.com/kubeshop/tracetest/server/executor/trigger"
"github.com/kubeshop/tracetest/server/model/events"
"github.com/kubeshop/tracetest/server/test"
"github.com/kubeshop/tracetest/server/test/trigger"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -66,6 +71,14 @@ func (r triggerExecuterWorker) ProcessItem(ctx context.Context, job Job) {
response, err := triggererObj.Trigger(ctx, job.Test, &triggerer.TriggerOptions{
TraceID: run.TraceID,
})
if err != nil {
response.Result.Error = &trigger.TriggerError{
ConnectionError: isConnectionError(err),
RunningOnContainer: isServerRunningInsideContainer(),
ErrorMessage: err.Error(),
}
}

run = r.handleExecutionResult(run, response, err)
run.SpanID = response.SpanID

Expand All @@ -89,3 +102,60 @@ func (r triggerExecuterWorker) handleExecutionResult(run test.Run, response trig

return run.SuccessfullyTriggered()
}

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)
if strings.HasPrefix(err.Error(), "dial ") {
return true
}

// it means a trigger timeout
if errors.Is(err, context.DeadlineExceeded) {
return true
}

err = errors.Unwrap(err)
}

return false
}

func isTargetLocalhost(job Job) bool {
var endpoint string
switch job.Test.Trigger.Type {
case trigger.TriggerTypeHTTP:
endpoint = job.Test.Trigger.HTTP.URL
case trigger.TriggerTypeGRPC:
endpoint = job.Test.Trigger.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
}
26 changes: 0 additions & 26 deletions server/executor/trigger_result_processor_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package executor
import (
"context"
"fmt"
"net/url"
"strings"

"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/model/events"
Expand Down Expand Up @@ -111,27 +109,3 @@ func (r triggerResultProcessorWorker) emitMismatchEndpointEvent(ctx context.Cont
r.handleError(job.Run, emitErr)
}
}

func isTargetLocalhost(job Job) bool {
var endpoint string
switch job.Test.Trigger.Type {
case trigger.TriggerTypeHTTP:
endpoint = job.Test.Trigger.HTTP.URL
case trigger.TriggerTypeGRPC:
endpoint = job.Test.Trigger.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"
}

0 comments on commit 42f7cbf

Please sign in to comment.