-
Notifications
You must be signed in to change notification settings - Fork 16
discovery: query mode #528
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package query_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/teststep" | ||
) | ||
|
||
func examplecloudResource() testprovider.Resource { | ||
return testprovider.Resource{ | ||
CreateResponse: &resource.CreateResponse{ | ||
NewState: tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"id": tftypes.String, | ||
"location": tftypes.String, | ||
"name": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), | ||
"location": tftypes.NewValue(tftypes.String, "westeurope"), | ||
"name": tftypes.NewValue(tftypes.String, "somevalue"), | ||
}, | ||
), | ||
NewIdentity: teststep.Pointer(tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"id": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), | ||
}, | ||
)), | ||
}, | ||
ReadResponse: &resource.ReadResponse{ | ||
NewState: tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"id": tftypes.String, | ||
"location": tftypes.String, | ||
"name": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), | ||
"location": tftypes.NewValue(tftypes.String, "westeurope"), | ||
"name": tftypes.NewValue(tftypes.String, "somevalue"), | ||
}, | ||
), | ||
NewIdentity: teststep.Pointer(tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"id": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), | ||
}, | ||
)), | ||
}, | ||
ImportStateResponse: &resource.ImportStateResponse{ | ||
State: tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"id": tftypes.String, | ||
"location": tftypes.String, | ||
"name": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), | ||
"location": tftypes.NewValue(tftypes.String, "westeurope"), | ||
"name": tftypes.NewValue(tftypes.String, "somevalue"), | ||
}, | ||
), | ||
Identity: teststep.Pointer(tftypes.NewValue( | ||
tftypes.Object{ | ||
AttributeTypes: map[string]tftypes.Type{ | ||
"id": tftypes.String, | ||
}, | ||
}, | ||
map[string]tftypes.Value{ | ||
"id": tftypes.NewValue(tftypes.String, "westeurope/somevalue"), | ||
}, | ||
)), | ||
}, | ||
SchemaResponse: &resource.SchemaResponse{ | ||
Schema: &tfprotov6.Schema{ | ||
Block: &tfprotov6.SchemaBlock{ | ||
Attributes: []*tfprotov6.SchemaAttribute{ | ||
ComputedStringAttribute("id"), | ||
RequiredStringAttribute("location"), | ||
RequiredStringAttribute("name"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
IdentitySchemaResponse: &resource.IdentitySchemaResponse{ | ||
Schema: &tfprotov6.ResourceIdentitySchema{ | ||
Version: 1, | ||
IdentityAttributes: []*tfprotov6.ResourceIdentitySchemaAttribute{ | ||
{ | ||
Name: "id", | ||
Type: tftypes.String, | ||
RequiredForImport: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package query_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
r "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/list" | ||
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
func TestQuery(t *testing.T) { | ||
t.Parallel() | ||
|
||
r.UnitTest(t, r.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
tfversion.SkipBelow(tfversion.Version1_13_0), // Query mode requires Terraform 1.13.0 or later | ||
}, | ||
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){ | ||
"examplecloud": providerserver.NewProviderServer(testprovider.Provider{ | ||
ListResources: map[string]testprovider.ListResource{ | ||
"examplecloud_containerette": { | ||
SchemaResponse: &list.SchemaResponse{ | ||
Schema: &tfprotov6.Schema{ | ||
Block: &tfprotov6.SchemaBlock{ | ||
Attributes: []*tfprotov6.SchemaAttribute{ | ||
ComputedStringAttribute("id"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
ListResultsStream: &list.ListResultsStream{ | ||
Results: func(push func(list.ListResult) bool) { | ||
}, | ||
}, | ||
}, | ||
}, | ||
Resources: map[string]testprovider.Resource{ | ||
"examplecloud_containerette": examplecloudResource(), | ||
}, | ||
}), | ||
}, | ||
Steps: []r.TestStep{ | ||
{ | ||
Query: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Query mode! |
||
Config: ` | ||
provider "examplecloud" {} | ||
list "examplecloud_containerette" "test" { | ||
provider = examplecloud | ||
config { | ||
id = "bat" | ||
} | ||
}`, | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied from |
||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package query_test | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func RequiredBoolAttribute(name string) *tfprotov6.SchemaAttribute { | ||
return &tfprotov6.SchemaAttribute{ | ||
Name: name, | ||
Type: tftypes.Bool, | ||
Required: true, | ||
} | ||
} | ||
|
||
func OptionalComputedListAttribute(name string, elementType tftypes.Type) *tfprotov6.SchemaAttribute { | ||
return &tfprotov6.SchemaAttribute{ | ||
Name: name, | ||
Type: tftypes.List{ElementType: elementType}, | ||
Optional: true, | ||
Computed: true, | ||
} | ||
} | ||
|
||
func RequiredListAttribute(name string, elementType tftypes.Type) *tfprotov6.SchemaAttribute { | ||
return &tfprotov6.SchemaAttribute{ | ||
Name: name, | ||
Type: tftypes.List{ElementType: elementType}, | ||
Required: true, | ||
} | ||
} | ||
|
||
func RequiredNumberAttribute(name string) *tfprotov6.SchemaAttribute { | ||
return &tfprotov6.SchemaAttribute{ | ||
Name: name, | ||
Type: tftypes.Number, | ||
Required: true, | ||
} | ||
} | ||
|
||
func ComputedStringAttribute(name string) *tfprotov6.SchemaAttribute { | ||
return &tfprotov6.SchemaAttribute{ | ||
Name: name, | ||
Type: tftypes.String, | ||
Computed: true, | ||
} | ||
} | ||
|
||
func OptionalStringAttribute(name string) *tfprotov6.SchemaAttribute { | ||
return &tfprotov6.SchemaAttribute{ | ||
Name: name, | ||
Type: tftypes.String, | ||
Optional: true, | ||
} | ||
} | ||
|
||
func RequiredStringAttribute(name string) *tfprotov6.SchemaAttribute { | ||
return &tfprotov6.SchemaAttribute{ | ||
Name: name, | ||
Type: tftypes.String, | ||
Required: true, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest | |
}, | ||
) | ||
|
||
fmt.Println("Writing provider configuration:", c.providerConfig(ctx, false)) | ||
err := wd.SetConfig(ctx, config, nil) | ||
|
||
if err != nil { | ||
|
@@ -254,7 +255,10 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest | |
|
||
testStepConfig = teststep.Configuration(confRequest) | ||
|
||
err = wd.SetConfig(ctx, testStepConfig, step.ConfigVariables) | ||
if !step.Query { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For purposes of this PR, Does |
||
fmt.Println("Writing pre-switch configuration:", rawCfg) | ||
err = wd.SetConfig(ctx, testStepConfig, step.ConfigVariables) | ||
} | ||
|
||
if err != nil { | ||
logging.HelperResourceError(ctx, | ||
|
@@ -356,6 +360,39 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest | |
continue | ||
} | ||
|
||
if step.Query { | ||
logging.HelperResourceTrace(ctx, "TestStep is Query mode") | ||
|
||
queryConfigRequest := teststep.ConfigurationRequest{ | ||
Raw: &step.Config, | ||
} | ||
err := wd.SetQuery(ctx, teststep.Configuration(queryConfigRequest), step.ConfigVariables) | ||
if err != nil { | ||
t.Fatalf("Step %d/%d error setting query: %s", stepNumber, len(c.Steps), err) | ||
} | ||
|
||
err = runProviderCommand(ctx, t, wd, providers, func() error { | ||
return wd.Init(ctx) | ||
}) | ||
if err != nil { | ||
t.Fatalf("Step %d/%d error running init: %s", stepNumber, len(c.Steps), err) | ||
} | ||
|
||
var queryOut []string | ||
err = runProviderCommand(ctx, t, wd, providers, func() error { | ||
var err error | ||
queryOut, err = wd.Query(ctx) | ||
return err | ||
}) | ||
if err != nil { | ||
fmt.Printf("Step %d/%d Query Output:\n%s\n", stepNumber, len(c.Steps), queryOut) | ||
t.Fatalf("Step %d/%d error running query: %s", stepNumber, len(c.Steps), err) | ||
} | ||
|
||
fmt.Printf("Step %d/%d Query Output:\n%s\n", stepNumber, len(c.Steps), queryOut) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This simply echoes the Terraform output. So a test only fails if In the real world, we would make assertions, call "query checks," ... |
||
continue | ||
} | ||
|
||
if cfg != nil { | ||
logging.HelperResourceTrace(ctx, "TestStep is Config mode") | ||
|
||
|
@@ -567,6 +604,7 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest. | |
|
||
testStepConfigDefer := teststep.Configuration(confRequest) | ||
|
||
fmt.Println("Writing the reset to original configuration:", rawCfg) | ||
err = wd.SetConfig(ctx, testStepConfigDefer, step.ConfigVariables) | ||
|
||
if err != nil { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied from
../importstate
.