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

feat(server): refactor shared packages #2402

Merged
merged 2 commits into from
Apr 18, 2023
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
2 changes: 1 addition & 1 deletion server/app/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/kubeshop/tracetest/server/executor"
"github.com/kubeshop/tracetest/server/executor/pollingprofile"
"github.com/kubeshop/tracetest/server/executor/trigger"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/tracedb"
"go.opentelemetry.io/otel/trace"
Expand Down
2 changes: 1 addition & 1 deletion server/assertions/selectors/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/kubeshop/tracetest/server/assertions/selectors"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
Expand Down
2 changes: 1 addition & 1 deletion server/config/configresource/config_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"fmt"
"os"

"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/resourcemanager"
)

Expand Down
2 changes: 1 addition & 1 deletion server/config/demoresource/demo_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"fmt"

"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/resourcemanager"
)

Expand Down
2 changes: 1 addition & 1 deletion server/config/demoresource/demo_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/gorilla/mux"
"github.com/kubeshop/tracetest/server/config/demoresource"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/resourcemanager"
rmtests "github.com/kubeshop/tracetest/server/resourcemanager/testutil"
"github.com/kubeshop/tracetest/server/testmock"
Expand Down
9 changes: 5 additions & 4 deletions server/executor/assertion_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import (
"github.com/kubeshop/tracetest/server/assertions/selectors"
"github.com/kubeshop/tracetest/server/expression"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/maps"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)

type AssertionExecutor interface {
Assert(context.Context, model.OrderedMap[model.SpanQuery, model.NamedAssertions], model.Trace, []expression.DataStore) (model.OrderedMap[model.SpanQuery, []model.AssertionResult], bool)
Assert(context.Context, maps.Ordered[model.SpanQuery, model.NamedAssertions], model.Trace, []expression.DataStore) (maps.Ordered[model.SpanQuery, []model.AssertionResult], bool)
}

type defaultAssertionExecutor struct{}

func (e defaultAssertionExecutor) Assert(_ context.Context, defs model.OrderedMap[model.SpanQuery, model.NamedAssertions], trace model.Trace, ds []expression.DataStore) (model.OrderedMap[model.SpanQuery, []model.AssertionResult], bool) {
testResult := model.OrderedMap[model.SpanQuery, []model.AssertionResult]{}
func (e defaultAssertionExecutor) Assert(_ context.Context, defs maps.Ordered[model.SpanQuery, model.NamedAssertions], trace model.Trace, ds []expression.DataStore) (maps.Ordered[model.SpanQuery, []model.AssertionResult], bool) {
testResult := maps.Ordered[model.SpanQuery, []model.AssertionResult]{}
allPassed := true
defs.ForEach(func(spanQuery model.SpanQuery, asserts model.NamedAssertions) error {
spans := selector(spanQuery).Filter(trace)
Expand Down Expand Up @@ -91,7 +92,7 @@ type instrumentedAssertionExecutor struct {
tracer trace.Tracer
}

func (e instrumentedAssertionExecutor) Assert(ctx context.Context, defs model.OrderedMap[model.SpanQuery, model.NamedAssertions], trace model.Trace, ds []expression.DataStore) (model.OrderedMap[model.SpanQuery, []model.AssertionResult], bool) {
func (e instrumentedAssertionExecutor) Assert(ctx context.Context, defs maps.Ordered[model.SpanQuery, model.NamedAssertions], trace model.Trace, ds []expression.DataStore) (maps.Ordered[model.SpanQuery, []model.AssertionResult], bool) {
ctx, span := e.tracer.Start(ctx, "Execute assertions")
defer span.End()

Expand Down
7 changes: 4 additions & 3 deletions server/executor/assertion_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/kubeshop/tracetest/server/expression"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/model/events"
"github.com/kubeshop/tracetest/server/pkg/maps"
"github.com/kubeshop/tracetest/server/subscription"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
Expand Down Expand Up @@ -190,7 +191,7 @@ func (e *defaultAssertionRunner) executeAssertions(ctx context.Context, req Asse
return run, nil
}

func (e *defaultAssertionRunner) emitFailedAssertions(ctx context.Context, req AssertionRequest, result model.OrderedMap[model.SpanQuery, []model.AssertionResult]) {
func (e *defaultAssertionRunner) emitFailedAssertions(ctx context.Context, req AssertionRequest, result maps.Ordered[model.SpanQuery, []model.AssertionResult]) {
for _, assertionResults := range result.Unordered() {
for _, assertionResult := range assertionResults {
for _, spanAssertionResult := range assertionResult.Results {
Expand Down Expand Up @@ -221,7 +222,7 @@ func (e *defaultAssertionRunner) emitFailedAssertions(ctx context.Context, req A
}
}

func createEnvironment(environment model.Environment, outputs model.OrderedMap[string, model.RunOutput]) model.Environment {
func createEnvironment(environment model.Environment, outputs maps.Ordered[string, model.RunOutput]) model.Environment {
outputVariables := make([]model.EnvironmentValue, 0)
outputs.ForEach(func(key string, val model.RunOutput) error {
outputVariables = append(outputVariables, model.EnvironmentValue{
Expand All @@ -246,7 +247,7 @@ func (e *defaultAssertionRunner) RunAssertions(ctx context.Context, request Asse
e.inputChannel <- request
}

func (e *defaultAssertionRunner) validateOutputResolution(ctx context.Context, request AssertionRequest, outputs model.OrderedMap[string, model.RunOutput]) {
func (e *defaultAssertionRunner) validateOutputResolution(ctx context.Context, request AssertionRequest, outputs maps.Ordered[string, model.RunOutput]) {
err := outputs.ForEach(func(outputName string, outputModel model.RunOutput) error {
if outputModel.Resolved {
return nil
Expand Down
27 changes: 14 additions & 13 deletions server/executor/assetion_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
"github.com/kubeshop/tracetest/server/assertions/comparator"
"github.com/kubeshop/tracetest/server/executor"
"github.com/kubeshop/tracetest/server/expression"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/pkg/maps"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
Expand All @@ -19,14 +20,14 @@ func TestAssertion(t *testing.T) {
spanID := id.NewRandGenerator().SpanID()
cases := []struct {
name string
testDef model.OrderedMap[model.SpanQuery, model.NamedAssertions]
testDef maps.Ordered[model.SpanQuery, model.NamedAssertions]
trace model.Trace
expectedResult model.OrderedMap[model.SpanQuery, []model.AssertionResult]
expectedResult maps.Ordered[model.SpanQuery, []model.AssertionResult]
expectedAllPassed bool
}{
{
name: "CanAssert",
testDef: (model.OrderedMap[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
testDef: (maps.Ordered[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
Assertions: []model.Assertion{
`attr:tracetest.span.duration = 2000ns`,
},
Expand All @@ -41,7 +42,7 @@ func TestAssertion(t *testing.T) {
},
},
expectedAllPassed: true,
expectedResult: (model.OrderedMap[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
expectedResult: (maps.Ordered[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
{
Assertion: `attr:tracetest.span.duration = 2000ns`,
Results: []model.SpanAssertionResult{
Expand All @@ -56,7 +57,7 @@ func TestAssertion(t *testing.T) {
},
{
name: "CanAssertOnSpanMatchCount",
testDef: (model.OrderedMap[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
testDef: (maps.Ordered[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
Assertions: []model.Assertion{
`attr:tracetest.selected_spans.count = 1`,
},
Expand All @@ -74,7 +75,7 @@ func TestAssertion(t *testing.T) {
},
},
expectedAllPassed: true,
expectedResult: (model.OrderedMap[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
expectedResult: (maps.Ordered[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
{
Assertion: `attr:tracetest.selected_spans.count = 1`,
Results: []model.SpanAssertionResult{
Expand All @@ -101,7 +102,7 @@ func TestAssertion(t *testing.T) {
// https://github.com/kubeshop/tracetest/issues/617
{
name: "ContainsWithJSON",
testDef: (model.OrderedMap[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
testDef: (maps.Ordered[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
Assertions: []model.Assertion{
`attr:http.response.body contains 52`,
`attr:tracetest.span.duration <= 21ms`,
Expand All @@ -117,7 +118,7 @@ func TestAssertion(t *testing.T) {
},
},
expectedAllPassed: true,
expectedResult: (model.OrderedMap[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
expectedResult: (maps.Ordered[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
{
Assertion: `attr:http.response.body contains 52`,
Results: []model.SpanAssertionResult{
Expand All @@ -143,7 +144,7 @@ func TestAssertion(t *testing.T) {
// https://github.com/kubeshop/tracetest/issues/1203
{
name: "DurationComparison",
testDef: (model.OrderedMap[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
testDef: (maps.Ordered[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
Assertions: []model.Assertion{
`attr:tracetest.span.duration <= 25ms`,
}}),
Expand All @@ -158,7 +159,7 @@ func TestAssertion(t *testing.T) {
},
},
expectedAllPassed: true,
expectedResult: (model.OrderedMap[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
expectedResult: (maps.Ordered[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
{
Assertion: `attr:tracetest.span.duration <= 25ms`,
Results: []model.SpanAssertionResult{
Expand All @@ -174,7 +175,7 @@ func TestAssertion(t *testing.T) {
// https://github.com/kubeshop/tracetest/issues/1421
{
name: "FailedAssertionsConvertDurationFieldsIntoDurationFormat",
testDef: (model.OrderedMap[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
testDef: (maps.Ordered[model.SpanQuery, model.NamedAssertions]{}).MustAdd(`span[service.name="Pokeshop"]`, model.NamedAssertions{
Assertions: []model.Assertion{
`attr:tracetest.span.duration <= 25ms`,
}}),
Expand All @@ -189,7 +190,7 @@ func TestAssertion(t *testing.T) {
},
},
expectedAllPassed: false,
expectedResult: (model.OrderedMap[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
expectedResult: (maps.Ordered[model.SpanQuery, []model.AssertionResult]{}).MustAdd(`span[service.name="Pokeshop"]`, []model.AssertionResult{
{
Assertion: `attr:tracetest.span.duration <= 25ms`,
Results: []model.SpanAssertionResult{
Expand Down
2 changes: 1 addition & 1 deletion server/executor/eventemitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"

"github.com/kubeshop/tracetest/server/executor"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/testdb"
"github.com/stretchr/testify/assert"
Expand Down
17 changes: 9 additions & 8 deletions server/executor/outputs_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"github.com/kubeshop/tracetest/server/assertions/selectors"
"github.com/kubeshop/tracetest/server/expression"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/maps"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
)

type OutputsProcessorFn func(context.Context, model.OrderedMap[string, model.Output], model.Trace, []expression.DataStore) (model.OrderedMap[string, model.RunOutput], error)
type OutputsProcessorFn func(context.Context, maps.Ordered[string, model.Output], model.Trace, []expression.DataStore) (maps.Ordered[string, model.RunOutput], error)

func InstrumentedOutputProcessor(tracer trace.Tracer) OutputsProcessorFn {
op := instrumentedOutputProcessor{tracer}
Expand All @@ -24,7 +25,7 @@ type instrumentedOutputProcessor struct {
tracer trace.Tracer
}

func (op instrumentedOutputProcessor) process(ctx context.Context, outputs model.OrderedMap[string, model.Output], t model.Trace, ds []expression.DataStore) (model.OrderedMap[string, model.RunOutput], error) {
func (op instrumentedOutputProcessor) process(ctx context.Context, outputs maps.Ordered[string, model.Output], t model.Trace, ds []expression.DataStore) (maps.Ordered[string, model.RunOutput], error) {
ctx, span := op.tracer.Start(ctx, "Process outputs")
defer span.End()

Expand All @@ -49,8 +50,8 @@ func (op instrumentedOutputProcessor) process(ctx context.Context, outputs model
return result, err
}

func outputProcessor(ctx context.Context, outputs model.OrderedMap[string, model.Output], tr model.Trace, ds []expression.DataStore) (model.OrderedMap[string, model.RunOutput], error) {
res := model.OrderedMap[string, model.RunOutput]{}
func outputProcessor(ctx context.Context, outputs maps.Ordered[string, model.Output], tr model.Trace, ds []expression.DataStore) (maps.Ordered[string, model.RunOutput], error) {
res := maps.Ordered[string, model.RunOutput]{}

parsed, err := parseOutputs(outputs)
if err != nil {
Expand Down Expand Up @@ -92,7 +93,7 @@ func outputProcessor(ctx context.Context, outputs model.OrderedMap[string, model
})

if err != nil {
return model.OrderedMap[string, model.RunOutput]{}, err
return maps.Ordered[string, model.RunOutput]{}, err
}

return res, nil
Expand All @@ -113,8 +114,8 @@ type parsedOutput struct {
expr expression.Expr
}

func parseOutputs(outputs model.OrderedMap[string, model.Output]) (model.OrderedMap[string, parsedOutput], error) {
var parsed model.OrderedMap[string, parsedOutput]
func parseOutputs(outputs maps.Ordered[string, model.Output]) (maps.Ordered[string, parsedOutput], error) {
var parsed maps.Ordered[string, parsedOutput]

parseErr := outputs.ForEach(func(key string, out model.Output) error {
expr, err := expression.Parse(out.Value)
Expand All @@ -135,7 +136,7 @@ func parseOutputs(outputs model.OrderedMap[string, model.Output]) (model.Ordered
})

if parseErr != nil {
return model.OrderedMap[string, parsedOutput]{}, parseErr
return maps.Ordered[string, parsedOutput]{}, parseErr
}

return parsed, nil
Expand Down
2 changes: 1 addition & 1 deletion server/executor/poller_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/kubeshop/tracetest/server/config"
"github.com/kubeshop/tracetest/server/executor"
"github.com/kubeshop/tracetest/server/executor/pollingprofile"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/testdb"
"github.com/kubeshop/tracetest/server/tracedb"
Expand Down
2 changes: 1 addition & 1 deletion server/executor/pollingprofile/polling_profile_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"math"
"time"

"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/resourcemanager"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

"github.com/gorilla/mux"
"github.com/kubeshop/tracetest/server/executor/pollingprofile"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/resourcemanager"
rmtests "github.com/kubeshop/tracetest/server/resourcemanager/testutil"
"github.com/kubeshop/tracetest/server/testmock"
Expand Down
2 changes: 1 addition & 1 deletion server/executor/run_stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"
"log"

"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/model/events"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/subscription"
)

Expand Down
2 changes: 1 addition & 1 deletion server/executor/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/kubeshop/tracetest/server/config"
"github.com/kubeshop/tracetest/server/executor"
"github.com/kubeshop/tracetest/server/executor/trigger"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/testdb"
"github.com/kubeshop/tracetest/server/tracedb"
Expand Down
3 changes: 2 additions & 1 deletion server/executor/transaction_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/maps"
"github.com/kubeshop/tracetest/server/subscription"
)

Expand Down Expand Up @@ -178,7 +179,7 @@ func (r persistentTransactionRunner) updateStepRun(ctx context.Context, tr model
return tr, nil
}

func mergeOutputsIntoEnv(env model.Environment, outputs model.OrderedMap[string, model.RunOutput]) model.Environment {
func mergeOutputsIntoEnv(env model.Environment, outputs maps.Ordered[string, model.RunOutput]) model.Environment {
newEnv := make([]model.EnvironmentValue, 0, outputs.Len())
outputs.ForEach(func(key string, val model.RunOutput) error {
newEnv = append(newEnv, model.EnvironmentValue{
Expand Down
3 changes: 2 additions & 1 deletion server/executor/transaction_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/kubeshop/tracetest/server/executor"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/maps"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/testmock"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -44,7 +45,7 @@ func (r *fakeTestRunner) Run(ctx context.Context, test model.Test, metadata mode

r.uid++

run.Outputs = (model.OrderedMap[string, model.RunOutput]{}).MustAdd("USER_ID", model.RunOutput{
run.Outputs = (maps.Ordered[string, model.RunOutput]{}).MustAdd("USER_ID", model.RunOutput{
Value: strconv.Itoa(r.uid),
})

Expand Down
2 changes: 1 addition & 1 deletion server/executor/trigger/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"testing"

"github.com/kubeshop/tracetest/server/executor/trigger"
"github.com/kubeshop/tracetest/server/id"
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/trace"
)
Expand Down
Loading