Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rm tf json files #1677

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .ci/cloudbuild-tests-integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ steps:
args:
[
"-c",
"mv /terraform/$_TERRAFORM_VERSION /bin/terraform && /usr/bin/make test-integration",
"/usr/bin/make test-integration",
]
env:
- TEST_PROJECT=$_TEST_PROJECT
- TEST_FOLDER_ID=$_TEST_FOLDER
- TEST_ANCESTRY=$_TEST_ANCESTRY
- TEST_ORG_ID=$_TEST_ORG
- TERRAFORM_VERSION=$_TERRAFORM_VERSION
tags:
- "ci"
- "integration"
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ examples/*.tfstate*
# binary
./cmd
bin/

# terraform config file for integration test
tf-dev-override.tfrc
11 changes: 0 additions & 11 deletions Dockerfile

This file was deleted.

25 changes: 16 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
TERRAFORM_VERSION=0.12.31

build_dir=./bin
TF_CONFIG_FILE=tf-dev-override.tfrc

build:
GO111MODULE=on go build -o ${build_dir}/tfplan2cai ./cmd/tfplan2cai

test:
GO111MODULE=on go test -short ./...
go version
terraform --version
./tf-dev-override.sh
TF_CLI_CONFIG_FILE="${PWD}/${TF_CONFIG_FILE}" GO111MODULE=on go test -short ./...

test-integration:
go version
terraform --version
go test -run=CLI ./...
./tf-dev-override.sh
TF_CLI_CONFIG_FILE="${PWD}/${TF_CONFIG_FILE}" go test -run=CLI ./...

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

build-docker:
docker build --build-arg TERRAFORM_VERSION=$(TERRAFORM_VERSION) -f ./Dockerfile -t terraform-google-conversion .

run-docker:
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;
docker run -it \
-v `pwd`:/terraform-google-conversion \
-v ${GOOGLE_APPLICATION_CREDENTIALS}:/terraform-google-conversion/credentials.json \
-w /terraform-google-conversion \
--entrypoint=/bin/bash \
--env TEST_PROJECT=${PROJECT_ID} \
--env GOOGLE_APPLICATION_CREDENTIALS=/terraform-google-conversion/credentials.json \
gcr.io/graphite-docker-images/go-plus;

release:
./release.sh ${VERSION}

.PHONY: test test-integration test-go-licenses build-docker run-docker release
.PHONY: build test test-integration test-go-licenses run-docker release
49 changes: 18 additions & 31 deletions docs/contributing/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,44 @@ If you want to contribute to Terraform Google Conversion, check out the [contrib
- [Contributing](#contributing)
- [Table of Contents](#table-of-contents)
- [Testing](#testing)
- [Docker](#docker)
- [Unit Test](#unit-test)
- [Integration Test](#integration-test)
- [Add a new resource](#add-a-new-resource)


## Testing

**Note:** Integration tests require a test project.

```
### Unit Test
```bash
# Unit tests
make test
```

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

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

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

```
make build-docker
```
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.

For obtaining a credentials file, run
```
gcloud auth application-default login
```

Start the Docker container:
```bash
# Integration tests (interacts with real APIs)

```
# obtain credentials
gcloud auth application-default login
export TEST_PROJECT=my-project-id
export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/credentials.json
export TEST_CREDENTIALS=~/.config/gcloud/application_default_credentials.json

# Spin up a docker container to run tests.
make run-docker
# install terraform, check Makefile for the default version of terraform.
/install-terraform.sh
```

Finally, run the integration tests inside the container:
```
# Inside the docker container,
# run this to setup dev override and run integration tests.
make test-integration
```


### Add a new resource

See [Add a new resource](./add_new_resource.md)
16 changes: 0 additions & 16 deletions install-terraform.sh

This file was deleted.

37 changes: 37 additions & 0 deletions tf-dev-override.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
set -ex

TF_CONFIG_FILE="tf-dev-override.tfrc"

go clean --modcache
go list -json -m github.com/hashicorp/terraform-provider-google-beta
REPLACE_DIR=`go list -json -m github.com/hashicorp/terraform-provider-google-beta | jq -r '.Dir // empty'`
VERSION=`go list -json -m github.com/hashicorp/terraform-provider-google-beta | jq -r .Version`

if [ ! -z "$REPLACE_DIR" ]
then
pushd $REPLACE_DIR
go install
popd
else
go install github.com/hashicorp/terraform-provider-google-beta@$VERSION
fi

# create terraform configuration file
if ! [ -f $TF_CONFIG_FILE ];then
cat <<EOF > $TF_CONFIG_FILE
provider_installation {
# Developer overrides will stop Terraform from downloading the listed
# providers their origin provider registries.
dev_overrides {
"hashicorp/google-beta" = "$GOPATH/bin"
}
# For all other providers, install them directly from their origin provider
# registries as normal. If you omit this, Terraform will _only_ use
# the dev_overrides block, and so no other providers will be available.
# Without this, show "Failed to query available provider packages"
# at terraform init
direct{}
}
EOF
fi
2 changes: 1 addition & 1 deletion tfplan2cai/test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestCLI(t *testing.T) {
// Test both offline and online mode.
for _, offline := range []bool{true, false} {
offline := offline
t.Run(fmt.Sprintf("v=0.12/tf=%s/offline=%t", c.name, offline), func(t *testing.T) {
t.Run(fmt.Sprintf("tf=%s/offline=%t", c.name, offline), func(t *testing.T) {
t.Parallel()
// Create a temporary directory for running terraform.
dir, err := os.MkdirTemp(tmpDir, "terraform")
Expand Down
6 changes: 5 additions & 1 deletion tfplan2cai/test/iam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ func TestIAMFetchFullResource(t *testing.T) {
}
defer os.RemoveAll(dir)

generateTestFiles(t, "../testdata/templates", dir, c.name+".tfplan.json")
generateTestFiles(t, "../testdata/templates", dir, c.name+".tf")

// Run terraform init and terraform apply to generate tfplan.json files
terraformWorkflow(t, dir, c.name)

path := filepath.Join(dir, c.name+".tfplan.json")

data, err := os.ReadFile(path)
Expand Down
3 changes: 0 additions & 3 deletions tfplan2cai/test/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ var (
// configs. It contains Google API resources that are expected to be returned
// after converting the terraform plan.
type testData struct {
// is not nil - Terraform 12 version used
TFVersion string
// provider "google"
Provider map[string]string
Project map[string]string
Expand Down Expand Up @@ -63,7 +61,6 @@ func init() {
//As time is not information in terraform resource data, time is fixed for testing purposes
fixedTime := time.Date(2021, time.April, 14, 15, 16, 17, 0, time.UTC)
data = &testData{
TFVersion: "0.12",
Provider: map[string]string{
"version": providerVersion,
"project": project,
Expand Down
Loading