2016-09-15 01:11:57 +00:00
|
|
|
package sql
|
2016-09-18 01:01:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coreos/dex/storage"
|
|
|
|
"github.com/coreos/dex/storage/conformance"
|
|
|
|
)
|
|
|
|
|
|
|
|
func withTimeout(t time.Duration, f func()) {
|
|
|
|
c := make(chan struct{})
|
|
|
|
defer close(c)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
case <-time.After(t):
|
|
|
|
// Dump a stack trace of the program. Useful for debugging deadlocks.
|
|
|
|
buf := make([]byte, 2<<20)
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", buf[:runtime.Stack(buf, true)])
|
|
|
|
panic("test took too long")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanDB(c *conn) error {
|
|
|
|
_, err := c.Exec(`
|
|
|
|
delete from client;
|
|
|
|
delete from auth_request;
|
|
|
|
delete from auth_code;
|
|
|
|
delete from refresh_token;
|
|
|
|
delete from keys;
|
|
|
|
`)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSQLite3(t *testing.T) {
|
|
|
|
newStorage := func() storage.Storage {
|
|
|
|
// NOTE(ericchiang): In memory means we only get one connection at a time. If we
|
|
|
|
// ever write tests that require using multiple connections, for instance to test
|
|
|
|
// transactions, we need to move to a file based system.
|
|
|
|
s := &SQLite3{":memory:"}
|
|
|
|
conn, err := s.open()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
withTimeout(time.Second*10, func() {
|
2016-10-13 01:48:09 +00:00
|
|
|
conformance.RunTests(t, newStorage)
|
2016-09-18 01:01:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgres(t *testing.T) {
|
|
|
|
if os.Getenv("DEX_POSTGRES_HOST") == "" {
|
|
|
|
t.Skip("postgres envs not set, skipping tests")
|
|
|
|
}
|
|
|
|
p := Postgres{
|
|
|
|
Database: os.Getenv("DEX_POSTGRES_DATABASE"),
|
|
|
|
User: os.Getenv("DEX_POSTGRES_USER"),
|
|
|
|
Password: os.Getenv("DEX_POSTGRES_PASSWORD"),
|
|
|
|
Host: os.Getenv("DEX_POSTGRES_HOST"),
|
|
|
|
SSL: PostgresSSL{
|
|
|
|
Mode: sslDisable, // Postgres container doesn't support SSL.
|
|
|
|
},
|
|
|
|
ConnectionTimeout: 5,
|
|
|
|
}
|
|
|
|
conn, err := p.open()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
newStorage := func() storage.Storage {
|
|
|
|
if err := cleanDB(conn); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
withTimeout(time.Minute*1, func() {
|
2016-10-13 01:48:09 +00:00
|
|
|
conformance.RunTests(t, newStorage)
|
2016-09-18 01:01:15 +00:00
|
|
|
})
|
|
|
|
}
|