mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: remove dbmem (#18803)
Remove the in-memory database. Addresses #15109.
This commit is contained in:
Generated
-3
@@ -12305,9 +12305,6 @@ const docTemplate = `{
|
||||
"http_cookies": {
|
||||
"$ref": "#/definitions/codersdk.HTTPCookieConfig"
|
||||
},
|
||||
"in_memory_database": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"job_hang_detector_interval": {
|
||||
"type": "integer"
|
||||
},
|
||||
|
||||
Generated
-3
@@ -10989,9 +10989,6 @@
|
||||
"http_cookies": {
|
||||
"$ref": "#/definitions/codersdk.HTTPCookieConfig"
|
||||
},
|
||||
"in_memory_database": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"job_hang_detector_interval": {
|
||||
"type": "integer"
|
||||
},
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmem"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/coder/v2/coderd/rbac"
|
||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
@@ -202,7 +202,7 @@ func TestInsertCustomRoles(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
db := dbmem.New()
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
rec := &coderdtest.RecordingAuthorizer{
|
||||
Wrapped: rbac.NewAuthorizer(prometheus.NewRegistry()),
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ import (
|
||||
"cdr.dev/slog"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/database/db2sdk"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmem"
|
||||
"github.com/coder/coder/v2/coderd/notifications"
|
||||
"github.com/coder/coder/v2/coderd/rbac/policy"
|
||||
"github.com/coder/coder/v2/codersdk"
|
||||
@@ -3661,148 +3660,119 @@ func (s *MethodTestSuite) TestExtraMethods() {
|
||||
func (s *MethodTestSuite) TestTailnetFunctions() {
|
||||
s.Run("CleanTailnetCoordinators", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete)
|
||||
}))
|
||||
s.Run("CleanTailnetLostPeers", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete)
|
||||
}))
|
||||
s.Run("CleanTailnetTunnels", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete)
|
||||
}))
|
||||
s.Run("DeleteAllTailnetClientSubscriptions", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(database.DeleteAllTailnetClientSubscriptionsParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete)
|
||||
}))
|
||||
s.Run("DeleteAllTailnetTunnels", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(database.DeleteAllTailnetTunnelsParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete)
|
||||
}))
|
||||
s.Run("DeleteCoordinator", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(uuid.New()).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete)
|
||||
}))
|
||||
s.Run("DeleteTailnetAgent", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(database.DeleteTailnetAgentParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate).Errors(sql.ErrNoRows).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate).Errors(sql.ErrNoRows)
|
||||
}))
|
||||
s.Run("DeleteTailnetClient", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(database.DeleteTailnetClientParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).Errors(sql.ErrNoRows).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).Errors(sql.ErrNoRows)
|
||||
}))
|
||||
s.Run("DeleteTailnetClientSubscription", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(database.DeleteTailnetClientSubscriptionParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete)
|
||||
}))
|
||||
s.Run("DeleteTailnetPeer", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(database.DeleteTailnetPeerParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented).
|
||||
ErrorsWithPG(sql.ErrNoRows)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).Errors(sql.ErrNoRows)
|
||||
}))
|
||||
s.Run("DeleteTailnetTunnel", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(database.DeleteTailnetTunnelParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented).
|
||||
ErrorsWithPG(sql.ErrNoRows)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionDelete).Errors(sql.ErrNoRows)
|
||||
}))
|
||||
s.Run("GetAllTailnetAgents", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetTailnetAgents", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(uuid.New()).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetTailnetClientsForAgent", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(uuid.New()).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetTailnetPeers", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(uuid.New()).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetTailnetTunnelPeerBindings", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(uuid.New()).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetTailnetTunnelPeerIDs", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(uuid.New()).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetAllTailnetCoordinators", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetAllTailnetPeers", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetAllTailnetTunnels", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionRead)
|
||||
}))
|
||||
s.Run("UpsertTailnetAgent", s.Subtest(func(db database.Store, check *expects) {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
check.Args(database.UpsertTailnetAgentParams{Node: json.RawMessage("{}")}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate)
|
||||
}))
|
||||
s.Run("UpsertTailnetClient", s.Subtest(func(db database.Store, check *expects) {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
check.Args(database.UpsertTailnetClientParams{Node: json.RawMessage("{}")}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate)
|
||||
}))
|
||||
s.Run("UpsertTailnetClientSubscription", s.Subtest(func(db database.Store, check *expects) {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
check.Args(database.UpsertTailnetClientSubscriptionParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate)
|
||||
}))
|
||||
s.Run("UpsertTailnetCoordinator", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(uuid.New()).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate)
|
||||
}))
|
||||
s.Run("UpsertTailnetPeer", s.Subtest(func(db database.Store, check *expects) {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
check.Args(database.UpsertTailnetPeerParams{
|
||||
Status: database.TailnetStatusOk,
|
||||
}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionCreate).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionCreate)
|
||||
}))
|
||||
s.Run("UpsertTailnetTunnel", s.Subtest(func(db database.Store, check *expects) {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
check.Args(database.UpsertTailnetTunnelParams{}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionCreate).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionCreate)
|
||||
}))
|
||||
s.Run("UpdateTailnetPeerStatusByCoordinator", s.Subtest(func(db database.Store, check *expects) {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
check.Args(database.UpdateTailnetPeerStatusByCoordinatorParams{Status: database.TailnetStatusOk}).
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTailnetCoordinator, policy.ActionUpdate)
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -4787,21 +4757,18 @@ func (s *MethodTestSuite) TestNotifications() {
|
||||
dbtestutil.DisableForeignKeysAndTriggers(s.T(), db)
|
||||
user := dbgen.User(s.T(), db, database.User{})
|
||||
check.Args(user.ID).Asserts(rbac.ResourceNotificationTemplate, policy.ActionRead).
|
||||
ErrorsWithPG(sql.ErrNoRows).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
ErrorsWithPG(sql.ErrNoRows)
|
||||
}))
|
||||
s.Run("GetNotificationTemplatesByKind", s.Subtest(func(db database.Store, check *expects) {
|
||||
check.Args(database.NotificationTemplateKindSystem).
|
||||
Asserts().
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts()
|
||||
// TODO(dannyk): add support for other database.NotificationTemplateKind types once implemented.
|
||||
}))
|
||||
s.Run("UpdateNotificationTemplateMethodByID", s.Subtest(func(db database.Store, check *expects) {
|
||||
check.Args(database.UpdateNotificationTemplateMethodByIDParams{
|
||||
Method: database.NullNotificationMethod{NotificationMethod: database.NotificationMethodWebhook, Valid: true},
|
||||
ID: notifications.TemplateWorkspaceDormant,
|
||||
}).Asserts(rbac.ResourceNotificationTemplate, policy.ActionUpdate).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
}).Asserts(rbac.ResourceNotificationTemplate, policy.ActionUpdate)
|
||||
}))
|
||||
|
||||
// Notification preferences
|
||||
@@ -5115,8 +5082,7 @@ func (s *MethodTestSuite) TestPrebuilds() {
|
||||
rbac.ResourceWorkspace.WithOwner(user.ID.String()).InOrg(org.ID), policy.ActionCreate,
|
||||
template, policy.ActionRead,
|
||||
template, policy.ActionUse,
|
||||
).ErrorsWithInMemDB(dbmem.ErrUnimplemented).
|
||||
ErrorsWithPG(sql.ErrNoRows)
|
||||
).Errors(sql.ErrNoRows)
|
||||
}))
|
||||
s.Run("GetPrebuildMetrics", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
@@ -5130,29 +5096,24 @@ func (s *MethodTestSuite) TestPrebuilds() {
|
||||
}))
|
||||
s.Run("CountInProgressPrebuilds", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceWorkspace.All(), policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceWorkspace.All(), policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetPresetsAtFailureLimit", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(int64(0)).
|
||||
Asserts(rbac.ResourceTemplate.All(), policy.ActionViewInsights).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTemplate.All(), policy.ActionViewInsights)
|
||||
}))
|
||||
s.Run("GetPresetsBackoff", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args(time.Time{}).
|
||||
Asserts(rbac.ResourceTemplate.All(), policy.ActionViewInsights).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTemplate.All(), policy.ActionViewInsights)
|
||||
}))
|
||||
s.Run("GetRunningPrebuiltWorkspaces", s.Subtest(func(_ database.Store, check *expects) {
|
||||
check.Args().
|
||||
Asserts(rbac.ResourceWorkspace.All(), policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceWorkspace.All(), policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetTemplatePresetsWithPrebuilds", s.Subtest(func(db database.Store, check *expects) {
|
||||
user := dbgen.User(s.T(), db, database.User{})
|
||||
check.Args(uuid.NullUUID{UUID: user.ID, Valid: true}).
|
||||
Asserts(rbac.ResourceTemplate.All(), policy.ActionRead).
|
||||
ErrorsWithInMemDB(dbmem.ErrUnimplemented)
|
||||
Asserts(rbac.ResourceTemplate.All(), policy.ActionRead)
|
||||
}))
|
||||
s.Run("GetPresetByID", s.Subtest(func(db database.Store, check *expects) {
|
||||
org := dbgen.Organization(s.T(), db, database.Organization{})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,209 +0,0 @@
|
||||
package dbmem_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbgen"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmem"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
)
|
||||
|
||||
// test that transactions don't deadlock, and that we don't see intermediate state.
|
||||
func TestInTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
uut := dbmem.New()
|
||||
|
||||
inTx := make(chan any)
|
||||
queriesDone := make(chan any)
|
||||
queriesStarted := make(chan any)
|
||||
go func() {
|
||||
err := uut.InTx(func(tx database.Store) error {
|
||||
close(inTx)
|
||||
_, err := tx.InsertOrganization(context.Background(), database.InsertOrganizationParams{
|
||||
Name: "1",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
<-queriesStarted
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
_, err = tx.InsertOrganization(context.Background(), database.InsertOrganizationParams{
|
||||
Name: "2",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
return nil
|
||||
}, nil)
|
||||
assert.NoError(t, err)
|
||||
}()
|
||||
var nums []int
|
||||
go func() {
|
||||
<-inTx
|
||||
for i := 0; i < 20; i++ {
|
||||
orgs, err := uut.GetOrganizations(context.Background(), database.GetOrganizationsParams{})
|
||||
if err != nil {
|
||||
assert.ErrorIs(t, err, sql.ErrNoRows)
|
||||
}
|
||||
nums = append(nums, len(orgs))
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
close(queriesDone)
|
||||
}()
|
||||
close(queriesStarted)
|
||||
<-queriesDone
|
||||
// ensure we never saw 1 org, only 0 or 2.
|
||||
for i := 0; i < 20; i++ {
|
||||
assert.NotEqual(t, 1, nums[i])
|
||||
}
|
||||
}
|
||||
|
||||
// TestUserOrder ensures that the fake database returns users sorted by username.
|
||||
func TestUserOrder(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := dbmem.New()
|
||||
now := dbtime.Now()
|
||||
|
||||
usernames := []string{"b-user", "d-user", "a-user", "c-user", "e-user"}
|
||||
for _, username := range usernames {
|
||||
dbgen.User(t, db, database.User{Username: username, CreatedAt: now})
|
||||
}
|
||||
|
||||
users, err := db.GetUsers(context.Background(), database.GetUsersParams{})
|
||||
require.NoError(t, err)
|
||||
require.Lenf(t, users, len(usernames), "expected %d users", len(usernames))
|
||||
|
||||
sort.Strings(usernames)
|
||||
for i, user := range users {
|
||||
require.Equal(t, usernames[i], user.Username)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProxyByHostname(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
db := dbmem.New()
|
||||
|
||||
// Insert a bunch of different proxies.
|
||||
proxies := []struct {
|
||||
name string
|
||||
accessURL string
|
||||
wildcardHostname string
|
||||
}{
|
||||
{
|
||||
name: "one",
|
||||
accessURL: "https://one.coder.com",
|
||||
wildcardHostname: "*.wildcard.one.coder.com",
|
||||
},
|
||||
{
|
||||
name: "two",
|
||||
accessURL: "https://two.coder.com",
|
||||
wildcardHostname: "*--suffix.two.coder.com",
|
||||
},
|
||||
}
|
||||
for _, p := range proxies {
|
||||
dbgen.WorkspaceProxy(t, db, database.WorkspaceProxy{
|
||||
Name: p.name,
|
||||
Url: p.accessURL,
|
||||
WildcardHostname: p.wildcardHostname,
|
||||
})
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
testHostname string
|
||||
allowAccessURL bool
|
||||
allowWildcardHost bool
|
||||
matchProxyName string
|
||||
}{
|
||||
{
|
||||
name: "NoMatch",
|
||||
testHostname: "test.com",
|
||||
allowAccessURL: true,
|
||||
allowWildcardHost: true,
|
||||
matchProxyName: "",
|
||||
},
|
||||
{
|
||||
name: "MatchAccessURL",
|
||||
testHostname: "one.coder.com",
|
||||
allowAccessURL: true,
|
||||
allowWildcardHost: true,
|
||||
matchProxyName: "one",
|
||||
},
|
||||
{
|
||||
name: "MatchWildcard",
|
||||
testHostname: "something.wildcard.one.coder.com",
|
||||
allowAccessURL: true,
|
||||
allowWildcardHost: true,
|
||||
matchProxyName: "one",
|
||||
},
|
||||
{
|
||||
name: "MatchSuffix",
|
||||
testHostname: "something--suffix.two.coder.com",
|
||||
allowAccessURL: true,
|
||||
allowWildcardHost: true,
|
||||
matchProxyName: "two",
|
||||
},
|
||||
{
|
||||
name: "ValidateHostname/1",
|
||||
testHostname: ".*ne.coder.com",
|
||||
allowAccessURL: true,
|
||||
allowWildcardHost: true,
|
||||
matchProxyName: "",
|
||||
},
|
||||
{
|
||||
name: "ValidateHostname/2",
|
||||
testHostname: "https://one.coder.com",
|
||||
allowAccessURL: true,
|
||||
allowWildcardHost: true,
|
||||
matchProxyName: "",
|
||||
},
|
||||
{
|
||||
name: "ValidateHostname/3",
|
||||
testHostname: "one.coder.com:8080/hello",
|
||||
allowAccessURL: true,
|
||||
allowWildcardHost: true,
|
||||
matchProxyName: "",
|
||||
},
|
||||
{
|
||||
name: "IgnoreAccessURLMatch",
|
||||
testHostname: "one.coder.com",
|
||||
allowAccessURL: false,
|
||||
allowWildcardHost: true,
|
||||
matchProxyName: "",
|
||||
},
|
||||
{
|
||||
name: "IgnoreWildcardMatch",
|
||||
testHostname: "hi.wildcard.one.coder.com",
|
||||
allowAccessURL: true,
|
||||
allowWildcardHost: false,
|
||||
matchProxyName: "",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
proxy, err := db.GetWorkspaceProxyByHostname(context.Background(), database.GetWorkspaceProxyByHostnameParams{
|
||||
Hostname: c.testHostname,
|
||||
AllowAccessUrl: c.allowAccessURL,
|
||||
AllowWildcardHostname: c.allowWildcardHost,
|
||||
})
|
||||
if c.matchProxyName == "" {
|
||||
require.ErrorIs(t, err, sql.ErrNoRows)
|
||||
require.Empty(t, proxy)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, proxy)
|
||||
require.Equal(t, c.matchProxyName, proxy.Name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -20,14 +20,15 @@ import (
|
||||
|
||||
"cdr.dev/slog"
|
||||
"github.com/coder/coder/v2/coderd/database"
|
||||
"github.com/coder/coder/v2/coderd/database/dbmem"
|
||||
"github.com/coder/coder/v2/coderd/database/pubsub"
|
||||
"github.com/coder/coder/v2/testutil"
|
||||
)
|
||||
|
||||
// WillUsePostgres returns true if a call to NewDB() will return a real, postgres-backed Store and Pubsub.
|
||||
// TODO(hugodutka): since we removed the in-memory database, this is always true,
|
||||
// and we need to remove this function. https://github.com/coder/internal/issues/758
|
||||
func WillUsePostgres() bool {
|
||||
return os.Getenv("DB") != ""
|
||||
return true
|
||||
}
|
||||
|
||||
type options struct {
|
||||
@@ -109,52 +110,48 @@ func NewDB(t testing.TB, opts ...Option) (database.Store, pubsub.Pubsub) {
|
||||
|
||||
var db database.Store
|
||||
var ps pubsub.Pubsub
|
||||
if WillUsePostgres() {
|
||||
connectionURL := os.Getenv("CODER_PG_CONNECTION_URL")
|
||||
if connectionURL == "" && o.url != "" {
|
||||
connectionURL = o.url
|
||||
}
|
||||
if connectionURL == "" {
|
||||
var err error
|
||||
connectionURL, err = Open(t)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
if o.fixedTimezone == "" {
|
||||
// To make sure we find timezone-related issues, we set the timezone
|
||||
// of the database to a non-UTC one.
|
||||
// The below was picked due to the following properties:
|
||||
// - It has a non-UTC offset
|
||||
// - It has a fractional hour UTC offset
|
||||
// - It includes a daylight savings time component
|
||||
o.fixedTimezone = DefaultTimezone
|
||||
}
|
||||
dbName := dbNameFromConnectionURL(t, connectionURL)
|
||||
setDBTimezone(t, connectionURL, dbName, o.fixedTimezone)
|
||||
|
||||
sqlDB, err := sql.Open("postgres", connectionURL)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
_ = sqlDB.Close()
|
||||
})
|
||||
if o.returnSQLDB != nil {
|
||||
o.returnSQLDB(sqlDB)
|
||||
}
|
||||
if o.dumpOnFailure {
|
||||
t.Cleanup(func() { DumpOnFailure(t, connectionURL) })
|
||||
}
|
||||
// Unit tests should not retry serial transaction failures.
|
||||
db = database.New(sqlDB, database.WithSerialRetryCount(1))
|
||||
|
||||
ps, err = pubsub.New(context.Background(), o.logger, sqlDB, connectionURL)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
_ = ps.Close()
|
||||
})
|
||||
} else {
|
||||
db = dbmem.New()
|
||||
ps = pubsub.NewInMemory()
|
||||
connectionURL := os.Getenv("CODER_PG_CONNECTION_URL")
|
||||
if connectionURL == "" && o.url != "" {
|
||||
connectionURL = o.url
|
||||
}
|
||||
if connectionURL == "" {
|
||||
var err error
|
||||
connectionURL, err = Open(t)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
if o.fixedTimezone == "" {
|
||||
// To make sure we find timezone-related issues, we set the timezone
|
||||
// of the database to a non-UTC one.
|
||||
// The below was picked due to the following properties:
|
||||
// - It has a non-UTC offset
|
||||
// - It has a fractional hour UTC offset
|
||||
// - It includes a daylight savings time component
|
||||
o.fixedTimezone = DefaultTimezone
|
||||
}
|
||||
dbName := dbNameFromConnectionURL(t, connectionURL)
|
||||
setDBTimezone(t, connectionURL, dbName, o.fixedTimezone)
|
||||
|
||||
sqlDB, err := sql.Open("postgres", connectionURL)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
_ = sqlDB.Close()
|
||||
})
|
||||
if o.returnSQLDB != nil {
|
||||
o.returnSQLDB(sqlDB)
|
||||
}
|
||||
if o.dumpOnFailure {
|
||||
t.Cleanup(func() { DumpOnFailure(t, connectionURL) })
|
||||
}
|
||||
// Unit tests should not retry serial transaction failures.
|
||||
db = database.New(sqlDB, database.WithSerialRetryCount(1))
|
||||
|
||||
ps, err = pubsub.New(context.Background(), o.logger, sqlDB, connectionURL)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
_ = ps.Close()
|
||||
})
|
||||
|
||||
return db, ps
|
||||
}
|
||||
|
||||
@@ -222,7 +222,6 @@ func (g userGenerator) withLink(lt database.LoginType, rawJSON json.RawMessage)
|
||||
err := sql.UpdateUserLinkRawJSON(context.Background(), user.ID, rawJSON)
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
// no need to test the json key logic in dbmem. Everything is type safe.
|
||||
var claims database.UserLinkClaims
|
||||
err := json.Unmarshal(rawJSON, &claims)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -1400,7 +1400,6 @@ func TestGetUsers_IncludeSystem(t *testing.T) {
|
||||
|
||||
// Given: a system user
|
||||
// postgres: introduced by migration coderd/database/migrations/00030*_system_user.up.sql
|
||||
// dbmem: created in dbmem/dbmem.go
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
other := dbgen.User(t, db, database.User{})
|
||||
users, err := db.GetUsers(ctx, database.GetUsersParams{
|
||||
|
||||
@@ -769,7 +769,7 @@ func TestNotificationTemplates_Golden(t *testing.T) {
|
||||
hello = "localhost"
|
||||
|
||||
from = "system@coder.com"
|
||||
hint = "run \"DB=ci make gen/golden-files\" and commit the changes"
|
||||
hint = "run \"make gen/golden-files\" and commit the changes"
|
||||
)
|
||||
|
||||
tests := []struct {
|
||||
|
||||
@@ -32,7 +32,6 @@ import (
|
||||
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
||||
"github.com/coder/coder/v2/coderd/database/dbfake"
|
||||
"github.com/coder/coder/v2/coderd/database/dbgen"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
||||
"github.com/coder/coder/v2/coderd/database/dbtime"
|
||||
"github.com/coder/coder/v2/coderd/rbac"
|
||||
"github.com/coder/coder/v2/coderd/util/ptr"
|
||||
@@ -1794,15 +1793,6 @@ func TestUsersFilter(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: This can be removed with dbmem
|
||||
if !dbtestutil.WillUsePostgres() {
|
||||
for i := range matched.Users {
|
||||
if len(matched.Users[i].OrganizationIDs) == 0 {
|
||||
matched.Users[i].OrganizationIDs = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
require.ElementsMatch(t, exp, matched.Users, "expected users returned")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user