Skip to content

Commit 4babc49

Browse files
rarescosmalunkan93
authored andcommitted
Split network policies tests between Calico & Cilium (#2746)
1 parent 360f8b5 commit 4babc49

File tree

7 files changed

+239
-44
lines changed

7 files changed

+239
-44
lines changed

tests/common/cypress/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ type Cluster = 'sc' | 'wc'
22
type GrafanaRole = 'Admin' | 'Editor' | 'Viewer'
33

44
declare const yqArgumentsToConfigFiles: (cluster: Cluster, expression: string) => string
5-
declare const userToSession: (user: string) => string
65

76
/// <reference types="cypress" />
87

tests/end-to-end/grafana/dashboards-admin.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('grafana admin dashboards', function () {
3636

3737
it('open the NetworkPolicy Dashboard', function () {
3838
cy.yqDig('sc', '.networkPlugin.type').then((value) => {
39-
if (value == 'calico') {
39+
if (value === 'calico') {
4040
cy.testGrafanaDashboard('NetworkPolicy Dashboard', false)
4141
cy.get(
4242
'[data-testid="data-testid Panel menu Packets allowed by NetworkPolicy going from pod"]'

tests/end-to-end/netpol/netpol.cy.js renamed to tests/end-to-end/netpol/netpol-calico.cy.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ function makePrometheusURL(/** @type {Cluster} */ cluster) {
1010
)
1111
}
1212

13-
describe('workload cluster network policies', function () {
13+
describe('workload cluster network policies (calico)', function () {
14+
before(function () {
15+
cy.yqDig('wc', '.networkPlugin.type').then(function (value) {
16+
if (value !== 'calico') {
17+
this.skip('not a calico cluster')
18+
}
19+
})
20+
})
21+
1422
it('are not dropping any packets from workloads', function () {
1523
cy.request('GET', makeQueryURL('wc', DROP_QUERY)).then((response) => {
1624
assertNoDrops(response, 'fw', 'from')
@@ -33,7 +41,15 @@ describe('workload cluster network policies', function () {
3341
})
3442
})
3543

36-
describe('service cluster network policies', function () {
44+
describe('service cluster network policies (calico)', function () {
45+
before(function () {
46+
cy.yqDig('sc', '.networkPlugin.type').then(function (value) {
47+
if (value !== 'calico') {
48+
this.skip('not a calico cluster')
49+
}
50+
})
51+
})
52+
3753
it('are not dropping any packets from workloads', function () {
3854
cy.request('GET', makeQueryURL('sc', DROP_QUERY)).then((response) => {
3955
assertNoDrops(response, 'fw', 'from')
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bats
2+
3+
setup_file() {
4+
load "../../bats.lib.bash"
5+
6+
cypress_setup "${ROOT}/tests/end-to-end/netpol/netpol-calico.cy.js"
7+
}
8+
9+
setup() {
10+
load "../../bats.lib.bash"
11+
load_assert
12+
}
13+
14+
teardown_file() {
15+
cypress_teardown
16+
}
17+
18+
@test "workload cluster network policies (calico) are not dropping any packets from workloads" {
19+
cypress_test "workload cluster network policies (calico) are not dropping any packets from workloads"
20+
}
21+
22+
@test "workload cluster network policies (calico) are not dropping any packets to workloads" {
23+
cypress_test "workload cluster network policies (calico) are not dropping any packets to workloads"
24+
}
25+
26+
@test "workload cluster network policies (calico) are accepting allowed traffic" {
27+
cypress_test "workload cluster network policies (calico) are accepting allowed traffic"
28+
}
29+
30+
@test "service cluster network policies (calico) are not dropping any packets from workloads" {
31+
cypress_test "service cluster network policies (calico) are not dropping any packets from workloads"
32+
}
33+
34+
@test "service cluster network policies (calico) are not dropping any packets to workloads" {
35+
cypress_test "service cluster network policies (calico) are not dropping any packets to workloads"
36+
}
37+
38+
@test "service cluster network policies (calico) are accepting allowed traffic" {
39+
cypress_test "service cluster network policies (calico) are accepting allowed traffic"
40+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
const DROP_QUERY = 'round(increase(hubble_drop_total{reason="POLICY_DENIED"}[5m]))'
2+
const ACCEPT_QUERY =
3+
'sum by (traffic_direction) (round(increase(hubble_flows_processed_total{verdict="FORWARDED"}[5m])))'
4+
5+
function makePrometheusURL(/** @type {Cluster} */ cluster) {
6+
const port = cluster === 'wc' ? Cypress.env('WC_PROXY_PORT') : Cypress.env('SC_PROXY_PORT')
7+
8+
return (
9+
`http://127.0.0.1:${port}/api/v1/namespaces/monitoring/services` +
10+
'/kube-prometheus-stack-prometheus:9090/proxy'
11+
)
12+
}
13+
14+
describe('workload cluster network policies (cilium)', function () {
15+
before(function () {
16+
cy.yqDig('wc', '.networkPlugin.type').then(function (value) {
17+
if (value !== 'cilium') {
18+
this.skip('not a cilium cluster')
19+
}
20+
})
21+
})
22+
23+
it('are not dropping any packets from workloads', function () {
24+
cy.request('GET', makeQueryURL('wc', DROP_QUERY)).then((response) => {
25+
assertNoDrops(response, 'egress', 'from')
26+
})
27+
})
28+
29+
it('are not dropping any packets to workloads', function () {
30+
cy.request('GET', makeQueryURL('wc', DROP_QUERY)).then((response) => {
31+
assertNoDrops(response, 'ingress', 'to')
32+
})
33+
})
34+
35+
it('are accepting allowed traffic', function () {
36+
cy.retryRequest({
37+
request: { method: 'GET', url: makeQueryURL('wc', ACCEPT_QUERY) },
38+
condition: acceptCondition,
39+
waitTime: 10000,
40+
attempts: 30,
41+
})
42+
})
43+
})
44+
45+
describe('service cluster network policies (cilium)', function () {
46+
before(function () {
47+
cy.yqDig('sc', '.networkPlugin.type').then(function (value) {
48+
if (value !== 'cilium') {
49+
this.skip('not a cilium cluster')
50+
}
51+
})
52+
})
53+
54+
it('are not dropping any packets from workloads', function () {
55+
cy.request('GET', makeQueryURL('sc', DROP_QUERY)).then((response) => {
56+
assertNoDrops(response, 'egress', 'from')
57+
})
58+
})
59+
60+
it('are not dropping any packets to workloads', function () {
61+
cy.request('GET', makeQueryURL('sc', DROP_QUERY)).then((response) => {
62+
assertNoDrops(response, 'ingress', 'to')
63+
})
64+
})
65+
66+
it('are accepting allowed traffic', function () {
67+
cy.retryRequest({
68+
request: { method: 'GET', url: makeQueryURL('sc', ACCEPT_QUERY) },
69+
condition: acceptCondition,
70+
waitTime: 10000,
71+
attempts: 30,
72+
})
73+
})
74+
})
75+
76+
const makeQueryURL = (/** @type {Cluster} */ cluster, query, serverTime = '') => {
77+
const metric = encodeURI(query)
78+
let returnValue = `${makePrometheusURL(cluster)}/api/v1/query?query=${metric}`
79+
if (serverTime !== '') {
80+
returnValue = `${returnValue}&${new URLSearchParams({ time: serverTime })}`
81+
}
82+
return returnValue
83+
}
84+
85+
const assertNoDrops = (response, trafficDirection, direction) => {
86+
expect(response.status).to.eq(200)
87+
expect(response.body.data.result).to.be.a('array')
88+
89+
const result = response.body.data.result
90+
91+
const drops = result.filter(filterNonZero(trafficDirection)).map((element) => mapDrops(element))
92+
93+
if (drops.length > 0) {
94+
cy.fail(formatError(drops, direction))
95+
}
96+
}
97+
98+
const acceptCondition = (response) => {
99+
try {
100+
expect(response.status).to.eq(200)
101+
expect(response.body.data.result).to.be.a('array')
102+
103+
const result = response.body.data.result
104+
105+
const innerAssert = (values) => {
106+
expect(values).to.be.an('array')
107+
expect(values).to.have.property('0').that.is.a('number').and.is.greaterThan(0)
108+
}
109+
110+
innerAssert(
111+
result.filter(filterNonZero('egress')).map((item) => Number.parseInt(item.value[1]))
112+
)
113+
innerAssert(
114+
result.filter(filterNonZero('ingress')).map((item) => Number.parseInt(item.value[1]))
115+
)
116+
return true
117+
} catch {
118+
return false
119+
}
120+
}
121+
122+
const filterNonZero = (trafficDirection) => {
123+
return (item) =>
124+
item.metric.traffic_direction === trafficDirection && item.value && item.value[1] !== '0'
125+
}
126+
127+
const mapDrops = (item) => {
128+
return {
129+
podName: item.metric.pod,
130+
podNamespace: item.metric.namespace,
131+
drops: Number.parseInt(item.value[1]),
132+
}
133+
}
134+
135+
const formatError = (drops, direction) => {
136+
const fmtDrops = drops
137+
.map((item) => `- ${item.podNamespace}/${item.podName} had ${item.drops} dropped packets`)
138+
.join('\n')
139+
return `\nFound packets dropped ${direction} workloads:\n${fmtDrops}\n`
140+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bats
2+
3+
setup_file() {
4+
load "../../bats.lib.bash"
5+
6+
cypress_setup "${ROOT}/tests/end-to-end/netpol/netpol-cilium.cy.js"
7+
}
8+
9+
setup() {
10+
load "../../bats.lib.bash"
11+
load_assert
12+
}
13+
14+
teardown_file() {
15+
cypress_teardown
16+
}
17+
18+
@test "workload cluster network policies (cilium) are not dropping any packets from workloads" {
19+
cypress_test "workload cluster network policies (cilium) are not dropping any packets from workloads"
20+
}
21+
22+
@test "workload cluster network policies (cilium) are not dropping any packets to workloads" {
23+
cypress_test "workload cluster network policies (cilium) are not dropping any packets to workloads"
24+
}
25+
26+
@test "workload cluster network policies (cilium) are accepting allowed traffic" {
27+
cypress_test "workload cluster network policies (cilium) are accepting allowed traffic"
28+
}
29+
30+
@test "service cluster network policies (cilium) are not dropping any packets from workloads" {
31+
cypress_test "service cluster network policies (cilium) are not dropping any packets from workloads"
32+
}
33+
34+
@test "service cluster network policies (cilium) are not dropping any packets to workloads" {
35+
cypress_test "service cluster network policies (cilium) are not dropping any packets to workloads"
36+
}
37+
38+
@test "service cluster network policies (cilium) are accepting allowed traffic" {
39+
cypress_test "service cluster network policies (cilium) are accepting allowed traffic"
40+
}

tests/end-to-end/netpol/netpol.gen.bats

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)