Skip to content

Commit 9feb1c8

Browse files
authored
feat: add local directory server for tests through containers (#567)
1 parent 82fef14 commit 9feb1c8

File tree

6 files changed

+101
-51
lines changed

6 files changed

+101
-51
lines changed

.github/workflows/pr.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jobs:
2424
- name: Version
2525
run: go version
2626

27+
- name: Setup local directory server
28+
run: make local-server
29+
2730
- name: Build, Validate, and Test
2831
run: |
2932
cd ${{ matrix.directory }}

Makefile

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
1-
.PHONY: default install build test quicktest fmt vet lint
1+
.PHONY: default install build test quicktest fmt vet lint
22

3-
# List of all release tags "supported" by our current Go version
4-
# E.g. ":go1.1:go1.2:go1.3:go1.4:go1.5:go1.6:go1.7:go1.8:go1.9:go1.10:go1.11:go1.12:"
5-
GO_RELEASE_TAGS := $(shell go list -f ':{{join (context.ReleaseTags) ":"}}:' runtime)
3+
default: fmt vet lint build quicktest
64

7-
# Only use the `-race` flag on newer versions of Go (version 1.3 and newer)
8-
ifeq (,$(findstring :go1.3:,$(GO_RELEASE_TAGS)))
9-
RACE_FLAG :=
10-
else
11-
RACE_FLAG := -race -cpu 1,2,4
5+
CONTAINER_CMD := $(shell command -v podman 2>/dev/null)
6+
ifeq ($(CONTAINER_CMD),)
7+
CONTAINER_CMD := $(shell command -v docker 2>/dev/null)
128
endif
139

14-
# Run `go vet` on Go 1.12 and newer. For Go 1.5-1.11, use `go tool vet`
15-
ifneq (,$(findstring :go1.12:,$(GO_RELEASE_TAGS)))
16-
GO_VET := go vet \
17-
-atomic \
18-
-bool \
19-
-copylocks \
20-
-nilfunc \
21-
-printf \
22-
-rangeloops \
23-
-unreachable \
24-
-unsafeptr \
25-
-unusedresult \
26-
.
27-
else ifneq (,$(findstring :go1.5:,$(GO_RELEASE_TAGS)))
28-
GO_VET := go tool vet \
29-
-atomic \
30-
-bool \
31-
-copylocks \
32-
-nilfunc \
33-
-printf \
34-
-shadow \
35-
-rangeloops \
36-
-unreachable \
37-
-unsafeptr \
38-
-unusedresult \
39-
.
40-
else
41-
GO_VET := @echo "go vet skipped -- not supported on this version of Go"
10+
# Check if we found either command
11+
ifeq ($(CONTAINER_CMD),)
12+
$(error Neither podman nor docker found in PATH)
4213
endif
4314

44-
default: fmt vet lint build quicktest
45-
4615
install:
4716
go get -t -v ./...
4817

4918
build:
5019
go build -v ./...
5120

52-
test:
53-
go test -v $(RACE_FLAG) -cover ./...
21+
LDAP_ADMIN_DN := cn=admin,dc=example,dc=com
22+
LDAP_ADMIN_PASSWORD := admin123
23+
LDAP_BASE_DN := dc=example,dc=com
24+
LDAP_URL := ldap://127.0.0.1:389
25+
CONTAINER_NAME := go-ldap-test
26+
27+
local-server:
28+
-$(CONTAINER_CMD) rm -f -t 10 $(CONTAINER_NAME)
29+
$(CONTAINER_CMD) \
30+
run \
31+
--rm \
32+
-d \
33+
--name $(CONTAINER_NAME) \
34+
--hostname "$(CONTAINER_NAME).example.com" \
35+
-p "127.0.0.1:3389:389" \
36+
-p "127.0.0.1:3636:636" \
37+
-e LDAP_ORGANISATION="Example Inc" \
38+
-e LDAP_DOMAIN="example.com" \
39+
-e LDAP_ADMIN_PASSWORD="$(LDAP_ADMIN_PASSWORD)" \
40+
-e LDAP_TLS_VERIFY_CLIENT="never" \
41+
docker.io/osixia/openldap:1.5.0
42+
43+
@echo "Waiting for LDAP server to be ready..."
44+
@$(CONTAINER_CMD) exec $(CONTAINER_NAME) /bin/sh -c 'until ldapsearch -x -H $(LDAP_URL) -b "$(LDAP_BASE_DN)" -D "$(LDAP_ADMIN_DN)" -w $(LDAP_ADMIN_PASSWORD) > /dev/null 2>&1; do echo "LDAP server not ready yet, waiting..."; sleep 2; done'
45+
@echo "LDAP server is ready!"
46+
47+
@echo "Configuring anonymous access..."
48+
@$(CONTAINER_CMD) exec $(CONTAINER_NAME) /bin/sh -c 'echo "dn: olcDatabase={1}mdb,cn=config\nchangetype: modify\nreplace: olcAccess\nolcAccess: {0}to * by * read" | ldapmodify -Y EXTERNAL -H ldapi:///'
49+
50+
$(CONTAINER_CMD) cp -a ./testdata $(CONTAINER_NAME):/
51+
@echo "Loading LDIF files..."
52+
@$(CONTAINER_CMD) exec $(CONTAINER_NAME) /bin/sh -c 'for file in /testdata/*.ldif; do echo "Processing $$file..."; cat "$$file" | ldapadd -v -x -H $(LDAP_URL) -D "$(LDAP_ADMIN_DN)" -w $(LDAP_ADMIN_PASSWORD); done'
53+
54+
delete-container:
55+
-$(CONTAINER_CMD) rm -f -t 10 $(CONTAINER_NAME)
5456

5557
quicktest:
5658
go test ./...
@@ -71,7 +73,17 @@ fmt:
7173
fi
7274

7375
vet:
74-
$(GO_VET)
76+
go vet \
77+
-atomic \
78+
-bool \
79+
-copylocks \
80+
-nilfunc \
81+
-printf \
82+
-rangeloops \
83+
-unreachable \
84+
-unsafeptr \
85+
-unusedresult \
86+
./...
7587

7688
# https://github.com/golang/lint
7789
# go get github.com/golang/lint/golint
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dn: ou=people,dc=example,dc=com
2+
objectClass: organizationalUnit
3+
ou: people
4+
5+
dn: ou=groups,dc=example,dc=com
6+
objectClass: organizationalUnit
7+
ou: groups

testdata/02_Groups.ldif

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dn: cn=DistributionList,ou=groups,dc=example,dc=com
2+
objectClass: groupOfNames
3+
objectClass: top
4+
cn: DistributionList
5+
member:
6+
member: cn=DoeJo,ou=people,dc=example,dc=com
7+
member: cn=MustermannMa,ou=people,dc=example,dc=com

testdata/02_People.ldif

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
dn: cn=DoeJo,ou=people,dc=example,dc=com
2+
objectClass: inetOrgPerson
3+
objectClass: organizationalPerson
4+
objectClass: person
5+
objectClass: top
6+
cn: DoeJo
7+
sn: Doe
8+
displayName: Doe, John
9+
givenName: John
10+
11+
12+
dn: cn=MustermannMa,ou=people,dc=example,dc=com
13+
objectClass: inetOrgPerson
14+
objectClass: organizationalPerson
15+
objectClass: person
16+
objectClass: top
17+
cn: MustermannMa
18+
sn: Mustermann
19+
displayName: Mustermann, Max
20+
givenName: Max
21+

v3/ldap_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import (
1010
)
1111

1212
const (
13-
ldapServer = "ldap://ldap.itd.umich.edu:389"
14-
ldapsServer = "ldaps://ldap.itd.umich.edu:636"
15-
baseDN = "dc=umich,dc=edu"
13+
ldapServer = "ldap://127.0.0.1:3389"
14+
ldapsServer = "ldaps://127.0.0.1:3636"
15+
baseDN = "dc=example,dc=com"
1616
)
1717

1818
var filter = []string{
19-
"(cn=cis-fac)",
20-
"(&(owner=*)(cn=cis-fac))",
21-
"(&(objectclass=rfc822mailgroup)(cn=*Computer*))",
22-
"(&(objectclass=rfc822mailgroup)(cn=*Mathematics*))",
19+
"(cn=DoeJo)",
20+
"(&(sn=Doe)(givenName=John))",
21+
"(|(sn=Doe)(givenName=Max*))",
22+
"(objectClass=inetOrgPerson)",
2323
}
2424

2525
var attributes = []string{
@@ -258,9 +258,9 @@ func TestCompare(t *testing.T) {
258258
}
259259
defer l.Close()
260260

261-
const dn = "cn=math mich,ou=User Groups,ou=Groups,dc=umich,dc=edu"
261+
const dn = "cn=DoeJo,ou=people,dc=example,dc=com"
262262
const attribute = "cn"
263-
const value = "math mich"
263+
const value = "DoeJo"
264264

265265
sr, err := l.Compare(dn, attribute, value)
266266
if err != nil {

0 commit comments

Comments
 (0)