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

chore: rename tests package to transaction and split repo into 2 #2800

Merged
merged 3 commits into from
Jun 22, 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
27 changes: 15 additions & 12 deletions server/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import (
"github.com/kubeshop/tracetest/server/resourcemanager"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/testdb"
"github.com/kubeshop/tracetest/server/tests"
"github.com/kubeshop/tracetest/server/tracedb"
"github.com/kubeshop/tracetest/server/traces"
"github.com/kubeshop/tracetest/server/tracing"
"github.com/kubeshop/tracetest/server/transaction"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -150,7 +150,8 @@ func (app *App) Start(opts ...appOption) error {
log.Fatal(err)
}

transactionsRepository := tests.NewTransactionsRepository(db, testDB)
transactionsRepository := transaction.NewRepository(db, testDB)
transactionRunRepository := transaction.NewRunRepository(db, testDB)

subscriptionManager := subscription.NewManager()
app.subscribeToConfigChanges(subscriptionManager)
Expand Down Expand Up @@ -206,7 +207,7 @@ func (app *App) Start(opts ...appOption) error {
dataStoreRepo,
linterRepo,
testDB,
transactionsRepository,
transactionRunRepository,
applicationTracer,
tracer,
subscriptionManager,
Expand Down Expand Up @@ -245,7 +246,7 @@ func (app *App) Start(opts ...appOption) error {

provisioner := provisioning.New()

router, mappers := controller(app.cfg, testDB, transactionsRepository, tracer, environmentRepo, rf, triggerRegistry)
router, mappers := controller(app.cfg, testDB, transactionsRepository, transactionRunRepository, tracer, environmentRepo, rf, triggerRegistry)
registerWSHandler(router, mappers, subscriptionManager)

// use the analytics middleware on complete router
Expand Down Expand Up @@ -367,10 +368,10 @@ func registerlinterResource(linterRepo *linterResource.Repository, router *mux.R
provisioner.AddResourceProvisioner(manager)
}

func registerTransactionResource(repo *tests.TransactionsRepository, router *mux.Router, provisioner *provisioning.Provisioner, tracer trace.Tracer) {
manager := resourcemanager.New[tests.Transaction](
tests.TransactionResourceName,
tests.TransactionResourceNamePlural,
func registerTransactionResource(repo *transaction.Repository, router *mux.Router, provisioner *provisioning.Provisioner, tracer trace.Tracer) {
manager := resourcemanager.New[transaction.Transaction](
transaction.TransactionResourceName,
transaction.TransactionResourceNamePlural,
repo,
resourcemanager.CanBeAugmented(),
resourcemanager.WithTracer(tracer),
Expand Down Expand Up @@ -467,30 +468,32 @@ func registerWSHandler(router *mux.Router, mappers mappings.Mappings, subscripti
func controller(
cfg httpServerConfig,
testDB model.Repository,
transactions *tests.TransactionsRepository,
transactionRepository *transaction.Repository,
transactionRunRepository *transaction.RunRepository,
tracer trace.Tracer,
environmentRepo *environment.Repository,
rf *runnerFacade,
triggerRegistry *trigger.Registry,
) (*mux.Router, mappings.Mappings) {
mappers := mappings.New(tracesConversionConfig(), comparator.DefaultRegistry(), testDB)

router := openapi.NewRouter(httpRouter(cfg, testDB, transactions, tracer, environmentRepo, rf, mappers, triggerRegistry))
router := openapi.NewRouter(httpRouter(cfg, testDB, transactionRepository, transactionRunRepository, tracer, environmentRepo, rf, mappers, triggerRegistry))

return router, mappers
}

func httpRouter(
cfg httpServerConfig,
testDB model.Repository,
transactions *tests.TransactionsRepository,
transactionRepo *transaction.Repository,
transactionRunRepo *transaction.RunRepository,
tracer trace.Tracer,
environmentRepo *environment.Repository,
rf *runnerFacade,
mappers mappings.Mappings,
triggerRegistry *trigger.Registry,
) openapi.Router {
controller := httpServer.NewController(testDB, transactions, tracedb.Factory(testDB), rf, mappers, environmentRepo, triggerRegistry, tracer, Version)
controller := httpServer.NewController(testDB, transactionRepo, transactionRunRepo, tracedb.Factory(testDB), rf, mappers, environmentRepo, triggerRegistry, tracer, Version)
apiApiController := openapi.NewApiApiController(controller)
customController := httpServer.NewCustomController(controller, apiApiController, openapi.DefaultErrorHandler, tracer)
httpRouter := customController
Expand Down
8 changes: 4 additions & 4 deletions server/app/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/id"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/tests"
"github.com/kubeshop/tracetest/server/tracedb"
"github.com/kubeshop/tracetest/server/transaction"
"go.opentelemetry.io/otel/trace"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func (rf runnerFacade) RunTest(ctx context.Context, test model.Test, rm model.Ru
return rf.runner.Run(ctx, test, rm, env)
}

func (rf runnerFacade) RunTransaction(ctx context.Context, tr tests.Transaction, rm model.RunMetadata, env environment.Environment) tests.TransactionRun {
func (rf runnerFacade) RunTransaction(ctx context.Context, tr transaction.Transaction, rm model.RunMetadata, env environment.Environment) transaction.TransactionRun {
return rf.transactionRunner.Run(ctx, tr, rm, env)
}

Expand All @@ -55,7 +55,7 @@ func newRunnerFacades(
dsRepo *datastore.Repository,
lintRepo *linterResource.Repository,
testDB model.Repository,
transactions *tests.TransactionsRepository,
transactionRunRepository *transaction.RunRepository,
appTracer trace.Tracer,
tracer trace.Tracer,
subscriptionManager *subscription.Manager,
Expand Down Expand Up @@ -119,7 +119,7 @@ func newRunnerFacades(
transactionRunner := executor.NewTransactionRunner(
runner,
testDB,
transactions,
transactionRunRepository,
subscriptionManager,
)

Expand Down
12 changes: 6 additions & 6 deletions server/executor/transaction_run_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"fmt"

"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/tests"
"github.com/kubeshop/tracetest/server/transaction"
)

type TransactionRunUpdater interface {
Update(context.Context, tests.TransactionRun) error
Update(context.Context, transaction.TransactionRun) error
}

type CompositeTransactionUpdater struct {
Expand All @@ -23,7 +23,7 @@ func (u CompositeTransactionUpdater) Add(l TransactionRunUpdater) CompositeTrans

var _ TransactionRunUpdater = CompositeTransactionUpdater{}

func (u CompositeTransactionUpdater) Update(ctx context.Context, run tests.TransactionRun) error {
func (u CompositeTransactionUpdater) Update(ctx context.Context, run transaction.TransactionRun) error {
for _, l := range u.listeners {
if err := l.Update(ctx, run); err != nil {
return fmt.Errorf("composite updating error: %w", err)
Expand All @@ -38,14 +38,14 @@ type dbTransactionUpdater struct {
}

type transactionUpdater interface {
UpdateRun(context.Context, tests.TransactionRun) error
UpdateRun(context.Context, transaction.TransactionRun) error
}

func NewDBTranasctionUpdater(repo transactionUpdater) TransactionRunUpdater {
return dbTransactionUpdater{repo}
}

func (u dbTransactionUpdater) Update(ctx context.Context, run tests.TransactionRun) error {
func (u dbTransactionUpdater) Update(ctx context.Context, run transaction.TransactionRun) error {
return u.repo.UpdateRun(ctx, run)
}

Expand All @@ -57,7 +57,7 @@ func NewSubscriptionTransactionUpdater(manager *subscription.Manager) Transactio
return subscriptionTransactionUpdater{manager}
}

func (u subscriptionTransactionUpdater) Update(ctx context.Context, run tests.TransactionRun) error {
func (u subscriptionTransactionUpdater) Update(ctx context.Context, run transaction.TransactionRun) error {
u.manager.PublishUpdate(subscription.Message{
ResourceID: run.ResourceID(),
Type: "result_update",
Expand Down
34 changes: 17 additions & 17 deletions server/executor/transaction_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/kubeshop/tracetest/server/model"
"github.com/kubeshop/tracetest/server/pkg/maps"
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/tests"
"github.com/kubeshop/tracetest/server/transaction"
)

type TransactionRunner interface {
Run(context.Context, tests.Transaction, model.RunMetadata, environment.Environment) tests.TransactionRun
Run(context.Context, transaction.Transaction, model.RunMetadata, environment.Environment) transaction.TransactionRun
}

type PersistentTransactionRunner interface {
Expand All @@ -22,7 +22,7 @@ type PersistentTransactionRunner interface {

type transactionRunRepository interface {
transactionUpdater
CreateRun(context.Context, tests.TransactionRun) (tests.TransactionRun, error)
CreateRun(context.Context, transaction.TransactionRun) (transaction.TransactionRun, error)
}

func NewTransactionRunner(
Expand All @@ -48,8 +48,8 @@ func NewTransactionRunner(

type transactionRunJob struct {
ctx context.Context
transaction tests.Transaction
run tests.TransactionRun
transaction transaction.Transaction
run transaction.TransactionRun
}

type persistentTransactionRunner struct {
Expand All @@ -62,7 +62,7 @@ type persistentTransactionRunner struct {
exit chan bool
}

func (r persistentTransactionRunner) Run(ctx context.Context, transaction tests.Transaction, metadata model.RunMetadata, environment environment.Environment) tests.TransactionRun {
func (r persistentTransactionRunner) Run(ctx context.Context, transaction transaction.Transaction, metadata model.RunMetadata, environment environment.Environment) transaction.TransactionRun {
run := transaction.NewRun()
run.Metadata = metadata
run.Environment = environment
Expand Down Expand Up @@ -104,18 +104,18 @@ func (r persistentTransactionRunner) Start(workers int) {
}
}

func (r persistentTransactionRunner) runTransaction(ctx context.Context, transaction tests.Transaction, run tests.TransactionRun) error {
run.State = tests.TransactionRunStateExecuting
func (r persistentTransactionRunner) runTransaction(ctx context.Context, tran transaction.Transaction, run transaction.TransactionRun) error {
run.State = transaction.TransactionRunStateExecuting

var err error

for step, test := range transaction.Steps {
for step, test := range tran.Steps {
run, err = r.runTransactionStep(ctx, run, step, test)
if err != nil {
return fmt.Errorf("could not execute step %d of transaction %s: %w", step, run.TransactionID, err)
}

if run.State == tests.TransactionRunStateFailed {
if run.State == transaction.TransactionRunStateFailed {
break
}

Expand All @@ -126,18 +126,18 @@ func (r persistentTransactionRunner) runTransaction(ctx context.Context, transac
}
}

if run.State != tests.TransactionRunStateFailed {
run.State = tests.TransactionRunStateFinished
if run.State != transaction.TransactionRunStateFailed {
run.State = transaction.TransactionRunStateFinished
}

return r.updater.Update(ctx, run)
}

func (r persistentTransactionRunner) runTransactionStep(ctx context.Context, tr tests.TransactionRun, step int, test model.Test) (tests.TransactionRun, error) {
func (r persistentTransactionRunner) runTransactionStep(ctx context.Context, tr transaction.TransactionRun, step int, test model.Test) (transaction.TransactionRun, error) {
testRun := r.testRunner.Run(ctx, test, tr.Metadata, tr.Environment)
tr, err := r.updateStepRun(ctx, tr, step, testRun)
if err != nil {
return tests.TransactionRun{}, fmt.Errorf("could not update transaction run: %w", err)
return transaction.TransactionRun{}, fmt.Errorf("could not update transaction run: %w", err)
}

done := make(chan bool)
Expand All @@ -146,7 +146,7 @@ func (r persistentTransactionRunner) runTransactionStep(ctx context.Context, tr
func(m subscription.Message) error {
testRun := m.Content.(model.Run)
if testRun.LastError != nil {
tr.State = tests.TransactionRunStateFailed
tr.State = transaction.TransactionRunStateFailed
tr.LastError = testRun.LastError
}

Expand Down Expand Up @@ -175,15 +175,15 @@ func (r persistentTransactionRunner) runTransactionStep(ctx context.Context, tr
return tr, err
}

func (r persistentTransactionRunner) updateStepRun(ctx context.Context, tr tests.TransactionRun, step int, run model.Run) (tests.TransactionRun, error) {
func (r persistentTransactionRunner) updateStepRun(ctx context.Context, tr transaction.TransactionRun, step int, run model.Run) (transaction.TransactionRun, error) {
if len(tr.Steps) <= step {
tr.Steps = append(tr.Steps, model.Run{})
}

tr.Steps[step] = run
err := r.updater.Update(ctx, tr)
if err != nil {
return tests.TransactionRun{}, fmt.Errorf("could not update transaction run: %w", err)
return transaction.TransactionRun{}, fmt.Errorf("could not update transaction run: %w", err)
}

return tr, nil
Expand Down
29 changes: 15 additions & 14 deletions server/executor/transaction_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/kubeshop/tracetest/server/subscription"
"github.com/kubeshop/tracetest/server/testdb"
"github.com/kubeshop/tracetest/server/testmock"
"github.com/kubeshop/tracetest/server/tests"
"github.com/kubeshop/tracetest/server/transaction"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -68,8 +68,8 @@ func (r *fakeTestRunner) Run(ctx context.Context, test model.Test, metadata mode
func TestTransactionRunner(t *testing.T) {

t.Run("NoErrors", func(t *testing.T) {
runTransactionRunnerTest(t, false, func(t *testing.T, actual tests.TransactionRun) {
assert.Equal(t, tests.TransactionRunStateFinished, actual.State)
runTransactionRunnerTest(t, false, func(t *testing.T, actual transaction.TransactionRun) {
assert.Equal(t, transaction.TransactionRunStateFinished, actual.State)
assert.Len(t, actual.Steps, 2)
assert.Equal(t, actual.Steps[0].State, model.RunStateFinished)
assert.Equal(t, actual.Steps[1].State, model.RunStateFinished)
Expand All @@ -91,8 +91,8 @@ func TestTransactionRunner(t *testing.T) {
})

t.Run("WithErrors", func(t *testing.T) {
runTransactionRunnerTest(t, true, func(t *testing.T, actual tests.TransactionRun) {
assert.Equal(t, tests.TransactionRunStateFailed, actual.State)
runTransactionRunnerTest(t, true, func(t *testing.T, actual transaction.TransactionRun) {
assert.Equal(t, transaction.TransactionRunStateFailed, actual.State)
require.Len(t, actual.Steps, 1)
assert.Equal(t, model.RunStateTriggerFailed, actual.Steps[0].State)
})
Expand All @@ -107,7 +107,7 @@ func getDB() (model.Repository, *sql.DB) {
return db, rawDB
}

func runTransactionRunnerTest(t *testing.T, withErrors bool, assert func(t *testing.T, actual tests.TransactionRun)) {
func runTransactionRunnerTest(t *testing.T, withErrors bool, assert func(t *testing.T, actual transaction.TransactionRun)) {
ctx := context.Background()
db, rawDB := getDB()

Expand All @@ -127,14 +127,15 @@ func runTransactionRunnerTest(t *testing.T, withErrors bool, assert func(t *test
require.NoError(t, err)

testsRepo, _ := testdb.Postgres(testdb.WithDB(rawDB))
transactionsRepo := tests.NewTransactionsRepository(rawDB, testsRepo)
transaction, err := transactionsRepo.Create(ctx, tests.Transaction{
transactionsRepo := transaction.NewRepository(rawDB, testsRepo)
transactionRunRepo := transaction.NewRunRepository(rawDB, testsRepo)
tran, err := transactionsRepo.Create(ctx, transaction.Transaction{
Name: "transaction",
StepIDs: []id.ID{test1.ID, test2.ID},
})
require.NoError(t, err)

transaction, err = transactionsRepo.GetAugmented(context.TODO(), transaction.ID)
tran, err = transactionsRepo.GetAugmented(context.TODO(), tran.ID)
require.NoError(t, err)

metadata := model.RunMetadata{
Expand All @@ -154,17 +155,17 @@ func runTransactionRunnerTest(t *testing.T, withErrors bool, assert func(t *test
})
require.NoError(t, err)

runner := executor.NewTransactionRunner(testRunner, db, transactionsRepo, subscriptionManager)
runner := executor.NewTransactionRunner(testRunner, db, transactionRunRepo, subscriptionManager)
runner.Start(1)

ctxWithTimeout, cancel := context.WithTimeout(ctx, 1*time.Second)
defer cancel()

transactionRun := runner.Run(ctxWithTimeout, transaction, metadata, env)
transactionRun := runner.Run(ctxWithTimeout, tran, metadata, env)

done := make(chan tests.TransactionRun, 1)
done := make(chan transaction.TransactionRun, 1)
sf := subscription.NewSubscriberFunction(func(m subscription.Message) error {
tr := m.Content.(tests.TransactionRun)
tr := m.Content.(transaction.TransactionRun)
if tr.State.IsFinal() {
done <- tr
}
Expand All @@ -173,7 +174,7 @@ func runTransactionRunnerTest(t *testing.T, withErrors bool, assert func(t *test
})
subscriptionManager.Subscribe(transactionRun.ResourceID(), sf)

var finalRun tests.TransactionRun
var finalRun transaction.TransactionRun
select {
case finalRun = <-done:
subscriptionManager.Unsubscribe(transactionRun.ResourceID(), sf.ID()) //cleanup to avoid race conditions
Expand Down
Loading