Skip to content

Commit 351fdb6

Browse files
Skip tests instead of using build tags (#311)
* Skip tests instead of using build tags The current way of working is annoying, I have to remove the build tags to make my IDE work Instead, we can skip the tests that we don't want to run. It will write a small message for everyone of these but at least, it's very clear what's happening (why tests aren't running) * Modify new datasource
1 parent 0367f15 commit 351fdb6

24 files changed

+115
-78
lines changed

GNUmakefile

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@ GRAFANA_VERSION ?= 8.2.5
33
testacc:
44
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m
55

6-
testacc-oss: TESTARGS+=-tags='oss'
7-
testacc-oss: testacc
6+
testacc-oss:
7+
TF_ACC_OSS=true make testacc
88

9-
testacc-enterprise: TESTARGS+=-tags='enterprise'
10-
testacc-enterprise: testacc
9+
testacc-enterprise:
10+
TF_ACC_ENTERPRISE=true make testacc
1111

12-
testacc-cloud: TESTARGS+=-tags='cloud'
13-
testacc-cloud: testacc
12+
testacc-cloud:
13+
TF_ACC_CLOUD=true make testacc
1414

1515
testacc-docker:
1616
GRAFANA_VERSION=$(GRAFANA_VERSION) \
1717
docker-compose \
1818
-f ./docker-compose.yml \
19-
run --rm grafana-provider \
20-
make testacc TESTARGS="-tags='oss' $(TESTARGS)"
19+
run --rm -e TESTARGS="$(TESTARGS)" \
20+
grafana-provider \
21+
make testacc-oss
2122

2223
testacc-docker-tls:
2324
GRAFANA_VERSION=$(GRAFANA_VERSION) \
2425
docker-compose \
2526
-f ./docker-compose.yml \
2627
-f ./docker-compose.tls.yml \
27-
run --rm grafana-provider \
28-
make testacc TESTARGS="-tags='oss' $(TESTARGS)"
28+
run --rm -e TESTARGS="$(TESTARGS)" \
29+
grafana-provider \
30+
make testacc-oss
2931

3032
changelog:
3133
@test $${RELEASE_VERSION?Please set environment variable RELEASE_VERSION}

grafana/data_source_folder_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build oss
2-
// +build oss
3-
41
package grafana
52

63
import (
@@ -12,6 +9,8 @@ import (
129
)
1310

1411
func TestAccDatasourceFolder(t *testing.T) {
12+
CheckOSSTestsEnabled(t)
13+
1514
var folder gapi.Folder
1615
checks := []resource.TestCheckFunc{
1716
testAccFolderCheckExists("grafana_folder.test", &folder),

grafana/data_source_synthetic_monitoring_probe_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build cloud
2-
// +build cloud
3-
41
package grafana
52

63
import (
@@ -10,6 +7,8 @@ import (
107
)
118

129
func TestAccDataSourceSyntheticMonitoringProbe(t *testing.T) {
10+
CheckCloudTestsEnabled(t)
11+
1312
resource.UnitTest(t, resource.TestCase{
1413
PreCheck: func() { testAccPreCheckCloud(t) },
1514
ProviderFactories: testAccProviderFactories,

grafana/data_source_synthetic_monitoring_probes_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build cloud
2-
// +build cloud
3-
41
package grafana
52

63
import (
@@ -10,6 +7,8 @@ import (
107
)
118

129
func TestAccDataSourceSyntheticMonitoringProbes(t *testing.T) {
10+
CheckCloudTestsEnabled(t)
11+
1312
resource.UnitTest(t, resource.TestCase{
1413
PreCheck: func() { testAccPreCheckCloud(t) },
1514
ProviderFactories: testAccProviderFactories,

grafana/data_source_user_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build oss
2-
// +build oss
3-
41
package grafana
52

63
import (
@@ -11,6 +8,8 @@ import (
118
)
129

1310
func TestAccDatasourceUser(t *testing.T) {
11+
CheckOSSTestsEnabled(t)
12+
1413
var user gapi.User
1514
checks := []resource.TestCheckFunc{
1615
testAccUserCheckExists("grafana_user.test", &user),

grafana/provider_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"io/ioutil"
66
"os"
7+
"strconv"
78
"sync"
89
"testing"
910

@@ -78,8 +79,7 @@ func testAccPreCheck(t *testing.T) {
7879
})
7980
}
8081

81-
// testAccPreCheckCloud should be called by acceptance tests in files where the
82-
// "cloud" build tag is present.
82+
// testAccPreCheckCloud should be called by cloud acceptance tests
8383
func testAccPreCheckCloud(t *testing.T) {
8484
testAccPreCheckEnv = append(testAccPreCheckEnv, "GRAFANA_SM_ACCESS_TOKEN")
8585
testAccPreCheck(t)
@@ -94,3 +94,36 @@ func testAccExample(t *testing.T, path string) string {
9494
}
9595
return string(example)
9696
}
97+
98+
func accTestsEnabled(t *testing.T, envVarName string) bool {
99+
v, ok := os.LookupEnv(envVarName)
100+
if !ok {
101+
return false
102+
}
103+
enabled, err := strconv.ParseBool(v)
104+
if err != nil {
105+
t.Fatalf("%s must be set to a boolean value", envVarName)
106+
}
107+
return enabled
108+
}
109+
110+
func CheckOSSTestsEnabled(t *testing.T) {
111+
t.Helper()
112+
if !accTestsEnabled(t, "TF_ACC_OSS") {
113+
t.Skip("TF_ACC_OSS must be set to a truthy value for OSS acceptance tests")
114+
}
115+
}
116+
117+
func CheckCloudTestsEnabled(t *testing.T) {
118+
t.Helper()
119+
if !accTestsEnabled(t, "TF_ACC_CLOUD") {
120+
t.Skip("TF_ACC_CLOUD must be set to a truthy value for Cloud acceptance tests")
121+
}
122+
}
123+
124+
func CheckEnterpriseTestsEnabled(t *testing.T) {
125+
t.Helper()
126+
if !accTestsEnabled(t, "TF_ACC_ENTERPRISE") {
127+
t.Skip("TF_ACC_ENTERPRISE must be set to a truthy value for Enterprise acceptance tests")
128+
}
129+
}

grafana/resource_alert_notification_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build oss
2-
// +build oss
3-
41
package grafana
52

63
import (
@@ -16,6 +13,8 @@ import (
1613
)
1714

1815
func TestAccAlertNotification_basic(t *testing.T) {
16+
CheckOSSTestsEnabled(t)
17+
1918
var alertNotification gapi.AlertNotification
2019

2120
resource.Test(t, resource.TestCase{
@@ -53,6 +52,8 @@ func TestAccAlertNotification_basic(t *testing.T) {
5352
}
5453

5554
func TestAccAlertNotification_disableResolveMessage(t *testing.T) {
55+
CheckOSSTestsEnabled(t)
56+
5657
var alertNotification gapi.AlertNotification
5758

5859
resource.Test(t, resource.TestCase{
@@ -90,6 +91,8 @@ func TestAccAlertNotification_disableResolveMessage(t *testing.T) {
9091
}
9192

9293
func TestAccAlertNotification_invalid_frequency(t *testing.T) {
94+
CheckOSSTestsEnabled(t)
95+
9396
var alertNotification gapi.AlertNotification
9497

9598
resource.Test(t, resource.TestCase{
@@ -106,6 +109,8 @@ func TestAccAlertNotification_invalid_frequency(t *testing.T) {
106109
}
107110

108111
func TestAccAlertNotification_reminder_no_frequency(t *testing.T) {
112+
CheckOSSTestsEnabled(t)
113+
109114
var alertNotification gapi.AlertNotification
110115

111116
resource.Test(t, resource.TestCase{

grafana/resource_api_key_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build oss
2-
// +build oss
3-
41
package grafana
52

63
import (
@@ -28,6 +25,8 @@ resource "grafana_api_key" "bar" {
2825
`
2926

3027
func TestAccGrafanaAuthKey(t *testing.T) {
28+
CheckOSSTestsEnabled(t)
29+
3130
resource.Test(t, resource.TestCase{
3231
PreCheck: func() { testAccPreCheck(t) },
3332
ProviderFactories: testAccProviderFactories,

grafana/resource_builtin_role_assignment_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build enterprise
2-
// +build enterprise
3-
41
package grafana
52

63
import (
@@ -14,6 +11,8 @@ import (
1411
)
1512

1613
func TestAccBuiltInRoleAssignment(t *testing.T) {
14+
CheckEnterpriseTestsEnabled(t)
15+
1716
var br gapi.BuiltInRoleAssignment
1817

1918
resource.Test(t, resource.TestCase{

grafana/resource_dashboard_permission_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
//go:build oss
2-
// +build oss
3-
41
package grafana
52

63
import (
@@ -13,6 +10,8 @@ import (
1310
)
1411

1512
func TestAccDashboardPermission_basic(t *testing.T) {
13+
CheckOSSTestsEnabled(t)
14+
1615
dashboardID := int64(-1)
1716

1817
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)