Skip to content

Commit 242e2e6

Browse files
authored
Merge pull request #22 from kcp-dev/e2e-tests
✨ Add end-to-end testing
2 parents 976d28c + a1924a1 commit 242e2e6

File tree

16 files changed

+1024
-27
lines changed

16 files changed

+1024
-27
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/_build/
22
/_tools/
33
/vendor/
4+
/.kcp.e2e/
5+
/.e2e/
46
.cover
57
*.kubeconfig
68
*.pem

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ linters:
4343
- predeclared
4444
- promlinter
4545
- staticcheck
46-
- tenv
4746
- unconvert
4847
- unused
48+
- usetesting
4949
- wastedassign
5050
- whitespace
5151
disable-all: true

.prow.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,22 @@ presubmits:
8787
requests:
8888
memory: 4Gi
8989
cpu: 2
90+
91+
- name: pull-api-syncagent-test-e2e
92+
always_run: true
93+
decorate: true
94+
clone_uri: "https://github.com/kcp-dev/api-syncagent"
95+
labels:
96+
preset-goproxy: "true"
97+
spec:
98+
containers:
99+
- image: ghcr.io/kcp-dev/infra/build:1.23.5-1
100+
command:
101+
- hack/ci/run-e2e-tests.sh
102+
resources:
103+
requests:
104+
memory: 4Gi
105+
cpu: 2
106+
# docker-in-docker needs privileged mode
107+
securityContext:
108+
privileged: true

Makefile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $(BUILD_DEST)/%: cmd/%
4040
go build $(GOTOOLFLAGS) -o $@ ./cmd/$*
4141

4242
GOLANGCI_LINT = _tools/golangci-lint
43-
GOLANGCI_LINT_VERSION = 1.63.4
43+
GOLANGCI_LINT_VERSION = 1.64.2
4444

4545
.PHONY: $(GOLANGCI_LINT)
4646
$(GOLANGCI_LINT):
@@ -91,6 +91,23 @@ $(YQ):
9191
${YQ_VERSION} \
9292
yq_*
9393

94+
KCP = _tools/kcp
95+
KCP_VERSION = 0.26.1
96+
97+
.PHONY: $(KCP)
98+
$(KCP):
99+
@hack/download-tool.sh \
100+
https://github.com/kcp-dev/kcp/releases/download/v${KCP_VERSION}/kcp_${KCP_VERSION}_${GOOS}_${GOARCH}.tar.gz \
101+
kcp \
102+
${KCP_VERSION}
103+
104+
ENVTEST = _tools/setup-envtest
105+
ENVTEST_VERSION = release-0.19
106+
107+
.PHONY: $(ENVTEST)
108+
$(ENVTEST):
109+
@GO_MODULE=true hack/download-tool.sh sigs.k8s.io/controller-runtime/tools/setup-envtest setup-envtest $(ENVTEST_VERSION)
110+
94111
.PHONY: test
95112
test:
96113
./hack/run-tests.sh

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22.0
55
require (
66
github.com/Masterminds/sprig/v3 v3.3.0
77
github.com/evanphx/json-patch/v5 v5.9.0
8+
github.com/go-logr/logr v1.4.2
89
github.com/go-logr/zapr v1.3.0
910
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20240817110845-a9eb9752bfeb
1011
github.com/kcp-dev/client-go v0.0.0-20240912145314-f5949d81732a
@@ -44,7 +45,6 @@ require (
4445
github.com/fatih/color v1.18.0 // indirect
4546
github.com/fsnotify/fsnotify v1.7.0 // indirect
4647
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
47-
github.com/go-logr/logr v1.4.2 // indirect
4848
github.com/go-openapi/jsonpointer v0.19.6 // indirect
4949
github.com/go-openapi/jsonreference v0.20.2 // indirect
5050
github.com/go-openapi/swag v0.22.4 // indirect

hack/ci/run-e2e-tests.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 The KCP Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -euo pipefail
18+
source hack/lib.sh
19+
20+
# have a place to store things
21+
if [ -z "${ARTIFACTS:-}" ]; then
22+
ARTIFACTS=.e2e/artifacts
23+
mkdir -p "$ARTIFACTS"
24+
fi
25+
26+
echodate "Build artifacts will be placed in $ARTIFACTS."
27+
export ARTIFACTS="$(realpath "$ARTIFACTS")"
28+
29+
# build the agent, we will start it many times during the tests
30+
echodate "Building the api-syncagent…"
31+
make build
32+
33+
# get kube envtest binaries
34+
echodate "Setting up Kube binaries…"
35+
make _tools/setup-envtest
36+
export KUBEBUILDER_ASSETS="$(_tools/setup-envtest use 1.31.0 --bin-dir _tools -p path)"
37+
KUBEBUILDER_ASSETS="$(realpath "$KUBEBUILDER_ASSETS")"
38+
39+
# start a shared kcp process
40+
make _tools/kcp
41+
42+
KCP_ROOT_DIRECTORY=.kcp.e2e
43+
KCP_LOGFILE="$ARTIFACTS/kcp.log"
44+
KCP_TOKENFILE=hack/ci/testdata/e2e-kcp.tokens
45+
46+
echodate "Starting kcp…"
47+
rm -rf "$KCP_ROOT_DIRECTORY" "$KCP_LOGFILE"
48+
_tools/kcp start \
49+
-v4 \
50+
--token-auth-file "$KCP_TOKENFILE" \
51+
--root-directory "$KCP_ROOT_DIRECTORY" 1>"$KCP_LOGFILE" 2>&1 &
52+
53+
stop_kcp() {
54+
echodate "Stopping kcp processes (set \$KEEP_KCP=true to not do this)…"
55+
pkill -e kcp
56+
}
57+
58+
if [[ -v KEEP_KCP ]] && $KEEP_KCP; then
59+
echodate "\$KEEP_KCP is set, will not stop kcp once the script is finished."
60+
else
61+
append_trap stop_kcp EXIT
62+
fi
63+
64+
# make the token available to the Go tests
65+
export KCP_AGENT_TOKEN="$(grep e2e "$KCP_TOKENFILE" | cut -f1 -d,)"
66+
67+
# Wait for kcp to be ready; this env name is also hardcoded in the Go tests.
68+
export KCP_KUBECONFIG="$KCP_ROOT_DIRECTORY/admin.kubeconfig"
69+
70+
# the tenancy API becomes available pretty late during startup, so it's a good readiness check
71+
if ! retry_linear 3 20 kubectl --kubeconfig "$KCP_KUBECONFIG" get workspaces; then
72+
echodate "kcp never became ready."
73+
exit 1
74+
fi
75+
76+
# makes it easier to reference thesefiles from various _test.go files.
77+
export ROOT_DIRECTORY="$(realpath .)"
78+
export KCP_KUBECONFIG="$(realpath "$KCP_KUBECONFIG")"
79+
export AGENT_BINARY="$(realpath _build/api-syncagent)"
80+
81+
# time to run the tests
82+
echodate "Running e2e tests…"
83+
(set -x; go test -tags e2e -timeout 2h -v ./test/e2e/...)
84+
85+
echodate "Done. :-)"

hack/ci/testdata/e2e-kcp.tokens

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
topphemmelig,api-syncagent-e2e,1111-2222-3333-4444,"api-syncagents"

hack/download-tool.sh

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
set -euo pipefail
1818

1919
cd $(dirname $0)/..
20-
source hack/lib.sh
2120

2221
mkdir -p _tools
2322
cd _tools
@@ -26,6 +25,8 @@ URL="$1"
2625
BINARY="$2"
2726
VERSION="$3"
2827
BINARY_PATTERN="${4:-**/$BINARY}"
28+
GO_MODULE=${GO_MODULE:-false}
29+
UNCOMPRESSED=${UNCOMPRESSED:-false}
2930

3031
# Check if and what version we installed already.
3132
versionFile="$BINARY.version"
@@ -45,27 +46,31 @@ fi
4546
cd tmp
4647

4748
echo "Downloading $BINARY version $VERSION" >&2
48-
curl --fail --silent -LO "$URL"
49-
archive="$(ls)"
5049

51-
UNCOMPRESSED=${UNCOMPRESSED:-false}
50+
if $GO_MODULE; then
51+
GOBIN=$(realpath .) go install "$URL@$VERSION"
52+
mv * "../$BINARY"
53+
else
54+
curl --fail --silent -LO "$URL"
55+
archive="$(ls)"
5256

53-
if ! $UNCOMPRESSED; then
54-
case "$archive" in
55-
*.tar.gz | *.tgz)
56-
tar xzf "$archive"
57-
;;
58-
*.zip)
59-
unzip "$archive"
60-
;;
61-
*)
62-
echo "Unknown file type: $archive" >&2
63-
exit 1
64-
esac
65-
fi
57+
if ! $UNCOMPRESSED; then
58+
case "$archive" in
59+
*.tar.gz | *.tgz)
60+
tar xzf "$archive"
61+
;;
62+
*.zip)
63+
unzip "$archive"
64+
;;
65+
*)
66+
echo "Unknown file type: $archive" >&2
67+
exit 1
68+
esac
69+
fi
6670

67-
mv $BINARY_PATTERN ../$BINARY
68-
chmod +x ../$BINARY
71+
mv $BINARY_PATTERN ../$BINARY
72+
chmod +x ../$BINARY
73+
fi
6974
)
7075

7176
rm -rf tmp

hack/lib.sh

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ retry() {
3636
# Works only with bash but doesn't fail on other shells
3737
start_time=$(date +%s)
3838
set +e
39-
actual_retry $@
39+
# We use an extra wrapping to write junit and have a timer
40+
retry_backoff $@
4041
rc=$?
4142
set -e
4243
elapsed_time=$(($(date +%s) - $start_time))
4344
write_junit "$rc" "$elapsed_time"
4445
return $rc
4546
}
4647

47-
# We use an extra wrapping to write junit and have a timer
48-
actual_retry() {
48+
retry_backoff() {
4949
retries=$1
5050
shift
5151

@@ -66,9 +66,33 @@ actual_retry() {
6666
return 0
6767
}
6868

69+
retry_linear() {
70+
delay=$1
71+
retries=$2
72+
shift
73+
shift
74+
75+
count=0
76+
until "$@"; do
77+
rc=$?
78+
count=$((count + 1))
79+
if [ $count -lt "$retries" ]; then
80+
echodate "[$count/$retries] Command returned $rc, retrying…"
81+
sleep $delay
82+
else
83+
echodate "Command returned $rc, no more retries left."
84+
return $rc
85+
fi
86+
done
87+
88+
echodate "Command succeeded."
89+
90+
return 0
91+
}
92+
6993
echodate() {
7094
# do not use -Is to keep this compatible with macOS
71-
echo "[$(date +%Y-%m-%dT%H:%M:%S%:z)]" "$@"
95+
echo "[$(date +%Y-%m-%dT%H:%M:%S%:z)]" "$@" > /dev/stderr
7296
}
7397

7498
write_junit() {

hack/verify-boilerplate.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ _tools/boilerplate \
2828
-exclude internal/certificates/triple \
2929
-exclude sdk/clientset \
3030
-exclude sdk/informers \
31-
-exclude sdk/listers
31+
-exclude sdk/listers \
32+
-exclude test/crds

0 commit comments

Comments
 (0)