istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/networking/core/loadbalancer/fuzz_test.go (about)

     1  // Copyright Istio Authors
     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 loadbalancer
    16  
    17  import (
    18  	"testing"
    19  
    20  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    21  	core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
    22  	endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
    23  
    24  	"istio.io/api/networking/v1alpha3"
    25  )
    26  
    27  func FuzzApplyLocalityLBSetting(f *testing.F) {
    28  	f.Fuzz(func(t *testing.T, data []byte) {
    29  		ff := fuzz.NewConsumer(data)
    30  		proxyLabels := make(map[string]string)
    31  		err := ff.FuzzMap(&proxyLabels)
    32  		if err != nil {
    33  			return
    34  		}
    35  		wrappedLocalityLbEndpoints := make([]*WrappedLocalityLbEndpoints, 0)
    36  		err = ff.CreateSlice(&wrappedLocalityLbEndpoints)
    37  		if err != nil {
    38  			return
    39  		}
    40  
    41  		loadAssignment := &endpoint.ClusterLoadAssignment{}
    42  		err = ff.GenerateStruct(loadAssignment)
    43  		if err != nil {
    44  			return
    45  		}
    46  
    47  		locality := &core.Locality{}
    48  		err = ff.GenerateStruct(locality)
    49  		if err != nil {
    50  			return
    51  		}
    52  
    53  		localityLB := &v1alpha3.LocalityLoadBalancerSetting{}
    54  		err = ff.GenerateStruct(localityLB)
    55  		if err != nil {
    56  			return
    57  		}
    58  
    59  		enableFailover, err := ff.GetBool()
    60  		if err != nil {
    61  			return
    62  		}
    63  		ApplyLocalityLoadBalancer(loadAssignment, wrappedLocalityLbEndpoints, locality, proxyLabels, localityLB, enableFailover)
    64  	})
    65  }