-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added unit testing to existing LDAP sync code
- Loading branch information
1 parent
0c6c715
commit 63a542d
Showing
10 changed files
with
763 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
pkg/cmd/experimental/syncgroups/ad/augmented_ldapinterface_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package ad | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/go-ldap/ldap" | ||
"github.com/openshift/origin/pkg/auth/ldaputil" | ||
"github.com/openshift/origin/pkg/auth/ldaputil/ldapclient" | ||
"github.com/openshift/origin/pkg/auth/ldaputil/testclient" | ||
) | ||
|
||
func TestGroupEntryFor(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
cacheSeed map[string]*ldap.Entry | ||
client ldapclient.Client | ||
baseDNOverride string | ||
expectedError error | ||
expectedEntry *ldap.Entry | ||
}{ | ||
{ | ||
name: "cached entries", | ||
cacheSeed: map[string]*ldap.Entry{ | ||
testGroupDN: testGroupEntry, | ||
}, | ||
expectedError: nil, | ||
expectedEntry: testGroupEntry, | ||
}, | ||
{ | ||
name: "search request error", | ||
baseDNOverride: "otherBaseDN", | ||
expectedError: ldaputil.NewQueryOutOfBoundsError(testGroupDN, "otherBaseDN"), | ||
expectedEntry: nil, | ||
}, | ||
{ | ||
name: "search error", | ||
client: testclient.NewMatchingSearchErrorClient(testclient.New(), testGroupDN, searchErr), | ||
expectedError: searchErr, | ||
expectedEntry: nil, | ||
}, | ||
{ | ||
name: "no error", | ||
client: testclient.NewDNMappingClient( | ||
testclient.New(), | ||
map[string][]*ldap.Entry{ | ||
testGroupDN: {testGroupEntry}, | ||
}, | ||
), | ||
expectedError: nil, | ||
expectedEntry: testGroupEntry, | ||
}, | ||
} | ||
for _, testCase := range testCases { | ||
ldapInterface := newTestAugmentedADLDAPInterface(testCase.client) | ||
if len(testCase.cacheSeed) > 0 { | ||
ldapInterface.cachedGroups = testCase.cacheSeed | ||
} | ||
if len(testCase.baseDNOverride) > 0 { | ||
ldapInterface.groupQuery.BaseDN = testCase.baseDNOverride | ||
} | ||
entry, err := ldapInterface.GroupEntryFor(testGroupDN) | ||
if !reflect.DeepEqual(err, testCase.expectedError) { | ||
t.Errorf("%s: incorrect error returned:\n\texpected:\n\t%v\n\tgot:\n\t%v\n", testCase.name, testCase.expectedError, err) | ||
} | ||
if !reflect.DeepEqual(entry, testCase.expectedEntry) { | ||
t.Errorf("%s: incorrect members returned:\n\texpected:\n\t%v\n\tgot:\n\t%v\n", testCase.name, testCase.expectedEntry, entry) | ||
} | ||
} | ||
} | ||
|
||
const ( | ||
groupsBaseDN string = "ou=groups,dc=example,dc=com" | ||
groupsBaseFilter string = "objectClass=groupOfNames" | ||
|
||
testGroupDN string = "cn=testGroup," + groupsBaseDN | ||
) | ||
|
||
var testGroupEntry *ldap.Entry = &ldap.Entry{ | ||
DN: testGroupDN, | ||
Attributes: []*ldap.EntryAttribute{ | ||
{ | ||
Name: "cn", | ||
Values: []string{"testGroup"}, | ||
ByteValues: [][]byte{[]byte("testGroup")}, | ||
}, | ||
{ | ||
Name: "member", | ||
Values: []string{testUserDN}, | ||
ByteValues: [][]byte{[]byte(testUserDN)}, | ||
}, | ||
}, | ||
} | ||
|
||
func newTestAugmentedADLDAPInterface(client ldapclient.Client) *AugmentedADLDAPInterface { | ||
return NewAugmentedADLDAPInterface(testclient.NewConfig(client), | ||
testUserQuery, | ||
testGroupMembershipAttributes, | ||
testUserNameAttributes, | ||
testGroupQuery, | ||
testGroupNameAttributes) | ||
} | ||
|
||
// below are common test implementations of LDAPInterface fields | ||
var testGroupQuery ldaputil.LDAPQueryOnAttribute = ldaputil.LDAPQueryOnAttribute{ | ||
LDAPQuery: ldaputil.LDAPQuery{ | ||
BaseDN: groupsBaseDN, | ||
Scope: ldaputil.ScopeWholeSubtree, | ||
DerefAliases: ldaputil.DerefAliasesAlways, | ||
TimeLimit: 0, | ||
Filter: groupsBaseFilter, | ||
}, | ||
QueryAttribute: "dn", | ||
} | ||
var testGroupNameAttributes []string = []string{"cn"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.