Skip to content
Merged
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
31 changes: 28 additions & 3 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,36 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
}

err = query.RunQueryChecks(ctx, t, queryOut, step.QueryResultChecks)
if err != nil {
t.Fatalf("Step %d/%d error running query checks: %s", stepNumber, len(c.Steps), err)

if step.ExpectError != nil {
logging.HelperResourceDebug(ctx, "Checking TestStep ExpectError")
if err == nil {
logging.HelperResourceError(ctx, "Error running query: expected an error but got none")
t.Fatalf("Step %d/%d error running query: expected an error but got none", stepNumber, len(c.Steps))
}
if !step.ExpectError.MatchString(err.Error()) {
logging.HelperResourceError(ctx, fmt.Sprintf("Error running query: expected an error with pattern (%s)", step.ExpectError.String()),
map[string]interface{}{logging.KeyError: err},
)
t.Fatalf("Step %d/%d error running query, expected an error with pattern (%s), no match on: %s", stepNumber, len(c.Steps), step.ExpectError.String(), err)
}
} else {
if err != nil && c.ErrorCheck != nil {
logging.HelperResourceDebug(ctx, "Calling TestCase ErrorCheck")
err = c.ErrorCheck(err)
logging.HelperResourceDebug(ctx, "Called TestCase ErrorCheck")
}
if err != nil {
logging.HelperResourceError(ctx, "Error running query",
map[string]interface{}{logging.KeyError: err},
)
t.Fatalf("Step %d/%d error running query checks: %s", stepNumber, len(c.Steps), err)
}
fmt.Printf("Step %d/%d Query Output:\n%s\n", stepNumber, len(c.Steps), queryOut)
}

fmt.Printf("Step %d/%d Query Output:\n%s\n", stepNumber, len(c.Steps), queryOut)
logging.HelperResourceDebug(ctx, "Finished TestStep")

continue
}

Expand Down
File renamed without changes.
260 changes: 260 additions & 0 deletions querycheck/contains_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
// SPDX-License-Identifier: MPL-2.0

package querycheck_test

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
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/internal/testing/testsdk/resource"
"github.com/hashicorp/terraform-plugin-testing/internal/teststep"
"github.com/hashicorp/terraform-plugin-testing/querycheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func dessertsThatStartWithPResource() testprovider.Resource {
Copy link
Member Author

Choose a reason for hiding this comment

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

These are just some silly examples to allow the addition of two simple tests to validate the changes here, they are by no means complete and intentionally minimal since there's currently work in flight over in #553 to refactor and flesh out all of the remaining tests for query checks.

return testprovider.Resource{
CreateResponse: &resource.CreateResponse{
NewState: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "pie"),
},
),
NewIdentity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "pie"),
},
)),
},
ReadResponse: &resource.ReadResponse{
NewState: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "pie"),
},
),
NewIdentity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "pie"),
},
)),
},
ImportStateResponse: &resource.ImportStateResponse{
State: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "pie"),
},
),
Identity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "pie"),
},
)),
},
SchemaResponse: &resource.SchemaResponse{
Schema: &tfprotov6.Schema{
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "name",
Type: tftypes.String,
Required: true,
},
},
},
},
},
IdentitySchemaResponse: &resource.IdentitySchemaResponse{
Schema: &tfprotov6.ResourceIdentitySchema{
Version: 1,
IdentityAttributes: []*tfprotov6.ResourceIdentitySchemaAttribute{
{
Name: "name",
Type: tftypes.String,
RequiredForImport: true,
},
},
},
},
}
}

func dessertsThatStartWithPListResource() testprovider.ListResource {
return testprovider.ListResource{
IncludeResource: true,
SchemaResponse: &list.SchemaResponse{
Schema: &tfprotov6.Schema{
Block: &tfprotov6.SchemaBlock{
Attributes: []*tfprotov6.SchemaAttribute{
{
Name: "group",
Type: tftypes.String,
Computed: true,
},
},
},
},
},
ListResultsStream: &list.ListResultsStream{
Results: func(push func(list.ListResult) bool) {
push(list.ListResult{
Identity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "pie"),
},
)),
DisplayName: "pie",
})
push(list.ListResult{
Identity: teststep.Pointer(tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
},
map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "pudding"),
},
)),
DisplayName: "pudding",
})
},
},
}
}

func TestContainsResourceWithName(t *testing.T) {
t.Parallel()

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_14_0),
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"dessertcloud": providerserver.NewProviderServer(testprovider.Provider{
ListResources: map[string]testprovider.ListResource{
"dessert_letter_p": dessertsThatStartWithPListResource(),
},
Resources: map[string]testprovider.Resource{
"dessert_letter_p": dessertsThatStartWithPResource(),
},
}),
},
Steps: []r.TestStep{
{
Query: true,
Config: `
provider "dessertcloud" {}

list "dessert_letter_p" "test" {
provider = dessertcloud

config {
group = "foo"
}
}

list "dessert_letter_p" "test2" {
provider = dessertcloud

config {
group = "bar"
}
}
`,
QueryResultChecks: []querycheck.QueryResultCheck{
querycheck.ContainsResourceWithName("dessert_letter_p.test", "pie"),
querycheck.ContainsResourceWithName("dessert_letter_p.test", "pudding"),
},
},
},
})
}

func TestContainsResourceWithName_NotFound(t *testing.T) {
t.Parallel()

r.UnitTest(t, r.TestCase{
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.SkipBelow(tfversion.Version1_14_0),
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"dessertcloud": providerserver.NewProviderServer(testprovider.Provider{
ListResources: map[string]testprovider.ListResource{
"dessert_letter_p": dessertsThatStartWithPListResource(),
},
Resources: map[string]testprovider.Resource{
"dessert_letter_p": dessertsThatStartWithPResource(),
},
}),
},
Steps: []r.TestStep{
{
Query: true,
Config: `
provider "dessertcloud" {}

list "dessert_letter_p" "test" {
provider = dessertcloud

config {
group = "foo"
}
}

list "dessert_letter_p" "test2" {
provider = dessertcloud

config {
group = "bar"
}
}
`,
QueryResultChecks: []querycheck.QueryResultCheck{
querycheck.ContainsResourceWithName("dessert_letter_p.test", "pavlova"),
},
ExpectError: regexp.MustCompile("expected to find resource with display name \"pavlova\" in results but resource was not found"),
},
},
})
}