Skip to content

Commit ba04b86

Browse files
Config Generation: Run tests on local provider code (#1711)
* Config Generation: Run tests on local provider code Rather than using v3.0.0 of the provider, we can run a local build of the code This will make tests faster and allows to test the integration between the generator and the provider * Fix json test
1 parent b8cda47 commit ba04b86

File tree

12 files changed

+32
-10
lines changed

12 files changed

+32
-10
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ website/vendor
2929
testdata/*.crt
3030
testdata/*.key
3131
testdata/integration/*
32+
testdata/plugins
3233
!testdata/integration/main.tf
3334
!testdata/integration/test.sh
3435

GNUmakefile

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ GRAFANA_VERSION ?= 11.0.0
22
DOCKER_COMPOSE_ARGS ?= --force-recreate --detach --remove-orphans --wait --renew-anon-volumes
33

44
testacc:
5+
go build -o testdata/plugins/registry.terraform.io/grafana/grafana/999.999.999/$$(go env GOOS)_$$(go env GOARCH)/terraform-provider-grafana_v999.999.999_$$(go env GOOS)_$$(go env GOARCH) .
56
TF_ACC=1 go test ./... -v $(TESTARGS) -timeout 120m
67

78
# Test OSS features

pkg/generate/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type CloudConfig struct {
3535
type TerraformInstallConfig struct {
3636
InstallDir string
3737
Version *version.Version
38+
PluginDir string
3839
}
3940

4041
type Config struct {

pkg/generate/generate_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,14 @@ func TestAccGenerate(t *testing.T) {
197197
OutputDir: tempDir,
198198
Clobber: true,
199199
Format: generate.OutputFormatHCL,
200-
ProviderVersion: "v3.0.0",
200+
ProviderVersion: "999.999.999", // Using the code from the current branch
201201
Grafana: &generate.GrafanaConfig{
202202
URL: "http://localhost:3000",
203203
Auth: "admin:admin",
204204
},
205205
TerraformInstallConfig: generate.TerraformInstallConfig{
206206
InstallDir: installDir,
207+
PluginDir: pluginDir(t),
207208
},
208209
}
209210
if tc.generateConfig != nil {
@@ -293,11 +294,14 @@ func TestAccGenerate_RestrictedPermissions(t *testing.T) {
293294
OutputDir: tempDir,
294295
Clobber: true,
295296
Format: generate.OutputFormatHCL,
296-
ProviderVersion: "v3.0.0",
297+
ProviderVersion: "999.999.999", // Using the code from the current branch
297298
Grafana: &generate.GrafanaConfig{
298299
URL: "http://localhost:3000",
299300
Auth: saToken.Payload.Key,
300301
},
302+
TerraformInstallConfig: generate.TerraformInstallConfig{
303+
PluginDir: pluginDir(t),
304+
},
301305
}
302306

303307
result := generate.Generate(context.Background(), &config)
@@ -382,3 +386,11 @@ func assertFilesSubdir(t *testing.T, gotFilesDir, expectedFilesDir, subdir strin
382386
assert.Equal(t, strings.TrimSpace(string(expectedContent)), strings.TrimSpace(string(gotContent)))
383387
}
384388
}
389+
390+
func pluginDir(t *testing.T) string {
391+
t.Helper()
392+
393+
repoRoot, err := filepath.Abs("../..")
394+
require.NoError(t, err)
395+
return filepath.Join(repoRoot, "testdata", "plugins")
396+
}

pkg/generate/terraform.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ func setupTerraform(cfg *Config) (*tfexec.Terraform, error) {
6565
return nil, fmt.Errorf("error running NewTerraform: %s", err)
6666
}
6767

68-
err = tf.Init(context.Background(), tfexec.Upgrade(true))
68+
initOptions := []tfexec.InitOption{
69+
tfexec.Upgrade(true),
70+
}
71+
if cfg.TerraformInstallConfig.PluginDir != "" {
72+
initOptions = append(initOptions, tfexec.PluginDir(cfg.TerraformInstallConfig.PluginDir))
73+
}
74+
75+
err = tf.Init(context.Background(), initOptions...)
6976
if err != nil {
7077
return nil, fmt.Errorf("error running Init: %s", err)
7178
}

pkg/generate/testdata/generate/alerting-in-org/provider.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
grafana = {
44
source = "grafana/grafana"
5-
version = "3.0.0"
5+
version = "999.999.999"
66
}
77
}
88
}

pkg/generate/testdata/generate/dashboard-filtered/provider.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
grafana = {
44
source = "grafana/grafana"
5-
version = "3.0.0"
5+
version = "999.999.999"
66
}
77
}
88
}

pkg/generate/testdata/generate/dashboard-json/provider.tf.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
{
1414
"grafana": {
1515
"source": "grafana/grafana",
16-
"version": "3.0.0"
16+
"version": "999.999.999"
1717
}
1818
}
1919
]

pkg/generate/testdata/generate/dashboard-restricted-permissions/provider.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
grafana = {
44
source = "grafana/grafana"
5-
version = "3.0.0"
5+
version = "999.999.999"
66
}
77
}
88
}

pkg/generate/testdata/generate/dashboard/provider.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
grafana = {
44
source = "grafana/grafana"
5-
version = "3.0.0"
5+
version = "999.999.999"
66
}
77
}
88
}

pkg/generate/testdata/generate/empty-with-creds/provider.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
grafana = {
44
source = "grafana/grafana"
5-
version = "3.0.0"
5+
version = "999.999.999"
66
}
77
}
88
}

pkg/generate/testdata/generate/empty/provider.tf

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
grafana = {
44
source = "grafana/grafana"
5-
version = "3.0.0"
5+
version = "999.999.999"
66
}
77
}
88
}

0 commit comments

Comments
 (0)