Debian Codegraph @

launcher/image/test/scripts/test_os_config_os_policy.sh

raw
1 #!/bin/bash
2 set -euo pipefail
3
4 if $1 == 'debug'
5 then
6 echo 'Running OS Config OS Policy enabled test'
7 else
8 echo 'Running OS Config OS Policy disabled test'
9 fi
10
11 cat <<EOT >> shutdown-ospolicy.yaml
12 osPolicies:
13 - id: shutdown-policy
14 mode: ENFORCEMENT
15 resourceGroups:
16 - resources:
17 id: shutdown-vm
18 exec:
19 validate:
20 interpreter: SHELL
21 script: if true; then sudo shutdown now; else exit 101; fi
22 enforce:
23 interpreter: SHELL
24 script: exit 100
25 instanceFilter:
26 inclusionLabels:
27 - labels:
28 shutdown-label: $2
29 rollout:
30 disruptionBudget:
31 percent: 100
32 minWaitDuration: 1s
33 EOT
34
35 gcloud compute instances add-labels $2 --labels=shutdown-label=$2 --zone=$3 || true
36 GCLOUD_OUTPUT=$(gcloud compute os-config os-policy-assignments create shutdown-policy --location=$3 --file=shutdown-ospolicy.yaml | tail -1 || true)
37
38 if echo $GCLOUD_OUTPUT | grep -q 'Created OS policy assignment [shutdown-policy]'
39 then
40 GCLOUD_OUTPUT=$(gcloud compute instances describe $2 --zone=$3 --format="value(status)" || true)
41 else
42 echo 'TEST FAILED: OS policy assignment could not be created'
43 echo 'TEST FAILED.' > /workspace/status.txt
44 exit 1
45 fi
46
47 if $1 == 'debug'
48 then
49 if echo $GCLOUD_OUTPUT | grep -q 'TERMINATED'
50 then
51 echo 'Success: OS policy assignment stops the VM'
52 else
53 echo 'TEST FAILED: VM did not terminate'
54 echo 'TEST FAILED.' > /workspace/status.txt
55 fi
56 else
57 if echo $GCLOUD_OUTPUT | grep -q 'TERMINATED'
58 then
59 echo 'TEST FAILED: VM incorrectly terminated'
60 echo 'TEST FAILED.' > /workspace/status.txt
61 else
62 echo 'Success: OS policy assignment does not affect VM'
63 fi
64 fi
65
66 gcloud compute os-config os-policy-assignments delete shutdown-policy --location=$3 --quiet || true