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

Add Terraform GKE and Helm modules tests with Terratest #1483

Merged
merged 10 commits into from
Sep 3, 2020

Conversation

aLekSer
Copy link
Collaborator

@aLekSer aLekSer commented Apr 20, 2020

Useful to check if Terraform configuration is valid.

What type of PR is this?
/kind feature

What this PR does / Why we need it:

Which issue(s) this PR fixes:

Closes #1227.

Special notes for your reviewer:
There is a need to make local run of the test in a way that root go.mod and go.sum does not get affected.
This Test framework also good at verification of different variables used by helm templates
https://github.com/gruntwork-io/terratest/blob/master/test/helm_basic_example_template_test.go and https://github.com/gruntwork-io/terratest-helm-testing-example .

@agones-bot
Copy link
Collaborator

Build Succeeded 👏

Build Id: 303de1e1-8da6-474e-93e7-2e248626d179

The following development artifacts have been built, and will exist for the next 30 days:

A preview of the website (the last 30 builds are retained):

To install this version:

  • git fetch https://github.com/GoogleCloudPlatform/agones.git pull/1483/head:pr_1483 && git checkout pr_1483
  • helm install ./install/helm/agones --namespace agones-system --name agones --set agones.image.tag=1.6.0-5e7e9b2

@aLekSer aLekSer changed the title Add Terraform and Helm tests with Terratest Add Terraform GKE and Helm modules tests with Terratest Apr 20, 2020
@aLekSer
Copy link
Collaborator Author

aLekSer commented Apr 20, 2020

Saving go test file from this PR in IDE, changes root go.mod file.
This is a side-effect of the IDE running go build on file save. I am using VS Code.
VS Code Issue name is Do not automatically update go.mod and go.sum files.
Using Vim does not have this side-effects.
It seems that changing go.mod persist only when gke_test.go file is opened at VS Code. So I assume this is a minor issue.
There is an option to change file extension from .go to avoid this, used this approach previously in ../test/sdk/restapi/http-api-test.go.nolint due to swagger-codegen issues.

@aLekSer aLekSer force-pushed the terraform/adding-tests branch 2 times, most recently from 823f136 to 2d75daa Compare April 20, 2020 18:49
@agones-bot
Copy link
Collaborator

Build Succeeded 👏

Build Id: f198327a-df67-4e8d-be5a-87dee0184fba

The following development artifacts have been built, and will exist for the next 30 days:

A preview of the website (the last 30 builds are retained):

To install this version:

  • git fetch https://github.com/GoogleCloudPlatform/agones.git pull/1483/head:pr_1483 && git checkout pr_1483
  • helm install ./install/helm/agones --namespace agones-system --name agones --set agones.image.tag=1.6.0-2d75daa

@aLekSer
Copy link
Collaborator Author

aLekSer commented Apr 20, 2020

Well it seems reasonable to split out terraform config files into a separate repo. This would speed up using all ./examples/terraform-submodules/ and would not clone the whole Agones repo. Terratest could also be moved there.

@aLekSer aLekSer force-pushed the terraform/adding-tests branch 2 times, most recently from 74e7506 to 7b317e6 Compare April 20, 2020 19:53
@aLekSer aLekSer marked this pull request as ready for review April 20, 2020 19:57
@aLekSer
Copy link
Collaborator Author

aLekSer commented Apr 20, 2020

This Terratest output (head and tail):

/.gocache:/root/.cache/go-build -e "KUBECONFIG=/root/.kube/config" -e "GO111MODULE=on" -w /go/src/agones.dev/agones  agones-build:e501e27b72 bash -c 'cd /go/src/agones.dev/agones/test/terraform && go test \                                                                                                                                                                                                         
        -timeout 1h -project agones-alexander '                                                                                                                                                            
TestTerraformGKEInstallConfig 2020-04-20T19:30:33Z retry.go:72: terraform [init -upgrade=false]
TestTerraformGKEInstallConfig 2020-04-20T19:30:33Z command.go:87: Running command terraform with args [init -upgrade=false]                                                                                
TestTerraformGKEInstallConfig 2020-04-20T19:30:33Z command.go:158: Initializing modules...                                                                                                                 
TestTerraformGKEInstallConfig 2020-04-20T19:30:33Z command.go:158:                                                                                                                                         
TestTerraformGKEInstallConfig 2020-04-20T19:30:33Z command.go:158: Initializing the backend...                                                                                                             
TestTerraformGKEInstallConfig 2020-04-20T19:30:33Z command.go:158:                                                                                                                                         
TestTerraformGKEInstallConfig 2020-04-20T19:30:33Z command.go:158: Initializing provider plugins...
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:158:
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:158: Terraform has been successfully initialized!                                                                                            
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:158:
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:158: You may now begin working with Terraform. Try running "terraform plan" to see                                                           
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:158: any changes that are required for your infrastructure. All Terraform commands                                                           
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:158: should now work.
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:87: Running command terraform with args [get -update]                                                                                        
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:158: - gke_cluster in ../../../install/terraform/modules/gke                                                                                 
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:158: - helm_agones in ../../../install/terraform/modules/helm                                                                                
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z retry.go:72: terraform [apply -input=false -auto-approve -var name=terratest-cluster -var values_file= -var project=agones-alexander -lock=false]       
TestTerraformGKEInstallConfig 2020-04-20T19:30:35Z command.go:87: Running command terraform with args [apply -input=false -auto-approve -var name=terratest-cluster -var values_file= -var project=agones-al
exander -lock=false]
TestTerraformGKEInstallConfig 2020-04-20T19:30:45Z command.go:158: module.gke_cluster.data.google_client_config.default: Refreshing state...                                                               
TestTerraformGKEInstallConfig 2020-04-20T19:30:49Z command.go:158: module.helm_agones.null_resource.helm_init: Creating...    
..
TestTerraformGKEInstallConfig 2020-04-20T19:39:45Z command.go:158:                                                                                                                                         
TestTerraformGKEInstallConfig 2020-04-20T19:39:45Z command.go:158: Destroy complete! Resources: 6 destroyed.                                                                                               
PASS                                                                                                                                                                                                       
ok      github.com/agones/agones/test/terraform 553.243s                                                                                                                                                   
by-it00060:build alexander.apalikov$  

@agones-bot
Copy link
Collaborator

Build Succeeded 👏

Build Id: 67e0ad44-ab32-4805-8a14-8ab1acd06768

The following development artifacts have been built, and will exist for the next 30 days:

A preview of the website (the last 30 builds are retained):

To install this version:

  • git fetch https://github.com/GoogleCloudPlatform/agones.git pull/1483/head:pr_1483 && git checkout pr_1483
  • helm install ./install/helm/agones --namespace agones-system --name agones --set agones.image.tag=1.6.0-74e7506

@agones-bot
Copy link
Collaborator

Build Succeeded 👏

Build Id: e70e5ce0-c911-4af7-b94a-aae5114b3bd0

The following development artifacts have been built, and will exist for the next 30 days:

A preview of the website (the last 30 builds are retained):

To install this version:

  • git fetch https://github.com/GoogleCloudPlatform/agones.git pull/1483/head:pr_1483 && git checkout pr_1483
  • helm install ./install/helm/agones --namespace agones-system --name agones --set agones.image.tag=1.6.0-7b317e6

@markmandel markmandel added area/operations Installation, updating, metrics etc area/tests Unit tests, e2e tests, anything to make sure things don't break labels Apr 23, 2020
Copy link
Member

@markmandel markmandel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really, really cool.

Do you envisage we should be doing this in our CI build pipeline?

(As per splitting out, maybe let's start a new ticket, work out a design of how we want to do that? Definitely not against it. Easy enough to configure a new Cloud Build setup, or maybe prow, also need to consider backward compatibility.)

func destroy(t *testing.T, options *terraform.Options) {
options.Targets = []string{"module.helm_agones.helm_release.agones"}
terraform.Destroy(t, options)
time.Sleep(30 * time.Second)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious - is there a way we could poll to see if this is complete, rather than waiting for a period of time? Waiting is probably pretty fragile.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gentle bump on this review.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning back to this as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the wait of kubectl services -n agones-system count would be 0 using terratest K8S module. Some updates to development .tf file was needed - creating kubeconfig file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no other options to poll the result or at least nothing else come to my mind. But previous approach build a "bridge" for creating more complex tests.

@aLekSer
Copy link
Collaborator Author

aLekSer commented May 12, 2020

go.mod was automatically changed by IDE after adding this test. Trying the idea to add same dependencies as in root go.mod into test/terraform one.

diff --git a/go.mod b/go.mod
index 0e80fa99..268cd898 100644
--- a/go.mod
+++ b/go.mod
@@ -3,50 +3,42 @@ module agones.dev/agones
 go 1.13

 require (
-       cloud.google.com/go v0.34.0
+       cloud.google.com/go v0.51.0
        contrib.go.opencensus.io/exporter/prometheus v0.1.0
        contrib.go.opencensus.io/exporter/stackdriver v0.8.0
        fortio.org/fortio v1.3.1
+       git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999 // indirect
        github.com/ahmetb/gen-crd-api-reference-docs v0.1.1
-       github.com/aws/aws-sdk-go v1.16.20 // indirect
        github.com/evanphx/json-patch v4.5.0+incompatible // indirect
        github.com/fsnotify/fsnotify v1.4.7
-       github.com/go-openapi/spec v0.19.0
-       github.com/gogo/protobuf v1.2.1 // indirect
+       github.com/go-openapi/spec v0.19.3

Seems to be go list related issue:
microsoft/vscode-go#2730 (comment)

@aLekSer
Copy link
Collaborator Author

aLekSer commented May 13, 2020

Trying the following solution to avoid updating root go.mod, add build tag// +build ignore: bazel-contrib/rules_go#2262 (comment)
Actually no luck with it.

@agones-bot
Copy link
Collaborator

Build Failed 😱

Build Id: e6ab66b7-591e-40dc-ae8d-2843676f3c82

To get permission to view the Cloud Build view, join the agones-discuss Google Group.

@agones-bot
Copy link
Collaborator

Build Failed 😱

Build Id: 1b828e64-96be-4fd0-972a-1164ce6fa673

To get permission to view the Cloud Build view, join the agones-discuss Google Group.

@aLekSer
Copy link
Collaborator Author

aLekSer commented May 13, 2020

make terraform-test run was successful:

--- PASS: TestTerraformGKEInstallConfig (627.42s)
    gke_test.go:33: Using project <projectName>
PASS
ok      terraform       627.463s

This PR is ready to review now.

Copy link
Member

@markmandel markmandel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, missed this on last review

@@ -84,3 +86,18 @@ gcloud-terraform-destroy-cluster: GCP_PROJECT ?= $(current_project)
gcloud-terraform-destroy-cluster:
$(DOCKER_RUN) bash -c 'cd $(mount_path)/build/terraform/gke && terraform destroy -var project=$(GCP_PROJECT) -auto-approve'

terraform-test: $(ensure-build-image)
ifndef GCP_PROJECT
$(eval GCP_PROJECT=$(shell sh -c "gcloud config get-value project 2> /dev/null"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be necessary if we put current_project back in.

@@ -15,17 +15,19 @@
# The GKE development cluster name
GCP_TF_CLUSTER_NAME ?= agones-tf-cluster

# the current project
current_project := $(shell $(DOCKER_RUN) bash -c "gcloud config get-value project 2> /dev/null")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised we're deleting current_project - this will break stuff 😢

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry for that, returning it back.

@aLekSer aLekSer force-pushed the terraform/adding-tests branch 2 times, most recently from f62841f to 34bcd8c Compare August 31, 2020 12:32
@agones-bot

This comment has been minimized.

This would get the necessary project name from docker run.
@agones-bot
Copy link
Collaborator

Build Succeeded 👏

Build Id: e5dc3a1d-4dde-41d9-912f-0309a5dae3bf

The following development artifacts have been built, and will exist for the next 30 days:

A preview of the website (the last 30 builds are retained):

To install this version:

  • git fetch https://github.com/googleforgames/agones.git pull/1483/head:pr_1483 && git checkout pr_1483
  • helm install ./install/helm/agones --namespace agones-system --name agones --set agones.image.tag=1.9.0-34bcd8c

@agones-bot
Copy link
Collaborator

Build Succeeded 👏

Build Id: 53305c71-862c-4ed3-811d-cde99c077dcc

The following development artifacts have been built, and will exist for the next 30 days:

A preview of the website (the last 30 builds are retained):

To install this version:

  • git fetch https://github.com/googleforgames/agones.git pull/1483/head:pr_1483 && git checkout pr_1483
  • helm install ./install/helm/agones --namespace agones-system --name agones --set agones.image.tag=1.9.0-960800b

@aLekSer
Copy link
Collaborator Author

aLekSer commented Sep 3, 2020

Fixed all comments, this PR is good to go now.

@agones-bot
Copy link
Collaborator

Build Succeeded 👏

Build Id: 70b0f1f0-5776-4095-aed8-98c738f1be08

The following development artifacts have been built, and will exist for the next 30 days:

A preview of the website (the last 30 builds are retained):

To install this version:

  • git fetch https://github.com/googleforgames/agones.git pull/1483/head:pr_1483 && git checkout pr_1483
  • helm install ./install/helm/agones --namespace agones-system --name agones --set agones.image.tag=1.9.0-ffc85ad

@google-oss-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aLekSer, markmandel

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-robot
Copy link

New changes are detected. LGTM label has been removed.

@agones-bot
Copy link
Collaborator

Build Succeeded 👏

Build Id: 33fe73e2-d74e-4149-bd59-a30ced240005

The following development artifacts have been built, and will exist for the next 30 days:

A preview of the website (the last 30 builds are retained):

To install this version:

  • git fetch https://github.com/googleforgames/agones.git pull/1483/head:pr_1483 && git checkout pr_1483
  • helm install ./install/helm/agones --namespace agones-system --name agones --set agones.image.tag=1.9.0-3c37f4d

@markmandel markmandel merged commit bbb8119 into googleforgames:master Sep 3, 2020
@markmandel markmandel added this to the 1.9.0 milestone Sep 3, 2020
ilkercelikyilmaz pushed a commit to ilkercelikyilmaz/agones that referenced this pull request Oct 23, 2020
…es#1483)

* Enhance tests, use kubectl module to verify svc

Co-authored-by: Mark Mandel <markmandel@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved area/operations Installation, updating, metrics etc area/tests Unit tests, e2e tests, anything to make sure things don't break cla: yes size/XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write Tests for Terraform configs
6 participants