Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Commit

Permalink
openyurt fuzzer feature (#67)
Browse files Browse the repository at this point in the history
Signed-off-by: huiwq1990 <huiwq1990@163.com>
  • Loading branch information
huiwq1990 authored Jul 30, 2022
1 parent fe8491b commit 3305f1e
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 1 deletion.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/openyurtio/yurt-app-manager
go 1.16

require (
github.com/onsi/gomega v1.13.0
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v2 v2.4.0
Expand Down
6 changes: 6 additions & 0 deletions tests/fuzz/Dockerfile.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM gcr.io/oss-fuzz-base/base-builder-go

COPY ./ $GOPATH/src/github.com/openyurtio/yurt-app-manager/
COPY ./tests/fuzz/oss_fuzz_build.sh $SRC/build.sh

WORKDIR $SRC
6 changes: 6 additions & 0 deletions tests/fuzz/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module github.com/openyurtio/yurt-app-manager/tests/fuzz

// This module is used only to avoid polluting the main module
// with fuzz dependencies.

go 1.16
37 changes: 37 additions & 0 deletions tests/fuzz/oss_fuzz_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2022 The OpenYurt Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env bash

set -euxo pipefail

GOPATH="${GOPATH:-/root/go}"
GO_SRC="${GOPATH}/src"
PROJECT_PATH="github.com/openyurtio/yurt-app-manager"

cd "${GO_SRC}"

# Move fuzzer to their respective directories.
# This removes dependency noises from the modules' go.mod and go.sum files.
cp "${PROJECT_PATH}/tests/fuzz/yurtappdaemon_fuzzer.go" "${PROJECT_PATH}/pkg/yurtappmanager/controller/yurtappdaemon/yurtappdaemon_fuzzer.go"
cp "${PROJECT_PATH}/tests/fuzz/yurtappset_fuzzer.go" "${PROJECT_PATH}/pkg/yurtappmanager/controller/yurtappset/yurtappset_fuzzer.go"

# compile fuzz tests for the runtime module
pushd "${PROJECT_PATH}"

go get -d github.com/AdaLogics/go-fuzz-headers
compile_go_fuzzer "${PROJECT_PATH}/pkg/yurtappmanager/controller/yurtappdaemon/" FuzzAppDaemonReconcile fuzz_yurtappdaemon_controller
compile_go_fuzzer "${PROJECT_PATH}/pkg/yurtappmanager/controller/yurtappset/" FuzzAppSetReconcile fuzz_yurtappset_controller

popd
20 changes: 20 additions & 0 deletions tests/fuzz/oss_fuzz_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2022 The OpenYurt Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/env bash

set -euxo pipefail

# run each fuzzer once to ensure they are working properly
find /out -type f -name "fuzz*" -exec echo {} -runs=1 \; | bash -e
72 changes: 72 additions & 0 deletions tests/fuzz/yurtappdaemon_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//go:build gofuzz
// +build gofuzz

/*
Copyright 2022 The OpenYurt Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package yurtappdaemon

import (
"context"

fuzz "github.com/AdaLogics/go-fuzz-headers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

appsv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1"
"github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/controller/yurtappdaemon/workloadcontroller"
)

var (
fuzzCtx = context.Background()
fakeSchemeForFuzzing = runtime.NewScheme()
)

func init() {
_ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing)
_ = appsv1alpha1.AddToScheme(fakeSchemeForFuzzing)
_ = corev1.AddToScheme(fakeSchemeForFuzzing)
}

func FuzzAppDaemonReconcile(data []byte) int {
f := fuzz.NewConsumer(data)

appDaemon := &appsv1alpha1.YurtAppDaemon{}
if err := f.GenerateStruct(appDaemon); err != nil {
return 0
}

clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects(
appDaemon,
).Build()

r := &ReconcileYurtAppDaemon{
Client: clientFake,
scheme: fakeSchemeForFuzzing,
recorder: record.NewFakeRecorder(10000),
controls: map[appsv1alpha1.TemplateType]workloadcontroller.WorkloadControllor{
appsv1alpha1.DeploymentTemplateType: &workloadcontroller.DeploymentControllor{Client: clientFake, Scheme: fakeSchemeForFuzzing},
},
}

_, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: types.NamespacedName{Name: appDaemon.Name, Namespace: appDaemon.Namespace}})
return 1
}
75 changes: 75 additions & 0 deletions tests/fuzz/yurtappset_fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//go:build gofuzz
// +build gofuzz

/*
Copyright 2022 The OpenYurt Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package yurtappset

import (
"context"

fuzz "github.com/AdaLogics/go-fuzz-headers"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

appsv1alpha1 "github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/apis/apps/v1alpha1"
"github.com/openyurtio/yurt-app-manager/pkg/yurtappmanager/controller/yurtappset/adapter"
)

var (
fuzzCtx = context.Background()
fakeSchemeForFuzzing = runtime.NewScheme()
)

func init() {
_ = clientgoscheme.AddToScheme(fakeSchemeForFuzzing)
_ = appsv1alpha1.AddToScheme(fakeSchemeForFuzzing)
_ = corev1.AddToScheme(fakeSchemeForFuzzing)
}

func FuzzAppSetReconcile(data []byte) int {
f := fuzz.NewConsumer(data)

appDaemon := &appsv1alpha1.YurtAppDaemon{}
if err := f.GenerateStruct(appDaemon); err != nil {
return 0
}

clientFake := fake.NewClientBuilder().WithScheme(fakeSchemeForFuzzing).WithObjects(
appDaemon,
).Build()

r := &ReconcileYurtAppSet{
Client: clientFake,
scheme: fakeSchemeForFuzzing,
recorder: record.NewFakeRecorder(10000),
poolControls: map[appsv1alpha1.TemplateType]ControlInterface{
appsv1alpha1.StatefulSetTemplateType: &PoolControl{Client: clientFake, scheme: fakeSchemeForFuzzing,
adapter: &adapter.StatefulSetAdapter{Client: clientFake, Scheme: fakeSchemeForFuzzing}},
appsv1alpha1.DeploymentTemplateType: &PoolControl{Client: clientFake, scheme: fakeSchemeForFuzzing,
adapter: &adapter.DeploymentAdapter{Client: clientFake, Scheme: fakeSchemeForFuzzing}},
},
}

_, _ = r.Reconcile(fuzzCtx, reconcile.Request{NamespacedName: types.NamespacedName{Name: appDaemon.Name, Namespace: appDaemon.Namespace}})
return 1
}

0 comments on commit 3305f1e

Please sign in to comment.