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

rapid test separate schema gen #2126

Draft
wants to merge 1 commit 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
41 changes: 41 additions & 0 deletions pkg/tests/cross-tests/rapid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,44 @@ func (*rapidTWithCleanup) Deadline() (time.Time, bool) {
func (rtc *rapidTWithCleanup) Cleanup(work func()) {
rtc.outerT.Cleanup(work)
}

func TestFixedSchema(outerT *testing.T) {
_, ok := os.LookupEnv("PULUMI_EXPERIMENTAL")
if !ok {
outerT.Skip("TODO - we do not currently pass all cases; using this as an exploration tool")
}
outerT.Parallel()

log.SetOutput(io.Discard)
mainResSchema := map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"bool_prop": {
Type: schema.TypeBool,
Optional: true,
},
}
mainRes := schema.Resource{Schema: mainResSchema}

outerT.Logf("Schema:\n%v\n", (&prettySchemaWrapper{schema.Schema{Elem: mainRes}}).GoString())

valueGen := rapid.Map(GenValue(mainRes), newPrettyValueWrapper)
rapid.Check(outerT, func(t *rapid.T) {
outerT.Logf("Iterating..")

config1 := valueGen.Draw(t, "config1")
t.Logf("Config1:\n%v\n", config1.GoString())
config2 := valueGen.Draw(t, "config2")
t.Logf("Config2:\n%v\n", config2.GoString())

tc := diffTestCase{
Resource: &mainRes,
Config1: config1.Value(),
Config2: config2.Value(),
}

runDiffCheck(&rapidTWithCleanup{t, outerT}, tc)
})
}
107 changes: 107 additions & 0 deletions pkg/tests/cross-tests/rapid_value_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package crosstests

import (
"fmt"

"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"pgregory.net/rapid"
)

var auxiliaryResourceSchema = map[string]*schema.Schema{
"true_bool": {
Type: schema.TypeBool,
Optional: true,
},
"false_bool": {
Type: schema.TypeBool,
Optional: true,
},
}

var auxiliaryResourceConfig = tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"true_bool": tftypes.Bool,
"false_bool": tftypes.Bool,
},
},
map[string]tftypes.Value{
"true_bool": tftypes.NewValue(tftypes.Bool, true),
"false_bool": tftypes.NewValue(tftypes.Bool, false),
},
)

// None of the functions here should call Draw
func GenValue(mainRes schema.Resource) *rapid.Generator[tftypes.Value] {
_, ok := mainRes.SchemaMap()["id"]
if !ok {
contract.Failf("Expected an id property.")
}
return rapid.Custom(func(t *rapid.T) tftypes.Value {
return GenBlock(t, mainRes)
})
}

func GenBlock(t *rapid.T, blockRes schema.Resource) tftypes.Value {
values := make(map[string]tftypes.Value, 0)

blockSchemaMap := blockRes.SchemaMap()
for key := range blockRes.CoreConfigSchema().Attributes {
if key == "id" {
continue
}
// gen attr
attrSchema, ok := blockSchemaMap[key]
if !ok {
panic(fmt.Sprintf("failed to find %s", key))
}
attrGenerator := GenAttr(t, attrSchema, key)
values[key] = attrGenerator
}

for range blockRes.CoreConfigSchema().BlockTypes {
// gen blocks
// TODO
}

types := make(map[string]tftypes.Type, len(values))
for key, val := range values {
types[key] = val.Type()
}

return tftypes.NewValue(tftypes.Object{AttributeTypes: types}, values)
}

func GenAttr(t *rapid.T, attrSchema *schema.Schema, attrName string) tftypes.Value {
switch attrSchema.Type {
case schema.TypeBool:
return genBool().Draw(t, attrName)
default:
panic("TODO")
}
}

func genBool() *rapid.Generator[tftypes.Value] {
// TODO Unknowns, computed, secret
return genBoolPlain()
}

func genBoolPlain() *rapid.Generator[tftypes.Value] {
return rapid.SampledFrom([]tftypes.Value{
tftypes.NewValue(tftypes.Bool, true),
tftypes.NewValue(tftypes.Bool, false),
})
}

// func genListNestedBlock(maxDepth int, parentName string) *rapid.Generator[tftypes.Value] {
// // TODO Unknowns, computed, secret
// return genListNestedBlockPlain(maxDepth, parentName)
// }

// func genListNestedBlockPlain(maxDepth int, parentName string) *rapid.Generator[tftypes.Value] {
// opts := []*rapid.Generator[tftypes.Value]{
// }
// return rapid.OneOf[](opts)
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 2024/06/17 09:57:11 TestCreateInputsConvergence [rapid] draw schemaDepth: 1
# 2024/06/17 09:57:11 TestCreateInputsConvergence [rapid] draw tv: crosstests.tb{schemaMap:map[string]*schema.Schema{}, typ:tftypes.Object{AttributeTypes:map[string]tftypes.Type{}, OptionalAttributes:map[string]struct {}(nil), _:[]struct {}(nil)}, valueGen:(*rapid.Generator[github.com/hashicorp/terraform-plugin-go/tftypes.Value])(0x14000fbc480)}
# 2024/06/17 09:57:11 TestCreateInputsConvergence Schema:
# schema.Schema{Elem: &schema.Resource{
# Schema: map[string]*schema.Schema{},
# }}
# 2024/06/17 09:57:11 TestCreateInputsConvergence [rapid] draw config1:
# t0 := tftypes.Object{}
# tftypes.NewValue(t0, map[string]tftypes.Value{
# })
# 2024/06/17 09:57:11 TestCreateInputsConvergence
# Error Trace: /Users/vvm/code/pulumi-terraform-bridge/pkg/tests/internal/pulcheck/pulcheck.go:63
# /Users/vvm/code/pulumi-terraform-bridge/pkg/tests/cross-tests/tf_driver.go:65
# /Users/vvm/code/pulumi-terraform-bridge/pkg/tests/cross-tests/input_check.go:53
# /Users/vvm/code/pulumi-terraform-bridge/pkg/tests/cross-tests/rapid_test.go:95
# /Users/vvm/go/pkg/mod/pgregory.net/[email protected]/engine.go:352
# /Users/vvm/go/pkg/mod/pgregory.net/[email protected]/engine.go:361
# /Users/vvm/go/pkg/mod/pgregory.net/[email protected]/engine.go:204
# /Users/vvm/go/pkg/mod/pgregory.net/[email protected]/engine.go:116
# /Users/vvm/code/pulumi-terraform-bridge/pkg/tests/cross-tests/rapid_test.go:75
# Error: Received unexpected error:
# resource crossprovider_test_res: All fields are ForceNew or Computed w/out Optional, Update is superfluous
# Test: TestCreateInputsConvergence
#
v0.4.8#9032794167843690456
0x0
0x0
0x0
0x0
0x0
0x0
0x0
0x0
0x0
Loading
Loading