Skip to content

Commit

Permalink
refactor: deduplicate beta checks (#1518)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serpentiel authored Jan 10, 2024
1 parent 02e59e9 commit 0b8cae8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
19 changes: 15 additions & 4 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/plugin/errmsg"
"github.com/aiven/terraform-provider-aiven/internal/plugin/util"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/server"
)
Expand All @@ -30,6 +31,16 @@ var (
}
)

var (
// ErrMustSetBetaEnvVar is an error that is returned when the PROVIDER_AIVEN_ENABLE_BETA environment variable is not
// set, but it is required for the concrete acceptance test to run.
ErrMustSetBetaEnvVar = "PROVIDER_AIVEN_ENABLE_BETA must be set for this test to run"

// ErrMustSetOrganizationUserIDEnvVar is an error that is returned when the AIVEN_ORGANIZATION_USER_ID environment
// variable is not set, but it is required for the concrete acceptance test to run.
ErrMustSetOrganizationUserIDEnvVar = "AIVEN_ORGANIZATION_USER_ID must be set for this test to run"
)

// GetTestAivenClient returns a new Aiven client that can be used for acceptance tests.
func GetTestAivenClient() *aiven.Client {
testAivenClientOnce.Do(func() {
Expand Down Expand Up @@ -84,12 +95,12 @@ func CommonTestDependencies(t *testing.T) *commonTestDependencies {
}

deps := &commonTestDependencies{
isBeta: os.Getenv("PROVIDER_AIVEN_ENABLE_BETA") != "",
isBeta: util.IsBeta(),
}

organizationName, ok := os.LookupEnv("AIVEN_ORGANIZATION_NAME")
if !ok {
t.Fatal("AIVEN_ORGANIZATION_NAME environment variable must be set for acceptance tests.")
t.Fatal("AIVEN_ORGANIZATION_NAME environment variable must be set for acceptance tests")
}
deps.organizationName = organizationName

Expand All @@ -113,11 +124,11 @@ const (
// It is used to perform any pre-test setup, such as environment variable validation.
func TestAccPreCheck(t *testing.T) {
if _, ok := os.LookupEnv("AIVEN_TOKEN"); !ok {
t.Fatal("AIVEN_TOKEN environment variable must be set for acceptance tests.")
t.Fatal("AIVEN_TOKEN environment variable must be set for acceptance tests")
}

if _, ok := os.LookupEnv("AIVEN_PROJECT_NAME"); !ok {
t.Log("AIVEN_PROJECT_NAME environment variable is not set. Some acceptance tests will be skipped.")
t.Log("AIVEN_PROJECT_NAME environment variable is not set. Some acceptance tests will be skipped")
}
}

Expand Down
9 changes: 3 additions & 6 deletions internal/plugin/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/plugin/errmsg"
"github.com/aiven/terraform-provider-aiven/internal/plugin/service/organization"
"github.com/aiven/terraform-provider-aiven/internal/plugin/util"
)

// AivenProvider is the provider implementation for Aiven.
Expand Down Expand Up @@ -113,10 +114,8 @@ func (p *AivenProvider) Resources(context.Context) []func() resource.Resource {
organization.NewOrganizationResource,
}

isBeta := os.Getenv("PROVIDER_AIVEN_ENABLE_BETA") != ""

// Add to a list of resources that are currently in beta.
if isBeta {
if util.IsBeta() {
betaResources := []func() resource.Resource{
organization.NewOrganizationUserGroupMembersResource,
organization.NewOrganizationGroupProjectResource,
Expand All @@ -136,10 +135,8 @@ func (p *AivenProvider) DataSources(context.Context) []func() datasource.DataSou
organization.NewOrganizationDataSource,
}

isBeta := os.Getenv("PROVIDER_AIVEN_ENABLE_BETA") != ""

// Add to a list of data sources that are currently in beta.
if isBeta {
if util.IsBeta() {
betaDataSources := []func() datasource.DataSource{
organization.NewOrganizationApplicationUserDataSource,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAccOrganizationApplicationUserResourceDataSource(t *testing.T) {
deps := acc.CommonTestDependencies(t)

if !deps.IsBeta() {
t.Skip("PROVIDER_AIVEN_ENABLE_BETA must be set for this test to run.")
t.Skip(acc.ErrMustSetBetaEnvVar)
}

name := "aiven_organization_application_user.foo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestAccOrganizationGroupProject(t *testing.T) {
deps := acc.CommonTestDependencies(t)

if !deps.IsBeta() {
t.Skip("PROVIDER_AIVEN_ENABLE_BETA must be set for this test to run.")
t.Skip(acc.ErrMustSetBetaEnvVar)
}

name := "aiven_organization_group_project.foo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TestAccOrganizationUserGroupMember(t *testing.T) {
deps := acc.CommonTestDependencies(t)

if !deps.IsBeta() {
t.Skip("PROVIDER_AIVEN_ENABLE_BETA must be set for this test to run.")
t.Skip(acc.ErrMustSetBetaEnvVar)
}

userID := deps.OrganizationUserID()
if userID == nil {
t.Skip("AIVEN_ORGANIZATION_USER_ID must be set for this test to run.")
t.Skip(acc.ErrMustSetOrganizationUserIDEnvVar)
}

name := "aiven_organization_user_group_member.foo"
Expand Down
12 changes: 11 additions & 1 deletion internal/plugin/util/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package util

import "strings"
import (
"os"
"strings"
)

// Ref is a helper function that returns a pointer to the value passed in.
func Ref[T any](v T) *T {
Expand All @@ -18,6 +21,13 @@ func Deref[T any](p *T) T {
return result
}

// IsBeta is a helper function that returns a flag that indicates whether the provider is in beta mode.
// This SHOULD NOT be used anywhere else except in the provider and acceptance tests initialization.
// In case this functionality is needed in tests, please use the acctest.CommonTestDependencies.IsBeta() function.
func IsBeta() bool {
return os.Getenv("PROVIDER_AIVEN_ENABLE_BETA") != ""
}

// ComposeID is a helper function that composes an ID from the parts passed in.
func ComposeID(parts ...string) string {
return strings.Join(parts, "/")
Expand Down

0 comments on commit 0b8cae8

Please sign in to comment.