github.com/tigera/api@v0.0.0-20240320170621-278e89a8c5fb/pkg/apis/projectcalico/v3/networkpolicy_test.go (about)

     1  // Copyright (c) 2017,2021 Tigera, Inc. All rights reserved.
     2  
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package v3_test
    16  
    17  import (
    18  	"reflect"
    19  
    20  	. "github.com/onsi/ginkgo"
    21  	. "github.com/onsi/gomega"
    22  
    23  	apiv3 "github.com/tigera/api/pkg/apis/projectcalico/v3"
    24  )
    25  
    26  var (
    27  	// gnpExtraFields is the set of fields that should be in GlobalNetworkPolicy but not
    28  	// NetworkPolicy.
    29  	gnpExtraFields = From("DoNotTrack", "PreDNAT", "ApplyOnForward", "NamespaceSelector")
    30  
    31  	// npExtraFields is the set of fields that should be in NetworkPolicy but not
    32  	// GlobalNetworkPolicy.
    33  	npExtraFields = From()
    34  )
    35  
    36  // These tests verify that the NetworkPolicySpec struct and the GlobalNetworkPolicySpec struct
    37  // are kept in sync.
    38  var _ = Describe("NetworkPolicySpec", func() {
    39  	var npFieldsByName map[string]reflect.StructField
    40  	var gnpFieldsByName map[string]reflect.StructField
    41  
    42  	BeforeEach(func() {
    43  		npFieldsByName = fieldsByName(apiv3.NetworkPolicySpec{})
    44  		gnpFieldsByName = fieldsByName(apiv3.GlobalNetworkPolicySpec{})
    45  	})
    46  
    47  	It("and GlobalNetworkPolicySpec shared fields should have the same tags", func() {
    48  		for n, f := range npFieldsByName {
    49  			if gf, ok := gnpFieldsByName[n]; ok {
    50  				Expect(f.Tag).To(Equal(gf.Tag), "Field "+n+" had different tag")
    51  			}
    52  		}
    53  	})
    54  
    55  	It("and GlobalNetworkPolicySpec shared fields should have the same types", func() {
    56  		for n, f := range npFieldsByName {
    57  			if gf, ok := gnpFieldsByName[n]; ok {
    58  				Expect(f.Type).To(Equal(gf.Type), "Field "+n+" had different type")
    59  			}
    60  		}
    61  	})
    62  
    63  	It("should not have any unexpected fields that GlobalNetworkPolicySpec doesn't have", func() {
    64  		for n := range npFieldsByName {
    65  			if npExtraFields.Contains(n) {
    66  				continue
    67  			}
    68  			Expect(gnpFieldsByName).To(HaveKey(n))
    69  		}
    70  	})
    71  
    72  	It("should contain all expected fields of GlobalNetworkPolicySpec", func() {
    73  		for n := range gnpFieldsByName {
    74  			if gnpExtraFields.Contains(n) {
    75  				continue
    76  			}
    77  			Expect(npFieldsByName).To(HaveKey(n))
    78  		}
    79  	})
    80  })
    81  
    82  func fieldsByName(example interface{}) map[string]reflect.StructField {
    83  	fields := map[string]reflect.StructField{}
    84  	t := reflect.TypeOf(example)
    85  	for i := 0; i < t.NumField(); i++ {
    86  		f := t.Field(i)
    87  		fields[f.Name] = f
    88  	}
    89  	return fields
    90  }
    91  
    92  type empty struct{}
    93  
    94  type set map[string]empty
    95  
    96  func From(members ...string) set {
    97  	s := set{}
    98  	for _, m := range members {
    99  		s[m] = empty{}
   100  	}
   101  	return s
   102  }
   103  
   104  func (s set) Contains(item string) bool {
   105  	_, present := s[item]
   106  	return present
   107  }