Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tdabasinskas committed Jan 27, 2023
1 parent d0a11fb commit 724f13b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ jobs:
name: Acceptance Tests
needs: build
runs-on: ubuntu-latest
env:
TF_ACC: "1"
services:
backstage:
image: roadiehq/community-backstage-image
Expand All @@ -82,13 +84,11 @@ jobs:
terraform_wrapper: false
- run: go mod download
- env:
TF_ACC: "1"
ACCTEST_SKIP_RESOURCE_TEST: "1"
BACKSTAGE_BASE_URL: "https://demo.backstage.io"
run: go test -v -cover ./backstage
timeout-minutes: 10
- env:
TF_ACC: "1"
BACKSTAGE_BASE_URL: "http://localhost:${{ job.services.backstage.ports[7000] }}"
run: go test -v -cover ./backstage/resource_location_test.go
run: go test -v -cover ./backstage -run TestAccResourceLocation
timeout-minutes: 10
5 changes: 4 additions & 1 deletion backstage/resource_location.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/tdabasinskas/go-backstage/backstage"
)
Expand Down Expand Up @@ -61,7 +62,9 @@ func (r *locationResource) Schema(_ context.Context, _ resource.SchemaRequest, r
"id": schema.StringAttribute{Computed: true, Description: descriptionLocationID, PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
}},
"type": schema.StringAttribute{Computed: true, MarkdownDescription: descriptionLocationType},
"type": schema.StringAttribute{Optional: true, Computed: true, MarkdownDescription: descriptionLocationType, Validators: []validator.String{}, PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
}},
"target": schema.StringAttribute{Required: true, Description: descriptionLocationTarget,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}},
"last_updated": schema.StringAttribute{Computed: true, Description: descriptionLocationLastUpdated},
Expand Down
47 changes: 47 additions & 0 deletions backstage/resource_location_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
//go:build !resources

package backstage

import (
"os"
"testing"

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

func TestAccResourceLocation(t *testing.T) {
if os.Getenv("ACCTEST_SKIP_RESOURCE_TEST") != "" {
t.Skip("Skipping as ACCTEST_SKIP_RESOURCE_LOCATION is set")
}

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create testing
{
Config: testAccProviderConfig + testAccResourceLocationConfig1,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("backstage_location.test", "type", "url"),
resource.TestCheckResourceAttr("backstage_location.test", "target", "http://test1"),
resource.TestCheckResourceAttrSet("backstage_location.test", "id"),
resource.TestCheckResourceAttrSet("backstage_location.test", "last_updated"),
),
},
// ImportState testing
{
ResourceName: "backstage_location.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"last_updated"},
},
// Update and Read testing
{
Config: testAccProviderConfig + testAccResourceLocationConfig2,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("backstage_location.test", "type", "url"),
resource.TestCheckResourceAttr("backstage_location.test", "target", "http://test2"),
),
},
},
})

}

const testAccResourceLocationConfig1 = `
resource "backstage_location" "test" {
target = "http://test1"
}
`
const testAccResourceLocationConfig2 = `
resource "backstage_location" "test" {
target = "http://test2"
}
`
5 changes: 4 additions & 1 deletion docs/resources/location.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ resource "backstage_location" "example" {

- `target` (String) Target as a string. Should be a valid URL.

### Optional

- `type` (String) Type of the location. Always `url`.

### Read-Only

- `id` (String) Identifier of the location.
- `last_updated` (String) Timestamp of the last Terraform update of the location.
- `type` (String) Type of the location. Always `url`.


0 comments on commit 724f13b

Please sign in to comment.