k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/registry/networking/networkpolicy/strategy_test.go (about) 1 /* 2 Copyright 2021 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 networkpolicy 18 19 import ( 20 "context" 21 "testing" 22 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/util/intstr" 25 api "k8s.io/kubernetes/pkg/apis/core" 26 "k8s.io/kubernetes/pkg/apis/networking" 27 ) 28 29 func makeNetworkPolicy(isIngress, isEgress, hasEndPort bool) *networking.NetworkPolicy { 30 31 protocolTCP := api.ProtocolTCP 32 endPort := int32(32000) 33 netPol := &networking.NetworkPolicy{ 34 ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "bar", Generation: 0}, 35 Spec: networking.NetworkPolicySpec{ 36 PodSelector: metav1.LabelSelector{ 37 MatchLabels: map[string]string{"a": "b"}, 38 }, 39 }, 40 } 41 egress := networking.NetworkPolicyEgressRule{ 42 To: []networking.NetworkPolicyPeer{ 43 { 44 NamespaceSelector: &metav1.LabelSelector{ 45 MatchLabels: map[string]string{"c": "d"}, 46 }, 47 }, 48 }, 49 } 50 51 ingress := networking.NetworkPolicyIngressRule{ 52 From: []networking.NetworkPolicyPeer{ 53 { 54 NamespaceSelector: &metav1.LabelSelector{ 55 MatchLabels: map[string]string{"c": "d"}, 56 }, 57 }, 58 }, 59 } 60 61 ports := []networking.NetworkPolicyPort{ 62 { 63 Protocol: &protocolTCP, 64 Port: &intstr.IntOrString{Type: intstr.Int, IntVal: 31000}, 65 }, 66 } 67 68 ingress.Ports = ports 69 egress.Ports = ports 70 71 if hasEndPort { 72 ingress.Ports[0].EndPort = &endPort 73 egress.Ports[0].EndPort = &endPort 74 } 75 76 if isIngress { 77 netPol.Spec.Ingress = append(netPol.Spec.Ingress, ingress) 78 } 79 80 if isEgress { 81 netPol.Spec.Egress = append(netPol.Spec.Egress, egress) 82 } 83 84 return netPol 85 } 86 87 func TestNetworkPolicyStrategy(t *testing.T) { 88 89 // Create a Network Policy containing EndPort defined to compare with the generated by the tests 90 netPol := makeNetworkPolicy(true, true, false) 91 92 Strategy.PrepareForCreate(context.Background(), netPol) 93 94 if netPol.Generation != 1 { 95 t.Errorf("Create: Test failed. Network Policy Generation should be 1, got %d", 96 netPol.Generation) 97 } 98 99 errs := Strategy.Validate(context.Background(), netPol) 100 if len(errs) != 0 { 101 t.Errorf("Unexpected error from validation for created Network Policy: %v", errs) 102 } 103 104 updatedNetPol := makeNetworkPolicy(true, true, true) 105 updatedNetPol.ObjectMeta.SetResourceVersion("1") 106 Strategy.PrepareForUpdate(context.Background(), updatedNetPol, netPol) 107 108 errs = Strategy.ValidateUpdate(context.Background(), updatedNetPol, netPol) 109 if len(errs) != 0 { 110 t.Errorf("Unexpected error from validation for updated Network Policy: %v", errs) 111 } 112 113 }