Skip to content

Commit 7bcdf17

Browse files
committed
feat: add first acceptance test for meshstack_location, runs locally
1 parent 9cae987 commit 7bcdf17

File tree

7 files changed

+64
-10
lines changed

7 files changed

+64
-10
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ website/vendor
3434

3535
# Keep windows files with windows line endings
3636
*.winfile eol=crlf
37+
38+
.env

examples/embed.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package examples
2+
3+
import _ "embed"
4+
5+
var (
6+
//go:embed resources/meshstack_location/resource.tf
7+
LocationResourceConfig string
8+
)

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ require (
99
github.com/hashicorp/terraform-plugin-go v0.29.0
1010
github.com/hashicorp/terraform-plugin-sdk/v2 v2.38.1
1111
github.com/hashicorp/terraform-plugin-testing v1.13.3
12+
github.com/joho/godotenv v1.5.1
13+
github.com/stretchr/testify v1.11.1
1214
)
1315

1416
require (
@@ -24,6 +26,7 @@ require (
2426
github.com/bgentry/speakeasy v0.1.0 // indirect
2527
github.com/bmatcuk/doublestar/v4 v4.9.1 // indirect
2628
github.com/cloudflare/circl v1.6.1 // indirect
29+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2730
github.com/fatih/color v1.18.0 // indirect
2831
github.com/golang/protobuf v1.5.4 // indirect
2932
github.com/google/go-cmp v0.7.0 // indirect
@@ -59,6 +62,7 @@ require (
5962
github.com/mitchellh/mapstructure v1.5.0 // indirect
6063
github.com/mitchellh/reflectwalk v1.0.2 // indirect
6164
github.com/oklog/run v1.2.0 // indirect
65+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
6266
github.com/posener/complete v1.2.3 // indirect
6367
github.com/shopspring/decimal v1.3.1 // indirect
6468
github.com/spf13/cast v1.5.0 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl
133133
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
134134
github.com/jhump/protoreflect v1.17.0 h1:qOEr613fac2lOuTgWN4tPAtLL7fUSbuJL5X5XumQh94=
135135
github.com/jhump/protoreflect v1.17.0/go.mod h1:h9+vUUL38jiBzck8ck+6G/aeMX8Z4QUY/NiJPwPNi+8=
136+
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
137+
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
136138
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
137139
github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
138140
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -190,8 +192,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
190192
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
191193
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
192194
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
193-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
194-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
195+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
196+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
195197
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
196198
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
197199
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package provider
2+
3+
import (
4+
_ "embed"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
8+
"github.com/meshcloud/terraform-provider-meshstack/examples"
9+
)
10+
11+
func TestAccLocation_example(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Steps: []resource.TestStep{
16+
{
17+
Config: examples.LocationResourceConfig,
18+
},
19+
},
20+
})
21+
}

internal/provider/provider.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ func (p *MeshStackProvider) Schema(ctx context.Context, req provider.SchemaReque
5454
}
5555
}
5656

57+
const (
58+
envKeyMeshstackEndpoint = "MESHSTACK_ENDPOINT"
59+
envKeyMeshstackApiKey = "MESHSTACK_API_KEY"
60+
envKeyMeshstackApiSecret = "MESHSTACK_API_SECRET"
61+
)
62+
5763
func (p *MeshStackProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
5864
var data MeshStackProviderModel
5965
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
@@ -63,7 +69,7 @@ func (p *MeshStackProvider) Configure(ctx context.Context, req provider.Configur
6369
endpoint = data.Endpoint.ValueString()
6470
} else {
6571
var ok bool
66-
endpoint, ok = os.LookupEnv("MESHSTACK_ENDPOINT")
72+
endpoint, ok = os.LookupEnv(envKeyMeshstackEndpoint)
6773
if !ok {
6874
resp.Diagnostics.AddError("Provider endpoint missing.", "Set provider.meshstack.endpoint or use MESHSTACK_ENDPOINT environment variable.")
6975
return
@@ -81,7 +87,7 @@ func (p *MeshStackProvider) Configure(ctx context.Context, req provider.Configur
8187
apiKey = data.ApiKey.ValueString()
8288
} else {
8389
var ok bool
84-
apiKey, ok = os.LookupEnv("MESHSTACK_API_KEY")
90+
apiKey, ok = os.LookupEnv(envKeyMeshstackApiKey)
8591
if !ok {
8692
resp.Diagnostics.AddError("Provider API key missing.", "Set provider.meshstack.apikey or use MESHSTACK_API_KEY environment variable.")
8793
return
@@ -93,7 +99,7 @@ func (p *MeshStackProvider) Configure(ctx context.Context, req provider.Configur
9399
apiSecret = data.ApiSecret.ValueString()
94100
} else {
95101
var ok bool
96-
apiSecret, ok = os.LookupEnv("MESHSTACK_API_SECRET")
102+
apiSecret, ok = os.LookupEnv(envKeyMeshstackApiSecret)
97103
if !ok {
98104
resp.Diagnostics.AddError("Provider API secret missing.", "Set provider.meshstack.apisecret or use MESHSTACK_API_SECRET environment variable.")
99105
return

internal/provider/provider_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
package provider
22

33
import (
4+
"log"
5+
"os"
6+
"strings"
47
"testing"
58

69
"github.com/hashicorp/terraform-plugin-framework/providerserver"
710
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
11+
"github.com/joho/godotenv"
12+
"github.com/stretchr/testify/require"
813
)
914

1015
// testAccProtoV6ProviderFactories are used to instantiate a provider during
1116
// acceptance testing. The factory function will be invoked for every Terraform
1217
// CLI command executed to create a provider server to which the CLI can
1318
// reattach.
14-
// nolint: unused
1519
var testAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
1620
"meshstack": providerserver.NewProtocol6WithError(New("test")()),
1721
}
1822

19-
// nolint: unused
23+
func init() {
24+
if err := godotenv.Load(); err == nil {
25+
log.Println("using .env file from current directory")
26+
}
27+
}
28+
2029
func testAccPreCheck(t *testing.T) {
21-
// You can add code here to run prior to any test case execution, for example assertions
22-
// about the appropriate environment variables being set are common to see in a pre-check
23-
// function.
30+
endpoint := os.Getenv(envKeyMeshstackEndpoint)
31+
require.Truef(t, strings.HasPrefix(endpoint, "http://localhost"),
32+
"Env %s='%s' does not start with http://localhost, only locally running meshStacks should be used for tests", envKeyMeshstackEndpoint, endpoint)
33+
require.NotEmptyf(t, os.Getenv(envKeyMeshstackApiKey), "Env %s empty, please set before running", envKeyMeshstackApiKey)
34+
require.NotEmptyf(t, os.Getenv(envKeyMeshstackApiSecret), "Env %s empty, please set before running", envKeyMeshstackApiSecret)
2435
}

0 commit comments

Comments
 (0)