github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/cmd/spicedb/migrate_integration_test.go (about)

     1  //go:build docker && image
     2  // +build docker,image
     3  
     4  package main
     5  
     6  import (
     7  	"bytes"
     8  	"context"
     9  	"fmt"
    10  	"slices"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/google/uuid"
    15  	"github.com/ory/dockertest/v3"
    16  	"github.com/ory/dockertest/v3/docker"
    17  	"github.com/stretchr/testify/require"
    18  
    19  	testdatastore "github.com/authzed/spicedb/internal/testserver/datastore"
    20  	"github.com/authzed/spicedb/pkg/datastore"
    21  )
    22  
    23  var toSkip = []string{"memory"}
    24  
    25  func TestMigrate(t *testing.T) {
    26  	bridgeNetworkName := fmt.Sprintf("bridge-%s", uuid.New().String())
    27  
    28  	pool, err := dockertest.NewPool("")
    29  	require.NoError(t, err)
    30  
    31  	// Create a bridge network for testing.
    32  	network, err := pool.Client.CreateNetwork(docker.CreateNetworkOptions{
    33  		Name: bridgeNetworkName,
    34  	})
    35  	require.NoError(t, err)
    36  	t.Cleanup(func() {
    37  		pool.Client.RemoveNetwork(network.ID)
    38  	})
    39  
    40  	for _, engineKey := range datastore.Engines {
    41  		if slices.Contains(toSkip, engineKey) {
    42  			continue
    43  		}
    44  
    45  		t.Run(engineKey, func(t *testing.T) {
    46  			engineKey := engineKey
    47  			t.Parallel()
    48  			r := testdatastore.RunDatastoreEngineWithBridge(t, engineKey, bridgeNetworkName)
    49  			db := r.NewDatabase(t)
    50  
    51  			envVars := []string{}
    52  			if wev, ok := r.(testdatastore.RunningEngineForTestWithEnvVars); ok {
    53  				envVars = wev.ExternalEnvVars()
    54  			}
    55  
    56  			// Run the migrate command and wait for it to complete.
    57  			resource, err := pool.RunWithOptions(&dockertest.RunOptions{
    58  				Repository: "authzed/spicedb",
    59  				Tag:        "ci",
    60  				Cmd:        []string{"migrate", "head", "--datastore-engine", engineKey, "--datastore-conn-uri", db},
    61  				NetworkID:  bridgeNetworkName,
    62  				Env:        envVars,
    63  			}, func(config *docker.HostConfig) {
    64  				config.RestartPolicy = docker.RestartPolicy{
    65  					Name: "no",
    66  				}
    67  			})
    68  			require.NoError(t, err)
    69  
    70  			waitCtx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    71  			defer cancel()
    72  
    73  			// Ensure the command completed successfully.
    74  			status, err := pool.Client.WaitContainerWithContext(resource.Container.ID, waitCtx)
    75  			require.NoError(t, err)
    76  
    77  			if status != 0 {
    78  				stream := new(bytes.Buffer)
    79  
    80  				lerr := pool.Client.Logs(docker.LogsOptions{
    81  					Context:      waitCtx,
    82  					OutputStream: stream,
    83  					ErrorStream:  stream,
    84  					Stdout:       true,
    85  					Stderr:       true,
    86  					Container:    resource.Container.ID,
    87  				})
    88  				require.NoError(t, lerr)
    89  
    90  				require.Fail(t, "Got non-zero exit code", stream.String())
    91  			}
    92  
    93  			t.Cleanup(func() {
    94  				// When you're done, kill and remove the container
    95  				pool.Purge(resource)
    96  			})
    97  		})
    98  	}
    99  }