Skip to content

Commit

Permalink
replace connection test result with structure from model package (#2259)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathnogueira authored Mar 28, 2023
1 parent 98d258c commit c9db177
Show file tree
Hide file tree
Showing 22 changed files with 208 additions and 153 deletions.
4 changes: 2 additions & 2 deletions server/executor/poller_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,8 @@ func (db *traceDBMock) GetTraceID() trace.TraceID {
func (db *traceDBMock) Connect(ctx context.Context) error { return nil }
func (db *traceDBMock) Close() error { return nil }
func (db *traceDBMock) Ready() bool { return true }
func (db *traceDBMock) TestConnection(ctx context.Context) connection.ConnectionTestResult {
return connection.ConnectionTestResult{}
func (db *traceDBMock) TestConnection(ctx context.Context) model.ConnectionResult {
return model.ConnectionResult{}
}

type traceDBState struct {
Expand Down
26 changes: 13 additions & 13 deletions server/http/mappings/datastore.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
package mappings

import (
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/openapi"
"github.com/kubeshop/tracetest/server/tracedb/connection"
)

func (m *OpenAPI) ConnectionTestResult(in connection.ConnectionTestResult) openapi.ConnectionResult {
func (m *OpenAPI) ConnectionTestResult(in model.ConnectionResult) openapi.ConnectionResult {
result := openapi.ConnectionResult{}

if in.EndpointLintTestResult.IsSet() {
result.PortCheck = m.ConnectionTestStep(in.EndpointLintTestResult)
if in.PortCheck.IsSet() {
result.PortCheck = m.ConnectionTestStep(in.PortCheck)
}

if in.ConnectivityTestResult.IsSet() {
result.Connectivity = m.ConnectionTestStep(in.ConnectivityTestResult)
if in.Connectivity.IsSet() {
result.Connectivity = m.ConnectionTestStep(in.Connectivity)
}

if in.AuthenticationTestResult.IsSet() {
result.Authentication = m.ConnectionTestStep(in.AuthenticationTestResult)
if in.Authentication.IsSet() {
result.Authentication = m.ConnectionTestStep(in.Authentication)
}

if in.TraceRetrievalTestResult.IsSet() {
result.FetchTraces = m.ConnectionTestStep(in.TraceRetrievalTestResult)
if in.FetchTraces.IsSet() {
result.FetchTraces = m.ConnectionTestStep(in.FetchTraces)
}

return result
}

func (m *OpenAPI) ConnectionTestStep(in connection.ConnectionTestStepResult) openapi.ConnectionTestStep {
func (m *OpenAPI) ConnectionTestStep(in model.ConnectionTestStep) openapi.ConnectionTestStep {
errMessage := ""
if in.Error != nil {
errMessage = in.Error.Error()
}

return openapi.ConnectionTestStep{
Passed: in.Status != connection.StatusFailed,
Message: in.OperationDescription,
Passed: in.Status != model.StatusFailed,
Message: in.Message,
Status: string(in.Status),
Error: errMessage,
}
Expand Down
35 changes: 35 additions & 0 deletions server/model/connection_test_result.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package model

type ConnectionResult struct {
PortCheck ConnectionTestStep
Connectivity ConnectionTestStep
Authentication ConnectionTestStep
FetchTraces ConnectionTestStep
}

func (c ConnectionResult) HasSucceed() bool {
return c.Connectivity.HasSucceed() && c.Authentication.HasSucceed() && c.FetchTraces.HasSucceed()
}

type ConnectionTestStep struct {
Passed bool
Status Status
Message string
Error error
}

func (r *ConnectionTestStep) HasSucceed() bool {
if r == nil {
return true
}

return r.Error == nil
}

func (r *ConnectionTestStep) IsSet() bool {
if r == nil {
return false
}

return r.Message != ""
}
49 changes: 49 additions & 0 deletions server/model/test_run_event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package model

import "time"

type (
Protocol string
Status string
)

var (
ProtocolHTTP Protocol = "http"
ProtocolGRPC Protocol = "grpc"
)

var (
StatusPassed Status = "passed"
StatusWarning Status = "warning"
StatusFailed Status = "failed"
)

type TestRunEvent struct {
Type string
Stage string
Description string
CreatedAt time.Time
TestId string
RunId string
DataStoreConnection ConnectionResult
Polling PollingInfo
Outputs []OutputInfo
}

type PollingInfo struct {
Type string
ReasonNextIteration string
IsComplete bool
Periodic *PeriodicPollingConfig
}

type PeriodicPollingConfig struct {
NumberSpans int32
NumberIterations int32
}

type OutputInfo struct {
LogLevel string
Message string
OutputName string
}
4 changes: 2 additions & 2 deletions server/tracedb/awsxray.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func (db *awsxrayDB) Close() error {
return nil
}

func (db *awsxrayDB) TestConnection(ctx context.Context) connection.ConnectionTestResult {
func (db *awsxrayDB) TestConnection(ctx context.Context) model.ConnectionResult {
url := fmt.Sprintf("xray.%s.amazonaws.com:443", db.region)
tester := connection.NewTester(
connection.WithConnectivityTest(connection.ConnectivityStep(connection.ProtocolHTTP, url)),
connection.WithConnectivityTest(connection.ConnectivityStep(model.ProtocolHTTP, url)),
connection.WithPollingTest(connection.TracePollingTestStep(db)),
connection.WithAuthenticationTest(connection.NewTestStep(func(ctx context.Context) (string, error) {
_, err := db.GetTraceByID(ctx, db.GetTraceID().String())
Expand Down
47 changes: 4 additions & 43 deletions server/tracedb/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,20 @@ import (
"net/url"
"strings"
"time"
)

const reachabilityTimeout = 5 * time.Second

type (
Protocol string
Status string
)

var (
ProtocolHTTP Protocol = "http"
ProtocolGRPC Protocol = "grpc"
"github.com/kubeshop/tracetest/server/model"
)

var (
StatusPassed Status = "passed"
StatusWarning Status = "warning"
StatusFailed Status = "failed"
)

type ConnectionTestResult struct {
EndpointLintTestResult ConnectionTestStepResult
ConnectivityTestResult ConnectionTestStepResult
AuthenticationTestResult ConnectionTestStepResult
TraceRetrievalTestResult ConnectionTestStepResult
}
const reachabilityTimeout = 5 * time.Second

var (
ErrTraceNotFound = errors.New("trace not found")
ErrInvalidConfiguration = errors.New("invalid data store configuration")
ErrConnectionFailed = errors.New("could not connect to data store")
)

func (c ConnectionTestResult) HasSucceed() bool {
return c.AuthenticationTestResult.HasSucceed() && c.ConnectivityTestResult.HasSucceed() && c.TraceRetrievalTestResult.HasSucceed()
}

type ConnectionTestStepResult struct {
OperationDescription string
Status Status
Error error
}

func (r ConnectionTestStepResult) HasSucceed() bool {
return r.Error == nil
}

func (r ConnectionTestStepResult) IsSet() bool {
return r.OperationDescription != ""
}

func CheckReachability(endpoint string, protocol Protocol) error {
if protocol == ProtocolHTTP {
func CheckReachability(endpoint string, protocol model.Protocol) error {
if protocol == model.ProtocolHTTP {
address, err := url.Parse(endpoint)
if err != nil {
return err
Expand Down
27 changes: 14 additions & 13 deletions server/tracedb/connection/connectivity_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"strings"

"github.com/hashicorp/go-multierror"
"github.com/kubeshop/tracetest/server/model"
)

type connectivityTestStep struct {
endpoints []string
protocol Protocol
protocol model.Protocol
}

var _ TestStep = &connectivityTestStep{}

func (s *connectivityTestStep) TestConnection(_ context.Context) ConnectionTestStepResult {
func (s *connectivityTestStep) TestConnection(_ context.Context) model.ConnectionTestStep {
unreachableEndpoints := make([]string, 0)
var connectionErr error
for _, endpoint := range s.endpoints {
Expand All @@ -30,29 +31,29 @@ func (s *connectivityTestStep) TestConnection(_ context.Context) ConnectionTestS
}

if len(s.endpoints) == 0 {
return ConnectionTestStepResult{
OperationDescription: "Tracetest tried to connect but no endpoints were provided",
Error: fmt.Errorf("no endpoints provided"),
return model.ConnectionTestStep{
Message: "Tracetest tried to connect but no endpoints were provided",
Error: fmt.Errorf("no endpoints provided"),
}
}

if connectionErr != nil {
endpoints := strings.Join(unreachableEndpoints, ", ")
return ConnectionTestStepResult{
OperationDescription: fmt.Sprintf("Tracetest tried to connect to the following endpoints and failed: %s", endpoints),
Status: StatusFailed,
Error: connectionErr,
return model.ConnectionTestStep{
Message: fmt.Sprintf("Tracetest tried to connect to the following endpoints and failed: %s", endpoints),
Status: model.StatusFailed,
Error: connectionErr,
}
}

endpoints := strings.Join(s.endpoints, ", ")
return ConnectionTestStepResult{
OperationDescription: fmt.Sprintf(`Tracetest connected to %s`, endpoints),
Status: StatusPassed,
return model.ConnectionTestStep{
Message: fmt.Sprintf(`Tracetest connected to %s`, endpoints),
Status: model.StatusPassed,
}
}

func ConnectivityStep(protocol Protocol, endpoints ...string) TestStep {
func ConnectivityStep(protocol model.Protocol, endpoints ...string) TestStep {
return &connectivityTestStep{
endpoints: endpoints,
protocol: protocol,
Expand Down
14 changes: 9 additions & 5 deletions server/tracedb/connection/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package connection

import "context"
import (
"context"

"github.com/kubeshop/tracetest/server/model"
)

func WithPortLintingTest(step TestStep) TesterOption {
return func(t *Tester) {
Expand Down Expand Up @@ -30,11 +34,11 @@ type functionTestStep struct {
fn func(ctx context.Context) (string, error)
}

func (s *functionTestStep) TestConnection(ctx context.Context) ConnectionTestStepResult {
func (s *functionTestStep) TestConnection(ctx context.Context) model.ConnectionTestStep {
str, err := s.fn(ctx)
return ConnectionTestStepResult{
OperationDescription: str,
Error: err,
return model.ConnectionTestStep{
Message: str,
Error: err,
}
}

Expand Down
16 changes: 9 additions & 7 deletions server/tracedb/connection/port_linting.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"regexp"
"strings"

"github.com/kubeshop/tracetest/server/model"
)

type portLinter struct {
Expand All @@ -23,22 +25,22 @@ func PortLinter(dataStoreName string, expectedPorts []string, endpoints ...strin
}
}

func (s *portLinter) TestConnection(ctx context.Context) ConnectionTestStepResult {
func (s *portLinter) TestConnection(ctx context.Context) model.ConnectionTestStep {
for _, endpoint := range s.endpoints {
port := parsePort(endpoint)

if !sliceContains(s.expectedPorts, port) {
suggestedPorts := formatAvailablePortsMessage(s.expectedPorts)
return ConnectionTestStepResult{
OperationDescription: fmt.Sprintf(`For %s, port "%s" is not the default port for accessing traces programmatically. Typically, %s uses port %s. If you continue experiencing issues, you may want to verify the correct port to specify.`, s.dataStoreName, port, s.dataStoreName, suggestedPorts),
Status: StatusWarning,
return model.ConnectionTestStep{
Message: fmt.Sprintf(`For %s, port "%s" is not the default port for accessing traces programmatically. Typically, %s uses port %s. If you continue experiencing issues, you may want to verify the correct port to specify.`, s.dataStoreName, port, s.dataStoreName, suggestedPorts),
Status: model.StatusWarning,
}
}
}

return ConnectionTestStepResult{
OperationDescription: `You are using a commonly used port`,
Status: StatusPassed,
return model.ConnectionTestStep{
Message: `You are using a commonly used port`,
Status: model.StatusPassed,
}
}

Expand Down
Loading

0 comments on commit c9db177

Please sign in to comment.