Skip to content

Commit

Permalink
Refactor fuzzing
Browse files Browse the repository at this point in the history
Structure the fuzz implementation to be closer to what go native will support.
Add Makefile target to enable smoketesting fuzzers.
Add smoketest as CI workflow.

Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
  • Loading branch information
Paulo Gomes committed Jan 14, 2022
1 parent 7f84416 commit 50c043e
Show file tree
Hide file tree
Showing 14 changed files with 734 additions and 639 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/cifuzz.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CIFuzz
on:
pull_request:
branches:
- main
jobs:
Fuzzing:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Restore Go cache
uses: actions/cache@v1
with:
path: /home/runner/work/_temp/_github_home/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Smoke test Fuzzers
run: make fuzz-smoketest
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

# Dependency directories (remove the comment below to include it)
# vendor/
testbin/
bin/
config/release/
config/crd/bases/gitrepositories.yaml
config/crd/bases/buckets.yaml

build/
21 changes: 20 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ENVTEST_ARCH ?= amd64
all: manager

# Download the envtest binaries to testbin
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
ENVTEST_ASSETS_DIR=$(shell pwd)/build/testbin
ENVTEST_KUBERNETES_VERSION?=latest
install-envtest: setup-envtest
mkdir -p ${ENVTEST_ASSETS_DIR}
Expand Down Expand Up @@ -147,3 +147,22 @@ GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef

# Build fuzzers
fuzz-build:
rm -rf $(shell pwd)/build/fuzz/
mkdir -p $(shell pwd)/build/fuzz/out/

docker build . --tag local-fuzzing:latest -f tests/fuzz/Dockerfile.builder
docker run --rm \
-e FUZZING_LANGUAGE=go -e SANITIZER=address \
-e CIFUZZ_DEBUG='True' -e OSS_FUZZ_PROJECT_NAME=fluxcd \
-v "$(shell pwd)/build/fuzz/out":/out \
local-fuzzing:latest

fuzz-smoketest: fuzz-build
docker run --rm \
-v "$(shell pwd)/build/fuzz/out":/out \
-v "$(shell pwd)/tests/fuzz/oss_fuzz_run.sh":/runner.sh \
local-fuzzing:latest \
bash -c "/runner.sh"
45 changes: 28 additions & 17 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var (
debugMode = os.Getenv("DEBUG_TEST") != ""
)

func TestMain(m *testing.M) {
func runInContext(registerControllers func(*testenv.Environment), run func() error, crdPath string) error {
var err error
utilruntime.Must(sourcev1.AddToScheme(scheme.Scheme))
utilruntime.Must(kustomizev1.AddToScheme(scheme.Scheme))
Expand All @@ -78,9 +78,7 @@ func TestMain(m *testing.M) {
controllerLog.SetLogger(zap.New(zap.WriteTo(os.Stderr), zap.UseDevMode(false)))
}

testEnv = testenv.New(testenv.WithCRDPath(
filepath.Join("..", "config", "crd", "bases"),
))
testEnv = testenv.New(testenv.WithCRDPath(crdPath))

testServer, err = testserver.NewTempArtifactServer()
if err != nil {
Expand All @@ -89,18 +87,7 @@ func TestMain(m *testing.M) {
fmt.Println("Starting the test storage server")
testServer.Start()

controllerName := "kustomize-controller"
testEventsH = controller.MakeEvents(testEnv, controllerName, nil)
testMetricsH = controller.MustMakeMetrics(testEnv)
reconciler := &KustomizationReconciler{
ControllerName: controllerName,
Client: testEnv,
EventRecorder: testEventsH.EventRecorder,
MetricsRecorder: testMetricsH.MetricsRecorder,
}
if err := (reconciler).SetupWithManager(testEnv, KustomizationReconcilerOptions{MaxConcurrentReconciles: 4}); err != nil {
panic(fmt.Sprintf("Failed to start GitRepositoryReconciler: %v", err))
}
registerControllers(testEnv)

go func() {
fmt.Println("Starting the test environment")
Expand Down Expand Up @@ -129,7 +116,7 @@ func TestMain(m *testing.M) {
panic(fmt.Sprintf("Failed to create k8s client: %v", err))
}

code := m.Run()
runErr := run()

if debugMode {
events := &corev1.EventList{}
Expand All @@ -152,6 +139,30 @@ func TestMain(m *testing.M) {
panic(fmt.Sprintf("Failed to remove storage server dir: %v", err))
}

return runErr
}

func TestMain(m *testing.M) {
code := 0

runInContext(func(testEnv *testenv.Environment) {
controllerName := "kustomize-controller"
testEventsH = controller.MakeEvents(testEnv, controllerName, nil)
testMetricsH = controller.MustMakeMetrics(testEnv)
reconciler := &KustomizationReconciler{
ControllerName: controllerName,
Client: testEnv,
EventRecorder: testEventsH.EventRecorder,
MetricsRecorder: testMetricsH.MetricsRecorder,
}
if err := (reconciler).SetupWithManager(testEnv, KustomizationReconcilerOptions{MaxConcurrentReconciles: 4}); err != nil {
panic(fmt.Sprintf("Failed to start KustomizationReconciler: %v", err))
}
}, func() error {
code = m.Run()
return nil
}, filepath.Join("..", "config", "crd", "bases"))

os.Exit(code)
}

Expand Down
29 changes: 0 additions & 29 deletions fuzz/Dockerfile

This file was deleted.

Loading

0 comments on commit 50c043e

Please sign in to comment.