Skip to content

Commit 918aea6

Browse files
authored
tfversion: Add SkipIfNotAlpha version check (#388)
* tfversion: Add `SkipIfNotAlpha` version check * changelog * missing var declaration
1 parent 7f6b899 commit 918aea6

File tree

6 files changed

+147
-6
lines changed

6 files changed

+147
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: FEATURES
2+
body: 'tfversion: Added `SkipIfNotAlpha` version check for testing experimental features
3+
of alpha Terraform builds.'
4+
time: 2024-11-08T16:06:34.662822-05:00
5+
custom:
6+
Issue: "388"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright (c) HashiCorp, Inc.
2+
# SPDX-License-Identifier: MPL-2.0
3+
4+
variable "length" {
5+
type = number
6+
}
7+
8+
variable "numeric" {
9+
type = bool
10+
}

plancheck/expect_deferred_change_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Test_ExpectDeferredChange_Reason_Match(t *testing.T) {
2424
r.Test(t, r.TestCase{
2525
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
2626
tfversion.SkipBelow(tfversion.Version1_9_0),
27-
tfversion.SkipIfNotPrerelease(),
27+
tfversion.SkipIfNotAlpha(),
2828
},
2929
AdditionalCLIOptions: &r.AdditionalCLIOptions{
3030
Plan: r.PlanOptions{AllowDeferral: true},
@@ -75,7 +75,7 @@ func Test_ExpectDeferredChange_Reason_NoMatch(t *testing.T) {
7575
r.Test(t, r.TestCase{
7676
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
7777
tfversion.SkipBelow(tfversion.Version1_9_0),
78-
tfversion.SkipIfNotPrerelease(),
78+
tfversion.SkipIfNotAlpha(),
7979
},
8080
AdditionalCLIOptions: &r.AdditionalCLIOptions{
8181
Plan: r.PlanOptions{AllowDeferral: true},
@@ -127,7 +127,7 @@ func Test_ExpectDeferredChange_NoDeferredChanges(t *testing.T) {
127127
r.Test(t, r.TestCase{
128128
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
129129
tfversion.SkipBelow(tfversion.Version1_9_0),
130-
tfversion.SkipIfNotPrerelease(),
130+
tfversion.SkipIfNotAlpha(),
131131
},
132132
AdditionalCLIOptions: &r.AdditionalCLIOptions{
133133
Plan: r.PlanOptions{AllowDeferral: true},

plancheck/expect_no_deferred_changes_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func Test_ExpectNoDeferredChange(t *testing.T) {
2424
r.Test(t, r.TestCase{
2525
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
2626
tfversion.SkipBelow(tfversion.Version1_9_0),
27-
tfversion.SkipIfNotPrerelease(),
27+
tfversion.SkipIfNotAlpha(),
2828
},
2929
AdditionalCLIOptions: &r.AdditionalCLIOptions{
3030
Plan: r.PlanOptions{AllowDeferral: true},
@@ -72,7 +72,7 @@ func Test_ExpectNoDeferredChange_OneDeferral(t *testing.T) {
7272
r.Test(t, r.TestCase{
7373
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
7474
tfversion.SkipBelow(tfversion.Version1_9_0),
75-
tfversion.SkipIfNotPrerelease(),
75+
tfversion.SkipIfNotAlpha(),
7676
},
7777
AdditionalCLIOptions: &r.AdditionalCLIOptions{
7878
Plan: r.PlanOptions{AllowDeferral: true},
@@ -124,7 +124,7 @@ func Test_ExpectNoDeferredChange_MultipleDeferrals(t *testing.T) {
124124
r.Test(t, r.TestCase{
125125
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
126126
tfversion.SkipBelow(tfversion.Version1_9_0),
127-
tfversion.SkipIfNotPrerelease(),
127+
tfversion.SkipIfNotAlpha(),
128128
},
129129
AdditionalCLIOptions: &r.AdditionalCLIOptions{
130130
Plan: r.PlanOptions{AllowDeferral: true},

tfversion/skip_if_not_alpha.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfversion
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"strings"
10+
)
11+
12+
// SkipIfNotAlpha will skip (pass) the test if the Terraform CLI
13+
// version is not an alpha prerelease (for example, 1.10.0-alpha20241023).
14+
//
15+
// Alpha builds of Terraform include experimental features, so this version check
16+
// can be used for acceptance testing of experimental features, such as deferred actions.
17+
func SkipIfNotAlpha() TerraformVersionCheck {
18+
return skipIfNotAlphaCheck{}
19+
}
20+
21+
// skipIfNotAlphaCheck implements the TerraformVersionCheck interface
22+
type skipIfNotAlphaCheck struct{}
23+
24+
// CheckTerraformVersion satisfies the TerraformVersionCheck interface.
25+
func (s skipIfNotAlphaCheck) CheckTerraformVersion(ctx context.Context, req CheckTerraformVersionRequest, resp *CheckTerraformVersionResponse) {
26+
if strings.Contains(req.TerraformVersion.Prerelease(), "alpha") {
27+
return
28+
}
29+
30+
resp.Skip = fmt.Sprintf("Terraform CLI version %s is not an alpha build: skipping test.", req.TerraformVersion)
31+
}

tfversion/skip_if_not_alpha_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package tfversion_test
5+
6+
import (
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
11+
r "github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
13+
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
14+
"github.com/hashicorp/terraform-plugin-testing/tfversion"
15+
16+
testinginterface "github.com/mitchellh/go-testing-interface"
17+
)
18+
19+
func Test_SkipIfNotAlpha_SkipTest_Stable(t *testing.T) { //nolint:paralleltest
20+
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
21+
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.8.0")
22+
23+
r.UnitTest(t, r.TestCase{
24+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
25+
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
26+
return nil, nil
27+
},
28+
},
29+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
30+
tfversion.SkipIfNotAlpha(),
31+
},
32+
Steps: []r.TestStep{
33+
{
34+
Config: `//non-empty config`,
35+
},
36+
},
37+
})
38+
}
39+
40+
func Test_SkipIfNotAlpha_SkipTest_Beta1(t *testing.T) { //nolint:paralleltest
41+
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
42+
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.8.0-beta1")
43+
44+
r.UnitTest(t, r.TestCase{
45+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
46+
"test": providerserver.NewProviderServer(testprovider.Provider{}),
47+
},
48+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
49+
tfversion.SkipIfNotAlpha(),
50+
},
51+
Steps: []r.TestStep{
52+
{
53+
Config: `//non-empty config`,
54+
},
55+
},
56+
})
57+
}
58+
func Test_SkipIfNotAlpha_SkipTest_RC(t *testing.T) { //nolint:paralleltest
59+
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
60+
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.8.0-rc2")
61+
62+
r.UnitTest(t, r.TestCase{
63+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
64+
"test": providerserver.NewProviderServer(testprovider.Provider{}),
65+
},
66+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
67+
tfversion.SkipIfNotAlpha(),
68+
},
69+
Steps: []r.TestStep{
70+
{
71+
Config: `//non-empty config`,
72+
},
73+
},
74+
})
75+
}
76+
77+
func Test_SkipIfNotAlpha_RunTest_Alpha(t *testing.T) { //nolint:paralleltest
78+
t.Setenv("TF_ACC_TERRAFORM_PATH", "")
79+
t.Setenv("TF_ACC_TERRAFORM_VERSION", "1.9.0-alpha20240501")
80+
81+
r.UnitTest(&testinginterface.RuntimeT{}, r.TestCase{
82+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
83+
"test": providerserver.NewProviderServer(testprovider.Provider{}),
84+
},
85+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
86+
tfversion.SkipIfNotAlpha(),
87+
},
88+
Steps: []r.TestStep{
89+
{
90+
Config: `//non-empty config`,
91+
},
92+
},
93+
})
94+
}

0 commit comments

Comments
 (0)