github.com/opentofu/opentofu@v1.7.1/Makefile (about)

     1  export PATH := $(abspath bin/):${PATH}
     2  
     3  # Dependency versions
     4  LICENSEI_VERSION = 0.9.0
     5  
     6  # generate runs `go generate` to build the dynamically generated
     7  # source files, except the protobuf stubs which are built instead with
     8  # "make protobuf".
     9  .PHONY: generate
    10  generate:
    11  	go generate ./...
    12  
    13  # We separate the protobuf generation because most development tasks on
    14  # OpenTofu do not involve changing protobuf files and protoc is not a
    15  # go-gettable dependency and so getting it installed can be inconvenient.
    16  #
    17  # If you are working on changes to protobuf interfaces, run this Makefile
    18  # target to be sure to regenerate all of the protobuf stubs using the expected
    19  # versions of protoc and the protoc Go plugins.
    20  .PHONY: protobuf
    21  protobuf:
    22  	go run ./tools/protobuf-compile .
    23  
    24  .PHONY: fmtcheck
    25  fmtcheck:
    26  	"$(CURDIR)/scripts/gofmtcheck.sh"
    27  
    28  .PHONY: importscheck
    29  importscheck:
    30  	"$(CURDIR)/scripts/goimportscheck.sh"
    31  
    32  .PHONY: staticcheck
    33  staticcheck:
    34  	"$(CURDIR)/scripts/staticcheck.sh"
    35  
    36  .PHONY: exhaustive
    37  exhaustive:
    38  	"$(CURDIR)/scripts/exhaustive.sh"
    39  
    40  # Run license check
    41  .PHONY: license-check
    42  license-check:
    43  	go mod vendor
    44  	licensei cache --debug
    45  	licensei check --debug
    46  	licensei header --debug
    47  	rm -rf vendor/
    48  	git diff --exit-code
    49  
    50  # Install dependencies
    51  deps: bin/licensei
    52  deps:
    53  
    54  bin/licensei: bin/licensei-${LICENSEI_VERSION}
    55  	@ln -sf licensei-${LICENSEI_VERSION} bin/licensei
    56  bin/licensei-${LICENSEI_VERSION}:
    57  	@mkdir -p bin
    58  	curl -sfL https://git.io/licensei | bash -s v${LICENSEI_VERSION}
    59  	@mv bin/licensei $@
    60  
    61  # disallow any parallelism (-j) for Make. This is necessary since some
    62  # commands during the build process create temporary files that collide
    63  # under parallel conditions.
    64  .NOTPARALLEL:
    65  
    66  # Integration tests
    67  #
    68  
    69  .PHONY: list-integration-tests
    70  list-integration-tests: ## Lists tests.
    71  	@ grep -h -E '^(test|integration)-.+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[1m%-30s\033[0m %s\n", $$1, $$2}'
    72  
    73  # integration test with s3 as backend
    74  .PHONY: test-s3
    75  
    76  define infoTestS3
    77  Test requires:
    78  * AWS Credentials to be configured
    79    - https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html
    80    - https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html
    81  * IAM Permissions in us-west-2
    82    - S3 CRUD operations on buckets which will follow the pattern tofu-test-*
    83    - DynamoDB CRUD operations on a Table named dynamoTable
    84  
    85  endef
    86  
    87  test-s3: ## Runs tests with s3 bucket as the backend.
    88  	@ $(info $(infoTestS3))
    89  	@ TF_S3_TEST=1 go test ./internal/backend/remote-state/s3/...
    90  
    91  # integration test with gcp as backend
    92  .PHONY: test-gcp
    93  
    94  define infoTestGCP
    95  This test requires a working set of default credentials on the host.
    96  You can configure those by running `gcloud auth application-default login`.
    97  Additionally, you'll need to set the following environment variables:
    98  - GOOGLE_REGION to a valid GCP region, e.g. us-west1
    99  - GOOGLE_PROJECT to a valid GCP project ID
   100  
   101  Note: The GCP tests leave behind a keyring, because those can't easily be deleted. It will be reused across test runs.
   102  
   103  endef
   104  
   105  test-gcp: ## Runs tests with gcp as the backend.
   106  	@ $(info $(infoTestGCP))
   107  	@ TF_ACC=1 go test ./internal/backend/remote-state/gcs/...
   108  	@ echo "Note: this test has left behind a keyring, because those can't easily be deleted. It will be reused across test runs."
   109  
   110  # integration test with postgres as backend
   111  .PHONY: test-pg test-pg-clean
   112  
   113  PG_PORT := 5432
   114  
   115  define infoTestPg
   116  Test requires:
   117  * Docker: https://docs.docker.com/engine/install/
   118  * Port: $(PG_PORT)
   119  
   120  endef
   121  
   122  test-pg: ## Runs tests with local Postgres instance as the backend.
   123  	@ $(info $(infoTestPg))
   124  	@ echo "Starting database"
   125  	@ make test-pg-clean
   126  	@ docker run --rm -d --name tofu-pg \
   127          -p $(PG_PORT):5432 \
   128          -e POSTGRES_PASSWORD=tofu \
   129          -e POSTGRES_USER=tofu \
   130          postgres:16-alpine3.17 1> /dev/null
   131  	@ docker exec tofu-pg /bin/bash -c 'until psql -U tofu -c "\q" 2> /dev/null; do echo "Database is getting ready, waiting"; sleep 1; done'
   132  	@ DATABASE_URL="postgres://tofu:tofu@localhost:$(PG_PORT)/tofu?sslmode=disable" \
   133   		TF_PG_TEST=1 go test ./internal/backend/remote-state/pg/...
   134  
   135  test-pg-clean: ## Cleans environment after `test-pg`.
   136  	@ docker rm -f tofu-pg 2> /dev/null
   137  
   138  # integration test with Azure as backend
   139  .PHONY: test-azure
   140  
   141  test-azure: ## Directs the developer to follow a runbook describing how to run Azure integration tests.
   142  	@ echo "To run Azure integration tests, please follow the runbook in internal/backend/remote-state/azure/README.md".
   143  	@ exit 1 # don't want the user to miss this
   144  
   145  # integration test with Consul as backend
   146  .PHONY: test-consul test-consul-clean
   147  
   148  define infoTestConsul
   149  Test requires:
   150  * Docker: https://docs.docker.com/engine/install/
   151  
   152  endef
   153  
   154  GO_VER := `cat $(PWD)/.go-version`
   155  
   156  test-consul: ## Runs tests using in docker container using Consul as the backend.
   157  	@ $(info $(infoTestConsul))
   158  	@ echo "Build docker image with Consul and Go v$(GO_VER)"
   159  	@ cd ./internal/backend/remote-state/consul &&\
   160    		docker build --build-arg="GO_VERSION=${GO_VER}" -t tofu-consul --progress=plain . &> /dev/null
   161  	@ echo "Run tests"
   162  	@ docker run --rm --name tofu-consul -v $(PWD):/app -e TF_CONSUL_TEST=1 -t tofu-consul \
   163   		test ./internal/backend/remote-state/consul/...
   164  
   165  test-consul-clean: ## Cleans environment after `test-consul`.
   166  	@ docker rmi -f tofu-consul:latest
   167  
   168  # integration test with kubernetes as backend
   169  .PHONY: test-kubernetes test-kubernetes-clean
   170  
   171  define infoTestK8s
   172  Test requires:
   173  * Git client
   174  * Docker: https://docs.docker.com/engine/install/
   175  Note! Please make sure that the docker configurations satisfy requirements: https://kind.sigs.k8s.io/docs/user/quick-start#settings-for-docker-desktop
   176  
   177  endef
   178  
   179  KIND_VERSION := v0.20.0
   180  
   181  test-kubernetes: test-kubernetes-clean ## Runs tests with a local kubernetes cluster as the backend.
   182  	@ $(info $(infoTestK8s))
   183  	@ echo "Installing KinD $(KIND_VERSION): https://kind.sigs.k8s.io/docs/user/quick-start/#installing-with-make"
   184  	@ git clone -c advice.detachedHead=false -q https://github.com/kubernetes-sigs/kind -b $(KIND_VERSION) /tmp/kind-repo 1> /dev/null && \
   185   		 cd /tmp/kind-repo &&\
   186   		 make build 1> /dev/null &&\
   187   		 mv ./bin/kind /tmp/tofuk8s &&\
   188   		 cd .. && rm -rf kind-repo
   189  	@ echo "Provisioning a cluster"
   190  	@ /tmp/tofuk8s -q create cluster --name tofu-kubernetes
   191  	@ /tmp/tofuk8s -q export kubeconfig --name tofu-kubernetes --kubeconfig /tmp/tofu-k8s-config
   192  	@ echo "Running tests"
   193  	@ KUBE_CONFIG_PATHS=/tmp/tofu-k8s-config TF_K8S_TEST=1 go test ./internal/backend/remote-state/kubernetes/...
   194  	@ echo "Deleting provisioned cluster"
   195  	@ make test-kubernetes-clean
   196  
   197  test-kubernetes-clean: ## Cleans environment after `test-kubernetes`.
   198  	@ test -s /tmp/tofu-k8s-config && rm /tmp/tofu-k8s-config || echo "" > /dev/null
   199  	@ test -s /tmp/tofuk8s && (/tmp/tofuk8s -q delete cluster --name tofu-kubernetes && rm /tmp/tofuk8s) || echo "" > /dev/null
   200  
   201  .PHONY:
   202  test-linux-install-instructions:
   203  	@cd "$(CURDIR)/website/docs/intro/install" && ./test-install-instructions.sh
   204  
   205  .PHONY:
   206  integration-tests: test-s3 test-pg test-consul test-kubernetes integration-tests-clean ## Runs all integration tests test.
   207  
   208  .PHONY:
   209  integration-tests-clean: test-pg-clean test-consul-clean test-kubernetes-clean ## Cleans environment after all integration tests.