Skip to content

Commit

Permalink
SUP-1076 Convert testing to framework (#361)
Browse files Browse the repository at this point in the history
* Update go.mod

* Rewrite pipeline tests to framework

* Rewrite pipeline schedule tests to framework

* Remove undefined destroy check func

* Change to framework testing

* Change all imports to testing framework

* Add changelog

* Deduplicate config in pipeline schedule test
  • Loading branch information
jradtilbrook authored Aug 21, 2023
1 parent e8f91bf commit 71fa938
Show file tree
Hide file tree
Showing 24 changed files with 838 additions and 1,522 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
## Unreleased

- Move archive pipeline config to provider [[PR #354](https://github.com/buildkite/terraform-provider-buildkite/pull/354)] @jradtilbrook
- SUP-1076 Convert testing to framework [[PR #361](https://github.com/buildkite/terraform-provider-buildkite/pull/361)] @jradtilbrook

## [v0.24.0](https://github.com/buildkite/terraform-provider-buildkite/compare/v0.23.0...v0.24.0)

Expand Down
2 changes: 1 addition & 1 deletion buildkite/data_source_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"regexp"
"testing"

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

func TestDataCluster_read(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions buildkite/data_source_meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
"testing"

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

// Confirm that we can create a new agent token, and then delete it without error
Expand Down
2 changes: 1 addition & 1 deletion buildkite/data_source_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

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

func TestAccDataOrganization(t *testing.T) {
Expand Down
81 changes: 41 additions & 40 deletions buildkite/data_source_pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,52 @@ package buildkite

import (
"fmt"
"regexp"
"testing"

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

// Confirm that we can create a new agent token, and then delete it without error
func TestAccDataPipeline_read(t *testing.T) {
var resourcePipeline PipelineNode
func TestAccBuildkitePipelineDataSource(t *testing.T) {
var pipeline getPipelinePipeline
pipelineName := acctest.RandString(12)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
CheckDestroy: testAccCheckPipelineResourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccDataPipelineConfigBasic("foo"),
Check: resource.ComposeAggregateTestCheckFunc(
// Confirm the pipeline exists in the buildkite API
testAccCheckPipelineExists("buildkite_pipeline.foobar", &resourcePipeline),
// Confirm the pipeline data source has the correct values in terraform state
resource.TestCheckResourceAttr("data.buildkite_pipeline.foobar", "name", "Test Pipeline foo"),
resource.TestCheckResourceAttr("data.buildkite_pipeline.foobar", "repository", "https://github.com/buildkite/terraform-provider-buildkite.git"),
resource.TestCheckResourceAttr("data.buildkite_pipeline.foobar", "default_branch", "main"),
resource.TestCheckResourceAttr("data.buildkite_pipeline.foobar", "description", "A test pipeline foo"),
resource.TestMatchResourceAttr("data.buildkite_pipeline.foobar", "webhook_url", regexp.MustCompile("^https://webhook.buildkite.com/deliver/.+")),
),
},
},
})
}

func testAccDataPipelineConfigBasic(name string) string {
config := `
resource "buildkite_pipeline" "foobar" {
name = "Test Pipeline %s"
repository = "https://github.com/buildkite/terraform-provider-buildkite.git"
default_branch = "main"
description = "A test pipeline %s"
steps = ""
loadPipeline := func(pipeline *getPipelinePipeline) resource.TestCheckFunc {
return func(s *terraform.State) error {
slug := fmt.Sprintf("%s/%s", getenv("BUILDKITE_ORGANIZATION_SLUG"), pipelineName)
resp, err := getPipeline(genqlientGraphql, slug)
pipeline = &resp.Pipeline
return err
}
}

data "buildkite_pipeline" "foobar" {
slug = buildkite_pipeline.foobar.slug
}
`
return fmt.Sprintf(config, name, name)
t.Run("pipeline datasource can be loaded from slug", func(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: protoV6ProviderFactories(),
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "buildkite_pipeline" "pipeline" {
name = "%s"
repository = "https://github.com/buildkite/terraform-provider-buildkite.git"
}
data "buildkite_pipeline" "pipeline" {
slug = buildkite_pipeline.pipeline.slug
}
`, pipelineName),
Check: resource.ComposeAggregateTestCheckFunc(
// Confirm the pipeline exists in the buildkite API
loadPipeline(&pipeline),
// Confirm the pipeline data source has the correct values in terraform state
resource.TestCheckResourceAttr("data.buildkite_pipeline.pipeline", "name", pipelineName),
resource.TestCheckResourceAttr("data.buildkite_pipeline.pipeline", "repository", "https://github.com/buildkite/terraform-provider-buildkite.git"),
resource.TestCheckResourceAttrPair("data.buildkite_pipeline.pipeline", "id", "buildkite_pipeline.pipeline", "id"),
),
},
},
})
})
}
2 changes: 1 addition & 1 deletion buildkite/data_source_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
"testing"

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

func testDatasourceTeamConfigID(name string) string {
Expand Down
8 changes: 4 additions & 4 deletions buildkite/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions buildkite/graphql/pipeline.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ query getPipeline($slug: ID!) {
# @genqlient(for: "PipelineCreateInput.pipelineTemplateId", omitempty: true)
# @genqlient(for: "PipelineCreateInput.visibility", omitempty: true)
# @genqlient(for: "PipelineCreateInput.defaultTimeoutInMinutes", pointer: true)
# @genqlient(for: "PipelineCreateInput.maximumTimeoutInMinutes", pointer: true)
mutation createPipeline(
$input: PipelineCreateInput!
) {
Expand All @@ -79,6 +80,7 @@ mutation createPipeline(
# @genqlient(for: "PipelineUpdateInput.archived", omitempty: true)
# @genqlient(for: "PipelineUpdateInput.nextBuildNumber", omitempty: true)
# @genqlient(for: "PipelineUpdateInput.defaultTimeoutInMinutes", pointer: true)
# @genqlient(for: "PipelineUpdateInput.maximumTimeoutInMinutes", pointer: true)
mutation updatePipeline(
$input: PipelineUpdateInput!
) {
Expand Down
4 changes: 2 additions & 2 deletions buildkite/resource_agent_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"strings"
"testing"

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

// Confirm that we can create a new agent token, and then delete it without error
Expand Down
4 changes: 2 additions & 2 deletions buildkite/resource_cluster_agent_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"

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

func testAccClusterAgentTokenBasic(description string) string {
Expand Down
4 changes: 2 additions & 2 deletions buildkite/resource_cluster_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strings"
"testing"

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

func testAccClusterQueueConfigBasic(name string) string {
Expand Down
6 changes: 3 additions & 3 deletions buildkite/resource_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"testing"

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

func testAccClusterBasic(name string) string {
Expand Down
4 changes: 2 additions & 2 deletions buildkite/resource_organization_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"testing"

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

func TestAccOrganizationSettings_create(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions buildkite/resource_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"testing"

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

func TestAccOrganization_create(t *testing.T) {
Expand Down
21 changes: 11 additions & 10 deletions buildkite/resource_pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand Down Expand Up @@ -191,6 +193,7 @@ func (p *pipelineResource) Create(ctx context.Context, req resource.CreateReques
// use the unsafe module to convert to an int. this is fine because the absolute max accepted by the API is much
// less than an int
defaultTimeoutInMinutes := (*int)(unsafe.Pointer(plan.DefaultTimeoutInMinutes.ValueInt64Pointer()))
maxTimeoutInMinutes := (*int)(unsafe.Pointer(plan.MaximumTimeoutInMinutes.ValueInt64Pointer()))

input := PipelineCreateInput{
AllowRebuilds: plan.AllowRebuilds.ValueBool(),
Expand All @@ -200,7 +203,7 @@ func (p *pipelineResource) Create(ctx context.Context, req resource.CreateReques
ClusterId: plan.ClusterId.ValueStringPointer(),
DefaultBranch: plan.DefaultBranch.ValueString(),
DefaultTimeoutInMinutes: defaultTimeoutInMinutes,
MaximumTimeoutInMinutes: int(plan.MaximumTimeoutInMinutes.ValueInt64()),
MaximumTimeoutInMinutes: maxTimeoutInMinutes,
Description: plan.Description.ValueString(),
Name: plan.Name.ValueString(),
OrganizationId: p.client.organizationId,
Expand Down Expand Up @@ -425,7 +428,9 @@ func (*pipelineResource) Schema(ctx context.Context, req resource.SchemaRequest,
},
"tags": schema.SetAttribute{
Optional: true,
Computed: true,
ElementType: types.StringType,
Default: setdefault.StaticValue(types.SetValueMust(types.StringType, []attr.Value{})),
},
"webhook_url": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -567,6 +572,7 @@ func (p *pipelineResource) Update(ctx context.Context, req resource.UpdateReques
}

defaultTimeoutInMinutes := (*int)(unsafe.Pointer(plan.DefaultTimeoutInMinutes.ValueInt64Pointer()))
maxTimeoutInMinutes := (*int)(unsafe.Pointer(plan.MaximumTimeoutInMinutes.ValueInt64Pointer()))

input := PipelineUpdateInput{
AllowRebuilds: plan.AllowRebuilds.ValueBool(),
Expand All @@ -576,7 +582,7 @@ func (p *pipelineResource) Update(ctx context.Context, req resource.UpdateReques
ClusterId: plan.ClusterId.ValueStringPointer(),
DefaultBranch: plan.DefaultBranch.ValueString(),
DefaultTimeoutInMinutes: defaultTimeoutInMinutes,
MaximumTimeoutInMinutes: int(plan.MaximumTimeoutInMinutes.ValueInt64()),
MaximumTimeoutInMinutes: maxTimeoutInMinutes,
Description: plan.Description.ValueString(),
Id: plan.Id.ValueString(),
Name: plan.Name.ValueString(),
Expand Down Expand Up @@ -654,14 +660,9 @@ func setPipelineModel(model *pipelineResourceModel, data pipelineResponse) {
model.Steps = types.StringValue(data.GetSteps().Yaml)
model.WebhookUrl = types.StringValue(data.GetWebhookURL())

var tags []types.String
if len(data.GetTags()) > 0 {
tags = make([]types.String, len(data.GetTags()))
for i, tag := range data.GetTags() {
tags[i] = types.StringValue(tag.Label)
}
} else {
tags = nil
tags := make([]types.String, len(data.GetTags()))
for i, tag := range data.GetTags() {
tags[i] = types.StringValue(tag.Label)
}
model.Tags = tags
teams := make([]*pipelineTeamModel, len(data.GetTeams().Edges))
Expand Down
1 change: 0 additions & 1 deletion buildkite/resource_pipeline_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ func (ps *pipelineSchedule) Update(ctx context.Context, req resource.UpdateReque

plan.Id = state.Id
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)

}

func (ps *pipelineSchedule) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
Expand Down
Loading

0 comments on commit 71fa938

Please sign in to comment.