github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/localdb/postgres/postgres_test.go (about)

     1  //go:build integration
     2  
     3  package postgres
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"strconv"
     9  	"testing"
    10  	"time"
    11  
    12  	dockertypes "github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/api/types/container"
    14  	"github.com/docker/go-connections/nat"
    15  	"github.com/filecoin-project/bacalhau/pkg/docker"
    16  	"github.com/filecoin-project/bacalhau/pkg/localdb/shared"
    17  	_ "github.com/filecoin-project/bacalhau/pkg/logger"
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  	"github.com/stretchr/testify/suite"
    21  )
    22  
    23  func TestPostgresSuite(t *testing.T) {
    24  	docker.MustHaveDocker(t)
    25  
    26  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
    27  	defer cancel()
    28  
    29  	client, err := docker.NewDockerClient()
    30  	require.NoError(t, err)
    31  	t.Cleanup(func() {
    32  		assert.NoError(t, client.Close())
    33  	})
    34  
    35  	require.NoError(t, client.PullImage(ctx, "postgres"))
    36  	c, err := client.ContainerCreate(ctx, &container.Config{
    37  		Image:        "postgres",
    38  		ExposedPorts: map[nat.Port]struct{}{},
    39  		Env:          []string{"POSTGRES_DB=postgres", "POSTGRES_USER=postgres", "POSTGRES_PASSWORD=postgres"},
    40  	}, &container.HostConfig{
    41  		PortBindings: map[nat.Port][]nat.PortBinding{
    42  			"5432/tcp": {{}},
    43  		},
    44  	}, nil, nil, fmt.Sprintf("postgres-%s", t.Name()))
    45  	require.NoError(t, err)
    46  
    47  	t.Cleanup(func() {
    48  		assert.NoError(t, client.RemoveContainer(context.Background(), c.ID))
    49  	})
    50  
    51  	require.NoError(t, client.ContainerStart(ctx, c.ID, dockertypes.ContainerStartOptions{}))
    52  
    53  	var status dockertypes.ContainerJSON
    54  	for {
    55  		status, err = client.ContainerInspect(ctx, c.ID)
    56  		require.NoError(t, err)
    57  
    58  		if status.State.Status == "running" {
    59  			break
    60  		}
    61  		time.Sleep(1 * time.Second)
    62  	}
    63  
    64  	port, err := strconv.Atoi(status.NetworkSettings.Ports["5432/tcp"][0].HostPort)
    65  	require.NoError(t, err)
    66  
    67  	var datastore *shared.GenericSQLDatastore
    68  	testingSuite := new(shared.GenericSQLSuite)
    69  	testingSuite.SetupHandler = func() *shared.GenericSQLDatastore {
    70  		if datastore == nil {
    71  			for {
    72  				datastore, err = NewPostgresDatastore(
    73  					"localhost",
    74  					port,
    75  					"postgres",
    76  					"postgres",
    77  					"postgres",
    78  					true,
    79  				)
    80  				if err != nil {
    81  					time.Sleep(1 * time.Second)
    82  				} else {
    83  					break
    84  				}
    85  			}
    86  		} else {
    87  			err := datastore.MigrateDown()
    88  			require.NoError(t, err)
    89  			err = datastore.MigrateUp()
    90  			require.NoError(t, err)
    91  		}
    92  		return datastore
    93  	}
    94  
    95  	suite.Run(t, testingSuite)
    96  }