Skip to content

Commit 192aa6b

Browse files
committed
chore: Refactor integration tests
Signed-off-by: Steve Hipwell <steve.hipwell@gmail.com>
1 parent 78196d6 commit 192aa6b

File tree

151 files changed

+6889
-11675
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+6889
-11675
lines changed

CONTRIBUTING.md

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Before submitting an issue or a pull request, please search the repository for e
1010

1111
## Submitting a pull request
1212

13-
0. Fork and clone the repository.
14-
0. Create a new branch: `git switch -c my-branch-name`.
15-
0. Make your change, add tests, and make sure the tests still pass.
16-
0. Push to your fork and submit a pull request.
17-
0. Pat yourself on the back and wait for your pull request to be reviewed and merged.
13+
1. Fork and clone the repository.
14+
2. Create a new branch: `git switch -c my-branch-name`.
15+
3. Make your change, add tests, and make sure the tests still pass.
16+
4. Push to your fork and submit a pull request.
17+
5. Pat yourself on the back and wait for your pull request to be reviewed and merged.
1818

1919
Here are a few things you can do that will increase the likelihood of your pull request being accepted:
2020

@@ -38,21 +38,28 @@ Once you have the repository cloned, there's a couple of additional steps you'll
3838
- You _must_ make sure that the "Template Repository" item in Settings is checked for this repo.
3939
- If you haven't already, generate a Personal Access Token (PAT) for authenticating your test runs.
4040
- Export the necessary configuration for authenticating your provider with GitHub
41+
4142
```sh
42-
export GITHUB_TOKEN=<token of a user with an organization account>
43-
export GITHUB_ORGANIZATION=<name of an organization>
43+
export GITHUB_TEST_AUTH_MODE="organization"
44+
export GITHUB_OWNER="<name of an organization>"
45+
export GITHUB_USERNAME="<username of the user who created the token>"
46+
export GITHUB_TOKEN="<token of a user with an organization account>"
4447
```
48+
4549
- Build the project with `make build`
4650
- Try an example test run from the default (`main`) branch, like `TF_LOG=DEBUG TF_ACC=1 go test -v ./... -run ^TestAccGithubRepositories`. All those tests should pass.
4751

4852
### Local Development Iteration
4953

5054
1. Write a test describing what you will fix. See [`github_label`](./github/resource_github_issue_label_test.go) for an example format.
5155
1. Run your test and observe it fail. Enabling debug output allows for observing the underlying requests and responses made as well as viewing state (search `STATE:`) generated during the acceptance test run.
56+
5257
```sh
5358
TF_LOG=DEBUG TF_ACC=1 go test -v ./... -run ^TestAccGithubIssueLabel
5459
```
60+
5561
1. Align the resource's implementation to your test case and observe it pass:
62+
5663
```sh
5764
TF_ACC=1 go test -v ./... -run ^TestAccGithubIssueLabel
5865
```
@@ -67,7 +74,8 @@ Println debugging can easily be used to obtain information about how code change
6774

6875
If a full debugger is desired, VSCode may be used. In order to do so,
6976

70-
0. Create a launch.json file with this configuration:
77+
1. Create a launch.json file with this configuration:
78+
7179
```json
7280
{
7381
"name": "Attach to Process",
@@ -77,18 +85,18 @@ If a full debugger is desired, VSCode may be used. In order to do so,
7785
"processId": 0,
7886
}
7987
```
80-
Setting a `processId` of 0 allows a dropdown to select the process of the provider.
8188

82-
0. Add a sleep call (e.g. `time.Sleep(10 * time.Second)`) in the [`func providerConfigure(p *schema.Provider) schema.ConfigureFunc`](https://github.com/integrations/terraform-provider-github/blob/cec7e175c50bb091feecdc96ba117067c35ee351/github/provider.go#L274C1-L274C64) before the immediate `return` call. This will allow time to connect the debugger while the provider is initializing, before any critical logic happens.
89+
Setting a `processId` of 0 allows a dropdown to select the process of the provider.
8390

84-
0. Build the terraform provider with debug flags enabled and copy it to the appropriate bin folder with a command like `go build -gcflags="all=-N -l" -o ~/go/bin/`.
91+
1. Add a sleep call (e.g. `time.Sleep(10 * time.Second)`) in the [`func providerConfigure(p *schema.Provider) schema.ConfigureFunc`](https://github.com/integrations/terraform-provider-github/blob/cec7e175c50bb091feecdc96ba117067c35ee351/github/provider.go#L274C1-L274C64) before the immediate `return` call. This will allow time to connect the debugger while the provider is initializing, before any critical logic happens.
8592

86-
0. Create or edit a `dev.tfrc` that points toward the newly-built binary, and export the `TF_CLI_CONFIG_FILE` variable to point to it. Further instructions on this process may be found in the [Building the provider](#using-a-local-version-of-the-provider) section.
93+
2. Build the terraform provider with debug flags enabled and copy it to the appropriate bin folder with a command like `go build -gcflags="all=-N -l" -o ~/go/bin/`.
8794

88-
0. Run a terraform command (e.g. `terraform apply`). While the provider pauses on initialization, go to VSCode and click "Attach to Process". In the search box that appears, type `terraform-provi` and select the terraform provider process.
95+
3. Create or edit a `dev.tfrc` that points toward the newly-built binary, and export the `TF_CLI_CONFIG_FILE` variable to point to it. Further instructions on this process may be found in the [Building the provider](#using-a-local-version-of-the-provider) section.
8996

90-
0. The debugger is now connected! During a typical terraform command, the plugin will be invoked multiple times. If the debugger disconnects and the plugin is invoked again later in the run, the developer will have to re-attach each time as the process ID changes.
97+
4. Run a terraform command (e.g. `terraform apply`). While the provider pauses on initialization, go to VSCode and click "Attach to Process". In the search box that appears, type `terraform-provi` and select the terraform provider process.
9198

99+
5. The debugger is now connected! During a typical terraform command, the plugin will be invoked multiple times. If the debugger disconnects and the plugin is invoked again later in the run, the developer will have to re-attach each time as the process ID changes.
92100

93101
## Manual Testing
94102

@@ -99,7 +107,7 @@ Manual testing should be performed on each PR opened in order to validate the pr
99107
Build the provider and specify the output directory:
100108

101109
```sh
102-
$ go build -gcflags="all=-N -l" -o ~/go/bin/
110+
go build -gcflags="all=-N -l" -o ~/go/bin/
103111
```
104112

105113
This enables verifying your locally built provider using examples available in the `examples/` directory.
@@ -121,7 +129,7 @@ provider_installation {
121129
}
122130
```
123131

124-
See https://www.terraform.io/docs/cli/config/config-file.html for more details.
132+
See <https://www.terraform.io/docs/cli/config/config-file.html> for more details.
125133

126134
When running examples, you should spot the following warning to confirm you are using a local build:
127135

@@ -137,37 +145,50 @@ The following provider development overrides are set in the CLI configuration:
137145
Commonly required environment variables are listed below:
138146

139147
```sh
140-
# enable debug logging
148+
# Enable debug logging
141149
export TF_LOG=DEBUG
142150

143-
# enable testing of organization scenarios instead of individual or anonymous
144-
export GITHUB_ORGANIZATION=
145-
146-
# enable testing of individual scenarios instead of organization or anonymous
147-
export GITHUB_OWNER=
151+
# Enables acceptance tests
152+
export TF_ACC="1"
148153

149-
# enable testing of enterprise appliances
154+
# Configure the URL override for GHES.
150155
export GITHUB_BASE_URL=
151156

152157
# enable testing of GitHub Paid features, these normally also require an organization e.g. repository push rulesets
153158
export GITHUB_PAID_FEATURES=true
154159

155-
# leverage helper accounts for tests requiring them
156-
# examples include:
157-
# - https://github.com/github-terraform-test-user
158-
# - https://github.com/terraformtesting
159-
export GITHUB_TEST_OWNER=
160-
export GITHUB_TEST_ORGANIZATION=
161-
export GITHUB_TEST_USER_TOKEN=
162-
```
160+
# Configure acceptance testing mode; one of anonymous, individual, organization, team or enterprise. If not set will default to anonymous
161+
export GITHUB_TEST_AUTH_MODE=
163162

164-
See [this project](https://github.com/terraformtesting/acceptance-tests) for more information on our old system for automated testing.
163+
# Configure authentication for testing
164+
export GITHUB_OWNER=
165+
export GITHUB_USERNAME=
166+
export GITHUB_TOKEN=
167+
168+
# Configure user level values
169+
export GITHUB_TEST_USER_REPOSITORY=
170+
171+
# Configure for the org under test
172+
export GITHUB_TEST_ORG_USER=
173+
export GITHUB_TEST_ORG_SECRET_NAME=
174+
export GITHUB_TEST_ORG_REPOSITORY=
175+
export GITHUB_TEST_ORG_TEMPLATE_REPOSITORY=
176+
export GITHUB_TEST_ORG_APP_INSTALLATION_ID=
177+
178+
# Configure external (non-org) users
179+
export GITHUB_TEST_EXTERNAL_USER=
180+
export GITHUB_TEST_EXTERNAL_USER_TOKEN=
181+
export GITHUB_TEST_EXTERNAL_USER2=
182+
183+
# Configure test options
184+
export GITHUB_TEST_ADVANCED_SECURITY=
185+
```
165186

166187
There are also a small amount of unit tests in the provider. Due to the nature of the provider, such tests are currently only recommended for exercising functionality completely internal to the provider. These may be executed by running `make test`.
167188

168189
### GitHub Organization
169190

170-
If you do not have an organization already that you are comfortable running tests against, you will need to [create one](https://help.github.com/en/articles/creating-a-new-organization-from-scratch). The free "Team for Open Source" org type is fine for these tests. The name of the organization must then be exported in your environment as `GITHUB_ORGANIZATION`.
191+
If you do not have an organization already that you are comfortable running tests against, you will need to [create one](https://help.github.com/en/articles/creating-a-new-organization-from-scratch). The free "Team for Open Source" org type is fine for these tests. The name of the organization must then be exported in your environment as `GITHUB_OWNER`.
171192

172193
Make sure that your organization has a `terraform-template-module` repository ([terraformtesting/terraform-template-module](https://github.com/terraformtesting/terraform-template-module) is an example you can clone) and that its "Template repository" item in Settings is checked.
173194

GNUmakefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ TEST?=$$(go list ./... |grep -v 'vendor')
22
WEBSITE_REPO=github.com/hashicorp/terraform-website
33
PKG_NAME=github
44

5+
export TESTARGS=-race -coverprofile=coverage.txt -covermode=atomic
6+
57
default: build
68

79
tools:
@@ -16,18 +18,18 @@ fmt:
1618
golangci-lint fmt ./...
1719

1820
fmtcheck:
19-
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"
21+
@sh -c "'$(CURDIR)/scripts/fmtcheck.sh'"
2022

2123
lint:
2224
@echo "==> Checking source code against linters..."
23-
golangci-lint run ./...
25+
golangci-lint run --fix ./...
2426

2527
test:
2628
CGO_ENABLED=0 go test ./...
2729
# commenting this out for release tooling, please run testacc instead
2830

29-
testacc: fmtcheck
30-
TF_ACC=1 CGO_ENABLED=0 go test $(TEST) -v $(TESTARGS) -timeout 120m
31+
testacc:
32+
TF_ACC=1 CGO_ENABLED=0 go test -run "^TestAcc*" $(TEST) -v $(TESTARGS) -timeout 120m -count=1
3133

3234
test-compile:
3335
@if [ "$(TEST)" = "./..." ]; then \

0 commit comments

Comments
 (0)