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 base linter test #13

Merged
merged 1 commit into from Jul 13, 2022
Merged

Conversation

lonelyCZ
Copy link
Contributor

@lonelyCZ lonelyCZ commented Jul 7, 2022

Signed-off-by: lonelyCZ 531187475@qq.com

Fixes #2

@lonelyCZ
Copy link
Contributor Author

lonelyCZ commented Jul 7, 2022

Do we need to check terraform and terragrunt code format by terraform fmt and terragrunt hclfmt?

/assign @irbekrm

@irbekrm
Copy link
Contributor

irbekrm commented Jul 8, 2022

Thank you for the PR @lonelyCZ !

Do we need to check terraform and terragrunt code format by terraform fmt and terragrunt hclfmt?

That's a good question. I see that this PR includes some changes from terraform fmt and terraform validate would not catch that.
I think that the test should not itself make changes using terraform fmt, but perhaps the test could check if there are no changes if terraform fmt is run? i.e by running the terraform fmt -diff -recursive and checking that there is no diff output. This would be similar to what we do in cert-manager https://github.com/cert-manager/cert-manager/blob/master/hack/verify-deps.sh#L65

What do you think?

Otherwise this looks good, next thing could be to add a CI test that runs this test #3

@lonelyCZ
Copy link
Contributor Author

The test is below

[root@master68 testing-addons]# scripts/verify-staticcheck.sh
cm-vault/common.tfvars
--- old/cm-vault/common.tfvars
+++ new/cm-vault/common.tfvars
@@ -1,3 +1,3 @@

 # Path to the kubeconfig file to use for connecting kubernetes cluster. Default is "~/.kube/config"
-kubeconfig_path="~/.kube/config"
+kubeconfig_path = "~/.kube/config"
cm-vault/vault-install/main.tf
--- old/cm-vault/vault-install/main.tf
+++ new/cm-vault/vault-install/main.tf
@@ -35,10 +35,10 @@
 # ready https://github.com/helm/helm/pull/10920
 resource "null_resource" "wait_for_vault" {
   provisioner "local-exec" {
-   environment = {
-    KUBECONFIG = pathexpand(var.kubeconfig_path)
-   }
-   command = "kubectl wait --for condition=Ready=true pod/vault-0 -n vault --timeout 300s"
+    environment = {
+      KUBECONFIG = pathexpand(var.kubeconfig_path)
+    }
+    command = "kubectl wait --for condition=Ready=true pod/vault-0 -n vault --timeout 300s"
   }
   depends_on = [
     helm_release.vault

Please run "terraform fmt -recursive" to format the codes.
[root@master68 testing-addons]# terraform fmt -recursive
cm-vault/common.tfvars
cm-vault/vault-install/main.tf
[root@master68 testing-addons]# scripts/verify-staticcheck.sh
ERRO[0000] 1 error occurred:
        * Invalid file format /root/lonelyCZ/testing-addons/cm-vault/vault-config/terragrunt.hcl

ERRO[0000] Unable to determine underlying exit code, so Terragrunt will exit with error code 1

Please run "terragrunt hclfmt" to format the codes.
[root@master68 testing-addons]# terragrunt hclfmt
INFO[0000] /root/lonelyCZ/testing-addons/cm-vault/vault-config/terragrunt.hcl was updated
[root@master68 testing-addons]# scripts/verify-staticcheck.sh
INFO[0000] The stack at /root/lonelyCZ/testing-addons will be processed in the following order for command init:
Group 1
- Module /root/lonelyCZ/testing-addons/cm-vault/cm-install
- Module /root/lonelyCZ/testing-addons/cm-vault/vault-install

Group 2
- Module /root/lonelyCZ/testing-addons/cm-vault/vault-config

Group 3
- Module /root/lonelyCZ/testing-addons/cm-vault/cm-config


Initializing the backend...

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/helm from the dependency lock file

Initializing provider plugins...
- Reusing previous version of hashicorp/helm from the dependency lock file
- Reusing previous version of hashicorp/null from the dependency lock file
- Using previously-installed hashicorp/helm v2.5.1

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
- Using previously-installed hashicorp/helm v2.5.1
- Using previously-installed hashicorp/null v3.1.1

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/vault from the dependency lock file
- Using previously-installed hashicorp/vault v3.5.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/kubernetes from the dependency lock file
- Using previously-installed hashicorp/kubernetes v2.11.0

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
INFO[0000] The stack at /root/lonelyCZ/testing-addons will be processed in the following order for command validate:
Group 1
- Module /root/lonelyCZ/testing-addons/cm-vault/cm-install
- Module /root/lonelyCZ/testing-addons/cm-vault/vault-install

Group 2
- Module /root/lonelyCZ/testing-addons/cm-vault/vault-config

Group 3
- Module /root/lonelyCZ/testing-addons/cm-vault/cm-config

Success! The configuration is valid.

Success! The configuration is valid.

Success! The configuration is valid.

Success! The configuration is valid.

Congratulations! All Terraform source files have passed staticcheck.

@lonelyCZ lonelyCZ removed their assignment Jul 11, 2022
@lonelyCZ
Copy link
Contributor Author

Thanks your suggestions, please review it again.

/assign @irbekrm

Signed-off-by: lonelyCZ <531187475@qq.com>
Copy link
Contributor

@irbekrm irbekrm left a comment

Choose a reason for hiding this comment

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

Thank you @lonelyCZ !

I've tested the script and saw that it now picks up incorrect formatting!

/lgtm

terragrunt run-all init

if terragrunt run-all validate; then
echo 'Congratulations! All Terraform source files have passed staticcheck.'
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for adding this! I think the positive success messages are nice to see 👍🏼

cd "${REPO_ROOT}"

# Check terraform codes format
if ! terraform fmt -recursive -diff -check; then
Copy link
Contributor

Choose a reason for hiding this comment

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

The -check flag is a good find 👍🏼

@jetstack-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: irbekrm, lonelyCZ

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

@jetstack-bot jetstack-bot merged commit a941344 into cert-manager:master Jul 13, 2022
@lonelyCZ lonelyCZ deleted the pr-base-test branch July 14, 2022 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add basic tests
3 participants