Repository navigation
356 lines (328 loc) · 15.4 KB
/
Copy pathtests-integration.yaml
File metadata and controls
356 lines (328 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
name: Tests | Integration
on:
pull_request:
workflow_dispatch:
# Called by the release workflow, to test the very package it is about to publish
workflow_call:
inputs:
dist-artifact:
description: Name of an artifact holding the built distributables. When given, the wheel in it is tested instead of the checkout.
type: string
default: ""
expected-version:
description: Version the installed cli must report. Only checked when dist-artifact is given.
type: string
default: ""
# Used by the ref-arch test
secrets:
AWS_ACCESS_KEY_ID:
required: true
AWS_SECRET_ACCESS_KEY:
required: true
AWS_DEVSTG_ACCOUNT_ID:
required: true
AWS_SECURITY_ACCOUNT_ID:
required: true
permissions:
contents: read
jobs:
# The first entry is the required check, so its name must stay exactly `integration_tests`.
# The others check compatibility: older and newer tofu/terraform releases, and macOS, which most
# of the team uses (BSD userland, /var -> /private/var symlinks). Public repo: standard runners
# cost nothing, but macOS ones are slower and capped at 5 concurrent jobs across the org.
integration_tests:
name: ${{ matrix.name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- name: integration_tests
os: ubuntu-latest
opentofu: 1.13.2
terraform: 1.9.8
- name: integration_tests_compat (ubuntu, older tofu and terraform)
os: ubuntu-latest
opentofu: 1.10.10
terraform: 1.12.2
- name: integration_tests_compat (ubuntu, latest tofu and terraform)
os: ubuntu-latest
opentofu: 1.13.9
terraform: 1.17.2
- name: integration_tests_compat (macos, latest tofu and terraform)
os: macos-latest
opentofu: 1.13.9
terraform: 1.17.2
env:
TERRAFORM_VERSION: ${{ matrix.terraform }}
OPENTOFU_VERSION: ${{ matrix.opentofu }}
BATS_LIB_PATH: /usr/local/lib/bats
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-python@v7
with:
python-version: '3.13'
# The bats tests drive the installed command
- uses: ./.github/actions/install-leverage
with:
dist-artifact: ${{ inputs.dist-artifact }}
expected-version: ${{ inputs.expected-version }}
- name: Install bats
run: |
echo "[INFO] Installing bats and its libraries"
git clone --depth 1 https://github.com/bats-core/bats-core.git /tmp/bats-core
sudo /tmp/bats-core/install.sh /usr/local
sudo mkdir -p "${BATS_LIB_PATH}"
sudo git clone --depth 1 https://github.com/bats-core/bats-support.git "${BATS_LIB_PATH}/bats-support"
sudo git clone --depth 1 https://github.com/bats-core/bats-assert.git "${BATS_LIB_PATH}/bats-assert"
- name: Install terraform and tofu
run: |
set -euo pipefail
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$(uname -m)" in
x86_64) arch=amd64 ;;
arm64|aarch64) arch=arm64 ;;
*) echo "[ERROR] Unsupported architecture $(uname -m)"; exit 1 ;;
esac
echo "[INFO] Installing terraform ${TERRAFORM_VERSION} and tofu ${OPENTOFU_VERSION} for ${os}_${arch}"
curl -fsSL -o /tmp/terraform.zip \
"https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_${os}_${arch}.zip"
curl -fsSL -o /tmp/tofu.zip \
"https://github.com/opentofu/opentofu/releases/download/v${OPENTOFU_VERSION}/tofu_${OPENTOFU_VERSION}_${os}_${arch}.zip"
sudo unzip -q -o /tmp/terraform.zip terraform -d /usr/local/bin
sudo unzip -q -o /tmp/tofu.zip tofu -d /usr/local/bin
terraform version
tofu version
- name: run_integration_tests
run: |
echo "[INFO] Running integration tests"
make test-int
shell: bash
# The ref-arch test applies real infrastructure in AWS, so on pull requests it only runs when it
# can test something: the cli, or the way it is installed, changed and the AWS credentials are
# available (they are not to pull requests from forks, nor to Dependabot's).
# It always runs when dispatched by hand or called by the release workflow.
# This is decided in a job, rather than with `paths` under `on:`, so the workflow always runs and
# integration_tests, a required check, never stays pending.
refarch_changes:
runs-on: ubuntu-latest
permissions:
pull-requests: read
outputs:
run: ${{ steps.decide.outputs.run }}
steps:
- name: Decide whether to run the ref-arch test
id: decide
env:
GH_TOKEN: ${{ github.token }}
EVENT_NAME: ${{ github.event_name }}
REPO: ${{ github.repository }}
PR_NUMBER: ${{ github.event.pull_request.number }}
HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
HAS_AWS_CREDENTIALS: ${{ secrets.AWS_ACCESS_KEY_ID != '' }}
run: |
set -euo pipefail
run=true
if [[ "${EVENT_NAME}" == "pull_request" ]]; then
if [[ "${HEAD_REPO}" != "${REPO}" ]]; then
echo "::notice::Skipping the ref-arch test: pull requests from forks have no access to the AWS credentials"
run=false
elif [[ "${HAS_AWS_CREDENTIALS}" != "true" ]]; then
echo "::notice::Skipping the ref-arch test: no AWS credentials available to this run"
run=false
else
changed_files="$(gh api --paginate "repos/${REPO}/pulls/${PR_NUMBER}/files" --jq '.[].filename')"
relevant='^(leverage/|pyproject\.toml$|poetry\.lock$|\.github/workflows/tests-integration\.yaml$|\.github/actions/)'
if ! grep -qE "${relevant}" <<< "${changed_files}"; then
echo "::notice::Skipping the ref-arch test: no changes to the cli nor to how it is installed"
run=false
fi
fi
fi
echo "[INFO] Run the ref-arch test: ${run}"
echo "run=${run}" >> "${GITHUB_OUTPUT}"
# Tests the cli end to end against the reference architecture: creates a project, then applies
# and destroys a layer of le-tf-infra-aws in the apps-devstg account.
integration_tests_cli_refarch:
needs: refarch_changes
if: needs.refarch_changes.outputs.run == 'true'
runs-on: ubuntu-latest
timeout-minutes: 30
# Every run applies and destroys the same layer, with the same resource names, in the same
# account, so no two of them may overlap: neither the jobs of one run (max-parallel below) nor
# those of different runs (pull requests, manual and release runs). A run never cancels
# another's round trip, which would leave its resources behind. GitHub keeps only one job
# pending per group, so a third one arriving cancels the pending one, which can be re-run.
concurrency:
group: refarch-cli-test-layer
cancel-in-progress: false
env:
# The version the reference architecture runs
OPENTOFU_VERSION: 1.13.2
LEVERAGE_INTERACTIVE: 0
defaults:
run:
shell: bash
strategy:
# Every job applies and destroys the same layer in the same account, so they must not overlap
max-parallel: 1
matrix:
# The oldest and newest supported versions; the unit tests cover every one of them
python-version: ['3.11', '3.15']
steps:
- uses: actions/checkout@v7
with:
persist-credentials: false
- uses: actions/setup-python@v7
with:
python-version: ${{ matrix.python-version }}
- uses: ./.github/actions/install-leverage
with:
dist-artifact: ${{ inputs.dist-artifact }}
expected-version: ${{ inputs.expected-version }}
# Without the wrapper, so tofu exit codes and output reach the cli untouched
- uses: opentofu/setup-opentofu@a1320f892987e89d278cc92dc5adc984fb93aca4 # v2.0.2
with:
tofu_version: ${{ env.OPENTOFU_VERSION }}
tofu_wrapper: false
- name: Project Init
run: |
set -euo pipefail
mkdir -p ../theadamproject
cd ../theadamproject
leverage project init
[[ -f project.yaml ]] || { echo "::error::project init did not create project.yaml"; exit 1; }
- name: Set project file and create
working-directory: ../theadamproject
run: |
set -euo pipefail
sed -i 's/<project name>/the-adam-project/' project.yaml
sed -i 's/<short project name>/bb/' project.yaml
sed -i 's/<aws+management@example\.com>/[email protected]/' project.yaml
sed -i 's/<aws+security@example\.com>/[email protected]/' project.yaml
sed -i 's/<aws+shared@example\.com>/[email protected]/' project.yaml
sed -i 's/<User>/bb/; s/<Name>/user/; s/<user\.name@example\.com>/[email protected]/' project.yaml
if grep -n '<[^>]*>' project.yaml; then
echo "::error::project.yaml still has unresolved template placeholders"
exit 1
fi
leverage project create
for account in config management security shared; do
[[ -d "${account}" ]] || { echo "::error::project create did not create ${account}"; exit 1; }
done
- name: Set up credentials
env:
DEPLOYMASTER_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
DEPLOYMASTER_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
DEVSTG_ACCOUNT_ID: ${{ secrets.AWS_DEVSTG_ACCOUNT_ID }}
SECURITY_ACCOUNT_ID: ${{ secrets.AWS_SECURITY_ACCOUNT_ID }}
run: |
set -euo pipefail
aws configure set aws_access_key_id "${DEPLOYMASTER_ACCESS_KEY_ID}" --profile bb-deploymaster
aws configure set aws_secret_access_key "${DEPLOYMASTER_SECRET_ACCESS_KEY}" --profile bb-deploymaster
aws configure set region us-east-1 --profile bb-apps-devstg-devops
aws configure set output json --profile bb-apps-devstg-devops
aws configure set role_arn "arn:aws:iam::${DEVSTG_ACCOUNT_ID}:role/DeployMaster" --profile bb-apps-devstg-devops
aws configure set source_profile bb-deploymaster --profile bb-apps-devstg-devops
aws configure set region us-east-1 --profile bb-security-oaar
aws configure set output json --profile bb-security-oaar
aws configure set role_arn "arn:aws:iam::${SECURITY_ACCOUNT_ID}:role/DeployMaster" --profile bb-security-oaar
aws configure set source_profile bb-deploymaster --profile bb-security-oaar
# The cli reads the credentials of project bb from ~/.aws/bb
mkdir -p ~/.aws/bb
cp ~/.aws/credentials ~/.aws/config ~/.aws/bb/
- name: Configure Reference Architecture
working-directory: ../theadamproject
env:
SECURITY_ACCOUNT_ID: ${{ secrets.AWS_SECURITY_ACCOUNT_ID }}
run: |
set -euo pipefail
cat << EOF > ./config/common.tfvars
project = "bb"
project_long = "binbash"
region_primary = "us-east-1"
region_secondary = "us-east-2"
vault_address = "vault_trash"
vault_token = "vault_trash"
sso_region = "us-east-1"
sso_enabled = false
sso_start_url = "sso_trash"
accounts = {
security = {
id = ${SECURITY_ACCOUNT_ID}
}
}
EOF
sed -i "s/^\(MFA_ENABLED=\)true/\1false/" build.env
- name: Test tofu
working-directory: ../theadamproject/security/us-east-1/base-tf-backend
run: leverage tofu init --skip-validation
- name: Test AWS
working-directory: ../theadamproject/security/us-east-1/base-tf-backend
env:
DEVSTG_ACCOUNT_ID: ${{ secrets.AWS_DEVSTG_ACCOUNT_ID }}
run: |
set -euo pipefail
account="$(leverage aws sts get-caller-identity --profile bb-apps-devstg-devops --query Account --output text)"
[[ "${account}" == "${DEVSTG_ACCOUNT_ID}" ]] || { echo "::error::the profile did not assume a role in the apps-devstg account"; exit 1; }
- name: Clone Testing Reference Architecture repo
run: git clone --depth 1 https://github.com/binbashar/le-tf-infra-aws.git ../theblairwitchproject
- name: Configure Testing Reference Architecture
working-directory: ../theblairwitchproject
env:
SECURITY_ACCOUNT_ID: ${{ secrets.AWS_SECURITY_ACCOUNT_ID }}
run: |
set -euo pipefail
cat << EOF > ./config/common.tfvars
project = "bb"
project_long = "binbash"
region_primary = "us-east-1"
region_secondary = "us-east-2"
vault_address = "vault_trash"
vault_token = "vault_trash"
sso_region = "us-east-1"
sso_enabled = false
sso_start_url = "sso_trash"
accounts = {
security = {
id = ${SECURITY_ACCOUNT_ID}
}
}
EOF
sed -i "s/^\(MFA_ENABLED=\)true/\1false/" build.env
# The reference architecture keeps cli-test-layer unprovisioned (its `provisioned` variable
# defaults to false), so every command below passes -var=provisioned=true: the test creates
# the layer's resources and destroys them within this run.
- name: Init the ref-arch layers
working-directory: ../theblairwitchproject/apps-devstg/global
run: leverage tofu init --layers cli-test-layer,base-identities
# Only an empty layer may be applied and destroyed. Resources that already exist belong to
# someone else, e.g. a /tofu apply test in le-tf-infra-aws, and must not be destroyed.
- name: Check the test layer is empty
id: layer_empty
working-directory: ../theblairwitchproject/apps-devstg/global
run: |
set -euo pipefail
rc=0; leverage tofu plan -detailed-exitcode -var=provisioned=true --layers cli-test-layer || rc=$?
if [[ ${rc} -eq 0 ]]; then
echo "::error::cli-test-layer already has its resources (provisioned by le-tf-infra-aws, or left over by a failed run), refusing to apply and destroy it"
exit 1
fi
[[ ${rc} -eq 2 ]] || { echo "::error::plan failed (rc=${rc})"; exit 1; }
- name: Apply the test layer
working-directory: ../theblairwitchproject/apps-devstg/global
run: leverage tofu apply -auto-approve -var=provisioned=true --layers cli-test-layer
- name: Check the apply converged
working-directory: ../theblairwitchproject/apps-devstg/global
run: |
set -euo pipefail
rc=0; leverage tofu plan -detailed-exitcode -var=provisioned=true --layers cli-test-layer || rc=$?
[[ ${rc} -eq 0 ]] || { echo "::error::post-apply plan not empty (rc=${rc})"; exit 1; }
# Cleans up whatever this run created, even if a previous step failed or the run was cancelled.
# Never runs when the layer was not empty, so it only destroys what this run created.
- name: Destroy the test layer
if: always() && steps.layer_empty.outcome == 'success'
working-directory: ../theblairwitchproject/apps-devstg/global
run: leverage tofu destroy -auto-approve -var=provisioned=true --layers cli-test-layer