Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ tasks:
build:
cmds:
- go build -o bin/manager main.go
fuzz:
desc: "Run fuzz tests with a configurable duration (default 30s per target)"
vars:
FUZZTIME: '{{.FUZZTIME | default "30s"}}'
cmds:
- go test ./api/v1alpha1/ -run=^$ -fuzz=FuzzPlatformMeshRoundTrip -fuzztime={{.FUZZTIME}} -count=1

docker-build:
cmds:
- docker build .
Expand Down
90 changes: 90 additions & 0 deletions api/v1alpha1/roundtrip_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package v1alpha1

import (
"encoding/json"
"testing"

"k8s.io/apimachinery/pkg/api/equality"
)

func FuzzPlatformMeshRoundTrip(f *testing.F) {
f.Add([]byte(`{
"spec": {
"exposure": {
"baseDomain": "example.com",
"port": 443,
"protocol": "https"
},
"kcp": {
"providerConnections": [{
"path": "root:orgs:default",
"secret": "provider-kubeconfig",
"external": false
}],
"extraWorkspaces": [{
"path": "root:orgs:extra",
"type": {"name": "universal", "path": "root"}
}]
},
"ocm": {
"repo": {"name": "platform-mesh"},
"component": {"name": "platform-mesh"},
"referencePath": [{"name": "ref1"}]
},
"featureToggles": [{"name": "feature-a", "parameters": {"key": "val"}}]
}
}`))
f.Add([]byte(`{
"spec": {
"kcp": {
"providerConnections": [{
"path": "root:orgs:prod",
"secret": "prod-secret",
"external": true,
"adminAuth": true
}],
"extraDefaultAPIBindings": [{
"workspaceTypePath": "root:types",
"export": "my-export",
"path": "root:orgs"
}]
},
"wait": {
"resourceTypes": [{
"name": "my-deploy",
"namespace": "default"
}]
}
}
}`))
f.Add([]byte(`{}`))

f.Fuzz(func(t *testing.T, data []byte) {
fuzzRoundTrip(t, data, &PlatformMesh{}, &PlatformMesh{})
})
}

// fuzzRoundTrip unmarshals arbitrary JSON into obj, marshals it back, unmarshals
// into obj2, and checks semantic equality. We use equality.Semantic.DeepEqual from
// k8s.io/apimachinery which treats nil and empty slices/maps as equivalent — the
// standard Kubernetes comparison semantic for API objects.
func fuzzRoundTrip[T any](t *testing.T, data []byte, obj *T, obj2 *T) {
t.Helper()

if err := json.Unmarshal(data, obj); err != nil {
return
}

roundtripped, err := json.Marshal(obj)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}

if err := json.Unmarshal(roundtripped, obj2); err != nil {
t.Fatalf("failed to unmarshal roundtripped data: %v", err)
}

if !equality.Semantic.DeepEqual(obj, obj2) {
t.Errorf("roundtrip mismatch for %T", obj)
}
}
Loading