Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] test:add a fuzz test for bigquerydataset #3350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
79 changes: 79 additions & 0 deletions pkg/controller/direct/bigquerydataset/roundtrip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2024 Google LLC
//
// 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 bigquerydataset

import (
"encoding/json"
"testing"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/bigquery/v1beta1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/fuzz"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func FuzzBigQueryDataSetSpec(f *testing.F) {

f.Fuzz(func(t *testing.T, seed int64) {
filler := fuzz.NewRandomFiller(seed, nil, nil)

// krm1
k1 := &krm.BigQueryDatasetSpec{}

// fill
filler.Fill(t, k1)

// ToProto
ctx := &direct.MapContext{}
p := BigQueryDatasetSpec_ToAPI(ctx, k1, "test")
if ctx.Err() != nil {
t.Fatalf("error converting KRM to proto: %v \n KRM: %s", ctx.Err(), prettyPrint(t, k1))
}
k2 := BigQueryDatasetSpec_FromAPI(ctx, p)
if ctx.Err() != nil {
t.Fatalf("error mapping from proto to KRM: %v", ctx.Err())
}
// Using cmpopts.IgnoreFields to ignore specific fields in ProjectRef
opts := cmp.Options{
// project ref
cmpopts.IgnoreFields(refs.ProjectRef{}, "External", "Name", "Namespace"),

// other ref
cmpopts.IgnoreFields(refs.KMSCryptoKeyRef{}, "External", "Name", "Namespace"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to ignore the reference field? I thought we can roundtrip between xxxRef.external and its corresponding API field? @acpana


// unroundtrippable fields (for now)
cmpopts.IgnoreFields(krm.BigQueryDatasetSpec{}, "ResourceID"),
cmpopts.IgnoreFields(krm.BigQueryDatasetSpec{}, "ProjectRef"),
cmpopts.IgnoreFields(krm.BigQueryDatasetSpec{}, "IsCaseInsensitive"),
}
// compare
if diff := cmp.Diff(k1, k2, opts...); diff != "" {
t.Logf("k1 = %v", k1)
t.Logf("k2 = %v", k2)
t.Errorf("roundtrip failed; diff:\n%s", diff)
}
})
}

func prettyPrint(t *testing.T, k *krm.BigQueryDatasetSpec) string {
encoded, err := json.MarshalIndent(k, "", " ")
if err != nil {
t.Fatalf("error encoding to json: %v", err)
}

return string(encoded)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
int64(23)
13 changes: 13 additions & 0 deletions pkg/test/fuzz/krmgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package fuzz
import (
"math/rand"
"reflect"
"strconv"
"testing"
)

Expand Down Expand Up @@ -107,6 +108,18 @@ func (rf *RandomFiller) fillWithRandom(t *testing.T, field reflect.Value) {

case reflect.Struct:
for i := 0; i < field.NumField(); i++ {
// specific to bigquerydataset
if field.Type().Field(i).Name == "MaxTimeTravelHours" {
structField := field.Field(i)
if structField.Kind() == reflect.Ptr {
if structField.IsNil() {
structField.Set(reflect.New(structField.Type().Elem()))
}
structField = structField.Elem()
}
structField.SetString(strconv.FormatInt(rf.randStream.Int63(), 10))
continue
}
rf.fillWithRandom(t, field.Field(i))
}

Expand Down
Loading