You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: CONTRIBUTING.md
+111-3
Original file line number
Diff line number
Diff line change
@@ -153,10 +153,24 @@ make unit NAME=TruncateLabel
153
153
You can view the code coverage report with either:
154
154
155
155
```bash
156
-
make view-cov-cli # on the CLI
156
+
make view-cov-cli # on the CLI
157
157
make view-cov-html # in the browser
158
158
```
159
159
160
+
#### How to write a unit test
161
+
162
+
In the `testfixtures/` directory, there is a file for each function. This leverages a pattern known as [Table-Driven Testing](https://github.com/golang/go/wiki/TableDrivenTests), and composes all of the test cases in one place.
163
+
164
+
The `corefunc/` directory contains the code and tests for the Go library. The `corefuncprovider/` directory contains the code and tests for the Terraform provider. The relevant `_test.go` files in each directory leverage the same test fixture. This ensures that the Go library code and the Terraform provider which implements the Go library function both pass the test cases.
165
+
166
+
If we leverage a third-party package for functionality, there will not be a test in the `corefunc/` directory, but the Terraform provider implementation will have a test in the `corefuncprovider/` directory.
167
+
168
+
* A unit test function lives in the `corefunc/` directory, and begins with the word `Test`.
169
+
* [Tutorial: Add a test](https://go.dev/doc/tutorial/add-a-test)
This will run Acceptance tests. Acceptance tests run the code through Terraform and test the Terraform communication pathway.
@@ -167,15 +181,39 @@ make acc
167
181
168
182
# Run one acceptance test
169
183
make acc NAME=TruncateLabel
184
+
185
+
# Run all acceptance tests with debug-level output
186
+
make acc DEBUG=true
187
+
188
+
# Run one acceptance test with debug-level output
189
+
make acc NAME=TruncateLabel DEBUG=true
170
190
```
171
191
172
192
You can view the code coverage report with either:
173
193
174
194
```bash
175
-
make view-cov-cli # on the CLI
195
+
make view-cov-cli # on the CLI
176
196
make view-cov-html # in the browser
177
197
```
178
198
199
+
#### How to write an acceptance test
200
+
201
+
In the `testfixtures/` directory, there is a file for each function. This leverages a pattern known as [Table-Driven Testing](https://github.com/golang/go/wiki/TableDrivenTests), and composes all of the test cases in one place.
202
+
203
+
The `corefunc/` directory contains the code and tests for the Go library. The `corefuncprovider/` directory contains the code and tests for the Terraform provider. The relevant `_test.go` files in each directory leverage the same test fixture. This ensures that the Go library code and the Terraform provider which implements the Go library function both pass the test cases.
204
+
205
+
If we leverage a third-party package for functionality, there will not be a test in the `corefunc/` directory, but the Terraform provider implementation will have a test in the `corefuncprovider/` directory.
206
+
207
+
To help keep things easy to understand, the acceptance tests use Go's [`text/template`](https://pkg.go.dev/text/template) package to generate the Terraform code that is used for the acceptance test. The documentation for the [`templatefile()`](https://developer.hashicorp.com/terraform/language/functions/templatefile) functionsays that templates for use with Terraform should use the `*.tftpl` extension:
208
+
209
+
>`*.tftpl` is the recommended naming pattern to use for your template files. Terraform will not prevent you from using other names, but following this convention will help your editor understand the content and likely provide better editing experience as a result.
210
+
211
+
* An acceptance testfunctionlivesin the `corefuncprovider/` directory, and begins with the word `TestAcc`.
212
+
* [Tutorial: Add a test](https://go.dev/doc/tutorial/add-a-test)
* [Tutorial: Code coverage for Go integration tests](https://go.dev/blog/integration-test-coverage)
216
+
179
217
### Documentation Examples as tests (and code coverage)
180
218
181
219
This will run the Documentation Examples as tests. This ensures that the examples we put in front of users actually work.
@@ -192,15 +230,43 @@ make view-cov-cli # on the CLI
192
230
make view-cov-html # in the browser
193
231
```
194
232
233
+
#### How to write a documentation example
234
+
235
+
The `corefunc/` directory contains the code and tests forthe Go library. If we leverage a third-party package for functionality, there will not be a testin the `corefunc/` directory.
236
+
237
+
* An example testfunctionlivesin the `corefunc/` directory, and begins with the word `Example`.
This will use `tfschema` (reads the provider schema) and `bats` (CLI testing framework) to verify that the provider exposes the correct schema. This test requires compiling and installing the provoider (which `go test` doesn't require.)
243
+
244
+
```bash
245
+
# Run all BATS tests
246
+
make bats
247
+
```
248
+
249
+
#### How to write a BATS test
250
+
195
251
### Fuzzer
196
252
253
+
Fuzzing is a type of automated testing which continuously manipulates inputs to a program to find bugs. Go fuzzing uses coverage guidance to intelligently walk through the code being fuzzed to find and report failures to the user. Since it can reach edge cases which humans often miss, fuzz testing can be particularly valuable for finding security exploits and vulnerabilities.
254
+
197
255
This will run the fuzzer for 10 minutes. [Learn more about fuzzing](https://go.dev/doc/tutorial/fuzz).
198
256
199
257
```bash
200
258
# May only run one fuzz test at a time
201
259
make fuzz NAME=TruncateLabel
202
260
```
203
261
262
+
#### How to write a fuzz test
263
+
264
+
The `corefunc/` directory contains the code and tests for the Go library. If we leverage a third-party package for functionality, there will not be a test in the `corefunc/` directory.
265
+
266
+
* A fuzzer test function lives in the `corefunc/` directory, and begins with the word `Fuzz`.
267
+
* [Documentation: Go fuzzing](https://go.dev/security/fuzz/)
268
+
* [Tutorial: Getting started with fuzzing](https://go.dev/doc/tutorial/fuzz)
269
+
204
270
## Benchmarks
205
271
206
272
Benchmarks test the performance of a package.
@@ -270,7 +336,16 @@ The way that these are written, `TruncateLabel` is the test. `balanced{number}`
270
336
271
337
For the middle part (`balanced{number}`), the number represents the number of characters I truncated the label to. I know from the implementation that different truncation lengths can trigger different code paths, and that anything under `6` results in a near-immediate return with no calculation necessary. We also know that the longer lengths similarly have less truncation logic to perform.
272
338
273
-
But the middle tests from ~10–80 are most likely to execute _all_ of the code in the function, which makes it the most intersting.
339
+
But the middle tests from ~10–80 are most likely to execute _all_ of the code in the function, which makes it the most interesting.
340
+
341
+
#### How to write a benchmark suite
342
+
343
+
The `corefunc/` directory contains the code and tests forthe Go library. If we leverage a third-party package for functionality, there will not be a testin the `corefunc/` directory.
344
+
345
+
* A fuzzer testfunctionlivesin the `corefunc/` directory, and begins with the word `Benchmark`.
346
+
* There is one `Benchmark`test which runs the tests serially. There is a second `Benchmark`test which runs the tests in parallel. This latter functionhas`Parallel` as the suffix of its name.
* [Go: The Complete Guide to Profiling Your Code](https://hackernoon.com/go-the-complete-guide-to-profiling-your-code-h51r3waz)
381
+
382
+
## Profile-guided optimization
383
+
384
+
Profile-guided optimization (PGO), also known as feedback-directed optimization (FDO), is a compiler optimization technique that feeds information (a profile) from representative runs of the application back into to the compiler for the next build of the application, which uses that information to make more informed optimization decisions. For example, the compiler may decide to more aggressively inline functions which the profile indicates are called frequently.
* [Tutorial: Profile-guided optimization in Go 1.21](https://go.dev/blog/pgo)
388
+
301
389
## Scanning for vulnerabilities
302
390
303
391
```bash
@@ -314,6 +402,16 @@ Generate the Terraform Registry-facing documentation.
314
402
make docs-provider
315
403
```
316
404
405
+
#### Terraform Provider Documentation
406
+
407
+
These are the patterns we follow for generating Terraform documentation. _Every_ resource/data source in the provider has at least one example. With a resource-specific template, we can implement multiple examples.
408
+
409
+
See `examples/data-sources/corefunc_env_ensure/` as an example of a custom template with multiple examples.
0 commit comments