code refactor (#1786)
* implement LDAP mock client Signed-off-by: hongming <talonwan@yunify.com> * update Signed-off-by: hongming <talonwan@yunify.com> * update Signed-off-by: hongming <talonwan@yunify.com> * resolve conflict Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
@@ -20,11 +20,13 @@ package ldap
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-ldap/ldap"
|
||||
"github.com/golang/mock/gomock"
|
||||
"k8s.io/klog"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
NewConn() (ldap.Client, error)
|
||||
Close()
|
||||
GroupSearchBase() string
|
||||
UserSearchBase() string
|
||||
}
|
||||
@@ -35,8 +37,8 @@ type poolClient struct {
|
||||
}
|
||||
|
||||
// panic if cannot connect to ldap service
|
||||
func NewLdapClient(options *Options, stopCh <-chan struct{}) (Client, error) {
|
||||
pool, err := NewChannelPool(8, 64, "kubesphere", func(s string) (ldap.Client, error) {
|
||||
func NewClient(options *Options, stopCh <-chan struct{}) (Client, error) {
|
||||
pool, err := newChannelPool(options.InitialCap, options.MaxCap, options.PoolName, func(s string) (ldap.Client, error) {
|
||||
conn, err := ldap.Dial("tcp", options.Host)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -46,7 +48,6 @@ func NewLdapClient(options *Options, stopCh <-chan struct{}) (Client, error) {
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
pool.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -57,13 +58,16 @@ func NewLdapClient(options *Options, stopCh <-chan struct{}) (Client, error) {
|
||||
|
||||
go func() {
|
||||
<-stopCh
|
||||
if client.pool != nil {
|
||||
client.pool.Close()
|
||||
}
|
||||
client.Close()
|
||||
}()
|
||||
|
||||
return client, nil
|
||||
}
|
||||
func (l *poolClient) Close() {
|
||||
if l.pool != nil {
|
||||
l.pool.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (l *poolClient) NewConn() (ldap.Client, error) {
|
||||
if l.pool == nil {
|
||||
@@ -81,7 +85,7 @@ func (l *poolClient) NewConn() (ldap.Client, error) {
|
||||
err = conn.Bind(l.options.ManagerDN, l.options.ManagerPassword)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
klog.Error(err)
|
||||
klog.Errorln(err)
|
||||
return nil, err
|
||||
}
|
||||
return conn, nil
|
||||
@@ -94,3 +98,25 @@ func (l *poolClient) GroupSearchBase() string {
|
||||
func (l *poolClient) UserSearchBase() string {
|
||||
return l.options.UserSearchBase
|
||||
}
|
||||
|
||||
func NewMockClient(options *Options, ctrl *gomock.Controller, setup func(client *MockClient)) (Client, error) {
|
||||
pool, err := newChannelPool(options.InitialCap, options.MaxCap, options.PoolName, func(s string) (ldap.Client, error) {
|
||||
conn := newMockClient(ctrl)
|
||||
conn.EXPECT().Bind(gomock.Any(), gomock.Any()).AnyTimes()
|
||||
conn.EXPECT().Close().AnyTimes()
|
||||
setup(conn)
|
||||
return conn, nil
|
||||
}, []uint16{ldap.LDAPResultAdminLimitExceeded, ldap.ErrorNetwork})
|
||||
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client := &poolClient{
|
||||
pool: pool,
|
||||
options: options,
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user