Commit 350571ac authored by Eric Chiang's avatar Eric Chiang

Merge pull request #323 from ericchiang/cleanup_functional

functional: don't fail if postgres or ldap isn't available
parents 118bbb6d bf88fe48
...@@ -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 host == "" {
t.Fatalf("%s not set", ldapEnvHost)
}
var port uint64 = 389
if h, p, err := net.SplitHostPort(host); err == nil {
port, err = strconv.ParseUint(p, 10, 16)
if err != nil { if err != nil {
fmt.Println("Unable to parse DEX_TEST_LDAP_URI") t.Fatalf("failed to parse port: %v", err)
os.Exit(1)
} }
host = h
} }
if len(extentions["X-BINDPW"]) > 0 { return LDAPServer{host, uint16(port), os.Getenv(ldapEnvBindName), os.Getenv(ldapEnvBindPass)}
ldapBindPw = extentions["X-BINDPW"]
}
} }
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 tests := []struct {
config connector.LDAPConnectorConfig
templates := template.New(connector.LDAPLoginPageTemplateName) wantErr bool
}{
ccr := db.NewConnectorConfigRepo(db.NewMemDB()) {
err := ccr.Set( config: connector.LDAPConnectorConfig{
[]connector.ConnectorConfig{&connector.LDAPConnectorConfig{ ID: "ldap",
ID: "ldap", ServerHost: server.Host,
ServerHost: ldapHost, ServerPort: server.Port + 1,
ServerPort: ldapPort + 1, },
}}, wantErr: true,
) },
if err != nil { {
t.Fatal(err) config: connector.LDAPConnectorConfig{
} ID: "ldap",
cc, err := ccr.GetConnectorByID(tx, "ldap") ServerHost: server.Host,
if err != nil { ServerPort: server.Port,
t.Fatal(err) },
} },
c, err := cc.Connector(ns, lf, templates) {
if err != nil { config: connector.LDAPConnectorConfig{
t.Fatal(err) ID: "ldap",
} ServerHost: server.Host,
err = c.Healthy() ServerPort: server.Port,
if err == nil { UseTLS: true,
t.Fatal(fmt.Errorf("LDAPConnector.Healty() supposed to fail, but succeeded!")) CertFile: "/tmp/ldap.crt",
} KeyFile: "/tmp/ldap.key",
} CaFile: "/tmp/openldap-ca.pem",
},
func TestConnectorLDAPConnectSuccess(t *testing.T) { },
var tx repo.Transaction {
var lf oidc.LoginFunc config: connector.LDAPConnectorConfig{
var ns url.URL ID: "ldap",
ServerHost: server.Host,
templates := template.New(connector.LDAPLoginPageTemplateName) ServerPort: server.Port + 247, // 636
UseSSL: true,
ccr := db.NewConnectorConfigRepo(db.NewMemDB()) CertFile: "/tmp/ldap.crt",
err := ccr.Set( KeyFile: "/tmp/ldap.key",
[]connector.ConnectorConfig{&connector.LDAPConnectorConfig{ CaFile: "/tmp/openldap-ca.pem",
ID: "ldap", },
ServerHost: ldapHost, },
ServerPort: ldapPort, }
}}, for i, tt := range tests {
) templates := template.New(connector.LDAPLoginPageTemplateName)
if err != nil { c, err := tt.config.Connector(url.URL{}, nil, templates)
t.Fatal(err) if err != nil {
} t.Errorf("case %d: failed to create connector: %v", i, err)
cc, err := ccr.GetConnectorByID(tx, "ldap") continue
if err != nil { }
t.Fatal(err) if err := c.Healthy(); err != nil {
} if !tt.wantErr {
c, err := cc.Connector(ns, lf, templates) t.Errorf("case %d: Healthy() returned error: %v", i, err)
if err != nil { }
t.Fatal(err) } else if tt.wantErr {
} t.Errorf("case %d: expected Healthy() to fail", i)
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",
ServerHost: ldapHost,
ServerPort: ldapPort,
UseTLS: true,
CertFile: "/tmp/ldap.crt",
KeyFile: "/tmp/ldap.key",
CaFile: "/tmp/openldap-ca.pem",
}},
)
if err != nil {
t.Fatal(err)
}
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",
ServerHost: ldapHost,
ServerPort: ldapPort + 247, // 636
UseSSL: true,
CertFile: "/tmp/ldap.crt",
KeyFile: "/tmp/ldap.key",
CaFile: "/tmp/openldap-ca.pem",
}},
)
if err != nil {
t.Fatal(err)
}
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)
} }
} }
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