Skip to content

Commit 51f16ae

Browse files
authored
Add e2e tests for Prefix (#161)
Mainly focusing on IPv4
1 parent b7bd71f commit 51f16ae

File tree

25 files changed

+903
-18
lines changed

25 files changed

+903
-18
lines changed

.github/env

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
golang-version=1.23
2+
kind-version=v0.25.0
3+
kind-image=kindest/node:v1.31.2

.github/workflows/e2e-tests.yaml

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Modified from https://github.com/prometheus-operator/prometheus-operator/blob/main/.github/workflows/e2e-feature-gated.yaml
2+
name: e2e-tests
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- v*
9+
pull_request:
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
12+
cancel-in-progress: true
13+
env:
14+
NETBOX_HOST: demo.netbox.dev
15+
AUTH_TOKEN: 0123456789abcdef0123456789abcdef01234567
16+
POD_NAMESPACE: default
17+
HTTPS_ENABLE: true
18+
NETBOX_RESTORATION_HASH_FIELD_NAME: netboxOperatorRestorationHash
19+
jobs:
20+
e2e-tests:
21+
name: E2E tests for netbox operator
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
25+
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
26+
with:
27+
go-version: 1.23.4
28+
- name: Import environment variables from file
29+
run: |
30+
cat ".github/env" >> "$GITHUB_ENV"
31+
echo "E2E_DIAGNOSTIC_DIRECTORY=$(mktemp -d)" >> "$GITHUB_ENV"
32+
- name: Start kind cluster
33+
uses: helm/kind-action@0025e74a8c7512023d06dc019c617aa3cf561fde # v1.10.0
34+
with:
35+
version: ${{ env.kind-version }}
36+
node_image: ${{ env.kind-image }}
37+
wait: 300s
38+
config: ./tests/e2e/kind-config.yaml
39+
cluster_name: e2e
40+
- name: Wait for cluster to finish bootstraping
41+
run: |
42+
echo "Waiting for all nodes to be ready..."
43+
kubectl wait --for=condition=Ready nodes --all --timeout=120s
44+
kubectl get nodes
45+
echo "Waiting for all pods to be ready..."
46+
kubectl wait --for=condition=Ready pods --all --all-namespaces --timeout=300s
47+
kubectl get pods -A
48+
echo "Cluster information"
49+
kubectl cluster-info
50+
- name: Setup kind cluster with required software such as NetBox
51+
run: |
52+
make create-kind
53+
- name: Deploy NetBox operator to the kind cluster
54+
run: |
55+
make deploy-kind
56+
- name: Run tests
57+
env:
58+
E2E_DIAGNOSTIC_DIRECTORY: ${{ env.E2E_DIAGNOSTIC_DIRECTORY }}
59+
run: |
60+
# # Very straight forward way of implementing a test and checking the result
61+
# kubectl apply -f config/samples/netbox_v1_prefixclaim.yaml
62+
# kubectl get prefixclaim,prefix,ipaddressclaim,ipaddress,iprange,iprangeclaim
63+
# kubectl wait --for=condition=ready --timeout=30s prefixclaim.netbox.dev/prefixclaim-sample
64+
65+
# Use Chainsaw
66+
make test-e2e
67+
- name: Upload diagnostics artifact
68+
if: ${{ failure() }}
69+
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
70+
with:
71+
name: cluster-state
72+
path: ${{ env.E2E_DIAGNOSTIC_DIRECTORY }}
73+
retention-days: 15

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Dockerfile.cross
99

1010
# Test binary, built with `go test -c`
1111
*.test
12+
database.sql
1213

1314
# Output of the go coverage tool, specifically when used with LiteIDE
1415
*.out

CONTRIBUTING.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ To optionally access the NetBox UI:
2626
- Port-forward NetBox: `kubectl port-forward deploy/netbox 8080:8080 -n default`
2727
- Open <http://localhost:8080> in your favorite browser and log in with the username `admin` and password `admin`, you will be able to access the local NetBox instance running in the kind cluster.
2828
- Open a new terminal window and export the following environment variables:
29-
30-
```bash
31-
export NETBOX_HOST="localhost:8080"
32-
export AUTH_TOKEN="0123456789abcdef0123456789abcdef01234567"
33-
export POD_NAMESPACE="default"
34-
export HTTPS_ENABLE="false"
35-
export NETBOX_RESTORATION_HASH_FIELD_NAME="netboxOperatorRestorationHash"
36-
```
29+
```bash
30+
export NETBOX_HOST="localhost:8080"
31+
export AUTH_TOKEN="0123456789abcdef0123456789abcdef01234567"
32+
export POD_NAMESPACE="default"
33+
export HTTPS_ENABLE="false"
34+
export NETBOX_RESTORATION_HASH_FIELD_NAME="netboxOperatorRestorationHash"
35+
```
3736

3837
- Run the NetBox Operator locally `make install && make run`
3938

@@ -46,14 +45,13 @@ Note: This requires a running NetBox instance that you can use (e.g. <https://de
4645
- Open <https://demo.netbox.dev/user/api-tokens/> and create a token "0123456789abcdef0123456789abcdef01234567" with default settings
4746
- Open <https://demo.netbox.dev/extras/custom-fields/add/> and create a custom field called "netboxOperatorRestorationHash" for Object types "IPAM > IP Address" and "IPAM > Prefix"
4847
- Open a new terminal window and export the following environment variables:
49-
50-
```bash
51-
export NETBOX_HOST="demo.netbox.dev"
52-
export AUTH_TOKEN="0123456789abcdef0123456789abcdef01234567"
53-
export POD_NAMESPACE="default"
54-
export HTTPS_ENABLE="true"
55-
export NETBOX_RESTORATION_HASH_FIELD_NAME="netboxOperatorRestorationHash"
56-
```
48+
```bash
49+
export NETBOX_HOST="demo.netbox.dev"
50+
export AUTH_TOKEN="0123456789abcdef0123456789abcdef01234567"
51+
export POD_NAMESPACE="default"
52+
export HTTPS_ENABLE="true"
53+
export NETBOX_RESTORATION_HASH_FIELD_NAME="netboxOperatorRestorationHash"
54+
```
5755

5856
- Run the NetBox Operator locally `make install && make run`
5957

@@ -130,3 +128,9 @@ make undeploy
130128
Run `make help` for more information on all potential `make` targets
131129

132130
More information can be found via the [Kubebuilder Documentation](https://book.kubebuilder.io/introduction.html)
131+
132+
## Running e2e tests locally
133+
134+
Please read the [README in the e2e test directory] for more information!
135+
136+
[README in the e2e test directory]: ./tests/e2e/README.md

Makefile

+15-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ install-$(GO_PACKAGE_NAME_GOLANGCI_LINT):
5050
echo "$(GO_PACKAGE_NAME_GOLANGCI_LINT) is installed" ; \
5151
fi
5252

53+
# check if chainsaw is installed or not
54+
GO_PACKAGE_NAME_CHAINSAW := chainsaw
55+
install-$(GO_PACKAGE_NAME_CHAINSAW):
56+
@if [ ! -x "$(GOBIN)/$(GO_PACKAGE_NAME_CHAINSAW)" ]; then \
57+
echo "Installing $(GO_PACKAGE_NAME_CHAINSAW)..." ; \
58+
go install github.com/kyverno/[email protected] ; \
59+
else \
60+
echo "$(GO_PACKAGE_NAME_CHAINSAW) is installed" ; \
61+
fi
62+
5363
.PHONY: all
5464
all: build
5565

@@ -223,6 +233,10 @@ envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
223233
$(ENVTEST): $(LOCALBIN)
224234
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@87bcfec
225235

226-
generate_mocks: # TODO: auto install go install go.uber.org/mock/mockgen@latest
236+
generate_mocks: ## TODO: auto install go install go.uber.org/mock/mockgen@latest
227237
mkdir -p ${GEN_DIR}
228238
mockgen -destination ${GEN_DIR}/${NETBOX_MOCKS_OUTPUT_FILE} -source=${INTERFACE_DEFITIONS_DIR}
239+
240+
.PHONY: test-e2e
241+
test-e2e: install-$(GO_PACKAGE_NAME_CHAINSAW)
242+
chainsaw test --namespace e2e

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ Please read [ParentPrefixSelector guide] for more information!
6464

6565
[ParentPrefixSelector guide]: ./ParentPrefixSelectorGuide.md
6666

67-
6867
# Project Distribution
6968

7069
Following are the steps to build the installer and distribute this project to users.

kind/load-data-job/local-demo-data.sql

+8
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ VALUES ('2024-06-14 09:57:11.709344+00', '2024-06-14 09:57:11.709359+00', '{"cus
6363
INSERT INTO public.ipam_prefix (created, last_updated, custom_field_data, prefix, status, is_pool, description, role_id, site_id, tenant_id, vlan_id, vrf_id, _children, _depth, mark_utilized, comments)
6464
VALUES ('2024-06-14 10:01:10.094083+00', '2024-06-14 10:01:10.094095+00', '{}', '2.0.0.0/16', 'active', false, '', NULL, NULL, 100, NULL, NULL, 0, 0, false, '');
6565

66+
-- 2.1.0.0/24
67+
INSERT INTO public.ipam_prefix (created, last_updated, custom_field_data, prefix, status, is_pool, description, role_id, site_id, tenant_id, vlan_id, vrf_id, _children, _depth, mark_utilized, comments)
68+
VALUES ('2024-06-14 10:01:10.094083+00', '2024-06-14 10:01:10.094095+00', '{}', '2.1.0.0/24', 'active', false, '', NULL, NULL, 100, NULL, NULL, 0, 0, false, '');
69+
70+
-- 2.2.0.0/24
71+
INSERT INTO public.ipam_prefix (created, last_updated, custom_field_data, prefix, status, is_pool, description, role_id, site_id, tenant_id, vlan_id, vrf_id, _children, _depth, mark_utilized, comments)
72+
VALUES ('2024-06-14 10:01:10.094083+00', '2024-06-14 10:01:10.094095+00', '{}', '2.2.0.0/24', 'active', false, '', NULL, NULL, 100, NULL, NULL, 0, 0, false, '');
73+
6674
-- 3.0.0.0/24 - 3.0.8.0/24 (watch out for the upper/lower-case)
6775
-- Pool 1, Production (IPv4)
6876
INSERT INTO public.ipam_prefix (created, last_updated, custom_field_data, prefix, status, is_pool, description, role_id, site_id, tenant_id, vlan_id, vrf_id, _children, _depth, mark_utilized, comments)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
apiVersion: chainsaw.kyverno.io/v1alpha1
2+
kind: Test
3+
metadata:
4+
name: prefixclaim-ipv4-apply-exhausted
5+
spec:
6+
steps:
7+
- name: Install CR 1
8+
description: Apply prefix claim CR 1
9+
try:
10+
- apply:
11+
file: netbox_v1_prefixclaim_1.yaml
12+
- assert:
13+
resource:
14+
apiVersion: netbox.dev/v1
15+
kind: PrefixClaim
16+
metadata:
17+
name: prefixclaim-ipv4-apply-prefixexhausted-1
18+
spec:
19+
comments: your comments
20+
description: some description
21+
parentPrefix: 2.1.0.0/24
22+
prefixLength: /25
23+
preserveInNetbox: false
24+
site: DM-Akron
25+
tenant: Dunder-Mifflin, Inc.
26+
status:
27+
parentPrefix: 2.1.0.0/24
28+
prefix: 2.1.0.0/25
29+
prefixName: prefixclaim-ipv4-apply-prefixexhausted-1
30+
- assert:
31+
resource:
32+
apiVersion: netbox.dev/v1
33+
kind: Prefix
34+
metadata:
35+
name: prefixclaim-ipv4-apply-prefixexhausted-1
36+
spec:
37+
comments: your comments
38+
description: some description
39+
prefix: 2.1.0.0/25
40+
site: DM-Akron
41+
tenant: Dunder-Mifflin, Inc.
42+
- name: Install CR 2
43+
description: Apply prefix claim CR 2
44+
try:
45+
- apply:
46+
file: netbox_v1_prefixclaim_2.yaml
47+
- assert:
48+
resource:
49+
apiVersion: netbox.dev/v1
50+
kind: PrefixClaim
51+
metadata:
52+
name: prefixclaim-ipv4-apply-prefixexhausted-2
53+
spec:
54+
comments: your comments
55+
description: some description
56+
parentPrefix: 2.1.0.0/24
57+
prefixLength: /25
58+
preserveInNetbox: false
59+
site: DM-Akron
60+
tenant: Dunder-Mifflin, Inc.
61+
status:
62+
parentPrefix: 2.1.0.0/24
63+
prefix: 2.1.0.128/25
64+
prefixName: prefixclaim-ipv4-apply-prefixexhausted-2
65+
- assert:
66+
resource:
67+
apiVersion: netbox.dev/v1
68+
kind: Prefix
69+
metadata:
70+
name: prefixclaim-ipv4-apply-prefixexhausted-2
71+
spec:
72+
comments: your comments
73+
description: some description
74+
prefix: 2.1.0.128/25
75+
site: DM-Akron
76+
tenant: Dunder-Mifflin, Inc.
77+
- name: Install CR 3
78+
description: Apply prefix claim CR 3
79+
try:
80+
- apply:
81+
file: netbox_v1_prefixclaim_3.yaml
82+
- assert:
83+
resource:
84+
apiVersion: v1
85+
count: 1
86+
kind: Event
87+
type: Warning
88+
reason: PrefixCRNotCreated
89+
source:
90+
component: prefix-claim-controller
91+
message: Failed to fetch new Prefix from NetBox. parent prefix exhausted, will restart the parent prefix selection process
92+
involvedObject:
93+
apiVersion: netbox.dev/v1
94+
kind: PrefixClaim
95+
name: prefixclaim-ipv4-apply-prefixexhausted-3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: netbox.dev/v1
2+
kind: PrefixClaim
3+
metadata:
4+
labels:
5+
app.kubernetes.io/name: netbox-operator
6+
app.kubernetes.io/managed-by: kustomize
7+
name: prefixclaim-ipv4-apply-prefixexhausted-1
8+
spec:
9+
tenant: "Dunder-Mifflin, Inc."
10+
site: "DM-Akron"
11+
description: "some description"
12+
comments: "your comments"
13+
preserveInNetbox: false
14+
parentPrefix: "2.1.0.0/24"
15+
prefixLength: "/25"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: netbox.dev/v1
2+
kind: PrefixClaim
3+
metadata:
4+
labels:
5+
app.kubernetes.io/name: netbox-operator
6+
app.kubernetes.io/managed-by: kustomize
7+
name: prefixclaim-ipv4-apply-prefixexhausted-2
8+
spec:
9+
tenant: "Dunder-Mifflin, Inc."
10+
site: "DM-Akron"
11+
description: "some description"
12+
comments: "your comments"
13+
preserveInNetbox: false
14+
parentPrefix: "2.1.0.0/24"
15+
prefixLength: "/25"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: netbox.dev/v1
2+
kind: PrefixClaim
3+
metadata:
4+
labels:
5+
app.kubernetes.io/name: netbox-operator
6+
app.kubernetes.io/managed-by: kustomize
7+
name: prefixclaim-ipv4-apply-prefixexhausted-3
8+
spec:
9+
tenant: "Dunder-Mifflin, Inc."
10+
site: "DM-Akron"
11+
description: "some description"
12+
comments: "your comments"
13+
preserveInNetbox: false
14+
parentPrefix: "2.1.0.0/24"
15+
prefixLength: "/25"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apiVersion: chainsaw.kyverno.io/v1alpha1
2+
kind: Test
3+
metadata:
4+
name: prefixclaim-ipv4-parentprefixselector-apply-succeed
5+
spec:
6+
steps:
7+
- name: Apply prefix claim CR and go through each field and check for equality
8+
try:
9+
- apply:
10+
file: netbox_v1_prefixclaim.yaml
11+
- assert:
12+
resource:
13+
apiVersion: netbox.dev/v1
14+
kind: PrefixClaim
15+
metadata:
16+
name: prefixclaim-ipv4-parentprefixselector-sample
17+
spec:
18+
comments: your comments
19+
description: some description
20+
parentPrefixSelector:
21+
environment: Production
22+
family: IPv4
23+
poolName: Pool 1
24+
site: DM-Buffalo
25+
tenant: MY_TENANT
26+
prefixLength: /31
27+
preserveInNetbox: true
28+
site: DM-Akron
29+
tenant: MY_TENANT
30+
status:
31+
parentPrefix: 3.0.1.0/24
32+
prefix: 3.0.1.0/31
33+
prefixName: prefixclaim-ipv4-parentprefixselector-sample
34+
- assert:
35+
resource:
36+
apiVersion: netbox.dev/v1
37+
kind: Prefix
38+
metadata:
39+
name: prefixclaim-ipv4-parentprefixselector-sample
40+
spec:
41+
comments: your comments
42+
description: some description
43+
prefix: 3.0.1.0/31
44+
preserveInNetbox: true
45+
tenant: MY_TENANT
46+
customFields:
47+
netboxOperatorRestorationHash: b0d7d37281d2f2735ff188bdfa4eda469e55b684

0 commit comments

Comments
 (0)