istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/fuzz/helm_reconciler_fuzzer.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 fuzz
    16  
    17  import (
    18  	fuzz "github.com/AdaLogics/go-fuzz-headers"
    19  	"sigs.k8s.io/controller-runtime/pkg/client"
    20  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    21  
    22  	"istio.io/istio/operator/pkg/helmreconciler"
    23  	"istio.io/istio/operator/pkg/name"
    24  	"istio.io/istio/operator/pkg/object"
    25  )
    26  
    27  type fakeClientWrapper struct {
    28  	client.Client
    29  }
    30  
    31  func FuzzHelmReconciler(data []byte) int {
    32  	f := fuzz.NewConsumer(data)
    33  	k8sobjBytes, err := f.GetBytes()
    34  	if err != nil {
    35  		return 0
    36  	}
    37  	k8obj, err := object.ParseYAMLToK8sObject(k8sobjBytes)
    38  	if err != nil {
    39  		return 0
    40  	}
    41  	m := name.Manifest{}
    42  	err = f.GenerateStruct(&m)
    43  	if err != nil {
    44  		return 0
    45  	}
    46  	obj := k8obj.UnstructuredObject()
    47  	gvk := obj.GetObjectKind().GroupVersionKind()
    48  	if len(gvk.Kind) == 0 {
    49  		return 0
    50  	}
    51  	if len(gvk.Version) == 0 {
    52  		return 0
    53  	}
    54  	cl := &fakeClientWrapper{fake.NewClientBuilder().WithRuntimeObjects(obj).Build()}
    55  	h, err := helmreconciler.NewHelmReconciler(cl, nil, nil, nil)
    56  	if err != nil {
    57  		return 0
    58  	}
    59  	_ = h.ApplyObject(obj)
    60  	_, _ = h.Reconcile()
    61  	_, _ = h.ApplyManifest(m)
    62  	return 1
    63  }