diff --git a/internal/acctest/acctest.go b/internal/acctest/acctest.go index 56d71b57b..06e1442d6 100644 --- a/internal/acctest/acctest.go +++ b/internal/acctest/acctest.go @@ -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" ) @@ -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() { @@ -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 @@ -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") } } diff --git a/internal/plugin/provider.go b/internal/plugin/provider.go index ec150b6f7..e66c4953e 100644 --- a/internal/plugin/provider.go +++ b/internal/plugin/provider.go @@ -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. @@ -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, @@ -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, } diff --git a/internal/plugin/service/organization/organization_application_user_test.go b/internal/plugin/service/organization/organization_application_user_test.go index ebe300d64..03bef1c65 100644 --- a/internal/plugin/service/organization/organization_application_user_test.go +++ b/internal/plugin/service/organization/organization_application_user_test.go @@ -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" diff --git a/internal/plugin/service/organization/organization_group_project_test.go b/internal/plugin/service/organization/organization_group_project_test.go index b24f4126e..2903d94e3 100644 --- a/internal/plugin/service/organization/organization_group_project_test.go +++ b/internal/plugin/service/organization/organization_group_project_test.go @@ -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" diff --git a/internal/plugin/service/organization/organization_user_group_member_test.go b/internal/plugin/service/organization/organization_user_group_member_test.go index 174c30ed6..c139087cc 100644 --- a/internal/plugin/service/organization/organization_user_group_member_test.go +++ b/internal/plugin/service/organization/organization_user_group_member_test.go @@ -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" diff --git a/internal/plugin/util/helpers.go b/internal/plugin/util/helpers.go index f3f497e6b..9fe43ccfa 100644 --- a/internal/plugin/util/helpers.go +++ b/internal/plugin/util/helpers.go @@ -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 { @@ -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, "/")