Skip to content

Commit 5e4d472

Browse files
authored
chore: fuzz testing (#190)
1 parent 0cba6e4 commit 5e4d472

File tree

5 files changed

+109
-3
lines changed

5 files changed

+109
-3
lines changed

.github/workflows/fuzz.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: fuzz
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
branches:
10+
- main
11+
12+
workflow_dispatch:
13+
14+
jobs:
15+
fuzz:
16+
runs-on: ubuntu-20.04
17+
steps:
18+
- name: Cancel previous runs
19+
uses: styfle/[email protected]
20+
if: github.event_type == 'pull_request'
21+
22+
- name: Checkout source
23+
uses: actions/checkout@v2
24+
25+
- name: Install Go
26+
uses: actions/setup-go@v2
27+
with:
28+
go-version: "^1.17"
29+
30+
- name: Fuzz
31+
run: go test -run ^TestFuzz$ cdr.dev/enterprise-helm/tests -count=1000
32+
working-directory: tests

templates/rbac.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ roleRef:
4242
kind: Role
4343
name: coder
4444
apiGroup: rbac.authorization.k8s.io
45-
{{- end }}
45+
{{- end }}

tests/fuzz_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package tests
2+
3+
import (
4+
"math/rand"
5+
"reflect"
6+
"testing"
7+
"time"
8+
9+
"github.com/mitchellh/reflectwalk"
10+
"github.com/pioz/faker"
11+
)
12+
13+
func TestFuzz(t *testing.T) {
14+
t.Parallel()
15+
16+
seed := time.Now().UnixNano()
17+
rand.Seed(seed)
18+
t.Log("seed:", seed)
19+
20+
c := &CoderValues{}
21+
22+
// First, populate all struct fields with random values.
23+
faker.Build(c)
24+
25+
// Then, we walk through the struct and unset fields based on a randomized
26+
// probability.
27+
reflectwalk.Walk(c, walker{rand: rand.Float64()})
28+
29+
// Finally, ensure the chart renders correctly. We only care that it renders,
30+
// not that it's valid Kubernetes spec.
31+
LoadChart(t).MustRender(t, func(cv *CoderValues) { *cv = *c })
32+
}
33+
34+
var _ reflectwalk.PrimitiveWalker = walker{}
35+
var _ reflectwalk.MapWalker = walker{}
36+
var _ reflectwalk.SliceWalker = walker{}
37+
var _ reflectwalk.ArrayWalker = walker{}
38+
39+
// walker randomly unassigns values based on the specified random chance.
40+
type walker struct {
41+
rand float64
42+
}
43+
44+
func (w walker) Primitive(v reflect.Value) error {
45+
w.processValue(v)
46+
return nil
47+
}
48+
49+
func (w walker) MapElem(_, _, _ reflect.Value) error { return nil }
50+
func (w walker) Map(v reflect.Value) error {
51+
w.processValue(v)
52+
return nil
53+
}
54+
55+
func (w walker) SliceElem(_ int, _ reflect.Value) error { return nil }
56+
func (w walker) Slice(v reflect.Value) error {
57+
w.processValue(v)
58+
return nil
59+
}
60+
func (w walker) ArrayElem(_ int, _ reflect.Value) error { return nil }
61+
func (w walker) Array(v reflect.Value) error {
62+
w.processValue(v)
63+
return nil
64+
}
65+
66+
func (w walker) processValue(v reflect.Value) {
67+
if v.CanSet() && rand.Float64() < w.rand {
68+
v.Set(reflect.Zero(v.Type()))
69+
}
70+
}

tests/go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go 1.17
44

55
require (
66
github.com/jinzhu/copier v0.3.4
7+
github.com/mitchellh/reflectwalk v1.0.2
8+
github.com/pioz/faker v1.7.2
79
github.com/stretchr/testify v1.7.0
810
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
911
helm.sh/helm/v3 v3.7.1
@@ -31,7 +33,6 @@ require (
3133
github.com/imdario/mergo v0.3.12 // indirect
3234
github.com/json-iterator/go v1.1.11 // indirect
3335
github.com/mitchellh/copystructure v1.1.1 // indirect
34-
github.com/mitchellh/reflectwalk v1.0.1 // indirect
3536
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3637
github.com/modern-go/reflect2 v1.0.1 // indirect
3738
github.com/pkg/errors v0.9.1 // indirect

tests/go.sum

+4-1
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,9 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
592592
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
593593
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
594594
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
595-
github.com/mitchellh/reflectwalk v1.0.1 h1:FVzMWA5RllMAKIdUSC8mdWo3XtwoecrH79BY70sEEpE=
596595
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
596+
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
597+
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
597598
github.com/moby/locker v1.0.1/go.mod h1:S7SDdo5zpBK84bzzVlKr2V0hz+7x9hWbYC/kq7oQppc=
598599
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
599600
github.com/moby/sys/mountinfo v0.4.0/go.mod h1:rEr8tzG/lsIZHBtN/JjGG+LMYx9eXgW2JI+6q0qou+A=
@@ -666,6 +667,8 @@ github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrap
666667
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
667668
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
668669
github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2/go.mod h1:iIss55rKnNBTvrwdmkUpLnDpZoAHvWaiq5+iMmen4AE=
670+
github.com/pioz/faker v1.7.2 h1:fterUcYCbMJ4TgFN8DSqhlwkgbSn9DfsQM1cadbPPw0=
671+
github.com/pioz/faker v1.7.2/go.mod h1:xSpay5w/oz1a6+ww0M3vfpe40pSIykeUPeWEc3TvVlc=
669672
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
670673
github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
671674
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

0 commit comments

Comments
 (0)