github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/patch/executor_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package patch
    19  
    20  import (
    21  	"context"
    22  	"testing"
    23  
    24  	fuzz "github.com/google/gofuzz"
    25  
    26  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    27  )
    28  
    29  func Test_executeRule_Fuzz(t *testing.T) {
    30  	// Making sure the executeRule never panics
    31  	for i := 0; i < 50; i++ {
    32  		f := fuzz.New()
    33  
    34  		// Prepare arguments
    35  		values := map[string]interface{}{}
    36  		spec := &bundlev1.Patch{
    37  			ApiVersion: "harp.elastic.co/v1",
    38  			Kind:       "BundlePatch",
    39  			Meta: &bundlev1.PatchMeta{
    40  				Name: "test-patch",
    41  			},
    42  			Spec: &bundlev1.PatchSpec{
    43  				Rules: []*bundlev1.PatchRule{
    44  					{
    45  						Package:  &bundlev1.PatchPackage{},
    46  						Selector: &bundlev1.PatchSelector{},
    47  					},
    48  				},
    49  			},
    50  		}
    51  		p := bundlev1.Package{
    52  			Name: "foo",
    53  			Secrets: &bundlev1.SecretChain{
    54  				Data: []*bundlev1.KV{
    55  					{
    56  						Key:   "k1",
    57  						Value: []byte("v1"),
    58  					},
    59  				},
    60  			},
    61  		}
    62  
    63  		var patchName string
    64  
    65  		f.Fuzz(&patchName)
    66  		f.Fuzz(&spec.Spec.Rules[0])
    67  
    68  		// Execute
    69  		executeRule(context.Background(), spec.Spec.Rules[0], &p, values)
    70  	}
    71  }
    72  
    73  func Test_compileSelector_Fuzz(t *testing.T) {
    74  	// Making sure the compileSelector never panics
    75  	for i := 0; i < 50; i++ {
    76  		f := fuzz.New()
    77  
    78  		// Prepare arguments
    79  		values := map[string]interface{}{}
    80  		spec := &bundlev1.Patch{
    81  			ApiVersion: "harp.elastic.co/v1",
    82  			Kind:       "BundlePatch",
    83  			Meta: &bundlev1.PatchMeta{
    84  				Name: "test-patch",
    85  			},
    86  			Spec: &bundlev1.PatchSpec{
    87  				Rules: []*bundlev1.PatchRule{
    88  					{
    89  						Package:  &bundlev1.PatchPackage{},
    90  						Selector: &bundlev1.PatchSelector{},
    91  					},
    92  				},
    93  			},
    94  		}
    95  
    96  		f.Fuzz(&spec.Spec.Rules[0].Selector)
    97  
    98  		// Execute
    99  		compileSelector(context.Background(), spec.Spec.Rules[0].Selector, values)
   100  	}
   101  }
   102  
   103  func Test_applyPackagePatch_Fuzz(t *testing.T) {
   104  	// Making sure the applyPatchPackage never panics
   105  	for i := 0; i < 500; i++ {
   106  		f := fuzz.New()
   107  
   108  		// Prepare arguments
   109  		values := map[string]interface{}{}
   110  		spec := &bundlev1.Patch{
   111  			ApiVersion: "harp.elastic.co/v1",
   112  			Kind:       "BundlePatch",
   113  			Meta: &bundlev1.PatchMeta{
   114  				Name: "test-patch",
   115  			},
   116  			Spec: &bundlev1.PatchSpec{
   117  				Rules: []*bundlev1.PatchRule{
   118  					{
   119  						Package:  &bundlev1.PatchPackage{},
   120  						Selector: &bundlev1.PatchSelector{},
   121  					},
   122  				},
   123  			},
   124  		}
   125  		p := bundlev1.Package{
   126  			Name: "foo",
   127  			Secrets: &bundlev1.SecretChain{
   128  				Data: []*bundlev1.KV{
   129  					{
   130  						Key:   "k1",
   131  						Value: []byte("v1"),
   132  					},
   133  				},
   134  			},
   135  		}
   136  
   137  		f.Fuzz(&spec.Spec.Rules[0].Package)
   138  
   139  		// Execute
   140  		applyPackagePatch(&p, spec.Spec.Rules[0].Package, values)
   141  	}
   142  }
   143  
   144  func Test_applySecretPatch_Fuzz(t *testing.T) {
   145  	// Making sure the applyPatchPackage never panics
   146  	for i := 0; i < 500; i++ {
   147  		f := fuzz.New()
   148  
   149  		// Prepare arguments
   150  		values := map[string]interface{}{
   151  			"foo": "test",
   152  		}
   153  		spec := &bundlev1.Patch{
   154  			ApiVersion: "harp.elastic.co/v1",
   155  			Kind:       "BundlePatch",
   156  			Meta: &bundlev1.PatchMeta{
   157  				Name: "test-patch",
   158  			},
   159  			Spec: &bundlev1.PatchSpec{
   160  				Rules: []*bundlev1.PatchRule{
   161  					{
   162  						Package: &bundlev1.PatchPackage{},
   163  						Selector: &bundlev1.PatchSelector{
   164  							MatchPath: &bundlev1.PatchSelectorMatchPath{
   165  								Strict: "foo",
   166  							},
   167  						},
   168  					},
   169  				},
   170  			},
   171  		}
   172  		file := bundlev1.Bundle{
   173  			Packages: []*bundlev1.Package{
   174  				{
   175  					Name: "foo",
   176  					Secrets: &bundlev1.SecretChain{
   177  						Data: []*bundlev1.KV{
   178  							{
   179  								Key:   "k1",
   180  								Value: []byte("v1"),
   181  							},
   182  						},
   183  					},
   184  				},
   185  			},
   186  		}
   187  
   188  		f.Fuzz(&spec.Spec.Rules[0].Package.Data)
   189  		f.Fuzz(&file.Packages[0].Secrets)
   190  
   191  		// Execute
   192  		applySecretPatch(file.Packages[0].Secrets, spec.Spec.Rules[0].Package.Data, values)
   193  	}
   194  }
   195  
   196  func Test_applySecretKVPatch_Fuzz(t *testing.T) {
   197  	// Making sure the applySecretKVPatch never panics
   198  	for i := 0; i < 500; i++ {
   199  		f := fuzz.New()
   200  
   201  		// Prepare arguments
   202  		values := map[string]interface{}{}
   203  		spec := &bundlev1.PatchOperation{
   204  			Add:    map[string]string{},
   205  			Remove: []string{},
   206  			Update: map[string]string{},
   207  		}
   208  		file := bundlev1.Bundle{
   209  			Packages: []*bundlev1.Package{
   210  				{
   211  					Name: "foo",
   212  					Secrets: &bundlev1.SecretChain{
   213  						Data: []*bundlev1.KV{
   214  							{
   215  								Key:   "k1",
   216  								Value: []byte("v1"),
   217  							},
   218  						},
   219  					},
   220  				},
   221  			},
   222  		}
   223  
   224  		f.Fuzz(&file.Packages[0].Secrets.Data)
   225  		f.Fuzz(&spec)
   226  
   227  		// Execute
   228  		applySecretKVPatch(file.Packages[0].Secrets.Data, spec, values)
   229  	}
   230  }