Skip to content

Commit

Permalink
Reorganise provider code to make it easy to scale tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-futurice committed Jun 27, 2024
1 parent d3d6b50 commit 1af500d
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package aclmapping

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package notificationpublisher

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package notificationpublisher

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package notificationrule

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package notificationruleproject

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package project

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider_test
package project_test

import (
"context"
Expand Down Expand Up @@ -42,8 +42,8 @@ func TestAccProjectResource_basic(t *testing.T) {
}

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
PreCheck: func() { testutils.TestAccPreCheck(t) },
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProjectConfigBasic(testDependencyTrack, projectName),
Expand Down Expand Up @@ -84,8 +84,8 @@ func TestAccProjectResource_description(t *testing.T) {
testUpdatedProject.Description = "Other description"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
PreCheck: func() { testutils.TestAccPreCheck(t) },
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProjectConfigDescription(testDependencyTrack, projectName, testProject.Description),
Expand Down Expand Up @@ -121,8 +121,8 @@ func TestAccProjectResource_inactive(t *testing.T) {
testUpdatedProject.Active = true

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
PreCheck: func() { testutils.TestAccPreCheck(t) },
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProjectConfigActivity(testDependencyTrack, projectName, testProject.Active),
Expand Down Expand Up @@ -158,8 +158,8 @@ func TestAccProjectResource_classifier(t *testing.T) {
testUpdatedProject.Classifier = "DEVICE"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
PreCheck: func() { testutils.TestAccPreCheck(t) },
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProjectConfigClassifier(testDependencyTrack, testProject.Name, testProject.Classifier),
Expand Down Expand Up @@ -200,8 +200,8 @@ func TestAccProjectResource_parent(t *testing.T) {
var parentProjectID, otherParentProjectID string

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
PreCheck: func() { testutils.TestAccPreCheck(t) },
ProtoV6ProviderFactories: testutils.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccProjectConfigParent(testDependencyTrack, projectName),
Expand Down
30 changes: 18 additions & 12 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ package provider

import (
"context"

dtrack "github.com/futurice/dependency-track-client-go"

"github.com/futurice/terraform-provider-dependencytrack/internal/provider/aclmapping"
"github.com/futurice/terraform-provider-dependencytrack/internal/provider/notificationpublisher"
"github.com/futurice/terraform-provider-dependencytrack/internal/provider/notificationrule"
"github.com/futurice/terraform-provider-dependencytrack/internal/provider/notificationruleproject"
"github.com/futurice/terraform-provider-dependencytrack/internal/provider/project"
"github.com/futurice/terraform-provider-dependencytrack/internal/provider/team"
"github.com/futurice/terraform-provider-dependencytrack/internal/provider/teamapikey"
"github.com/futurice/terraform-provider-dependencytrack/internal/provider/teampermission"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/function"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand Down Expand Up @@ -97,21 +103,21 @@ func (p *DependencyTrackProvider) Configure(ctx context.Context, req provider.Co

func (p *DependencyTrackProvider) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewTeamResource,
NewTeamAPIKeyResource,
NewTeamPermissionResource,
NewProjectResource,
NewACLMappingResource,
NewNotificationRuleResource,
NewNotificationRuleProjectResource,
NewNotificationPublisherResource,
team.NewTeamResource,
teamapikey.NewTeamAPIKeyResource,
teampermission.NewTeamPermissionResource,
project.NewProjectResource,
aclmapping.NewACLMappingResource,
notificationrule.NewNotificationRuleResource,
notificationruleproject.NewNotificationRuleProjectResource,
notificationpublisher.NewNotificationPublisherResource,
}
}

func (p *DependencyTrackProvider) DataSources(ctx context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewTeamDataSource,
NewNotificationPublisherDataSource,
team.NewTeamDataSource,
notificationpublisher.NewNotificationPublisherDataSource,
}
}

Expand Down
26 changes: 0 additions & 26 deletions internal/provider/provider_test.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package team

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package team

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package teamapikey

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package provider
package teampermission

import (
"context"
Expand Down
40 changes: 31 additions & 9 deletions internal/testutils/terraform.go → internal/testutils/acctest.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package testutils

import (
"fmt"
"github.com/futurice/terraform-provider-dependencytrack/internal/provider"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

// 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
// reattach.
var TestAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"dependencytrack": providerserver.NewProtocol6WithError(provider.New("test")()),
}

func TestAccPreCheck(t *testing.T) {
// You can add code here to run prior to any test case execution, for example assertions
// about the appropriate environment variables being set are common to see in a pre-check
// function.
}

// TestAccCheckDelay does not check anything, but can be used to introduce a delay into the test for
// debugging the created resources before Terraform goes on to delete them.
func TestAccCheckDelay(d time.Duration) resource.TestCheckFunc {
return func(state *terraform.State) error {
time.Sleep(d)
return nil
}
}

func GetResourceID(state *terraform.State, resourceName string) (uuid.UUID, error) {
res, ok := state.RootModule().Resources[resourceName]
if !ok {
Expand Down Expand Up @@ -38,12 +69,3 @@ func TestAccCheckGetResourceID(resourceName string, uuidOutput *string) resource
return nil
}
}

// TestAccCheckDelay does not check anything, but can be used to introduce a delay into the test for
// debugging the created resources before Terraform goes on to delete them.
func TestAccCheckDelay(d time.Duration) resource.TestCheckFunc {
return func(state *terraform.State) error {
time.Sleep(d)
return nil
}
}

0 comments on commit 1af500d

Please sign in to comment.