Skip to content

Commit

Permalink
feat: move snapshot to the tooling folder and add db package
Browse files Browse the repository at this point in the history
  • Loading branch information
renan061 committed Aug 30, 2024
1 parent c08c88a commit ce710f2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/rollupsmachine/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
77 changes: 77 additions & 0 deletions test/tooling/db/db.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ce710f2

Please sign in to comment.