ci: Run tests using PostgreSQL database and mock (#49)

* ci: Run tests using PostgreSQL database and mock

This allows us to use the mock database for quick iterative testing,
and have confidence from CI using a real PostgreSQL database.

PostgreSQL tests are only ran on Linux. They are *really* slow on MacOS
and Windows runners, and don't provide much additional confidence.

* Only run PostgreSQL tests once for speed

* Fix race condition of log after close

Not all resources were cleaned up immediately after a peer connection was
closed. DataChannels could have a goroutine exit after Close() prior to this.

* Fix comment
This commit is contained in:
Kyle Carberry
2022-01-22 15:58:26 -06:00
committed by GitHub
parent dfddaf10a2
commit 50d8151995
6 changed files with 43 additions and 8 deletions

View File

@ -2,8 +2,10 @@ package coderdtest
import (
"context"
"database/sql"
"net/http/httptest"
"net/url"
"os"
"testing"
"github.com/stretchr/testify/require"
@ -11,7 +13,9 @@ import (
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/coderd"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/database"
"github.com/coder/coder/database/databasefake"
"github.com/coder/coder/database/postgres"
)
// Server represents a test instance of coderd.
@ -27,6 +31,20 @@ type Server struct {
func New(t *testing.T) Server {
// This can be hotswapped for a live database instance.
db := databasefake.New()
if os.Getenv("DB") != "" {
connectionURL, close, err := postgres.Open()
require.NoError(t, err)
t.Cleanup(close)
sqlDB, err := sql.Open("postgres", connectionURL)
require.NoError(t, err)
t.Cleanup(func() {
_ = sqlDB.Close()
})
err = database.Migrate(sqlDB)
require.NoError(t, err)
db = database.New(sqlDB)
}
handler := coderd.New(&coderd.Options{
Logger: slogtest.Make(t, nil),
Database: db,