Commit bf88fe48 authored by Eric Chiang's avatar Eric Chiang

functional: don't fail if postgres or ldap isn't availabl

parent 118bbb6d
...@@ -11,7 +11,9 @@ go: ...@@ -11,7 +11,9 @@ go:
env: env:
- DEX_TEST_DSN="postgres://postgres@127.0.0.1:15432/postgres?sslmode=disable" ISOLATED=true - DEX_TEST_DSN="postgres://postgres@127.0.0.1:15432/postgres?sslmode=disable" ISOLATED=true
DEX_TEST_LDAP_URI="ldap://tlstest.local:1389/????bindname=cn%3Dadmin%2Cdc%3Dexample%2Cdc%3Dorg,X-BINDPW=admin" DEX_TEST_LDAP_HOST="tlstest.local:1389"
DEX_TEST_LDAP_BINDNAME="cn=admin,dc=example,dc=org"
DEX_TEST_LDAP_BINDPASS="admin"
install: install:
- go get golang.org/x/tools/cmd/cover - go get golang.org/x/tools/cmd/cover
......
...@@ -19,19 +19,11 @@ import ( ...@@ -19,19 +19,11 @@ import (
"github.com/coreos/dex/session" "github.com/coreos/dex/session"
) )
var ( func connect(t *testing.T) *gorp.DbMap {
dsn string dsn := os.Getenv("DEX_TEST_DSN")
)
func init() {
dsn = os.Getenv("DEX_TEST_DSN")
if dsn == "" { if dsn == "" {
fmt.Println("Unable to proceed with empty env var DEX_TEST_DSN") t.Fatal("Unable to proceed with empty env var DEX_TEST_DSN")
os.Exit(1)
} }
}
func connect(t *testing.T) *gorp.DbMap {
c, err := db.NewConnection(db.Config{DSN: dsn}) c, err := db.NewConnection(db.Config{DSN: dsn})
if err != nil { if err != nil {
t.Fatalf("Unable to connect to database: %v", err) t.Fatalf("Unable to connect to database: %v", err)
......
...@@ -28,6 +28,10 @@ var connConfigExample = []byte(`[ ...@@ -28,6 +28,10 @@ var connConfigExample = []byte(`[
]`) ]`)
func TestDexctlCommands(t *testing.T) { func TestDexctlCommands(t *testing.T) {
dsn := os.Getenv("DEX_TEST_DSN")
if dsn == "" {
t.Fatal("Unable to proceed with empty env var DEX_TEST_DSN")
}
if strings.HasPrefix(dsn, "sqlite3://") { if strings.HasPrefix(dsn, "sqlite3://") {
t.Skip("only test dexctl conmand with postgres") t.Skip("only test dexctl conmand with postgres")
} }
......
...@@ -3,16 +3,13 @@ package functional ...@@ -3,16 +3,13 @@ package functional
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"net"
"net/url" "net/url"
"os" "os"
"strconv" "strconv"
"strings"
"testing" "testing"
"github.com/coreos/dex/connector" "github.com/coreos/dex/connector"
"github.com/coreos/dex/db"
"github.com/coreos/dex/repo"
"github.com/coreos/go-oidc/oidc"
"gopkg.in/ldap.v2" "gopkg.in/ldap.v2"
) )
...@@ -23,202 +20,106 @@ var ( ...@@ -23,202 +20,106 @@ var (
ldapBindPw string ldapBindPw string
) )
func init() { type LDAPServer struct {
ldapuri := os.Getenv("DEX_TEST_LDAP_URI") Host string
if ldapuri == "" { Port uint16
fmt.Println("Unable to proceed with empty env var " + BindDN string
"DEX_TEST_LDAP_URI") BindPw string
os.Exit(1) }
}
u, err := url.Parse(ldapuri)
if err != nil {
fmt.Println("Unable to parse DEX_TEST_LDAP_URI")
os.Exit(1)
}
if strings.Index(u.RawQuery, "?") < 0 {
fmt.Println("Unable to parse DEX_TEST_LDAP_URI")
os.Exit(1)
}
extentions := make(map[string]string)
kvs := strings.Split(strings.TrimLeft(u.RawQuery, "?"), ",")
for i := range kvs {
fmt.Println(kvs[i])
kv := strings.Split(kvs[i], "=")
if len(kv) < 2 {
fmt.Println("Unable to parse DEX_TEST_LDAP_URI")
os.Exit(1)
}
extentions[kv[0]] = kv[1]
}
hostport := strings.Split(u.Host, ":")
port := 389
if len(hostport) > 1 {
port, _ = strconv.Atoi(hostport[1])
}
ldapHost = hostport[0] const (
ldapPort = uint16(port) ldapEnvHost = "DEX_TEST_LDAP_HOST"
ldapEnvBindName = "DEX_TEST_LDAP_BINDNAME"
ldapEnvBindPass = "DEX_TEST_LDAP_BINDPASS"
)
if len(extentions["bindname"]) > 0 { func ldapServer(t *testing.T) LDAPServer {
ldapBindDN, err = url.QueryUnescape(extentions["bindname"]) host := os.Getenv(ldapEnvHost)
if err != nil { if host == "" {
fmt.Println("Unable to parse DEX_TEST_LDAP_URI") t.Fatalf("%s not set", ldapEnvHost)
os.Exit(1)
} }
var port uint64 = 389
if h, p, err := net.SplitHostPort(host); err == nil {
port, err = strconv.ParseUint(p, 10, 16)
if err != nil {
t.Fatalf("failed to parse port: %v", err)
} }
if len(extentions["X-BINDPW"]) > 0 { host = h
ldapBindPw = extentions["X-BINDPW"]
} }
return LDAPServer{host, uint16(port), os.Getenv(ldapEnvBindName), os.Getenv(ldapEnvBindPass)}
} }
func TestLDAPConnect(t *testing.T) { func TestLDAPConnect(t *testing.T) {
fmt.Println("ldapHost: ", ldapHost) server := ldapServer(t)
fmt.Println("ldapPort: ", ldapPort) l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", server.Host, server.Port))
fmt.Println("ldapBindDN: ", ldapBindDN)
fmt.Println("ldapBindPw: ", ldapBindPw)
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", ldapHost, ldapPort))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
err = l.Bind(ldapBindDN, ldapBindPw) err = l.Bind(server.BindDN, server.BindPw)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
l.Close() l.Close()
} }
func TestConnectorLDAPConnectFail(t *testing.T) { func TestConnectorLDAPHealthy(t *testing.T) {
var tx repo.Transaction server := ldapServer(t)
var lf oidc.LoginFunc
var ns url.URL
templates := template.New(connector.LDAPLoginPageTemplateName) tests := []struct {
config connector.LDAPConnectorConfig
ccr := db.NewConnectorConfigRepo(db.NewMemDB()) wantErr bool
err := ccr.Set( }{
[]connector.ConnectorConfig{&connector.LDAPConnectorConfig{ {
config: connector.LDAPConnectorConfig{
ID: "ldap", ID: "ldap",
ServerHost: ldapHost, ServerHost: server.Host,
ServerPort: ldapPort + 1, ServerPort: server.Port + 1,
}}, },
) wantErr: true,
if err != nil { },
t.Fatal(err) {
} config: connector.LDAPConnectorConfig{
cc, err := ccr.GetConnectorByID(tx, "ldap")
if err != nil {
t.Fatal(err)
}
c, err := cc.Connector(ns, lf, templates)
if err != nil {
t.Fatal(err)
}
err = c.Healthy()
if err == nil {
t.Fatal(fmt.Errorf("LDAPConnector.Healty() supposed to fail, but succeeded!"))
}
}
func TestConnectorLDAPConnectSuccess(t *testing.T) {
var tx repo.Transaction
var lf oidc.LoginFunc
var ns url.URL
templates := template.New(connector.LDAPLoginPageTemplateName)
ccr := db.NewConnectorConfigRepo(db.NewMemDB())
err := ccr.Set(
[]connector.ConnectorConfig{&connector.LDAPConnectorConfig{
ID: "ldap", ID: "ldap",
ServerHost: ldapHost, ServerHost: server.Host,
ServerPort: ldapPort, ServerPort: server.Port,
}}, },
) },
if err != nil { {
t.Fatal(err) config: connector.LDAPConnectorConfig{
}
cc, err := ccr.GetConnectorByID(tx, "ldap")
if err != nil {
t.Fatal(err)
}
c, err := cc.Connector(ns, lf, templates)
if err != nil {
t.Fatal(err)
}
err = c.Healthy()
if err != nil {
t.Fatal(err)
}
}
func TestConnectorLDAPcaFilecertFileConnectTLS(t *testing.T) {
var tx repo.Transaction
var lf oidc.LoginFunc
var ns url.URL
templates := template.New(connector.LDAPLoginPageTemplateName)
ccr := db.NewConnectorConfigRepo(db.NewMemDB())
err := ccr.Set(
[]connector.ConnectorConfig{&connector.LDAPConnectorConfig{
ID: "ldap", ID: "ldap",
ServerHost: ldapHost, ServerHost: server.Host,
ServerPort: ldapPort, ServerPort: server.Port,
UseTLS: true, UseTLS: true,
CertFile: "/tmp/ldap.crt", CertFile: "/tmp/ldap.crt",
KeyFile: "/tmp/ldap.key", KeyFile: "/tmp/ldap.key",
CaFile: "/tmp/openldap-ca.pem", CaFile: "/tmp/openldap-ca.pem",
}}, },
) },
if err != nil { {
t.Fatal(err) config: connector.LDAPConnectorConfig{
}
cc, err := ccr.GetConnectorByID(tx, "ldap")
if err != nil {
t.Fatal(err)
}
c, err := cc.Connector(ns, lf, templates)
if err != nil {
t.Fatal(err)
}
err = c.Healthy()
if err != nil {
t.Fatal(err)
}
}
func TestConnectorLDAPcaFilecertFileConnectSSL(t *testing.T) {
var tx repo.Transaction
var lf oidc.LoginFunc
var ns url.URL
templates := template.New(connector.LDAPLoginPageTemplateName)
ccr := db.NewConnectorConfigRepo(db.NewMemDB())
err := ccr.Set(
[]connector.ConnectorConfig{&connector.LDAPConnectorConfig{
ID: "ldap", ID: "ldap",
ServerHost: ldapHost, ServerHost: server.Host,
ServerPort: ldapPort + 247, // 636 ServerPort: server.Port + 247, // 636
UseSSL: true, UseSSL: true,
CertFile: "/tmp/ldap.crt", CertFile: "/tmp/ldap.crt",
KeyFile: "/tmp/ldap.key", KeyFile: "/tmp/ldap.key",
CaFile: "/tmp/openldap-ca.pem", CaFile: "/tmp/openldap-ca.pem",
}}, },
) },
if err != nil {
t.Fatal(err)
} }
cc, err := ccr.GetConnectorByID(tx, "ldap") for i, tt := range tests {
templates := template.New(connector.LDAPLoginPageTemplateName)
c, err := tt.config.Connector(url.URL{}, nil, templates)
if err != nil { if err != nil {
t.Fatal(err) t.Errorf("case %d: failed to create connector: %v", i, err)
continue
} }
c, err := cc.Connector(ns, lf, templates) if err := c.Healthy(); err != nil {
if err != nil { if !tt.wantErr {
t.Fatal(err) t.Errorf("case %d: Healthy() returned error: %v", i, err)
}
} else if tt.wantErr {
t.Errorf("case %d: expected Healthy() to fail", i)
} }
err = c.Healthy()
if err != nil {
t.Fatal(err)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment