Skip to content

Commit 199289e

Browse files
Paddy Carverbflad
andcommitted
Upgrade to latest terraform-plugin-go, add logging sink.
Upgrade to the latest version of terraform-plugin-go, bringing support for terraform-plugin-log. Use the terraform-plugin-log logging sink when setting up the helper/resource test framework. The idea here is that we'll use the logging sink to reroute all terraform-plugin-log logs, and leave the `log.Print` logs the way they are with the existing override. Over time, it may be nice to migrate those logs to use terraform-plugin-log, probably by extending `helper/logging.GetTestLogContext` to include a root SDK logger. In theory, this PR should result in no logs being written to the CLI during testing, which was happening in hashicorp/terraform-plugin-go#93. It should respect `TF_LOG`, `TF_LOG_PATH`, `TF_ACC_LOG_PATH`, and `TF_LOG_PATH_MASK`, as well as any terraform-plugin-log style environment variables in use. This also fixes `TF_ACC_LOG_PATH` to record the logs from the provider under test, not just the logs from Terraform and any providers the provider under test depends on. Co-authored-by: Brian Flad <bflad417@gmail.com>
1 parent 6b1ff72 commit 199289e

File tree

5 files changed

+111
-20
lines changed

5 files changed

+111
-20
lines changed

go.mod

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ require (
1313
github.com/golang/mock v1.4.3
1414
github.com/google/go-cmp v0.5.6
1515
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
16-
github.com/hashicorp/go-hclog v0.15.0
16+
github.com/hashicorp/go-hclog v0.16.1
1717
github.com/hashicorp/go-multierror v1.1.1
1818
github.com/hashicorp/go-plugin v1.4.1
19-
github.com/hashicorp/go-uuid v1.0.1
19+
github.com/hashicorp/go-uuid v1.0.2
2020
github.com/hashicorp/go-version v1.3.0
2121
github.com/hashicorp/hc-install v0.3.1
2222
github.com/hashicorp/hcl/v2 v2.3.0
2323
github.com/hashicorp/logutils v1.0.0
2424
github.com/hashicorp/terraform-exec v0.15.0
2525
github.com/hashicorp/terraform-json v0.13.0
26-
github.com/hashicorp/terraform-plugin-go v0.4.0
26+
github.com/hashicorp/terraform-plugin-go v0.5.0
27+
github.com/hashicorp/terraform-plugin-log v0.2.0
2728
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
2829
github.com/kylelemons/godebug v1.1.0 // indirect
2930
github.com/mitchellh/copystructure v1.2.0
30-
github.com/mitchellh/go-testing-interface v1.0.4
31+
github.com/mitchellh/go-testing-interface v1.14.1
3132
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
3233
github.com/mitchellh/mapstructure v1.1.2
3334
github.com/mitchellh/reflectwalk v1.0.2

go.sum

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
136136
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
137137
github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
138138
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
139-
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
139+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
140140
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
141141
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
142142
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
@@ -158,6 +158,7 @@ github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brv
158158
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
159159
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
160160
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
161+
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
161162
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
162163
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
163164
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUKaCaGKZ/dR2roBXv0vKbSCnssIldfQdI=
@@ -166,8 +167,8 @@ github.com/hashicorp/go-getter v1.5.3 h1:NF5+zOlQegim+w/EUhSLh6QhXHmZMEeHLQzllkQ
166167
github.com/hashicorp/go-getter v1.5.3/go.mod h1:BrrV/1clo8cCYu6mxvboYg+KutTiFnXjMEgDD8+i7ZI=
167168
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
168169
github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
169-
github.com/hashicorp/go-hclog v0.15.0 h1:qMuK0wxsoW4D0ddCCYwPSTm4KQv1X1ke3WmPWZ0Mvsk=
170-
github.com/hashicorp/go-hclog v0.15.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
170+
github.com/hashicorp/go-hclog v0.16.1 h1:IVQwpTGNRRIHafnTs2dQLIk4ENtneRIEEJWOVDqz99o=
171+
github.com/hashicorp/go-hclog v0.16.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
171172
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
172173
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
173174
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
@@ -177,9 +178,10 @@ github.com/hashicorp/go-plugin v1.4.1/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ3
177178
github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo=
178179
github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I=
179180
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
180-
github.com/hashicorp/go-uuid v1.0.1 h1:fv1ep09latC32wFoVwnqcnKJGnMSdBanPczbHAYm1BE=
181-
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
181+
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
182+
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
182183
github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
184+
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
183185
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
184186
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
185187
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
@@ -194,8 +196,14 @@ github.com/hashicorp/terraform-exec v0.15.0 h1:cqjh4d8HYNQrDoEmlSGelHmg2DYDh5yay
194196
github.com/hashicorp/terraform-exec v0.15.0/go.mod h1:H4IG8ZxanU+NW0ZpDRNsvh9f0ul7C0nHP+rUR/CHs7I=
195197
github.com/hashicorp/terraform-json v0.13.0 h1:Li9L+lKD1FO5RVFRM1mMMIBDoUHslOniyEi5CM+FWGY=
196198
github.com/hashicorp/terraform-json v0.13.0/go.mod h1:y5OdLBCT+rxbwnpxZs9kGL7R9ExU76+cpdY8zHwoazk=
197-
github.com/hashicorp/terraform-plugin-go v0.4.0 h1:LFbXNeLDo0J/wR0kUzSPq0RpdmFh2gNedzU0n/gzPAo=
198-
github.com/hashicorp/terraform-plugin-go v0.4.0/go.mod h1:7u/6nt6vaiwcWE2GuJKbJwNlDFnf5n95xKw4hqIVr58=
199+
github.com/hashicorp/terraform-plugin-go v0.5.0 h1:+gCDdF0hcYCm0YBTxrP4+K1NGIS5ZKZBKDORBewLJmg=
200+
github.com/hashicorp/terraform-plugin-go v0.5.0/go.mod h1:PAVN26PNGpkkmsvva1qfriae5Arky3xl3NfzKa8XFVM=
201+
github.com/hashicorp/terraform-plugin-log v0.2.0 h1:rjflRuBqCnSk3UHOR25MP1G5BDLKktTA6lNjjcAnBfI=
202+
github.com/hashicorp/terraform-plugin-log v0.2.0/go.mod h1:E1kJmapEHzqu1x6M++gjvhzM2yMQNXPVWZRCB8sgYjg=
203+
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 h1:1FGtlkJw87UsTMg5s8jrekrHmUPUJaMcu6ELiVhQrNw=
204+
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896/go.mod h1:bzBPnUIkI0RxauU8Dqo+2KrZZ28Cf48s8V6IHt3p4co=
205+
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
206+
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
199207
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
200208
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
201209
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
@@ -250,8 +258,8 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG
250258
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
251259
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
252260
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
253-
github.com/mitchellh/go-testing-interface v1.0.4 h1:ZU1VNC02qyufSZsjjs7+khruk2fKvbQ3TwRV/IBCeFA=
254-
github.com/mitchellh/go-testing-interface v1.0.4/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
261+
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
262+
github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8=
255263
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
256264
github.com/mitchellh/go-wordwrap v1.0.0 h1:6GlHJ/LTGMrIJbwgdqdl2eEH8o+Exx/0m8ir9Gns0u4=
257265
github.com/mitchellh/go-wordwrap v1.0.0/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
@@ -299,6 +307,7 @@ github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6e
299307
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
300308
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
301309
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
310+
github.com/zclconf/go-cty v1.1.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
302311
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
303312
github.com/zclconf/go-cty v1.9.1 h1:viqrgQwFl5UpSxc046qblj78wZXVDFnSOufaOTER+cc=
304313
github.com/zclconf/go-cty v1.9.1/go.mod h1:vVKLxnk3puL4qRAv72AO+W99LUD4da90g3uUAzyuvAk=
@@ -367,6 +376,7 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
367376
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
368377
golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
369378
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
379+
golang.org/x/net v0.0.0-20191009170851-d66e71096ffb/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
370380
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
371381
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
372382
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -380,6 +390,7 @@ golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/
380390
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
381391
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
382392
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
393+
golang.org/x/net v0.0.0-20210119194325-5f4716e94777/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
383394
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
384395
golang.org/x/net v0.0.0-20210326060303-6b1517762897 h1:KrsHThm5nFk34YtATK1LsThyGhGbGe1olrte/HInHvs=
385396
golang.org/x/net v0.0.0-20210326060303-6b1517762897/go.mod h1:uSPa2vr4CLtc/ILN5odXGNXS6mhrKVzTaCXzk9m6W3k=

helper/logging/logging.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package logging
22

33
import (
4+
"context"
45
"fmt"
56
"io"
67
"io/ioutil"
@@ -10,27 +11,42 @@ import (
1011
"syscall"
1112

1213
"github.com/hashicorp/logutils"
14+
"github.com/hashicorp/terraform-plugin-log/tfsdklog"
1315
testing "github.com/mitchellh/go-testing-interface"
1416
)
1517

1618
// These are the environmental variables that determine if we log, and if
1719
// we log whether or not the log should go to a file.
1820
const (
19-
EnvLog = "TF_LOG" // Set to True
20-
EnvLogFile = "TF_LOG_PATH" // Set to a file
21+
EnvLog = "TF_LOG" // See ValidLevels
22+
EnvLogFile = "TF_LOG_PATH" // Set to a file
23+
EnvAccLogFile = "TF_ACC_LOG_PATH" // Set to a file
2124
// EnvLogPathMask splits test log files by name.
2225
EnvLogPathMask = "TF_LOG_PATH_MASK"
2326
)
2427

2528
var ValidLevels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERROR"}
2629

27-
// LogOutput determines where we should send logs (if anywhere) and the log level.
30+
// LogOutput determines where we should send logs (if anywhere) and the log
31+
// level. This only effects this log.Print* functions called in the provider
32+
// under test. Dependency providers for the provider under test will have their
33+
// logging controlled by Terraform itself and managed with the TF_ACC_LOG_PATH
34+
// environment variable. Calls to tflog.* will have their output managed by the
35+
// tfsdklog sink.
2836
func LogOutput(t testing.T) (logOutput io.Writer, err error) {
2937
logOutput = ioutil.Discard
3038

3139
logLevel := LogLevel()
3240
if logLevel == "" {
33-
return
41+
if os.Getenv(EnvAccLogFile) != "" {
42+
// plugintest defaults to TRACE when TF_ACC_LOG_PATH is
43+
// set for Terraform and dependency providers of the
44+
// provider under test. We should do the same for the
45+
// provider under test.
46+
logLevel = "TRACE"
47+
} else {
48+
return
49+
}
3450
}
3551

3652
logOutput = os.Stderr
@@ -42,6 +58,14 @@ func LogOutput(t testing.T) (logOutput io.Writer, err error) {
4258
}
4359
}
4460

61+
if logPath := os.Getenv(EnvAccLogFile); logPath != "" {
62+
var err error
63+
logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
64+
if err != nil {
65+
return nil, err
66+
}
67+
}
68+
4569
if logPathMask := os.Getenv(EnvLogPathMask); logPathMask != "" {
4670
// Escape special characters which may appear if we have subtests
4771
testName := strings.Replace(t.Name(), "/", "__", -1)
@@ -114,3 +138,12 @@ func isValidLogLevel(level string) bool {
114138

115139
return false
116140
}
141+
142+
// GetTestLogContext creates a context that is registered to the SDK log sink.
143+
// This function is for internal usage only and is not supported by the project's
144+
// compatibility promises.
145+
func GetTestLogContext(t testing.T) context.Context {
146+
ctx := context.Background()
147+
ctx = tfsdklog.RegisterTestSink(ctx, t)
148+
return ctx
149+
}

helper/resource/plugin.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/hashicorp/terraform-exec/tfexec"
1414
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
1515
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1718
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugintest"
1819
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
@@ -34,7 +35,7 @@ func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir,
3435
// reattach behavior in Terraform. This ensures we get test coverage
3536
// and enables the use of delve as a debugger.
3637

37-
ctx, cancel := context.WithCancel(context.Background())
38+
ctx, cancel := context.WithCancel(logging.GetTestLogContext(t))
3839
defer cancel()
3940

4041
// this is needed so Terraform doesn't default to expecting protocol 4;
@@ -93,6 +94,8 @@ func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir,
9394
Output: ioutil.Discard,
9495
}),
9596
NoLogOutputOverride: true,
97+
UseTFLogSink: t,
98+
ProviderAddr: getProviderAddr(providerName),
9699
}
97100

98101
// let's actually start the provider server
@@ -170,6 +173,8 @@ func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir,
170173
Output: ioutil.Discard,
171174
}),
172175
NoLogOutputOverride: true,
176+
UseTFLogSink: t,
177+
ProviderAddr: getProviderAddr(providerName),
173178
}
174179

175180
// let's actually start the provider server
@@ -244,6 +249,8 @@ func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir,
244249
Output: ioutil.Discard,
245250
}),
246251
NoLogOutputOverride: true,
252+
UseTFLogSink: t,
253+
ProviderAddr: getProviderAddr(providerName),
247254
}
248255

249256
// let's actually start the provider server
@@ -316,3 +323,17 @@ func runProviderCommand(t testing.T, f func() error, wd *plugintest.WorkingDir,
316323
// Terraform commands
317324
return err
318325
}
326+
327+
func getProviderAddr(name string) string {
328+
host := "registry.terraform.io"
329+
namespace := "hashicorp"
330+
if v := os.Getenv("TF_ACC_PROVIDER_NAMESPACE"); v != "" {
331+
namespace = v
332+
}
333+
if v := os.Getenv("TF_ACC_PROVIDER_HOST"); v != "" {
334+
host = v
335+
}
336+
return strings.TrimSuffix(host, "/") + "/" +
337+
strings.TrimSuffix(namespace, "/") + "/" +
338+
name
339+
}

plugin/serve.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55

66
hclog "github.com/hashicorp/go-hclog"
77
"github.com/hashicorp/go-plugin"
8+
testing "github.com/mitchellh/go-testing-interface"
89
"google.golang.org/grpc"
910

1011
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
11-
tf5server "github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server"
12+
"github.com/hashicorp/terraform-plugin-go/tfprotov5/tf5server"
1213
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
13-
tf6server "github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"
14+
"github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516
)
1617

@@ -55,6 +56,14 @@ type ServeOpts struct {
5556
// adapter. This should only be used when running the plugin in
5657
// acceptance tests.
5758
NoLogOutputOverride bool
59+
60+
// UseTFLogSink is the testing.T for a test function that will turn on
61+
// the terraform-plugin-log logging sink.
62+
UseTFLogSink testing.T
63+
64+
// ProviderAddr is the address of the provider under test, like
65+
// registry.terraform.io/hashicorp/random.
66+
ProviderAddr string
5867
}
5968

6069
// Serve serves a plugin. This function never returns and should be the final
@@ -95,27 +104,43 @@ func Serve(opts *ServeOpts) {
95104
// assume we have either a v5 or a v6 provider
96105
if opts.GRPCProviderFunc != nil {
97106
provider := opts.GRPCProviderFunc()
107+
addr := opts.ProviderAddr
108+
if addr == "" {
109+
addr = "provider"
110+
}
98111
serveConfig.VersionedPlugins = map[int]plugin.PluginSet{
99112
5: {
100113
ProviderPluginName: &tf5server.GRPCProviderPlugin{
101114
GRPCProvider: func() tfprotov5.ProviderServer {
102115
return provider
103116
},
117+
Name: addr,
104118
},
105119
},
106120
}
121+
if opts.UseTFLogSink != nil {
122+
serveConfig.VersionedPlugins[5][ProviderPluginName].(*tf5server.GRPCProviderPlugin).Opts = append(serveConfig.VersionedPlugins[5][ProviderPluginName].(*tf5server.GRPCProviderPlugin).Opts, tf5server.WithLoggingSink(opts.UseTFLogSink))
123+
}
107124

108125
} else if opts.GRPCProviderV6Func != nil {
109126
provider := opts.GRPCProviderV6Func()
127+
addr := opts.ProviderAddr
128+
if addr == "" {
129+
addr = "provider"
130+
}
110131
serveConfig.VersionedPlugins = map[int]plugin.PluginSet{
111132
6: {
112133
ProviderPluginName: &tf6server.GRPCProviderPlugin{
113134
GRPCProvider: func() tfprotov6.ProviderServer {
114135
return provider
115136
},
137+
Name: addr,
116138
},
117139
},
118140
}
141+
if opts.UseTFLogSink != nil {
142+
serveConfig.VersionedPlugins[6][ProviderPluginName].(*tf6server.GRPCProviderPlugin).Opts = append(serveConfig.VersionedPlugins[6][ProviderPluginName].(*tf6server.GRPCProviderPlugin).Opts, tf6server.WithLoggingSink(opts.UseTFLogSink))
143+
}
119144

120145
}
121146

0 commit comments

Comments
 (0)