Skip to content

Commit 936951c

Browse files
committed
Put extended tests into separate test files
Signed-off-by: Dyanngg <[email protected]>
1 parent fd118ad commit 936951c

4 files changed

+167
-106
lines changed

conformance/tests/admin-network-policy-core-egress-tcp-rules.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -217,56 +217,3 @@ var AdminNetworkPolicyEgressTCP = suite.ConformanceTest{
217217
})
218218
},
219219
}
220-
221-
var AdminNetworkPolicyEgressNamedPort = suite.ConformanceTest{
222-
ShortName: "AdminNetworkPolicyEgressNamedPort",
223-
Description: "Tests support for egress traffic on a named port using admin network policy API based on a server and client model",
224-
Features: []suite.SupportedFeature{
225-
suite.SupportAdminNetworkPolicy,
226-
suite.SupportAdminNetworkPolicyNamedPorts,
227-
},
228-
Manifests: []string{"base/admin_network_policy/core-egress-tcp-rules.yaml"},
229-
Test: func(t *testing.T, s *suite.ConformanceTestSuite) {
230-
231-
t.Run("Should support an 'allow-egress' policy for named port", func(t *testing.T) {
232-
ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout)
233-
defer cancel()
234-
// This test uses `egress-tcp` ANP
235-
// cedric-diggory-1 is our server pod in hufflepuff namespace
236-
serverPod := &v1.Pod{}
237-
err := s.Client.Get(ctx, client.ObjectKey{
238-
Namespace: "network-policy-conformance-hufflepuff",
239-
Name: "cedric-diggory-1",
240-
}, serverPod)
241-
require.NoErrorf(t, err, "unable to fetch the server pod")
242-
anp := &v1alpha1.AdminNetworkPolicy{}
243-
err = s.Client.Get(ctx, client.ObjectKey{
244-
Name: "egress-tcp",
245-
}, anp)
246-
require.NoErrorf(t, err, "unable to fetch the admin network policy")
247-
namedPortRule := anp.DeepCopy().Spec.Egress[5]
248-
webPort := "web"
249-
// replace the tcp port 8080 rule as named port rule which translate to tcp port 80 instead
250-
namedPortRule.Ports = &[]v1alpha1.AdminNetworkPolicyPort{
251-
{
252-
NamedPort: &webPort,
253-
},
254-
}
255-
anp.Spec.Egress[5] = namedPortRule
256-
err = s.Client.Update(ctx, anp)
257-
require.NoErrorf(t, err, "unable to update the admin network policy")
258-
// harry-potter-0 is our client pod in gryffindor namespace
259-
// ensure egress is ALLOWED to hufflepuff from gryffindor at the web port, which is defined as TCP at port 80 in pod spec
260-
// egressRule at index5 should take effect
261-
success := kubernetes.PokeServer(t, s.ClientSet, &s.KubeConfig, "network-policy-conformance-gryffindor", "harry-potter-0", "tcp",
262-
serverPod.Status.PodIP, int32(80), s.TimeoutConfig.RequestTimeout, true)
263-
assert.True(t, success)
264-
// harry-potter-1 is our client pod in gryffindor namespace
265-
// ensure egress is DENIED to hufflepuff from gryffindor for rest of the traffic; egressRule at index6 should take effect
266-
success = kubernetes.PokeServer(t, s.ClientSet, &s.KubeConfig, "network-policy-conformance-gryffindor", "harry-potter-1", "tcp",
267-
serverPod.Status.PodIP, int32(8080), s.TimeoutConfig.RequestTimeout, false)
268-
assert.True(t, success)
269-
})
270-
271-
},
272-
}

conformance/tests/admin-network-policy-core-ingress-udp-rules.go

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -217,56 +217,3 @@ var AdminNetworkPolicyIngressUDP = suite.ConformanceTest{
217217
})
218218
},
219219
}
220-
221-
var AdminNetworkPolicyIngressNamedPort = suite.ConformanceTest{
222-
ShortName: "AdminNetworkPolicyIngressNamedPort",
223-
Description: "Tests support for ingress traffic on a named port using admin network policy API based on a server and client model",
224-
Features: []suite.SupportedFeature{
225-
suite.SupportAdminNetworkPolicy,
226-
suite.SupportAdminNetworkPolicyNamedPorts,
227-
},
228-
Manifests: []string{"base/admin_network_policy/core-ingress-udp-rules.yaml"},
229-
Test: func(t *testing.T, s *suite.ConformanceTestSuite) {
230-
231-
t.Run("Should support an 'allow-ingress' policy for named port", func(t *testing.T) {
232-
ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout)
233-
defer cancel()
234-
// This test uses `ingress-udp` ANP
235-
// cedric-diggory-1 is our server pod in hufflepuff namespace
236-
serverPod := &v1.Pod{}
237-
err := s.Client.Get(ctx, client.ObjectKey{
238-
Namespace: "network-policy-conformance-hufflepuff",
239-
Name: "cedric-diggory-1",
240-
}, serverPod)
241-
require.NoErrorf(t, err, "unable to fetch the server pod")
242-
anp := &v1alpha1.AdminNetworkPolicy{}
243-
err = s.Client.Get(ctx, client.ObjectKey{
244-
Name: "ingress-udp",
245-
}, anp)
246-
require.NoErrorf(t, err, "unable to fetch the admin network policy")
247-
dnsPortRule := anp.DeepCopy().Spec.Ingress[5]
248-
dnsPort := "dns"
249-
// rewrite the udp port 53 rule as named port rule
250-
dnsPortRule.Ports = &[]v1alpha1.AdminNetworkPolicyPort{
251-
{
252-
NamedPort: &dnsPort,
253-
},
254-
}
255-
anp.Spec.Ingress[5] = dnsPortRule
256-
err = s.Client.Update(ctx, anp)
257-
require.NoErrorf(t, err, "unable to update the admin network policy")
258-
// harry-potter-0 is our client pod in gryffindor namespace
259-
// ensure ingress is ALLOWED from gryffindor to hufflepuff at the dns port, which is defined as UDP at port 53 in pod spec
260-
// modified ingressRule at index5 should take effect
261-
success := kubernetes.PokeServer(t, s.ClientSet, &s.KubeConfig, "network-policy-conformance-gryffindor", "harry-potter-0", "udp",
262-
serverPod.Status.PodIP, int32(53), s.TimeoutConfig.RequestTimeout, true)
263-
assert.True(t, success)
264-
// harry-potter-1 is our client pod in gryfindor namespace
265-
// ensure ingress is DENIED from gryffindor to hufflepuff for rest of the traffic; ingressRule at index6 should take effect
266-
success = kubernetes.PokeServer(t, s.ClientSet, &s.KubeConfig, "network-policy-conformance-gryffindor", "harry-potter-1", "udp",
267-
serverPod.Status.PodIP, int32(5353), s.TimeoutConfig.RequestTimeout, false)
268-
assert.True(t, success)
269-
})
270-
271-
},
272-
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package tests
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
v1 "k8s.io/api/core/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
28+
"sigs.k8s.io/network-policy-api/apis/v1alpha1"
29+
"sigs.k8s.io/network-policy-api/conformance/utils/kubernetes"
30+
"sigs.k8s.io/network-policy-api/conformance/utils/suite"
31+
)
32+
33+
var AdminNetworkPolicyEgressNamedPort = suite.ConformanceTest{
34+
ShortName: "AdminNetworkPolicyEgressNamedPort",
35+
Description: "Tests support for egress traffic on a named port using admin network policy API based on a server and client model",
36+
Features: []suite.SupportedFeature{
37+
suite.SupportAdminNetworkPolicy,
38+
suite.SupportAdminNetworkPolicyNamedPorts,
39+
},
40+
Manifests: []string{"base/admin_network_policy/core-egress-tcp-rules.yaml"},
41+
Test: func(t *testing.T, s *suite.ConformanceTestSuite) {
42+
43+
t.Run("Should support an 'allow-egress' policy for named port", func(t *testing.T) {
44+
ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout)
45+
defer cancel()
46+
// This test uses `egress-tcp` ANP
47+
// cedric-diggory-1 is our server pod in hufflepuff namespace
48+
serverPod := &v1.Pod{}
49+
err := s.Client.Get(ctx, client.ObjectKey{
50+
Namespace: "network-policy-conformance-hufflepuff",
51+
Name: "cedric-diggory-1",
52+
}, serverPod)
53+
require.NoErrorf(t, err, "unable to fetch the server pod")
54+
anp := &v1alpha1.AdminNetworkPolicy{}
55+
err = s.Client.Get(ctx, client.ObjectKey{
56+
Name: "egress-tcp",
57+
}, anp)
58+
require.NoErrorf(t, err, "unable to fetch the admin network policy")
59+
namedPortRule := anp.DeepCopy().Spec.Egress[5]
60+
webPort := "web"
61+
// replace the tcp port 8080 rule as named port rule which translate to tcp port 80 instead
62+
namedPortRule.Ports = &[]v1alpha1.AdminNetworkPolicyPort{
63+
{
64+
NamedPort: &webPort,
65+
},
66+
}
67+
anp.Spec.Egress[5] = namedPortRule
68+
err = s.Client.Update(ctx, anp)
69+
require.NoErrorf(t, err, "unable to update the admin network policy")
70+
// harry-potter-0 is our client pod in gryffindor namespace
71+
// ensure egress is ALLOWED to hufflepuff from gryffindor at the web port, which is defined as TCP at port 80 in pod spec
72+
// egressRule at index5 should take effect
73+
success := kubernetes.PokeServer(t, s.ClientSet, &s.KubeConfig, "network-policy-conformance-gryffindor", "harry-potter-0", "tcp",
74+
serverPod.Status.PodIP, int32(80), s.TimeoutConfig.RequestTimeout, true)
75+
assert.True(t, success)
76+
// harry-potter-1 is our client pod in gryffindor namespace
77+
// ensure egress is DENIED to hufflepuff from gryffindor for rest of the traffic; egressRule at index6 should take effect
78+
success = kubernetes.PokeServer(t, s.ClientSet, &s.KubeConfig, "network-policy-conformance-gryffindor", "harry-potter-1", "tcp",
79+
serverPod.Status.PodIP, int32(8080), s.TimeoutConfig.RequestTimeout, false)
80+
assert.True(t, success)
81+
})
82+
83+
},
84+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package tests
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
v1 "k8s.io/api/core/v1"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
28+
"sigs.k8s.io/network-policy-api/apis/v1alpha1"
29+
"sigs.k8s.io/network-policy-api/conformance/utils/kubernetes"
30+
"sigs.k8s.io/network-policy-api/conformance/utils/suite"
31+
)
32+
33+
var AdminNetworkPolicyIngressNamedPort = suite.ConformanceTest{
34+
ShortName: "AdminNetworkPolicyIngressNamedPort",
35+
Description: "Tests support for ingress traffic on a named port using admin network policy API based on a server and client model",
36+
Features: []suite.SupportedFeature{
37+
suite.SupportAdminNetworkPolicy,
38+
suite.SupportAdminNetworkPolicyNamedPorts,
39+
},
40+
Manifests: []string{"base/admin_network_policy/core-ingress-udp-rules.yaml"},
41+
Test: func(t *testing.T, s *suite.ConformanceTestSuite) {
42+
43+
t.Run("Should support an 'allow-ingress' policy for named port", func(t *testing.T) {
44+
ctx, cancel := context.WithTimeout(context.Background(), s.TimeoutConfig.GetTimeout)
45+
defer cancel()
46+
// This test uses `ingress-udp` ANP
47+
// cedric-diggory-1 is our server pod in hufflepuff namespace
48+
serverPod := &v1.Pod{}
49+
err := s.Client.Get(ctx, client.ObjectKey{
50+
Namespace: "network-policy-conformance-hufflepuff",
51+
Name: "cedric-diggory-1",
52+
}, serverPod)
53+
require.NoErrorf(t, err, "unable to fetch the server pod")
54+
anp := &v1alpha1.AdminNetworkPolicy{}
55+
err = s.Client.Get(ctx, client.ObjectKey{
56+
Name: "ingress-udp",
57+
}, anp)
58+
require.NoErrorf(t, err, "unable to fetch the admin network policy")
59+
dnsPortRule := anp.DeepCopy().Spec.Ingress[5]
60+
dnsPort := "dns"
61+
// rewrite the udp port 53 rule as named port rule
62+
dnsPortRule.Ports = &[]v1alpha1.AdminNetworkPolicyPort{
63+
{
64+
NamedPort: &dnsPort,
65+
},
66+
}
67+
anp.Spec.Ingress[5] = dnsPortRule
68+
err = s.Client.Update(ctx, anp)
69+
require.NoErrorf(t, err, "unable to update the admin network policy")
70+
// harry-potter-0 is our client pod in gryffindor namespace
71+
// ensure ingress is ALLOWED from gryffindor to hufflepuff at the dns port, which is defined as UDP at port 53 in pod spec
72+
// modified ingressRule at index5 should take effect
73+
success := kubernetes.PokeServer(t, s.ClientSet, &s.KubeConfig, "network-policy-conformance-gryffindor", "harry-potter-0", "udp",
74+
serverPod.Status.PodIP, int32(53), s.TimeoutConfig.RequestTimeout, true)
75+
assert.True(t, success)
76+
// harry-potter-1 is our client pod in gryfindor namespace
77+
// ensure ingress is DENIED from gryffindor to hufflepuff for rest of the traffic; ingressRule at index6 should take effect
78+
success = kubernetes.PokeServer(t, s.ClientSet, &s.KubeConfig, "network-policy-conformance-gryffindor", "harry-potter-1", "udp",
79+
serverPod.Status.PodIP, int32(5353), s.TimeoutConfig.RequestTimeout, false)
80+
assert.True(t, success)
81+
})
82+
},
83+
}

0 commit comments

Comments
 (0)