Skip to content

Commit f4341db

Browse files
committed
docs: Updated the Contribution Guide.
1 parent cf79e84 commit f4341db

File tree

1 file changed

+169
-54
lines changed

1 file changed

+169
-54
lines changed

CONTRIBUTING.md

+169-54
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,62 @@
11
# Contribution Guide
22

3+
## Initial setup
4+
5+
The majority of development is done on macOS, so we have some helpers set-up to streamline setup.
6+
7+
### Prerequisites
8+
9+
* [Xcode CLI tools](https://mac.install.guide/commandlinetools/1.html)
10+
* [Homebrew](https://mac.install.guide/homebrew/index.html)
11+
12+
### Setting-up
13+
14+
1. For Mac users, you can install all of the Homebrew and Go packages together.
15+
16+
```bash
17+
make install-tools-mac
18+
```
19+
20+
Obviously, this won't work on Linux, so ensure that the following packages are installed from your system's package manager.
21+
22+
* Go 1.21+ (primary language)
23+
* Node.js 18+ (linting tools)
24+
* Python 3.11+ (linting tools)
25+
* `jq` (shell scripting tools)
26+
* `pre-commit` (linting tools)
27+
28+
1. Running `make` in the root of the repo, by itself, will display a list of tasks and what they do. The ones highlighted in yellow are the ones that are most frequently used, or combine running multiple sub-tasks with one convenient command.
29+
30+
```bash
31+
make
32+
```
33+
34+
1. After the core dependencies are installed, install the Git hooks. These will validate code on commit and reject anything that does not meet this project's best practices.
35+
36+
```bash
37+
make install-hooks
38+
```
39+
40+
1. This project uses [Conventional Commits](https://www.conventionalcommits.org). If you are unfamiliar with this pattern of writing your commit messages, please read the spec. This project supports the following keywords:
41+
42+
| Keyword | Description |
43+
|------------|------------------------------------------------------------------------------------|
44+
| `build` | Changes that affect the build system or external dependencies. |
45+
| `ci` | Changes to our CI configuration files and scripts. |
46+
| `deps` | Updating dependencies. |
47+
| `docs` | Documentation only changes. |
48+
| `feat` | A new feature. |
49+
| `fix` | A bug fix. |
50+
| `lint` | Linter or static analysis-related changes. |
51+
| `perf` | A code change that improves performance. |
52+
| `refactor` | A code change that neither fixes a bug nor adds a feature. |
53+
| `style` | Changes that do not affect the meaning of the code (whitespace, formatting, etc.). |
54+
| `test` | Adding missing tests or correcting existing tests, benchmarks, fuzzing, etc. |
55+
356
## Build provider from source
457
5-
> **IMPORTANT:** You probably want **"Install provider (as a user)"** instead.
58+
> [!IMPORTANT]
59+
> You probably want **"Install provider (as a user)"** instead.
660
761
This will build the provider for the current OS and CPU architecture, and install it into your Go binary path.
862
@@ -18,21 +72,13 @@ This will build the provider for the current OS and CPU architecture, and instal
1872
1973
By default, Terraform expects to download providers over the internet. Instead, we're going to download our own from GitHub Enterprise, and we need to tell Terraform how to find it.
2074

21-
We're going to assume you already have [Homebrew](https://mac.install.guide/homebrew/index.html) and the [Xcode CLI tools](https://mac.install.guide/commandlinetools/1.html) installed.
22-
23-
1. Install [Go](https://go.dev). For Mac users, it's as simple as…
24-
25-
```bash
26-
brew install go
27-
```
28-
2975
1. Find where your installed Go binaries live.
3076

3177
```bash
3278
./find-go-bin.sh
3379
```
3480

35-
1. Update your `~/.terraformrc` file with these contents:
81+
1. Update your `~/.terraformrc` file with these contents, replacing `<GO_BIN_PATH>` with the result from `./find-go-bin.sh`.
3682

3783
```hcl
3884
provider_installation {
@@ -44,13 +90,6 @@ We're going to assume you already have [Homebrew](https://mac.install.guide/home
4490
}
4591
```
4692

47-
1. Replace `<GO_BIN_PATH>` with the path to your Go binaries, above.
48-
49-
```bash
50-
# After running `make build`...
51-
dirname $(which terraform-provider-corefunc)
52-
```
53-
5493
## Ensure the provider is installed correctly
5594

5695
```bash
@@ -76,95 +115,174 @@ You can apply this plan to save these new output values to the Terraform state,
76115

77116
## Testing and fuzzing
78117

79-
The `nproc` binary is commonly available on most Linux distributions. To install in Alpine Linux or macOS, you should install the `coreutils` package from `apk` (Alpine Linux) or `brew` (macOS).
118+
The `nproc` binary is commonly available on most Linux distributions. If it's not installed, go back to the top of this document and follow instructions.
80119
81120
### Unit tests (and code coverage)
82121
83122
This will run Unit and Fuzz tests. This tests the low-level Go code, but not the Terraform integration wrapped around it.
84123
85124
```bash
86-
go test -count=1 -parallel=$(nproc) -timeout 30s -coverpkg=./... -coverprofile=_coverage.txt -v ./...
125+
make unit
87126
```
88127
89128
You can view the code coverage report with:
90129
91130
```bash
92-
go tool cover -html=_coverage.txt
131+
make view-cov
93132
```
94133
95134
### Terraform provider acceptance tests (and code coverage)
96135
97136
This will run all tests: Unit, Acceptance, and Fuzz. Acceptance tests run the code through Terraform and test the Terraform communication pathway.
98137
99138
```bash
100-
TF_ACC=1 go test -count=1 -parallel=$(nproc) -timeout 30s -coverpkg=./... -coverprofile=_coverage.txt -v ./...
139+
make acc
101140
```
102141
103142
You can view the code coverage report with:
104143
105144
```bash
106-
go tool cover -html=_coverage.txt
145+
make view-cov
107146
```
108147
109148
### Fuzzer
110149
111150
This will run the fuzzer for 10 minutes. [Learn more about fuzzing](https://go.dev/doc/tutorial/fuzz).
112151
113152
```bash
114-
go test -fuzz=Fuzz -fuzztime 10m -v ./corefunc/.
153+
make fuzz
115154
```
116155
117-
### Benchmarks
156+
### Running individual tests
118157
119-
Benchmarks test the performance/scalability of a package. Different versions of the benchmarks execute different code paths inside the functions.
158+
We don't have `make` tasks for individual tests, however, if you run one of the testing `make` tasks, the first line of the output will show you the command being run. For example:
120159

121160
```bash
161+
# Line breaks added for readability.
122162
go test \
123-
-bench=. \
124-
-benchtime=10s \
125-
-benchmem \
126-
-cpuprofile=_cpu.out \
127-
-memprofile=_mem.out \
128-
-trace=_trace.out \
129-
./corefunc \
130-
| tee _bench.txt
163+
-count=1 \
164+
-parallel=$(nproc) \
165+
-timeout 30s \
166+
-coverpkg=./... \
167+
-coverprofile=__coverage.out \
168+
-v ./...
131169
```
132170

133-
Then:
171+
You can add `-run` followed by a Go regexp pattern to match the names of the tests you want to run.
134172

135173
```bash
136-
go tool pprof -http :8080 _cpu.out
174+
# Line breaks added for readability.
175+
go test \
176+
-run "TruncateLabel" \
177+
-count=1 \
178+
-parallel=$(nproc) \
179+
-timeout 30s \
180+
-coverpkg=./corefunc/... \
181+
-coverprofile=__coverage.out \
182+
-v ./corefunc/...
137183
```
138184

185+
## Benchmarks
186+
187+
Benchmarks test the performance of a package.
188+
189+
### Quick benchmarks
190+
191+
You can run a _quick_ benchmark (relatively speaking) that gets some data we can measure. It benchmarks the results of:
192+
193+
* Each test
194+
* …with each test case
195+
* …serially as well as in parallel
196+
139197
```bash
140-
go tool pprof -http :8081 _mem.out
198+
make quickbench
141199
```
142200

201+
### Benchmark analysis
202+
203+
If you want to compare benchmarks between code changes, you'll need to run a _more complete_ benchmark which will provide enough data for analysis. It benchmarks the results of:
204+
205+
* Each test
206+
* …with each test case
207+
* …serially as well as in parallel
208+
* …multiple times over
209+
143210
```bash
144-
go tool trace _trace.out
211+
make bench
145212
```
146213
214+
Benchmark files are timestamped, so you can compare benchmark results across runs. See [benchstat documentation](https://pkg.go.dev/golang.org/x/perf/cmd/benchstat) to understand how to most effectively slice the results.
215+
216+
> [!NOTE]
217+
> If you put old before new, values in the right-most column represent the size of the decrease (negative values are better). If you reverse them and push new before old, values in the right-most column represent the size of the increase (positive values are better).
218+
147219
```bash
148-
go install golang.org/x/perf/cmd/benchstat@latest
149-
benchstat _bench.txt
220+
benchstat Current=__bench-{new}.out Previous=__bench-{old}.out
150221
```
151222
152-
When you're done, clean-up.
223+
It will show you something like this:
224+
225+
```plain
226+
goos: darwin
227+
goarch: arm64
228+
pkg: github.com/northwood-labs/terraform-provider-corefunc/corefunc
229+
│ Current │ Previous │
230+
│ sec/op │ sec/op vs base │
231+
TruncateLabel/balanced0-10 2.100n ± 1% 103.600n ± 3% +4833.33% (p=0.002 n=6)
232+
TruncateLabel/balanced3-10 2.104n ± 0% 103.750n ± 2% +4831.08% (p=0.002 n=6)
233+
TruncateLabel/balanced5-10 2.103n ± 1% 104.000n ± 1% +4845.32% (p=0.002 n=6)
234+
TruncateLabel/balanced7-10 69.41n ± 3% 213.20n ± 1% +207.16% (p=0.002 n=6)
235+
TruncateLabel/balanced8-10 68.75n ± 2% 214.55n ± 4% +212.07% (p=0.002 n=6)
236+
TruncateLabel/balanced10-10 67.67n ± 4% 215.85n ± 2% +219.00% (p=0.002 n=6)
237+
TruncateLabel/balanced30-10 72.97n ± 5% 220.50n ± 5% +202.18% (p=0.002 n=6)
238+
TruncateLabel/balanced64-10 74.80n ± 1% 220.05n ± 1% +194.20% (p=0.002 n=6)
239+
TruncateLabel/balanced80-10 33.37n ± 3% 103.45n ± 2% +210.06% (p=0.002 n=6)
240+
TruncateLabel/balanced100-10 33.83n ± 2% 103.45n ± 1% +205.75% (p=0.002 n=6)
241+
TruncateLabel/balanced120-10 33.48n ± 1% 103.75n ± 1% +209.84% (p=0.002 n=6)
242+
TruncateLabel/balanced128-10 34.49n ± 5% 103.90n ± 1% +201.20% (p=0.002 n=6)
243+
TruncateLabel/balanced256-10 34.15n ± 5% 104.20n ± 3% +205.17% (p=0.002 n=6)
244+
245+
[…snip…]
246+
```
247+
248+
I had to go into each file with VS Code, select the benchmarking lines, and sort _ascending_, then sort _numerically_ to get the lines in an order that makes sense.
249+
250+
The way that these are written, `TruncateLabel` is the test. `balanced{number}` is the test-case. And the `-10` is the number of CPU cores I have on my machine.
251+
252+
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.
253+
254+
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.
255+
256+
### Exploring profiler data
257+
258+
Then, you can view the CPU profiler results…
153259
154260
```bash
155-
rm -f _*.out _*.txt *.test
261+
make view-cpupprof
156262
```
157263
158-
## Scanning for vulnerabilities
264+
…view the memory profiler results…
159265
160266
```bash
161-
go install golang.org/x/vuln/cmd/govulncheck@latest
162-
govulncheck -test ./...
267+
make view-mempprof
163268
```
164269
270+
…or view the trace results.
271+
165272
```bash
166-
go install github.com/google/osv-scanner/cmd/osv-scanner@v1
167-
osv-scanner -r .
273+
make view-trace
274+
```
275+
276+
When you're done, clean-up.
277+
278+
```bash
279+
make clean-bench
280+
```
281+
282+
## Scanning for vulnerabilities
283+
284+
```bash
285+
make vuln
168286
```
169287

170288
## Previewing documentation
@@ -174,26 +292,23 @@ osv-scanner -r .
174292
Generate the Terraform Registry-facing documentation.
175293

176294
```bash
177-
go generate -v ./...
295+
make docs-provider
178296
```
179297

180-
### CLI Go Documentation
298+
### Go CLI Documentation
181299

182300
You can get the package documentation on the CLI using the `go doc` command.
183301

184302
```bash
185-
go doc -C corefunc/ -all
303+
make docs-cli
186304
```
187305

188-
### Local HTTP Go Documentation Server
306+
### Go Documentation Server
189307

190308
```bash
191-
go install golang.org/x/tools/cmd/godoc@latest
192-
godoc -index -links
309+
make docs-serve
193310
```
194311

195-
Then open <http://localhost:6060/pkg/github.com/northwood-labs/terraform-provider-corefunc/corefunc/> in your web browser.
196-
197312
## Running the debugger
198313

199314
> **IMPORTANT:** At present, we only provide support for debugging in VSCode. Debugging through other tools may work, but they are untested.

0 commit comments

Comments
 (0)