|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/e2b-dev/infra/packages/db/pkg/testutils" |
| 11 | +) |
| 12 | + |
| 13 | +// Inserting into env_build_assignments fills env_builds.env_id and team_id |
| 14 | +// from the first assignment, only while they are NULL, in a single UPDATE. |
| 15 | +func TestBuildAssignmentBackfillsBothColumnsInOneUpdate(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + db := testutils.SetupDatabase(t) |
| 19 | + ctx := t.Context() |
| 20 | + sqlDB, err := sql.Open("pgx", db.ConnStr()) |
| 21 | + require.NoError(t, err) |
| 22 | + t.Cleanup(func() { require.NoError(t, sqlDB.Close()) }) |
| 23 | + |
| 24 | + team, otherTeam := testutils.CreateTestTeam(t, db), testutils.CreateTestTeam(t, db) |
| 25 | + template := testutils.CreateTestTemplate(t, db, team) |
| 26 | + otherTemplate := testutils.CreateTestTemplate(t, db, otherTeam) |
| 27 | + |
| 28 | + insertBuild := func(t *testing.T, envID *string, teamID *uuid.UUID) uuid.UUID { |
| 29 | + t.Helper() |
| 30 | + id := uuid.New() |
| 31 | + _, err := sqlDB.ExecContext(ctx, `INSERT INTO public.env_builds |
| 32 | + (id, env_id, team_id, status, vcpu, ram_mb, free_disk_size_mb, kernel_version, firecracker_version, cluster_node_id, created_at, updated_at) |
| 33 | + VALUES ($1, $2, $3, 'success', 2, 2048, 512, '6.1.0', '1.4.0', 'test-node', NOW(), NOW())`, id, envID, teamID) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + return id |
| 37 | + } |
| 38 | + // assign inserts one assignment and returns how many env_builds row |
| 39 | + // versions the insert wrote. The xact counters also carry this backend's |
| 40 | + // unflushed counts from earlier transactions, so take the difference around |
| 41 | + // the insert. |
| 42 | + assign := func(t *testing.T, envID string, buildID uuid.UUID, tag string) int64 { |
| 43 | + t.Helper() |
| 44 | + tx, err := sqlDB.BeginTx(ctx, nil) |
| 45 | + require.NoError(t, err) |
| 46 | + defer func() { _ = tx.Rollback() }() |
| 47 | + updates := func() int64 { |
| 48 | + var n int64 |
| 49 | + require.NoError(t, tx.QueryRowContext(ctx, |
| 50 | + `SELECT coalesce((SELECT n_tup_upd FROM pg_stat_xact_user_tables WHERE relid = 'public.env_builds'::regclass), 0)`).Scan(&n)) |
| 51 | + |
| 52 | + return n |
| 53 | + } |
| 54 | + before := updates() |
| 55 | + _, err = tx.ExecContext(ctx, `INSERT INTO public.env_build_assignments (env_id, build_id, tag, source, created_at) |
| 56 | + VALUES ($1, $2, $3, 'app', NOW())`, envID, buildID, tag) |
| 57 | + require.NoError(t, err) |
| 58 | + written := updates() - before |
| 59 | + require.NoError(t, tx.Commit()) |
| 60 | + |
| 61 | + return written |
| 62 | + } |
| 63 | + read := func(t *testing.T, buildID uuid.UUID) (*string, *uuid.UUID) { |
| 64 | + t.Helper() |
| 65 | + var envID *string |
| 66 | + var teamID *uuid.UUID |
| 67 | + require.NoError(t, sqlDB.QueryRowContext(ctx, `SELECT env_id, team_id FROM public.env_builds WHERE id = $1`, buildID).Scan(&envID, &teamID)) |
| 68 | + |
| 69 | + return envID, teamID |
| 70 | + } |
| 71 | + |
| 72 | + preset := "preset-env" |
| 73 | + fresh := insertBuild(t, nil, nil) |
| 74 | + envOnly := insertBuild(t, &preset, nil) |
| 75 | + both := insertBuild(t, &preset, &otherTeam) |
| 76 | + |
| 77 | + for _, tc := range []struct { |
| 78 | + name string |
| 79 | + envID string |
| 80 | + buildID uuid.UUID |
| 81 | + tag string |
| 82 | + wantUpdates int64 |
| 83 | + wantEnv string |
| 84 | + wantTeam uuid.UUID |
| 85 | + }{ |
| 86 | + {"fills both columns", template, fresh, "default", 1, template, team}, |
| 87 | + {"keeps a set env_id and fills team_id", template, envOnly, "default", 1, preset, team}, |
| 88 | + {"leaves both set columns alone", template, both, "default", 0, preset, otherTeam}, |
| 89 | + {"first assignment wins", otherTemplate, fresh, "other", 0, template, team}, |
| 90 | + } { |
| 91 | + require.Equal(t, tc.wantUpdates, assign(t, tc.envID, tc.buildID, tc.tag), tc.name) |
| 92 | + envID, teamID := read(t, tc.buildID) |
| 93 | + require.NotNil(t, envID, tc.name) |
| 94 | + require.NotNil(t, teamID, tc.name) |
| 95 | + require.Equal(t, tc.wantEnv, *envID, tc.name) |
| 96 | + require.Equal(t, tc.wantTeam, *teamID, tc.name) |
| 97 | + } |
| 98 | +} |
0 commit comments