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

tests: krm fuzzer for listing #3378

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
125 changes: 125 additions & 0 deletions pkg/controller/direct/bigqueryanalyticshub/fuzzers_listing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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 bigqueryanalyticshub

import (
"math/rand"
"reflect"
"testing"

krm "github.com/GoogleCloudPlatform/k8s-config-connector/apis/bigqueryanalyticshub/v1alpha1"
refs "github.com/GoogleCloudPlatform/k8s-config-connector/apis/refs/v1beta1"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"

"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test"
"github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/fuzz"
)

var ValidCategories = []string{
"CATEGORY_UNSPECIFIED",
"CATEGORY_OTHERS",
"CATEGORY_ADVERTISING_AND_MARKETING",
"CATEGORY_COMMERCE",
"CATEGORY_CLIMATE_AND_ENVIRONMENT",
"CATEGORY_DEMOGRAPHICS",
"CATEGORY_ECONOMICS",
"CATEGORY_EDUCATION",
"CATEGORY_ENERGY",
"CATEGORY_FINANCIAL",
"CATEGORY_GAMING",
"CATEGORY_GEOSPATIAL",
"CATEGORY_HEALTHCARE_AND_LIFE_SCIENCE",
"CATEGORY_MEDIA",
"CATEGORY_PUBLIC_SECTOR",
"CATEGORY_RETAIL",
"CATEGORY_SPORTS",
"CATEGORY_SCIENCE_AND_RESEARCH",
"CATEGORY_TRANSPORTATION_AND_LOGISTICS",
"CATEGORY_TRAVEL_AND_TOURISM",
}

// KRM ->ToProto-> proto ->FromProto-> KRM
func FuzzListingSpec(f *testing.F) {
// spec
f.Fuzz(func(t *testing.T, seed int64) {
randStream := rand.New(rand.NewSource(seed))

overrides := &fuzz.FillerConfig{
FieldOverrides: map[string]fuzz.OverrideFiller{
".Categories": func(t *testing.T, fieldName string, field reflect.Value) {
// up to two fields can be set for this slice
slice := reflect.MakeSlice(field.Type(), 2, 2)

perms := randStream.Perm(len(ValidCategories))
// invariants: len(perms) >> 2
slice.Index(0).SetString(ValidCategories[perms[0]])
slice.Index(1).SetString(ValidCategories[perms[1]])

field.Set(slice)
},
".DiscoveryType": func(t *testing.T, fieldName string, field reflect.Value) {

values := []string{"DISCOVERY_TYPE_PRIVATE", "DISCOVERY_TYPE_PUBLIC"}
selectedValue := values[randStream.Intn(len(values))]
field.Elem().SetString(selectedValue)
},
},
}
filler := fuzz.NewRandomFiller(&fuzz.FillerConfig{
Stream: randStream,
FieldOverrides: overrides.FieldOverrides,
})

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

// fill
filler.Fill(t, k1)

// ToProto
mapCtx := &direct.MapContext{}
p := BigQueryAnalyticsHubListingSpec_ToProto(mapCtx, k1)
if mapCtx.Err() != nil {
t.Fatalf("error converting KRM to proto: %v \n KRM: %s", mapCtx.Err(), test.PrettyPrintJSON(t, k1))
}

// krm2 : FromProto
mapCtx = &direct.MapContext{}
k2 := BigQueryAnalyticsHubListingSpec_FromProto(mapCtx, p)
if mapCtx.Err() != nil {
t.Fatalf("error mapping from proto to KRM: %v", mapCtx.Err())
}

// Using cmpopts.IgnoreFields to ignore specific fields in ProjectRef
opts := cmp.Options{
cmpopts.IgnoreFields(krm.BigQueryAnalyticsHubListingSpec{}, "Location"),
cmpopts.IgnoreFields(krm.BigQueryAnalyticsHubListingSpec{}, "ResourceID"),
cmpopts.IgnoreFields(krm.BigQueryAnalyticsHubListingSpec{}, "ProjectRef"),
cmpopts.IgnoreFields(krm.BigQueryAnalyticsHubListingSpec{}, "DataExchangeRef"),
cmpopts.IgnoreFields(refs.BigQueryTableRef{}, "Name", "Namespace"),
cmpopts.IgnoreFields(krm.BigQueryDatasetSource{}, "DatasetRef"),
cmpopts.IgnoreFields(refs.BigQueryDatasetRef{}, "External", "Name", "Namespace"),
}

// compare
if diff := cmp.Diff(k1, k2, opts...); diff != "" {
t.Logf("k1 =\n%v", test.PrettyPrintYAML(t, k1))
t.Logf("k2 =\n%v", test.PrettyPrintYAML(t, k2))
t.Errorf("roundtrip failed; diff:\n%s", diff)
}
})
}
7 changes: 7 additions & 0 deletions pkg/test/fuzz/krmgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ func (rf *RandomFiller) fillWithRandom(t *testing.T, fieldName string, field ref
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
}
// todo acpana dedup w call below
if rf.fieldOverrides != nil {
if override, ok := rf.fieldOverrides[fieldName]; ok {
override(t, fieldName, field)
return
}
}
rf.fillWithRandom(t, fieldName, field.Elem())
return
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package test

import (
"encoding/json"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -242,3 +243,32 @@ func CompareGoldenObject(t *testing.T, p string, got []byte) {
t.Logf("wrote updated golden output to %s", p)
}
}

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

return string(encoded)
}

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

otherK := new(T)
err = json.Unmarshal(encoded, &otherK)
if err != nil {
t.Fatalf("error decoding from json: %v", err)
}

yEncoded, err := yaml.Marshal(otherK)
if err != nil {
t.Fatalf("error encoding to yaml: %v", err)
}

return string(yEncoded)
}
Loading