|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2025 Google LLC. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http=//www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Set SCRIPT_DIR to the current directory of this file. |
| 18 | +SCRIPT_DIR=$(cd -P "$(dirname "$0")" >/dev/null 2>&1 && pwd) |
| 19 | +SCRIPT_FILE="${SCRIPT_DIR}/$(basename "$0")" |
| 20 | + |
| 21 | +## |
| 22 | +## Local Development |
| 23 | +## |
| 24 | +## These functions should be used to run the local development process |
| 25 | +## |
| 26 | + |
| 27 | +## clean - Cleans the build output |
| 28 | +function clean() { |
| 29 | + if [[ -d '.tools' ]] ; then |
| 30 | + rm -rf .tools |
| 31 | + fi |
| 32 | +} |
| 33 | + |
| 34 | +## build - Builds the project without running tests. |
| 35 | +function build() { |
| 36 | + go build ./... |
| 37 | +} |
| 38 | + |
| 39 | +## test - Runs local unit tests. |
| 40 | +function test() { |
| 41 | + go test -v -race -cover -short |
| 42 | +} |
| 43 | + |
| 44 | +## e2e - Runs end-to-end integration tests. |
| 45 | +function e2e() { |
| 46 | + if [[ ! -f .envrc ]] ; then |
| 47 | + write_e2e_env .envrc |
| 48 | + fi |
| 49 | + source .envrc |
| 50 | + e2e_ci |
| 51 | +} |
| 52 | + |
| 53 | +function e2e_ci() { |
| 54 | + go test -v -race -cover ./e2e_mysql_test.go ./e2e_postgres_test.go ./e2e_sqlserver_test.go | tee test_results.txt |
| 55 | +} |
| 56 | + |
| 57 | +## fix - Fixes code format. |
| 58 | +function fix() { |
| 59 | + go mod tidy |
| 60 | + go fmt ./... |
| 61 | +} |
| 62 | + |
| 63 | +## lint - runs the linters |
| 64 | +function lint() { |
| 65 | + # run golangci-lint |
| 66 | + mkdir -p "$SCRIPT_DIR/.tools" |
| 67 | + if [[ ! -f "$SCRIPT_DIR/.tools/golangci-lint" ]] ; then |
| 68 | + GOBIN= "$SCRIPT_DIR/.tools" go install github.com/golangci/golangci-lint/cmd/ [email protected] |
| 69 | + fi |
| 70 | + "$SCRIPT_DIR/.tools/golangci-lint" run --timeout 3m |
| 71 | + |
| 72 | + # Check the commit includes a go.mod that is fully |
| 73 | + # up to date. |
| 74 | + fix |
| 75 | + if [[ -d "$SCRIPT_DIR/.git" ]] ; then |
| 76 | + git diff --exit-code |
| 77 | + fi |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +# write_e2e_env - Loads secrets from the gcloud project and writes |
| 82 | +# them to target/e2e.env to run e2e tests. |
| 83 | +# |
| 84 | +function write_e2e_env(){ |
| 85 | + # All secrets used by the e2e tests in the form <env_name>=<secret_name> |
| 86 | + secret_vars=( |
| 87 | + MYSQL_CONNECTION_NAME=MYSQL_CONNECTION_NAME |
| 88 | + MYSQL_USER=MYSQL_USER |
| 89 | + MYSQL_USER_IAM=MYSQL_USER_IAM_GO |
| 90 | + MYSQL_PASS=MYSQL_PASS |
| 91 | + MYSQL_DB=MYSQL_DB |
| 92 | + MYSQL_MCP_CONNECTION_NAME=MYSQL_MCP_CONNECTION_NAME |
| 93 | + MYSQL_MCP_PASS=MYSQL_MCP_PASS |
| 94 | + POSTGRES_CONNECTION_NAME=POSTGRES_CONNECTION_NAME |
| 95 | + POSTGRES_USER=POSTGRES_USER |
| 96 | + POSTGRES_USER_IAM=POSTGRES_USER_IAM_GO |
| 97 | + POSTGRES_PASS=POSTGRES_PASS |
| 98 | + POSTGRES_DB=POSTGRES_DB |
| 99 | + POSTGRES_CAS_CONNECTION_NAME=POSTGRES_CAS_CONNECTION_NAME |
| 100 | + POSTGRES_CAS_PASS=POSTGRES_CAS_PASS |
| 101 | + POSTGRES_CUSTOMER_CAS_CONNECTION_NAME=POSTGRES_CUSTOMER_CAS_CONNECTION_NAME |
| 102 | + POSTGRES_CUSTOMER_CAS_PASS=POSTGRES_CUSTOMER_CAS_PASS |
| 103 | + POSTGRES_CUSTOMER_CAS_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_DOMAIN_NAME |
| 104 | + POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME |
| 105 | + POSTGRES_MCP_CONNECTION_NAME=POSTGRES_MCP_CONNECTION_NAME |
| 106 | + POSTGRES_MCP_PASS=POSTGRES_MCP_PASS |
| 107 | + SQLSERVER_CONNECTION_NAME=SQLSERVER_CONNECTION_NAME |
| 108 | + SQLSERVER_USER=SQLSERVER_USER |
| 109 | + SQLSERVER_PASS=SQLSERVER_PASS |
| 110 | + SQLSERVER_DB=SQLSERVER_DB |
| 111 | + QUOTA_PROJECT=QUOTA_PROJECT |
| 112 | + ) |
| 113 | + |
| 114 | + if [[ -z "$TEST_PROJECT" ]] ; then |
| 115 | + echo "Set TEST_PROJECT environment variable to the project containing" |
| 116 | + echo "the e2e test suite secrets." |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + |
| 120 | + echo "Getting test secrets from $TEST_PROJECT into $1" |
| 121 | + { |
| 122 | + for env_name in "${secret_vars[@]}" ; do |
| 123 | + env_var_name="${env_name%%=*}" |
| 124 | + secret_name="${env_name##*=}" |
| 125 | + set -x |
| 126 | + val=$(gcloud secrets versions access latest --project "$TEST_PROJECT" --secret="$secret_name") |
| 127 | + echo "export $env_var_name='$val'" |
| 128 | + done |
| 129 | + } > "$1" |
| 130 | + |
| 131 | +} |
| 132 | + |
| 133 | +## help - prints the help details |
| 134 | +## |
| 135 | +function help() { |
| 136 | + # This will print the comments beginning with ## above each function |
| 137 | + # in this file. |
| 138 | + |
| 139 | + echo "build.sh <command> <arguments>" |
| 140 | + echo |
| 141 | + echo "Commands to assist with local development and CI builds." |
| 142 | + echo |
| 143 | + echo "Commands:" |
| 144 | + echo |
| 145 | + grep -e '^##' "$SCRIPT_FILE" | sed -e 's/##/ /' |
| 146 | +} |
| 147 | + |
| 148 | +set -euo pipefail |
| 149 | + |
| 150 | +# Check CLI Arguments |
| 151 | +if [[ "$#" -lt 1 ]] ; then |
| 152 | + help |
| 153 | + exit 1 |
| 154 | +fi |
| 155 | + |
| 156 | +cd "$SCRIPT_DIR" |
| 157 | + |
| 158 | +"$@" |
| 159 | + |
0 commit comments