Skip to content

Commit

Permalink
fix: inspect storage container before seeding buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
sweatybridge committed Oct 7, 2024
1 parent 333a2ca commit 9365527
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
11 changes: 7 additions & 4 deletions internal/db/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/errdefs"
Expand Down Expand Up @@ -54,9 +55,11 @@ func Run(ctx context.Context, version string, config pgconn.Config, fsys afero.F
return err
}
// Seed objects from supabase/buckets directory
if utils.Config.Storage.Enabled {
if err := start.WaitForHealthyService(ctx, 30*time.Second, utils.StorageId); err != nil {
return err
if resp, err := utils.Docker.ContainerInspect(ctx, utils.StorageId); err == nil {
if resp.State.Health == nil || resp.State.Health.Status != types.Healthy {
if err := start.WaitForHealthyService(ctx, 30*time.Second, utils.StorageId); err != nil {
return err
}
}
if err := buckets.Run(ctx, "", false, fsys); err != nil {
return err
Expand Down Expand Up @@ -212,7 +215,7 @@ func restartServices(ctx context.Context) error {
services := listServicesToRestart()
result := utils.WaitAll(services, func(id string) error {
if err := utils.Docker.ContainerRestart(ctx, id, container.StopOptions{}); err != nil && !errdefs.IsNotFound(err) {
return errors.Errorf("Failed to restart %s: %w", id, err)
return errors.Errorf("failed to restart %s: %w", id, err)
}
return nil
})
Expand Down
74 changes: 69 additions & 5 deletions internal/db/reset/reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/supabase/cli/internal/utils"
"github.com/supabase/cli/pkg/migration"
"github.com/supabase/cli/pkg/pgtest"
"github.com/supabase/cli/pkg/storage"
)

func TestResetCommand(t *testing.T) {
Expand All @@ -38,6 +39,69 @@ func TestResetCommand(t *testing.T) {
Database: "postgres",
}

t.Run("seeds storage after reset", func(t *testing.T) {
utils.DbId = "test-reset"
utils.ConfigId = "test-config"
utils.Config.Db.MajorVersion = 15
// Setup in-memory fs
fsys := afero.NewMemMapFs()
// Setup mock docker
require.NoError(t, apitest.MockDocker(utils.Docker))
defer gock.OffAll()
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId).
Reply(http.StatusOK).
JSON(types.ContainerJSON{})
gock.New(utils.Docker.DaemonHost()).
Delete("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId).
Reply(http.StatusOK)
gock.New(utils.Docker.DaemonHost()).
Delete("/v" + utils.Docker.ClientVersion() + "/volumes/" + utils.DbId).
Reply(http.StatusOK)
apitest.MockDockerStart(utils.Docker, utils.GetRegistryImageUrl(utils.Config.Db.Image), utils.DbId)
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.DbId + "/json").
Reply(http.StatusOK).
JSON(types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Running: true,
Health: &types.Health{Status: types.Healthy},
},
}})
// Setup mock postgres
conn := pgtest.NewConn()
defer conn.Close(t)
// Restarts services
utils.StorageId = "test-storage"
utils.GotrueId = "test-auth"
utils.RealtimeId = "test-realtime"
utils.PoolerId = "test-pooler"
for _, container := range listServicesToRestart() {
gock.New(utils.Docker.DaemonHost()).
Post("/v" + utils.Docker.ClientVersion() + "/containers/" + container + "/restart").
Reply(http.StatusOK)
}
// Seeds storage
gock.New(utils.Docker.DaemonHost()).
Get("/v" + utils.Docker.ClientVersion() + "/containers/" + utils.StorageId + "/json").
Reply(http.StatusOK).
JSON(types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Running: true,
Health: &types.Health{Status: types.Healthy},
},
}})
gock.New(utils.Config.Api.ExternalUrl).
Get("/storage/v1/bucket").
Reply(http.StatusOK).
JSON([]storage.BucketResponse{})
// Run test
err := Run(context.Background(), "", dbConfig, fsys, conn.Intercept)
// Check error
assert.NoError(t, err)
assert.Empty(t, apitest.ListUnmatchedRequests())
})

t.Run("throws error on context canceled", func(t *testing.T) {
// Setup in-memory fs
fsys := afero.NewMemMapFs()
Expand Down Expand Up @@ -225,7 +289,7 @@ func TestRestartDatabase(t *testing.T) {
JSON(types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Running: true,
Health: &types.Health{Status: "healthy"},
Health: &types.Health{Status: types.Healthy},
},
}})
// Restarts services
Expand Down Expand Up @@ -260,7 +324,7 @@ func TestRestartDatabase(t *testing.T) {
JSON(types.ContainerJSON{ContainerJSONBase: &types.ContainerJSONBase{
State: &types.ContainerState{
Running: true,
Health: &types.Health{Status: "healthy"},
Health: &types.Health{Status: types.Healthy},
},
}})
// Restarts services
Expand All @@ -279,9 +343,9 @@ func TestRestartDatabase(t *testing.T) {
// Run test
err := RestartDatabase(context.Background(), io.Discard)
// Check error
assert.ErrorContains(t, err, "Failed to restart "+utils.StorageId)
assert.ErrorContains(t, err, "Failed to restart "+utils.GotrueId)
assert.ErrorContains(t, err, "Failed to restart "+utils.RealtimeId)
assert.ErrorContains(t, err, "failed to restart "+utils.StorageId)
assert.ErrorContains(t, err, "failed to restart "+utils.GotrueId)
assert.ErrorContains(t, err, "failed to restart "+utils.RealtimeId)
assert.Empty(t, apitest.ListUnmatchedRequests())
})

Expand Down

0 comments on commit 9365527

Please sign in to comment.