Skip to content

Commit

Permalink
adding tests, resource test not working due to bug, will work when pu…
Browse files Browse the repository at this point in the history
…blished to registry - hashicorp/terraform-plugin-sdk#1171
  • Loading branch information
kamsandhu93 committed May 25, 2023
1 parent b00fdfa commit 10a9b56
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 28 deletions.
6 changes: 3 additions & 3 deletions GNUmakefile
Expand Up @@ -2,7 +2,7 @@ default: testacc

# Install binary at ~/go/bin
install:
go build -o ~/go/bin/terraform-provider-gqldenring-tfprov
go build -o ~/go/bin/terraform-provider-gqldenring

init-%:
cd examples/$* && terraform init
Expand All @@ -15,5 +15,5 @@ apply-%:

# Run acceptance tests
.PHONY: testacc
testacc:
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m
testacc: install
TF_ACC_PROVIDER_NAMESPACE=github.com/kamsandhu93/ TF_CLI_CONFIG_FILE=~/.terraformrc TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m
2 changes: 1 addition & 1 deletion examples/data-sources/weapons.tf
@@ -1,7 +1,7 @@
terraform {
required_providers {
gqldenring = {
source = "github.com/kamsandhu93/gqldenring-tfprov"
source = "github.com/kamsandhu93/gqldenring"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
@@ -1,7 +1,7 @@
terraform {
required_providers {
gqldenring = {
source = "github.com/kamsandhu93/gqldenring-tfprov"
source = "github.com/kamsandhu93/gqldenring"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/resources/weapon.tf
@@ -1,7 +1,7 @@
terraform {
required_providers {
gqldenring = {
source = "github.com/kamsandhu93/gqldenring-tfprov"
source = "github.com/kamsandhu93/gqldenring"
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions internal/provider/provider_test.go
Expand Up @@ -7,6 +7,16 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

const (
// providerConfig is a shared configuration to combine with the actual
// test configuration
providerConfig = `
provider "gqldenring" {
endpoint = "http://localhost:8080/query"
}
`
)

// testAccProtoV6ProviderFactories are used to instantiate a provider during
// acceptance testing. The factory function will be invoked for every Terraform
// CLI command executed to create a provider server to which the CLI can
Expand Down
8 changes: 7 additions & 1 deletion internal/provider/weapon_data_source.go
Expand Up @@ -24,6 +24,7 @@ type WeaponsDataSource struct {
// WeaponsDataSourceModel describes the data source data model.
type WeaponsDataSourceModel struct {
Weapons []WeaponModel `tfsdk:"weapons"`
Id types.String `tfsdk:"id"`
}

type WeaponModel struct {
Expand Down Expand Up @@ -55,7 +56,9 @@ func (d *WeaponsDataSource) Schema(ctx context.Context, req datasource.SchemaReq
},
},
},
},
"id": schema.StringAttribute{
Computed: true,
}},
}
}

Expand Down Expand Up @@ -96,8 +99,11 @@ func (d *WeaponsDataSource) Read(ctx context.Context, req datasource.ReadRequest
}

state.Weapons = append(state.Weapons, _weapon)

}

state.Id = types.StringValue("placeholder")

// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/weapon_data_source_test.go
Expand Up @@ -13,9 +13,9 @@ func TestAccWeaponsDataSource(t *testing.T) {
Steps: []resource.TestStep{
// Read testing
{
Config: testAccWeaponsDataSourceConfig,
Config: providerConfig + testAccWeaponsDataSourceConfig,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.gqldenring_weapons.test", "id", "example-id"),
resource.TestCheckResourceAttr("data.gqldenring_weapons.test", "id", "placeholder"),
),
},
},
Expand Down
29 changes: 11 additions & 18 deletions internal/provider/weapon_resource_test.go
Expand Up @@ -2,9 +2,8 @@ package provider

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"testing"
)

func TestAccExampleResource(t *testing.T) {
Expand All @@ -14,40 +13,34 @@ func TestAccExampleResource(t *testing.T) {
Steps: []resource.TestStep{
// Create and Read testing
{
Config: testAccExampleResourceConfig("one"),
Config: providerConfig + testAccWeaponResourceConfig("one"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("scaffolding_example.test", "configurable_attribute", "one"),
resource.TestCheckResourceAttr("scaffolding_example.test", "defaulted", "example value when not configured"),
resource.TestCheckResourceAttr("scaffolding_example.test", "id", "example-id"),
resource.TestCheckResourceAttr("gqldenring_weapon.test", "name", "one"),
resource.TestCheckResourceAttr("gqldenring_weapon.test", "custom", "true"),
),
},
// ImportState testing
{
ResourceName: "scaffolding_example.test",
ResourceName: "gqldenring_weapon.test",
ImportState: true,
ImportStateVerify: true,
// This is not normally necessary, but is here because this
// example code does not have an actual upstream service.
// Once the Read method is able to refresh information from
// the upstream service, this can be removed.
ImportStateVerifyIgnore: []string{"configurable_attribute", "defaulted"},
},
// Update and Read testing
{
Config: testAccWeaponResourceConfig("two"),
Config: providerConfig + testAccWeaponResourceConfig("two"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("gqldenring_weapon.test", "configurable_attribute", "two"),
resource.TestCheckResourceAttr("gqldenring_weapon.test", "name", "two"),
),
},
// Delete testing automatically occurs in TestCase
},
})
}

func testAccWeaponResourceConfig(configurableAttribute string) string {
func testAccWeaponResourceConfig(name string) string {
return fmt.Sprintf(`
resource "gldenring_weapon" "test" {
configurable_attribute = %[1]q
resource "gqldenring_weapon" "test" {
name = %[1]q
}
`, configurableAttribute)
`, name)
}
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -36,7 +36,7 @@ func main() {

opts := providerserver.ServeOpts{
// TODO: Update this string with the published name of your provider.
Address: "github.com/kamsandhu93/gqldenring-tfprov",
Address: "github.com/kamsandhu93/gqldenring",
Debug: debug,
}

Expand Down

0 comments on commit 10a9b56

Please sign in to comment.