Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge CheckOSSTestsEnabled and CheckOSSTestsSemver helpers #1107

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

func TestAccGrafanaServiceAccountFromCloud(t *testing.T) {
testutils.CheckCloudAPITestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")

var stack gapi.Stack
prefix := "tfserviceaccounttest"
Expand Down
3 changes: 1 addition & 2 deletions internal/resources/grafana/data_source_library_panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (
)

func TestAccDatasourceLibraryPanel(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0")
testutils.CheckOSSTestsEnabled(t, ">=8.0.0")

var panel gapi.LibraryPanel
// var dashboard gapi.Dashboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

func TestAccContactPoint_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.0.0")
testutils.CheckOSSTestsEnabled(t, ">=9.0.0")

var points []gapi.ContactPoint

Expand Down Expand Up @@ -72,8 +71,7 @@ func TestAccContactPoint_basic(t *testing.T) {
}

func TestAccContactPoint_compound(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.0.0")
testutils.CheckOSSTestsEnabled(t, ">=9.0.0")

var points []gapi.ContactPoint

Expand Down Expand Up @@ -141,8 +139,7 @@ func TestAccContactPoint_compound(t *testing.T) {
}

func TestAccContactPoint_notifiers(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

var points []gapi.ContactPoint

Expand Down Expand Up @@ -344,8 +341,7 @@ func TestAccContactPoint_notifiers(t *testing.T) {
}

func TestAccContactPoint_empty(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

resource.ParallelTest(t, resource.TestCase{
ProviderFactories: testutils.ProviderFactories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

func TestAccMessageTemplate_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.0.0")
testutils.CheckOSSTestsEnabled(t, ">=9.0.0")

var tmpl gapi.AlertingMessageTemplate

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

func TestAccMuteTiming_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">9.0.0")
testutils.CheckOSSTestsEnabled(t, ">9.0.0")

var mt gapi.MuteTiming

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

func TestAccNotificationPolicy_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

// TODO: Make parallizable
resource.Test(t, resource.TestCase{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

func TestAccAlertRule_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

var group gapi.RuleGroup

Expand Down Expand Up @@ -103,8 +102,7 @@ func TestAccAlertRule_basic(t *testing.T) {
}

func TestAccAlertRule_model(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

var group gapi.RuleGroup

Expand Down Expand Up @@ -148,8 +146,7 @@ func TestAccAlertRule_model(t *testing.T) {
}

func TestAccAlertRule_compound(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

var group gapi.RuleGroup

Expand Down
3 changes: 1 addition & 2 deletions internal/resources/grafana/resource_annotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func TestAccAnnotation_basic(t *testing.T) {
}

func TestAccAnnotation_dashboardUID(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.0.0")
testutils.CheckOSSTestsEnabled(t, ">=9.0.0")

var annotation gapi.Annotation
var org gapi.Org
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import (
)

func TestAccDashboardPermission_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.0.0") // Dashboard UIDs are only available as references in Grafana 9+
testutils.CheckOSSTestsEnabled(t, ">=9.0.0") // Dashboard UIDs are only available as references in Grafana 9+

dashboardUID := ""

Expand Down
3 changes: 1 addition & 2 deletions internal/resources/grafana/resource_dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ func TestAccDashboard_folder(t *testing.T) {
}

func TestAccDashboard_folder_uid(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0") // UID in folders were added in v8
testutils.CheckOSSTestsEnabled(t, ">=8.0.0") // UID in folders were added in v8

var dashboard gapi.Dashboard
var folder goapi.Folder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

func TestAccFolderPermission_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.0.0") // Folder permissions only work for service accounts in Grafana 9+, so we're just not testing versions before 9.
testutils.CheckOSSTestsEnabled(t, ">=9.0.0") // Folder permissions only work for service accounts in Grafana 9+, so we're just not testing versions before 9.

folderUID := "uninitialized"

Expand Down
3 changes: 1 addition & 2 deletions internal/resources/grafana/resource_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ func TestAccFolder_PreventDeletion(t *testing.T) {

// This is a bug in Grafana, not the provider. It was fixed in 9.2.7+ and 9.3.0+, this test will check for regressions
func TestAccFolder_createFromDifferentRoles(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.2.7")
testutils.CheckOSSTestsEnabled(t, ">=9.2.7")

for _, tc := range []struct {
role string
Expand Down
15 changes: 5 additions & 10 deletions internal/resources/grafana/resource_library_panel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import (
)

func TestAccLibraryPanel_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0")
testutils.CheckOSSTestsEnabled(t, ">=8.0.0")

var panel gapi.LibraryPanel

Expand Down Expand Up @@ -58,8 +57,7 @@ func TestAccLibraryPanel_basic(t *testing.T) {
}

func TestAccLibraryPanel_computed_config(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0")
testutils.CheckOSSTestsEnabled(t, ">=8.0.0")

var panel gapi.LibraryPanel

Expand All @@ -82,8 +80,7 @@ func TestAccLibraryPanel_computed_config(t *testing.T) {
}

func TestAccLibraryPanel_folder(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0")
testutils.CheckOSSTestsEnabled(t, ">=8.0.0")

var panel gapi.LibraryPanel
var folder goapi.Folder
Expand Down Expand Up @@ -111,8 +108,7 @@ func TestAccLibraryPanel_folder(t *testing.T) {
}

func TestAccLibraryPanel_dashboard(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0")
testutils.CheckOSSTestsEnabled(t, ">=8.0.0")

var panel gapi.LibraryPanel
var dashboard gapi.Dashboard
Expand All @@ -137,8 +133,7 @@ func TestAccLibraryPanel_dashboard(t *testing.T) {
}

func TestAccLibraryPanel_inOrg(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0")
testutils.CheckOSSTestsEnabled(t, ">=8.0.0")

var panel gapi.LibraryPanel
orgName := acctest.RandString(10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import (
)

func TestAccResourceOrganizationPreferences_WithDashboardID(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=8.0.0")
testutils.CheckOSSTestsEnabled(t, ">=8.0.0")
testAccResourceOrganizationPreferences(t, false)
}

func TestAccResourceOrganizationPreferences_WithDashboardUID(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.0.0") // UID support was added in 9.0.0
testutils.CheckOSSTestsEnabled(t, ">=9.0.0") // UID support was added in 9.0.0
testAccResourceOrganizationPreferences(t, true)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
)

func TestAccServiceAccountPermission(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.2.4")
testutils.CheckOSSTestsEnabled(t, ">=9.2.4")

name := acctest.RandString(10)

Expand Down
6 changes: 2 additions & 4 deletions internal/resources/grafana/resource_service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
)

func TestAccServiceAccount_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

var sa gapi.ServiceAccountDTO
var updatedSA gapi.ServiceAccountDTO
Expand Down Expand Up @@ -63,8 +62,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
}

func TestAccServiceAccount_many(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

name := acctest.RandString(10)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import (
)

func TestAccServiceAccountToken_basic(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

name := acctest.RandString(10)
var sa gapi.ServiceAccountDTO
Expand Down Expand Up @@ -49,8 +48,7 @@ func TestAccServiceAccountToken_basic(t *testing.T) {
}

func TestAccServiceAccountToken_inOrg(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">=9.1.0")
testutils.CheckOSSTestsEnabled(t, ">=9.1.0")

name := acctest.RandString(10)
var org gapi.Org
Expand Down
6 changes: 2 additions & 4 deletions internal/resources/grafana/resource_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ func TestAccTeam_basic(t *testing.T) {
}

func TestAccTeam_preferences(t *testing.T) {
testutils.CheckOSSTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">= 9.0.0") // Dashboard UID is only available in Grafana 9.0.0+
testutils.CheckOSSTestsEnabled(t, ">= 9.0.0") // Dashboard UID is only available in Grafana 9.0.0+

var team gapi.Team
teamName := acctest.RandString(5)
Expand Down Expand Up @@ -105,8 +104,7 @@ func TestAccTeam_preferences(t *testing.T) {
}

func TestAccTeam_teamSync(t *testing.T) {
testutils.CheckEnterpriseTestsEnabled(t)
testutils.CheckOSSTestsSemver(t, ">= 8.0.0")
testutils.CheckEnterpriseTestsEnabled(t, ">= 8.0.0")

var team gapi.Team
teamName := acctest.RandString(5)
Expand Down
47 changes: 28 additions & 19 deletions internal/testutils/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func IsUnitTest(t *testing.T) {
}

// CheckOSSTestsEnabled checks if the OSS acceptance tests are enabled. This should be the first line of any test that uses Grafana OSS features only
func CheckOSSTestsEnabled(t *testing.T) {
func CheckOSSTestsEnabled(t *testing.T, semverConstraintOptional ...string) {
t.Helper()

if !AccTestsEnabled("TF_ACC_OSS") {
Expand All @@ -125,23 +125,7 @@ func CheckOSSTestsEnabled(t *testing.T) {
"GRAFANA_AUTH",
"GRAFANA_VERSION",
)
}

// CheckOSSTestsSemver allows to skip tests that are not supported by the Grafana OSS version
func CheckOSSTestsSemver(t *testing.T, semverConstraint string) {
t.Helper()

versionStr := os.Getenv("GRAFANA_VERSION")
if semverConstraint != "" && versionStr != "" {
version := semver.MustParse(versionStr)
c, err := semver.NewConstraint(semverConstraint)
if err != nil {
t.Fatalf("invalid constraint %s: %v", semverConstraint, err)
}
if !c.Check(version) {
t.Skipf("skipping test for Grafana version `%s`, constraint `%s`", versionStr, semverConstraint)
}
}
checkSemverConstraint(t, semverConstraintOptional...)
}

// CheckCloudTestsEnabled checks if the cloud tests are enabled. This should be the first line of any test that tests Cloud API features
Expand Down Expand Up @@ -172,7 +156,7 @@ func CheckCloudInstanceTestsEnabled(t *testing.T) {
}

// CheckEnterpriseTestsEnabled checks if the enterprise tests are enabled. This should be the first line of any test that tests Grafana Enterprise features
func CheckEnterpriseTestsEnabled(t *testing.T) {
func CheckEnterpriseTestsEnabled(t *testing.T, semverConstraintOptional ...string) {
t.Helper()

if !AccTestsEnabled("TF_ACC_ENTERPRISE") {
Expand All @@ -183,4 +167,29 @@ func CheckEnterpriseTestsEnabled(t *testing.T) {
"GRAFANA_URL",
"GRAFANA_AUTH",
)
checkSemverConstraint(t, semverConstraintOptional...)
}

func checkSemverConstraint(t *testing.T, semverConstraintOptional ...string) {
t.Helper()

if len(semverConstraintOptional) > 1 {
panic("checkSemverConstraint accepts at most one argument")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call using a panic here! I was going to suggest t.Fatal instead, but then I realized it is not the test that fails but actually the test's definition, so this is definitely one of the right usages of panic 👏🏽

}
if len(semverConstraintOptional) == 0 {
return
}

semverConstraint := semverConstraintOptional[0]
versionStr := os.Getenv("GRAFANA_VERSION")
if semverConstraint != "" && versionStr != "" {
version := semver.MustParse(versionStr)
c, err := semver.NewConstraint(semverConstraint)
if err != nil {
t.Fatalf("invalid constraint %s: %v", semverConstraint, err)
}
if !c.Check(version) {
t.Skipf("skipping test for Grafana version `%s`, constraint `%s`", versionStr, semverConstraint)
}
}
}