From 37328f2de1b1418e2366244eb15b9f24eec82ba8 Mon Sep 17 00:00:00 2001 From: Renan Santos Date: Fri, 30 Aug 2024 10:19:33 -0300 Subject: [PATCH] feat: move snapshot to the tooling folder and add db package --- pkg/rollupsmachine/machine_test.go | 2 +- test/tooling/db/db.go | 77 +++++++++++++++++++++++++ test/{ => tooling}/snapshot/snapshot.go | 6 ++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 test/tooling/db/db.go rename test/{ => tooling}/snapshot/snapshot.go (96%) diff --git a/pkg/rollupsmachine/machine_test.go b/pkg/rollupsmachine/machine_test.go index d25b7edc6..c430b51f8 100644 --- a/pkg/rollupsmachine/machine_test.go +++ b/pkg/rollupsmachine/machine_test.go @@ -13,7 +13,7 @@ import ( "github.com/cartesi/rollups-node/pkg/emulator" "github.com/cartesi/rollups-node/pkg/rollupsmachine/cartesimachine" - "github.com/cartesi/rollups-node/test/snapshot" + "github.com/cartesi/rollups-node/test/tooling/snapshot" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" diff --git a/test/tooling/db/db.go b/test/tooling/db/db.go new file mode 100644 index 000000000..61123d7c6 --- /dev/null +++ b/test/tooling/db/db.go @@ -0,0 +1,77 @@ +// (c) Cartesi and individual authors (see AUTHORS) +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) + +package db + +import ( + "context" + "os" + "time" + + "github.com/cartesi/rollups-node/internal/repository/schema" + "github.com/testcontainers/testcontainers-go" + "github.com/testcontainers/testcontainers-go/modules/postgres" + "github.com/testcontainers/testcontainers-go/wait" +) + +const ( + postgresImage = "postgres:16-alpine" + postgresDatabase = "cartesinode" + postgresUsername = "admin" + postgresPassword = "password" +) + +func Setup(ctx context.Context) (string, error) { + endpoint, ok := os.LookupEnv("TESTS_POSTGRES_ENDPOINT") + if !ok { + container, err := SetupContainer(ctx) + if err != nil { + return "", err + } + + endpoint, err = container.ConnectionString(ctx, "sslmode=disable") + if err != nil { + return "", err + } + } + + err := SetupSchema(endpoint) + if err != nil { + return "", err + } + + return endpoint, nil +} + +func SetupContainer(ctx context.Context) (*postgres.PostgresContainer, error) { + log := "database system is ready to accept connections" + occurrences := 2 //nolint: mnd + timeout := 10 * time.Second //nolint: mnd + strategy := wait.ForLog(log).WithOccurrence(occurrences).WithStartupTimeout(timeout) + return postgres.Run(ctx, + postgresImage, + postgres.WithDatabase(postgresDatabase), + postgres.WithUsername(postgresUsername), + postgres.WithPassword(postgresPassword), + testcontainers.WithWaitStrategy(strategy)) +} + +func SetupSchema(endpoint string) error { + schema, err := schema.New(endpoint) + if err != nil { + return err + } + defer schema.Close() + + err = schema.Downgrade() + if err != nil { + return err + } + + err = schema.Upgrade() + if err != nil { + return err + } + + return nil +} diff --git a/test/snapshot/snapshot.go b/test/tooling/snapshot/snapshot.go similarity index 96% rename from test/snapshot/snapshot.go rename to test/tooling/snapshot/snapshot.go index 6b9a75c2b..6cba9fe3d 100644 --- a/test/snapshot/snapshot.go +++ b/test/tooling/snapshot/snapshot.go @@ -22,6 +22,12 @@ const ramLength = 64 << 20 // It can be redefined in case the files are not in the default folder. var ImagesPath = "/usr/share/cartesi-machine/images/" +func init() { + if value, ok := os.LookupEnv("TESTS_IMAGES_PATH"); ok { + ImagesPath = value + } +} + type Snapshot struct { id string // an unique id used to avoid name clashing temp string // path to the temporary directory containing snapshot relevant files