Skip to content

Commit

Permalink
chore: add crd schema fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
linghan-hub committed Sep 5, 2023
1 parent 8890c84 commit e5c5ea5
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 34 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,10 @@ ifeq (, $(shell which kubectl))
endif
KUBECTL=$(shell which kubectl)

.PHONY: fuzz
fuzz: ## test for crd schema.
go test -v -C apis/schema/install

##@ End-to-end (E2E) tests
.PHONY: render-smoke-testdata-manifests
render-smoke-testdata-manifests: ## Update E2E test dataset
Expand Down
115 changes: 115 additions & 0 deletions apis/schema/fuzzer/fuzzer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package fuzzer

import (
"context"
"log"
"strings"

fuzz "github.com/google/gofuzz"
"github.com/sirupsen/logrus"
crdv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/client-go/dynamic"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
dataprotectionv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
extensionsv1alpha1 "github.com/apecloud/kubeblocks/apis/extensions/v1alpha1"
"github.com/apecloud/kubeblocks/internal/cli/types"
e2eutil "github.com/apecloud/kubeblocks/test/e2e/util"
)

var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
func(s *appsv1alpha1.Cluster, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *appsv1alpha1.ClusterVersion, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *appsv1alpha1.ClusterDefinition, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *appsv1alpha1.ClusterComponentDefinition, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *appsv1alpha1.ClusterComponentDefinition, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *appsv1alpha1.OpsRequest, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *appsv1alpha1.BackupPolicyTemplate, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *appsv1alpha1.ConfigConstraint, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *dataprotectionv1alpha1.Backup, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *dataprotectionv1alpha1.BackupPolicy, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *dataprotectionv1alpha1.BackupRepo, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *dataprotectionv1alpha1.BackupTool, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *dataprotectionv1alpha1.RestoreJob, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
func(s *extensionsv1alpha1.Addon, c fuzz.Continue) {
c.FuzzNoCustom(s)
},
}
}

func UnstructuredToCRD(u *unstructured.Unstructured) (*crdv1.CustomResourceDefinition, error) {

var crd crdv1.CustomResourceDefinition

err := runtime.DefaultUnstructuredConverter.FromUnstructured(u.UnstructuredContent(), &crd)
if err != nil {
return nil, err
}

return &crd, nil
}

func GetAllCRDs() []*crdv1.CustomResourceDefinition {
cfg, err := e2eutil.GetConfig()
if err != nil {
logrus.WithError(err).Fatal("could not get config")
}

dynamic, e := dynamic.NewForConfig(cfg)
if e != nil {
logrus.WithError(e).Fatal("could not generate dynamic client for config")
}

crds, err := dynamic.Resource(types.CustomResourceDefinitionGVR()).List(context.TODO(), metav1.ListOptions{})

if err != nil {
logrus.WithError(err).Fatal("could not get CRDs")
}

if crds == nil || len(crds.Items) == 0 {
log.Println("No CRDs found")
}

var crdsToTest []*crdv1.CustomResourceDefinition

for _, crd := range crds.Items {
name := crd.GetName()
if strings.Contains(name, "kubeblocks.io") {
c, _ := UnstructuredToCRD(&crd)

crdsToTest = append(crdsToTest, c)
}
}
return crdsToTest
}
20 changes: 20 additions & 0 deletions apis/schema/install/crds_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package install

import (
"testing"

"k8s.io/apimachinery/pkg/apis/meta/fuzzer"
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
crdfuzz "kmodules.xyz/crd-schema-fuzz"

fuzz "github.com/apecloud/kubeblocks/apis/schema/fuzzer"
)

func TestCrdTypes(t *testing.T) {
Install(clientsetscheme.Scheme)
crds := fuzz.GetAllCRDs()
for _, crd := range crds {
crdfuzz.SchemaFuzzTestForV1CRD(t, clientsetscheme.Scheme, crd, fuzzer.Funcs)
}

}
20 changes: 20 additions & 0 deletions apis/schema/install/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package install

import (
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
dataprotectionv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
extensionsv1alpha1 "github.com/apecloud/kubeblocks/apis/extensions/v1alpha1"
)

// Install registers the API group and adds types to a scheme
func Install(scheme *runtime.Scheme) {
utilruntime.Must(appsv1alpha1.AddToScheme(scheme))
utilruntime.Must(scheme.SetVersionPriority(appsv1alpha1.SchemeGroupVersion))
utilruntime.Must(dataprotectionv1alpha1.AddToScheme(scheme))
utilruntime.Must(scheme.SetVersionPriority(dataprotectionv1alpha1.SchemeGroupVersion))
utilruntime.Must(extensionsv1alpha1.AddToScheme(scheme))
utilruntime.Must(scheme.SetVersionPriority(extensionsv1alpha1.SchemeGroupVersion))
}
2 changes: 1 addition & 1 deletion deploy/mongodb-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## @param version cluster version
##
version: mongodb-5.0.14
version: mongodb-5.0

## @param mode cluster topology mode, standalone or replicaset
##
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang/mock v1.6.0
github.com/google/go-cmp v0.5.9
github.com/google/gofuzz v1.2.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-version v1.6.0
Expand Down Expand Up @@ -118,6 +119,7 @@ require (
k8s.io/kubelet v0.26.1
k8s.io/metrics v0.26.3
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2
kmodules.xyz/crd-schema-fuzz v0.25.0
sigs.k8s.io/controller-runtime v0.14.6
sigs.k8s.io/kustomize/kyaml v0.13.9
sigs.k8s.io/yaml v1.3.0
Expand Down Expand Up @@ -235,7 +237,6 @@ require (
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-containerregistry v0.14.0 // indirect
github.com/google/go-intervals v0.0.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20221103000818-d260c55eee4c // indirect
github.com/google/s2a-go v0.1.3 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2995,6 +2995,8 @@ k8s.io/utils v0.0.0-20200729134348-d5654de09c73/go.mod h1:jPW/WVKK9YHAvNhRxK0md/
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
kmodules.xyz/crd-schema-fuzz v0.25.0 h1:c5ZxNRqJak1bkGhECmyrKpzKGThFMB4088Kynyvngbc=
kmodules.xyz/crd-schema-fuzz v0.25.0/go.mod h1:VigFz19GwCxMGhb3YjCtlSXmfXb0J/g9du1So6rvqsk=
oras.land/oras-go v1.2.2 h1:0E9tOHUfrNH7TCDk5KU0jVBEzCqbfdyuVfGmJ7ZeRPE=
oras.land/oras-go v1.2.2/go.mod h1:Apa81sKoZPpP7CDciE006tSZ0x3Q3+dOoBcMZ/aNxvw=
periph.io/x/host/v3 v3.8.0 h1:T5ojZ2wvnZHGPS4h95N2ZpcCyHnsvH3YRZ1UUUiv5CQ=
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/testdata/smoketest/mongodb/00_mongodbcluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ metadata:
namespace: default
labels:
helm.sh/chart: mongodb-cluster-0.7.0-alpha.0
app.kubernetes.io/version: "5.0.14"
app.kubernetes.io/version: "5.0"
app.kubernetes.io/instance: mongodb-cluster
spec:
clusterVersionRef: mongodb-5.0.14
clusterVersionRef: mongodb-5.0
terminationPolicy: Delete
affinity:
podAntiAffinity: Preferred
Expand Down
30 changes: 0 additions & 30 deletions test/e2e/testdata/smoketest/wesql/14_backup_snapshot_restore.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
---
# Source: apecloud-mysql-cluster/templates/serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: kb-mysql-cluster
labels:
app.kubernetes.io/name: mysql-cluster
app.kubernetes.io/instance: mycluster
app.kubernetes.io/version: "8.0.30"
app.kubernetes.io/managed-by: Helm
---
# Source: apecloud-mysql-cluster/templates/role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand All @@ -29,25 +18,6 @@ rules:
verbs:
- create
---
# Source: apecloud-mysql-cluster/templates/rolebinding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: kb-mysql-cluster
labels:
app.kubernetes.io/name: apecloud-mysql-cluster
app.kubernetes.io/instance: mycluster
app.kubernetes.io/version: "8.0.30"
app.kubernetes.io/managed-by: Helm
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: kb-mycluster-apecloud-mysql-cluster
subjects:
- kind: ServiceAccount
name: kb-mycluster-apecloud-mysql-cluster
namespace: default
---
apiVersion: apps.kubeblocks.io/v1alpha1
kind: Cluster
metadata:
Expand Down

0 comments on commit e5c5ea5

Please sign in to comment.