Skip to content

Commit fb06388

Browse files
authored
Use tf dev override in integration test (#1588)
* Use tf dev override in integration test * use go-plus terraform version to run integration test * Fix tf config file * not copy tf config file to /tmp
1 parent da4b918 commit fb06388

File tree

7 files changed

+62
-68
lines changed

7 files changed

+62
-68
lines changed

.ci/cloudbuild-tests-integration.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ steps:
2222
args:
2323
[
2424
"-c",
25-
"mv /terraform/$_TERRAFORM_VERSION /bin/terraform && /usr/bin/make test-integration",
25+
"/usr/bin/make test-integration",
2626
]
2727
env:
2828
- TEST_PROJECT=$_TEST_PROJECT
2929
- TEST_FOLDER_ID=$_TEST_FOLDER
3030
- TEST_ANCESTRY=$_TEST_ANCESTRY
3131
- TEST_ORG_ID=$_TEST_ORG
32-
- TERRAFORM_VERSION=$_TERRAFORM_VERSION
3332
tags:
3433
- "ci"
3534
- "integration"

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ examples/*.tfstate*
1717
# binary
1818
./cmd
1919
bin/
20+
21+
# terraform config file for integration test
22+
tf-dev-override.tfrc

Dockerfile

-11
This file was deleted.

Makefile

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
TERRAFORM_VERSION=0.12.31
2-
31
build_dir=./bin
2+
TF_CONFIG_FILE=tf-dev-override.tfrc
43

54
build:
65
GO111MODULE=on go build -o ${build_dir}/tfplan2cai ./cmd/tfplan2cai
@@ -11,19 +10,24 @@ test:
1110
test-integration:
1211
go version
1312
terraform --version
14-
go test -run=CLI ./...
13+
./tf-dev-override.sh
14+
TF_CLI_CONFIG_FILE="${PWD}/${TF_CONFIG_FILE}" go test -run=CLI ./...
1515

1616
test-go-licenses:
1717
cd .. && go version && go install github.com/google/go-licenses@latest
1818
$$(go env GOPATH)/bin/go-licenses check ./... --ignore github.com/dnaeon/go-vcr
1919

20-
build-docker:
21-
docker build --build-arg TERRAFORM_VERSION=$(TERRAFORM_VERSION) -f ./Dockerfile -t terraform-google-conversion .
22-
2320
run-docker:
24-
docker run -it -v `pwd`:/terraform-google-conversion -v ${GOOGLE_APPLICATION_CREDENTIALS}:/terraform-google-conversion/credentials.json --entrypoint=/bin/bash --env TEST_PROJECT=${PROJECT_ID} --env GOOGLE_APPLICATION_CREDENTIALS=/terraform-google-conversion/credentials.json terraform-google-conversion;
21+
docker run -it \
22+
-v `pwd`:/terraform-google-conversion \
23+
-v ${GOOGLE_APPLICATION_CREDENTIALS}:/terraform-google-conversion/credentials.json \
24+
-w /terraform-google-conversion \
25+
--entrypoint=/bin/bash \
26+
--env TEST_PROJECT=${PROJECT_ID} \
27+
--env GOOGLE_APPLICATION_CREDENTIALS=/terraform-google-conversion/credentials.json \
28+
gcr.io/graphite-docker-images/go-plus;
2529

2630
release:
2731
./release.sh ${VERSION}
2832

29-
.PHONY: test test-integration test-go-licenses build-docker run-docker release
33+
.PHONY: build test test-integration test-go-licenses run-docker release

docs/contributing/index.md

+18-31
Original file line numberDiff line numberDiff line change
@@ -7,57 +7,44 @@ If you want to contribute to Terraform Google Conversion, check out the [contrib
77
- [Contributing](#contributing)
88
- [Table of Contents](#table-of-contents)
99
- [Testing](#testing)
10-
- [Docker](#docker)
10+
- [Unit Test](#unit-test)
11+
- [Integration Test](#integration-test)
1112
- [Add a new resource](#add-a-new-resource)
1213

1314

1415
## Testing
1516

16-
**Note:** Integration tests require a test project.
17-
18-
```
17+
### Unit Test
18+
```bash
1919
# Unit tests
2020
make test
21+
```
2122

22-
# Integration tests (interacts with real APIs)
23-
gcloud auth application-default login
24-
export TEST_PROJECT=my-project-id
25-
export TEST_CREDENTIALS=~/.config/gcloud/application_default_credentials.json
26-
make test-integration
23+
### Integration Test
2724

28-
# Specific integration test
29-
make build
30-
go test -v -run=<test name or prefix> ./tfplan2cai/test
31-
```
25+
**Note:** Integration tests require a test project.
3226

33-
### Docker
3427
It is better to run the integration tests inside a Docker container to match the CI/CD pipeline.
35-
First, build the Docker container:
3628

37-
```
38-
make build-docker
39-
```
29+
The integration test installs the provider binary and creates a terraform dev override file tf-dev-override.tfrc. [Dev override](https://googlecloudplatform.github.io/magic-modules/develop/run-tests/#optional-test-manually) is to make sure the test runs against the provider version specified in the go module. You can place your own config within tf-dev-override.tfrc. If the file already exists, it will not be overwritten.
4030

41-
For obtaining a credentials file, run
42-
```
43-
gcloud auth application-default login
44-
```
45-
46-
Start the Docker container:
31+
```bash
32+
# Integration tests (interacts with real APIs)
4733

48-
```
34+
# obtain credentials
35+
gcloud auth application-default login
4936
export TEST_PROJECT=my-project-id
50-
export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/credentials.json
37+
export TEST_CREDENTIALS=~/.config/gcloud/application_default_credentials.json
38+
39+
# Spin up a docker container to run tests.
5140
make run-docker
52-
# install terraform, check Makefile for the default version of terraform.
53-
/install-terraform.sh
54-
```
5541

56-
Finally, run the integration tests inside the container:
57-
```
42+
# Inside the docker container,
43+
# run this to setup dev override and run integration tests.
5844
make test-integration
5945
```
6046

47+
6148
### Add a new resource
6249

6350
See [Add a new resource](./add_new_resource.md)

install-terraform.sh

-16
This file was deleted.

tf-dev-override.sh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
set -ex
3+
4+
TF_CONFIG_FILE="tf-dev-override.tfrc"
5+
6+
# required for go install terraform-provider-google-beta
7+
go mod download github.com/hashicorp/terraform-plugin-mux
8+
9+
go install github.com/hashicorp/terraform-provider-google-beta
10+
11+
# create terraform configuration file
12+
if ! [ -f $TF_CONFIG_FILE ];then
13+
cat <<EOF > $TF_CONFIG_FILE
14+
provider_installation {
15+
# Developer overrides will stop Terraform from downloading the listed
16+
# providers their origin provider registries.
17+
dev_overrides {
18+
"hashicorp/google-beta" = "$GOPATH/bin"
19+
}
20+
# For all other providers, install them directly from their origin provider
21+
# registries as normal. If you omit this, Terraform will _only_ use
22+
# the dev_overrides block, and so no other providers will be available.
23+
# Without this, show "Failed to query available provider packages"
24+
# at terraform init
25+
direct{}
26+
}
27+
EOF
28+
fi

0 commit comments

Comments
 (0)