Skip to content

test(component): add Test_34 NetworkNeighbors CIDR collapse e2e to Matthias CIDR Collapse#861

Open
entlein wants to merge 6 commits into
kubescape:mainfrom
k8sstormcenter:ct/networkneighbors-cidr-collapse
Open

test(component): add Test_34 NetworkNeighbors CIDR collapse e2e to Matthias CIDR Collapse#861
entlein wants to merge 6 commits into
kubescape:mainfrom
k8sstormcenter:ct/networkneighbors-cidr-collapse

Conversation

@entlein

@entlein entlein commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

End-to-end component test for CIDR-based collapsing of NetworkNeighbor entries (storage-side PreSave path). Models a curl client with two external destinations — an S3-style endpoint clustered in a single /24 and a broader endpoint spanning a /16 — and asserts each group collapses to its covering CIDR block. Reads the result via the dynamic client so the test compiles against storage types lacking the plural ipAddresses field and passes only when storage implements the collapse.

How to Test

this is only a test file, it does not touch the prod code-base

Depends on

kubescape/storage#348 and kubescape/storage#349

Summary by CodeRabbit

  • Tests
    • Added end-to-end coverage for collapsing network-neighborhood egress IP ranges into CIDR representations, including threshold/floor behavior and persistence validation.
    • Verifies expected CIDR entries after collapsing (including splitting when the floor changes) and that targeted host entries are removed.
    • Adds scenario deployments to generate IPv4 and IPv6 egress patterns used by the new test.

@coderabbitai

coderabbitai Bot commented Jul 23, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Added end-to-end component coverage for NetworkNeighborhood CIDR collapsing. The test configures collapse thresholds, generates IPv4 and IPv6 traffic with learner workloads, reads persisted results dynamically, and validates CIDR splitting and removal of covered host entries.

Changes

NetworkNeighborhood CIDR collapse

Layer / File(s) Summary
Collapse configuration and collection helpers
tests/component_test.go
Adds dynamic resource access, CollapseConfiguration create-or-update handling, learner deployment setup, and extraction of persisted CIDR and host entries.
CIDR learner workload fixtures
tests/resources/networkneighbors-*.yaml
Adds BusyBox Deployments that probe dense, split-range, scattered, and IPv6 address sets over TCP/443.
End-to-end collapse assertions
tests/component_test.go
Adds validation across /16 and /28 floors, checking exact CIDR covers, removal of covered host entries, and optional IPv6 results; also reformats an existing labels map.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ComponentTest
  participant DynamicClient
  participant LearnerDeployments
  participant NetworkNeighborhood
  ComponentTest->>DynamicClient: apply CollapseConfiguration
  ComponentTest->>LearnerDeployments: deploy CIDR probe workloads
  LearnerDeployments->>NetworkNeighborhood: generate observed egress IPs
  ComponentTest->>NetworkNeighborhood: wait for completion and read object
  NetworkNeighborhood-->>ComponentTest: collapsed CIDRs and remaining host entries
  ComponentTest->>ComponentTest: assert expected IPv4 and IPv6 coverage
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding an end-to-end NetworkNeighbors CIDR collapse test.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@entlein
entlein marked this pull request as draft July 23, 2026 12:42

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/component_test.go (1)

3325-3339: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Collect the collapsed payload after Eventually succeeds.

require.Eventually runs the condition in a goroutine, while finalRaw is later read in the test goroutine after passing the predicate. Capture the GET/Update result when the condition finally returns true, or re-GET once the poll succeeds, instead of letting a condition goroutine mutate captured test state.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/component_test.go` around lines 3325 - 3339, Avoid mutating the
captured finalRaw variable inside the require.Eventually predicate. Let the
predicate only verify that the GET payload contains wantS3CIDR and
wantBroadCIDR, then re-GET the resource after Eventually succeeds and marshal
that result into finalRaw in the test goroutine.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/component_test.go`:
- Around line 3302-3321: Add cleanup to the test using NewRandomNamespace so
both the created NetworkNeighborhood and ns.Name namespace are deleted
automatically via t.Cleanup or defer. Register cleanup after each resource is
successfully created, using the appropriate storageClient NetworkNeighborhoods
and Namespaces operations, while preserving the existing test behavior.

---

Nitpick comments:
In `@tests/component_test.go`:
- Around line 3325-3339: Avoid mutating the captured finalRaw variable inside
the require.Eventually predicate. Let the predicate only verify that the GET
payload contains wantS3CIDR and wantBroadCIDR, then re-GET the resource after
Eventually succeeds and marshal that result into finalRaw in the test goroutine.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f0420f63-cb6e-4c21-b4a5-b2ab07e74ebc

📥 Commits

Reviewing files that changed from the base of the PR and between 6a86345 and d0dd055.

📒 Files selected for processing (1)
  • tests/component_test.go

Comment thread tests/component_test.go Outdated
Entlein added 2 commits July 23, 2026 14:46
End-to-end component test for CIDR-based collapsing of NetworkNeighbor
entries (storage-side PreSave path). Models a curl client with two external
destinations — an S3-style endpoint clustered in a single /24 and a broader
endpoint spanning a /16 — and asserts each group collapses to its covering
CIDR block. Reads the result via the dynamic client so the test compiles
against storage types lacking the plural ipAddresses field and passes only
when storage implements the collapse.

Signed-off-by: entlein <einentlein@gmail.com>
…+ dump on failure

Storage wires SetCollapseSettings(collapseSettingsFromCRD), so with no CR
present the effective network threshold isn't the assumed 50 default and the
groups never collapsed. Apply an explicit CollapseConfiguration (default) with
networkIPGroupThreshold=5, and replace the silent Eventually with a manual poll
that logs the stored NetworkNeighborhood if collapse never appears, so a failing
run shows whether the collapse ran with unexpected CIDRs or not at all.

Signed-off-by: entlein <einentlein@gmail.com>
@entlein
entlein force-pushed the ct/networkneighbors-cidr-collapse branch from e4c55ae to b63fc26 Compare July 23, 2026 12:46
entlein added 4 commits July 23, 2026 15:30
Signed-off-by: entlein <einentlein@gmail.com>
…pected NN as a fixutre

Signed-off-by: entlein <einentlein@gmail.com>
…dpoints AFTER the collapsconfig was changed

Signed-off-by: entlein <einentlein@gmail.com>
Signed-off-by: entlein <einentlein@gmail.com>
@entlein
entlein marked this pull request as ready for review July 23, 2026 19:26

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
tests/resources/networkneighbors-cidr-fanout.yaml (1)

17-25: 🔒 Security & Privacy | 🔵 Trivial | ⚡ Quick win

Run all learner fixtures with a restricted security context.

These nc loops do not need root or privilege escalation, yet every fixture defaults to both. They will also be rejected by Pod Security Restricted enforcement. Add runAsNonRoot, a non-root UID, RuntimeDefault seccomp, allowPrivilegeEscalation: false, and dropped capabilities consistently.

  • tests/resources/networkneighbors-cidr-fanout.yaml#L17-L25: add pod/container security contexts.
  • tests/resources/networkneighbors-cidr-spread.yaml#L17-L26: add pod/container security contexts.
  • tests/resources/networkneighbors-s3-27.yaml#L17-L23: add pod/container security contexts.
  • tests/resources/networkneighbors-s3-28.yaml#L17-L23: add pod/container security contexts.
  • tests/resources/networkneighbors-scattered.yaml#L17-L24: add pod/container security contexts.
  • tests/resources/networkneighbors-v6-124.yaml#L17-L23: add pod/container security contexts.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/resources/networkneighbors-cidr-fanout.yaml` around lines 17 - 25,
Apply restricted pod and container security contexts to the learner fixtures in
tests/resources/networkneighbors-cidr-fanout.yaml:17-25,
tests/resources/networkneighbors-cidr-spread.yaml:17-26,
tests/resources/networkneighbors-s3-27.yaml:17-23,
tests/resources/networkneighbors-s3-28.yaml:17-23,
tests/resources/networkneighbors-scattered.yaml:17-24, and
tests/resources/networkneighbors-v6-124.yaml:17-23. Configure runAsNonRoot, a
non-root UID, RuntimeDefault seccomp, allowPrivilegeEscalation false, and
dropped capabilities consistently at the appropriate pod/container levels.

Source: Linters/SAST tools

tests/resources/networkneighbors-cidr-spread.yaml (1)

17-26: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Pin the remaining BusyBox learner fixtures.

tests/resources/networkneighbors-cidr-spread.yaml, tests/resources/networkneighbors-s3-27.yaml, tests/resources/networkneighbors-s3-28.yaml, tests/resources/networkneighbors-scattered.yaml, and tests/resources/networkneighbors-v6-124.yaml still use mutable image: busybox; align them with the existing busybox:1.36 fixture.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/resources/networkneighbors-cidr-spread.yaml` around lines 17 - 26,
Replace the mutable busybox image reference with busybox:1.36 in the container
fixtures at tests/resources/networkneighbors-cidr-spread.yaml (17-26),
tests/resources/networkneighbors-s3-27.yaml (17-23),
tests/resources/networkneighbors-s3-28.yaml (17-23),
tests/resources/networkneighbors-scattered.yaml (17-24), and
tests/resources/networkneighbors-v6-124.yaml (17-23); make no other fixture
changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/component_test.go`:
- Around line 3380-3382: Update the cleanup around applyCollapseFloor to
snapshot whether the default CollapseConfiguration exists and retain its
original spec before mutation. In the t.Cleanup callback, restore the saved spec
when the resource pre-existed, and delete it only when this test created it;
preserve the existing dynamic client and ccCollapseGVR usage.

---

Nitpick comments:
In `@tests/resources/networkneighbors-cidr-fanout.yaml`:
- Around line 17-25: Apply restricted pod and container security contexts to the
learner fixtures in tests/resources/networkneighbors-cidr-fanout.yaml:17-25,
tests/resources/networkneighbors-cidr-spread.yaml:17-26,
tests/resources/networkneighbors-s3-27.yaml:17-23,
tests/resources/networkneighbors-s3-28.yaml:17-23,
tests/resources/networkneighbors-scattered.yaml:17-24, and
tests/resources/networkneighbors-v6-124.yaml:17-23. Configure runAsNonRoot, a
non-root UID, RuntimeDefault seccomp, allowPrivilegeEscalation false, and
dropped capabilities consistently at the appropriate pod/container levels.

In `@tests/resources/networkneighbors-cidr-spread.yaml`:
- Around line 17-26: Replace the mutable busybox image reference with
busybox:1.36 in the container fixtures at
tests/resources/networkneighbors-cidr-spread.yaml (17-26),
tests/resources/networkneighbors-s3-27.yaml (17-23),
tests/resources/networkneighbors-s3-28.yaml (17-23),
tests/resources/networkneighbors-scattered.yaml (17-24), and
tests/resources/networkneighbors-v6-124.yaml (17-23); make no other fixture
changes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 069c2cbd-97db-467c-bb35-0b5e85edfd2a

📥 Commits

Reviewing files that changed from the base of the PR and between d0dd055 and 01a55ef.

📒 Files selected for processing (7)
  • tests/component_test.go
  • tests/resources/networkneighbors-cidr-fanout.yaml
  • tests/resources/networkneighbors-cidr-spread.yaml
  • tests/resources/networkneighbors-s3-27.yaml
  • tests/resources/networkneighbors-s3-28.yaml
  • tests/resources/networkneighbors-scattered.yaml
  • tests/resources/networkneighbors-v6-124.yaml

Comment thread tests/component_test.go
Comment on lines +3380 to +3382
k8sClient := k8sinterface.NewKubernetesApi()
dyn := dynamic.NewForConfigOrDie(k8sClient.K8SConfig)
t.Cleanup(func() { _ = dyn.Resource(ccCollapseGVR).Delete(context.Background(), "default", metav1.DeleteOptions{}) })

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Restore a pre-existing CollapseConfiguration.

applyCollapseFloor updates default when it already exists, but Line 3382 always deletes it. Running this test can therefore remove cluster configuration owned by the environment or another test. Snapshot whether default existed before mutation; restore its previous spec on cleanup, and delete it only when this test created it.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/component_test.go` around lines 3380 - 3382, Update the cleanup around
applyCollapseFloor to snapshot whether the default CollapseConfiguration exists
and retain its original spec before mutation. In the t.Cleanup callback, restore
the saved spec when the resource pre-existed, and delete it only when this test
created it; preserve the existing dynamic client and ccCollapseGVR usage.

@entlein

entlein commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Collapse benchmark

The component run above is green (--- PASS: Test_34_NetworkNeighborsCIDRCollapse (380.97s)).

collapseIPGroups runs once per NetworkNeighborhood save (PreSave). go test -bench -benchmem (linux/arm64; treat as order-of-magnitude):

input ns/op B/op allocs/op
collapse — 200 hosts + 2 CIDRs, /16 floor ~76µs 228 KB 284
collapse — same, /24 floor ~250µs 388 KB 2836
cover — 256 scattered hosts, /16 floor ~59µs 77 KB 541
cover — same, /24 floor ~60µs 88 KB 924

The /24 rows are the worst case: a held /16 pass-through splits into 256 /24 children under the tighter floor (bounded per prefix). Realistic incremental input — pass-through blocks already floor-capped — tracks the /16 rows. Negligible for a write-time path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant