Skip to content

Commit

Permalink
Updating tests to isolate database resources and connections per modu…
Browse files Browse the repository at this point in the history
…le (#2409)

* Isolating database resources per module

* Removing cleanup step to avoid overengineering
  • Loading branch information
danielbdias committed Apr 20, 2023
1 parent bbadc1a commit edd9a5e
Show file tree
Hide file tree
Showing 20 changed files with 267 additions and 123 deletions.
2 changes: 1 addition & 1 deletion server/Makefile
Expand Up @@ -19,7 +19,7 @@ init-submodule:
git submodule update

test: # run go tests for this application
go test -timeout 90s -coverprofile=coverage.out ./...
go test -timeout 150s -coverprofile=coverage.out ./...

vet: ## run vet tool to analyze the code for suspicious, abnormal, or useless code
go vet -structtag=false ./...
Expand Down
15 changes: 5 additions & 10 deletions server/config/configresource/config_resource_test.go
Expand Up @@ -38,9 +38,8 @@ func TestPublishing(t *testing.T) {

publisher.On("Publish", configresource.ResourceID, updated)

db := testmock.MustGetRawTestingDatabase()
repo := configresource.NewRepository(
testmock.MustCreateRandomMigratedDatabase(db),
testmock.CreateMigratedDatabase(),
configresource.WithPublisher(publisher),
)

Expand All @@ -52,13 +51,12 @@ func TestPublishing(t *testing.T) {
}

func TestIsAnalyticsEnabled(t *testing.T) {
db := testmock.MustGetRawTestingDatabase()
t.Run("DefaultValues", func(t *testing.T) {
restore := cleanEnv()
defer restore()

repo := configresource.NewRepository(
testmock.MustCreateRandomMigratedDatabase(db),
testmock.CreateMigratedDatabase(),
)

cfg := repo.Current(context.TODO())
Expand All @@ -70,7 +68,7 @@ func TestIsAnalyticsEnabled(t *testing.T) {
restore := cleanEnv()
defer restore()
repo := configresource.NewRepository(
testmock.MustCreateRandomMigratedDatabase(db),
testmock.CreateMigratedDatabase(),
)
repo.Update(context.TODO(), configresource.Config{
AnalyticsEnabled: false,
Expand All @@ -84,7 +82,7 @@ func TestIsAnalyticsEnabled(t *testing.T) {
restore := cleanEnv()
defer restore()
repo := configresource.NewRepository(
testmock.MustCreateRandomMigratedDatabase(db),
testmock.CreateMigratedDatabase(),
)
repo.Update(context.TODO(), configresource.Config{
AnalyticsEnabled: true,
Expand All @@ -99,14 +97,11 @@ func TestIsAnalyticsEnabled(t *testing.T) {
}

func TestConfigResource(t *testing.T) {

db := testmock.MustGetRawTestingDatabase()

rmtests.TestResourceType(t, rmtests.ResourceTypeTest{
ResourceTypeSingular: configresource.ResourceName,
ResourceTypePlural: configresource.ResourceNamePlural,
RegisterManagerFn: func(router *mux.Router) resourcemanager.Manager {
db := testmock.MustCreateRandomMigratedDatabase(db)
db := testmock.CreateMigratedDatabase()
configRepo := configresource.NewRepository(db)

manager := resourcemanager.New[configresource.Config](
Expand Down
18 changes: 18 additions & 0 deletions server/config/configresource/main_test.go
@@ -0,0 +1,18 @@
package configresource_test

import (
"os"
"testing"

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

func TestMain(m *testing.M) {
testmock.StartTestEnvironment()

exitVal := m.Run()

testmock.StopTestEnvironment()

os.Exit(exitVal)
}
6 changes: 2 additions & 4 deletions server/config/demoresource/demo_resource_test.go
Expand Up @@ -13,7 +13,6 @@ import (
)

func TestPokeshopDemoResource(t *testing.T) {
db := testmock.MustGetRawTestingDatabase()
sampleDemo := demoresource.Demo{
ID: "1",
Name: "dev",
Expand Down Expand Up @@ -51,7 +50,7 @@ func TestPokeshopDemoResource(t *testing.T) {
ResourceTypeSingular: demoresource.ResourceName,
ResourceTypePlural: demoresource.ResourceNamePlural,
RegisterManagerFn: func(router *mux.Router) resourcemanager.Manager {
db := testmock.MustCreateRandomMigratedDatabase(db)
db := testmock.CreateMigratedDatabase()
demoRepository := demoresource.NewRepository(db)

manager := resourcemanager.New[demoresource.Demo](
Expand Down Expand Up @@ -108,7 +107,6 @@ func TestPokeshopDemoResource(t *testing.T) {
}

func TestOpenTelemetryStoreDemoResource(t *testing.T) {
db := testmock.MustGetRawTestingDatabase()
sampleDemo := demoresource.Demo{
ID: "1",
Name: "dev",
Expand Down Expand Up @@ -152,7 +150,7 @@ func TestOpenTelemetryStoreDemoResource(t *testing.T) {
ResourceTypeSingular: demoresource.ResourceName,
ResourceTypePlural: demoresource.ResourceNamePlural,
RegisterManagerFn: func(router *mux.Router) resourcemanager.Manager {
db := testmock.MustCreateRandomMigratedDatabase(db)
db := testmock.CreateMigratedDatabase()
demoRepository := demoresource.NewRepository(db)

manager := resourcemanager.New[demoresource.Demo](
Expand Down
18 changes: 18 additions & 0 deletions server/config/demoresource/main_test.go
@@ -0,0 +1,18 @@
package demoresource_test

import (
"os"
"testing"

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

func TestMain(m *testing.M) {
testmock.StartTestEnvironment()

exitVal := m.Run()

testmock.StopTestEnvironment()

os.Exit(exitVal)
}
18 changes: 18 additions & 0 deletions server/executor/main_test.go
@@ -0,0 +1,18 @@
package executor_test

import (
"os"
"testing"

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

func TestMain(m *testing.M) {
testmock.StartTestEnvironment()

exitVal := m.Run()

testmock.StopTestEnvironment()

os.Exit(exitVal)
}
18 changes: 18 additions & 0 deletions server/executor/pollingprofile/main_test.go
@@ -0,0 +1,18 @@
package pollingprofile_test

import (
"os"
"testing"

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

func TestMain(m *testing.M) {
testmock.StartTestEnvironment()

exitVal := m.Run()

testmock.StopTestEnvironment()

os.Exit(exitVal)
}
26 changes: 1 addition & 25 deletions server/executor/pollingprofile/polling_profile_resource_test.go
Expand Up @@ -12,23 +12,11 @@ import (
)

func TestPollingProfileResource(t *testing.T) {
db := testmock.MustGetRawTestingDatabase()
// sampleProfile := pollingprofile.PollingProfile{
// ID: "1",
// Name: "test",
// Default: true,
// Strategy: pollingprofile.Periodic,
// Periodic: &pollingprofile.PeriodicPollingConfig{
// RetryDelay: "10s",
// Timeout: "30m",
// },
// }

rmtests.TestResourceType(t, rmtests.ResourceTypeTest{
ResourceTypeSingular: pollingprofile.ResourceName,
ResourceTypePlural: pollingprofile.ResourceNamePlural,
RegisterManagerFn: func(router *mux.Router) resourcemanager.Manager {
db := testmock.MustCreateRandomMigratedDatabase(db)
db := testmock.CreateMigratedDatabase()
pollingProfileRepo := pollingprofile.NewRepository(db)

manager := resourcemanager.New[pollingprofile.PollingProfile](
Expand All @@ -42,17 +30,6 @@ func TestPollingProfileResource(t *testing.T) {

return manager
},
// Prepare: func(t *testing.T, op rmtests.Operation, manager resourcemanager.Manager) {
// pollingProfileRepo := manager.Handler().(*pollingprofile.Repository)
// switch op {
// case rmtests.OperationGetSuccess,
// pollingProfileRepo.Update(context.TODO(), sampleProfile)
// case rmtests.OperationListPaginatedSuccess:
// pollingProfileRepo.Create(context.TODO(), sampleProfile)
// pollingProfileRepo.Create(context.TODO(), secondSampleProfile)
// pollingProfileRepo.Create(context.TODO(), thirdSampleProfile)
// }
// },
SampleJSON: `{
"type": "PollingProfile",
"spec": {
Expand Down Expand Up @@ -80,7 +57,6 @@ func TestPollingProfileResource(t *testing.T) {
}
}`,
},
// TODO: remove this when we support multiple profiles
rmtests.ExcludeOperations(
rmtests.OperationGetNotFound,
rmtests.OperationUpdateNotFound,
Expand Down
7 changes: 2 additions & 5 deletions server/executor/transaction_runner_test.go
Expand Up @@ -96,13 +96,10 @@ func TestTransactionRunner(t *testing.T) {
}

func getDB() (model.Repository, func()) {
db, err := testmock.GetTestingDatabase()
if err != nil {
panic(err)
}
db := testmock.GetTestingDatabase()

clean := func() {
err = db.Drop()
err := db.Drop()
if err != nil {
panic(err)
}
Expand Down
18 changes: 18 additions & 0 deletions server/integration/main_test.go
@@ -0,0 +1,18 @@
package integration_test

import (
"os"
"testing"

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

func TestMain(m *testing.M) {
testmock.StartTestEnvironment()

exitVal := m.Run()

testmock.StopTestEnvironment()

os.Exit(exitVal)
}
18 changes: 18 additions & 0 deletions server/migrations/main_test.go
@@ -0,0 +1,18 @@
package migrations_test

import (
"os"
"testing"

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

func TestMain(m *testing.M) {
testmock.StartTestEnvironment()

exitVal := m.Run()

testmock.StopTestEnvironment()

os.Exit(exitVal)
}
7 changes: 3 additions & 4 deletions server/migrations/migrations_test.go
Expand Up @@ -17,16 +17,15 @@ import (
)

func TestMigrations(t *testing.T) {
db, err := testmock.GetRawTestingDatabase()
require.NoError(t, err)
db := testmock.GetRawTestingDatabase()

t.Run("applying migrations", func(t *testing.T) {
_, err = testdb.Postgres(testdb.WithDB(db))
_, err := testdb.Postgres(testdb.WithDB(db))
require.NoError(t, err, "postgres migrations up should not fail")
})

t.Run("rolling back migrations", func(t *testing.T) {
err = rollback(db)
err := rollback(db)
assert.NoError(t, err, "rollback should not fail")
})
}
Expand Down
18 changes: 18 additions & 0 deletions server/provisioning/main_test.go
@@ -0,0 +1,18 @@
package provisioning_test

import (
"os"
"testing"

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

func TestMain(m *testing.M) {
testmock.StartTestEnvironment()

exitVal := m.Run()

testmock.StopTestEnvironment()

os.Exit(exitVal)
}
4 changes: 2 additions & 2 deletions server/provisioning/provisioning_test.go
Expand Up @@ -30,7 +30,7 @@ func TestFromFile(t *testing.T) {
assert.ErrorIs(t, err, provisioning.ErrFileNotExists)
})

db := testmock.MustGetRawTestingDatabase()
db := testmock.GetRawTestingDatabase()

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
Expand All @@ -50,7 +50,7 @@ func TestFromFile(t *testing.T) {
}

func TestFromEnv(t *testing.T) {
db := testmock.MustGetRawTestingDatabase()
db := testmock.GetRawTestingDatabase()

t.Run("Empty", func(t *testing.T) {
provisioner := provisioning.New()
Expand Down
1 change: 0 additions & 1 deletion server/resourcemanager/testutil/test_resource.go
Expand Up @@ -110,7 +110,6 @@ func testOperation(t *testing.T, op operationTester, rt ResourceTypeTest) {
for _, ct := range contentTypeConverters {
t.Run(ct.name, func(t *testing.T) {
ct := ct
t.Parallel()

testOperationForContentType(t, op, ct, rt)
})
Expand Down
5 changes: 1 addition & 4 deletions server/testdb/data_stores_test.go
Expand Up @@ -174,14 +174,11 @@ func TestGetDataStores(t *testing.T) {
}

func TestDataStoreProvisioner(t *testing.T) {

db := testmock.MustGetRawTestingDatabase()

rmtests.TestResourceType(t, rmtests.ResourceTypeTest{
ResourceTypeSingular: testdb.DataStoreResourceName,
ResourceTypePlural: testdb.DataStoreResourceName,
RegisterManagerFn: func(router *mux.Router) resourcemanager.Manager {
db := testmock.MustCreateRandomMigratedDatabase(db)
db := testmock.CreateMigratedDatabase()
dsRepo, err := testdb.Postgres(testdb.WithDB(db))
require.NoError(t, err)

Expand Down
18 changes: 18 additions & 0 deletions server/testdb/main_test.go
@@ -0,0 +1,18 @@
package testdb_test

import (
"os"
"testing"

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

func TestMain(m *testing.M) {
testmock.StartTestEnvironment()

exitVal := m.Run()

testmock.StopTestEnvironment()

os.Exit(exitVal)
}

0 comments on commit edd9a5e

Please sign in to comment.