Skip to content

Commit

Permalink
add integration test framework
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmoraisjr committed Feb 29, 2024
1 parent c1c83f0 commit aa55160
Show file tree
Hide file tree
Showing 9 changed files with 586 additions and 9 deletions.
31 changes: 27 additions & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,34 @@ jobs:
steps:
- uses: actions/setup-go@v4
with:
go-version: 1.21.6
go-version: "1.21.6"
- uses: actions/checkout@v4
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
- name: Run build
run: go build -o haproxy-ingress pkg/main.go
- name: Run tests
run: go test ./...
run: make build
- name: Run unit tests
run: make test
integration:
runs-on: ubuntu-latest
steps:
- name: Install dependencies
run: sudo apt-get install -y lua-json
- name: Install HAProxy
uses: timwolla/action-install-haproxy@main
id: install-haproxy
with:
branch: "2.2"
use_openssl: yes
use_lua: yes
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: "1.21.6"
- uses: actions/checkout@v4
- uses: docker/login-action@v3
with:
username: jcmoraisjr
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Run integration tests
run: make test-integration
22 changes: 20 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ KUBECONFIG?=$(HOME)/.kube/config
CONTROLLER_CONFIGMAP?=
CONTROLLER_ARGS?=

LOCALBIN?=$(shell pwd)/bin
LOCAL_GOTESTSUM=$(LOCALBIN)/gotestsum
LOCAL_SETUP_ENVTEST=$(LOCALBIN)/setup-envtest

.PHONY: build
build:
CGO_ENABLED=0 go build \
Expand All @@ -32,14 +36,28 @@ run: build
--configmap=$(CONTROLLER_CONFIGMAP)\
$(CONTROLLER_ARGS)

,PHONY: gotestsum
gotestsum:
test -x $(LOCAL_GOTESTSUM) || GOBIN=$(LOCALBIN) go install gotest.tools/gotestsum@latest

.PHONY: lint
lint:
golangci-lint run

.PHONY: test
test: lint
test: lint gotestsum
## fix race and add -race param
go test -tags cgo ./...
$(LOCAL_GOTESTSUM) -- -tags=cgo ./pkg/...

.PHONY: setup-envtest
setup-envtest:
test -x $(LOCAL_SETUP_ENVTEST) || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
$(LOCAL_SETUP_ENVTEST) use 1.29.1 --bin-dir $(LOCALBIN)

.PHONY: test-integration
test-integration: gotestsum setup-envtest
KUBEBUILDER_ASSETS="$(shell $(LOCAL_SETUP_ENVTEST) use 1.29.1 --bin-dir $(LOCALBIN) -i -p path)"\
$(LOCAL_GOTESTSUM) -- -count=1 -tags=cgo ./tests/integration/...

.PHONY: linux-build
linux-build:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ The following `make` targets are supported:

* `build` (default): Compiles HAProxy Ingress using the default OS and arch, and generates an executable at `bin/controller`.
* `run`: Runs HAProxy Ingress locally.
* `lint`: Runs [`golangci-lint`](https://golangci-lint.run/).
* `lint`: Runs [`golangci-lint`](https://golangci-lint.run/), needs golangci-lint in the path.
* `test`: Runs unit tests.
* `test-integration`: Runs integration tests, needs haproxy 2.2+ in the path.
* `linux-build`: Compiles HAProxy Ingress and generates an ELF (Linux) executable despite the source platform at `rootfs/haproxy-ingress-controller`. Used by `image` step.
* `image`: Compiles HAProxy Ingress locally and generates a Docker image.
* `docker-build`: Compiles HAProxy Ingress and generates a Docker image using a multi-stage Dockerfile.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/prometheus/client_golang v1.18.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.18.0
gopkg.in/go-playground/pool.v3 v3.1.1
Expand Down Expand Up @@ -58,6 +59,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand Down
14 changes: 12 additions & 2 deletions pkg/converters/utils/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,25 @@ type Endpoint struct {
}

func createEndpoints(endpoints *api.Endpoints, svcPort *api.ServicePort) (ready, notReady []*Endpoint, err error) {
var ipOverride string
if ann := endpoints.Annotations; ann != nil {
ipOverride = ann["haproxy-ingress.github.io/ip-override"]
}
resolveIP := func(ip string) string {
if ipOverride != "" {
return ipOverride
}
return ip
}
for _, subset := range endpoints.Subsets {
for _, epPort := range subset.Ports {
if matchPort(svcPort, &epPort) {
port := int(epPort.Port)
for _, addr := range subset.Addresses {
ready = append(ready, newEndpoint(addr.IP, port, addr.TargetRef))
ready = append(ready, newEndpoint(resolveIP(addr.IP), port, addr.TargetRef))
}
for _, addr := range subset.NotReadyAddresses {
notReady = append(notReady, newEndpoint(addr.IP, port, addr.TargetRef))
notReady = append(notReady, newEndpoint(resolveIP(addr.IP), port, addr.TargetRef))
}
}
}
Expand Down
Loading

0 comments on commit aa55160

Please sign in to comment.