| #cloud-config |
| # |
| # Copyright 2020 Google LLC |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| write_files: |
| - path: /tmp/preloader-test/test.sh |
| permissions: 0644 |
| owner: root |
| content: | |
| set -o errexit |
| set -o pipefail |
| |
| # Templated by cloudbuild config that runs this test. |
| OEM_SIZE_TH=%s |
| |
| trap 'fail exiting due to errors' EXIT |
| |
| fail() { |
| echo "TestFail: $@" |
| } |
| |
| testHello() { |
| if [[ ! -f /mnt/stateful_partition/hello ]]; then |
| echo "/mnt/stateful_partition/hello is not a valid file (does it exist?)" |
| echo "testHello fail" |
| RESULT="fail" |
| return |
| fi |
| expected="hello" |
| actual=$(cat /mnt/stateful_partition/hello) |
| if [[ "${expected}" != "${actual}" ]]; then |
| echo "/mnt/stateful_partition/hello contains ${actual} instead of ${expected}" |
| echo "testHello fail" |
| RESULT="fail" |
| return |
| fi |
| echo "testHello pass" |
| } |
| |
| testOEMHello(){ |
| if [[ ! -f /usr/share/oem/hello ]]; then |
| echo "/usr/share/oem/hello is not a valid file (does it exist?)" |
| echo "testOEMHello fail" |
| RESULT="fail" |
| return |
| fi |
| expected="hello" |
| actual=$(cat /usr/share/oem/hello) |
| if [[ "${expected}" != "${actual}" ]]; then |
| echo "/usr/share/oem/hello contains ${actual} instead of ${expected}" |
| echo "testOEMHello fail" |
| RESULT="fail" |
| return |
| fi |
| echo "testHello pass" |
| } |
| |
| testUbuntuImage() { |
| expected="ubuntu" |
| actual=$(docker images --format {{.Repository}}) |
| if [[ "${expected}" != "${actual}" ]]; then |
| echo "expected docker images: ${expected}" |
| echo "actual docker images: ${actual}" |
| echo "testUbuntuImage fail" |
| RESULT="fail" |
| return |
| fi |
| echo "testUbuntuImage pass" |
| } |
| |
| testHomeDir() { |
| expected="chronos" |
| actual=$(ls /home) |
| if [[ "${expected}" != "${actual}" ]]; then |
| echo "expected home contents: ${expected}" |
| echo "actual home contents: ${actual}" |
| echo "testHomeDir fail" |
| RESULT="fail" |
| return |
| fi |
| echo "testHomeDir pass" |
| } |
| |
| testWorkdirClean() { |
| if [[ -d "/var/lib/.cos-customizer" ]]; then |
| echo "/var/lib/.cos-customizer exists" |
| echo "testWorkdirClean fail" |
| RESULT="fail" |
| return |
| fi |
| echo "testWorkdirClean pass" |
| } |
| |
| testOEMSize(){ |
| actual=$(df --output=size /usr/share/oem --block-size=$((1<<20)) | tail -n 1) |
| if [[ "${actual}" -le "${OEM_SIZE_TH}" ]]; then |
| echo "OEM size 80% threshold: ${OEM_SIZE_TH} MB" |
| echo "actual OEM size: ${actual} MB" |
| echo "testOEMSize fail" |
| RESULT="fail" |
| return |
| fi |
| echo "testOEMSize pass" |
| } |
| |
| main() { |
| RESULT="pass" |
| testHello |
| testUbuntuImage |
| testHomeDir |
| testWorkdirClean |
| testOEMSize |
| testOEMHello |
| if [[ "${RESULT}" == "fail" ]]; then |
| exit 1 |
| fi |
| } |
| |
| main 2>&1 | sed "s/^/TestStatus: /" |
| trap - EXIT |
| echo "TestPass: all tests passed" |
| |
| - path: /etc/systemd/system/preloader-test.service |
| permissions: 0644 |
| owner: root |
| content: | |
| [Unit] |
| Description=Preloader test |
| Wants=network-online.target gcr-online.target docker.service |
| After=network-online.target gcr-online.target docker.service |
| |
| [Service] |
| Type=oneshot |
| RemainAfterExit=yes |
| User=root |
| ExecStart=/bin/bash /tmp/preloader-test/test.sh |
| StandardOutput=tty |
| StandardError=tty |
| TTYPath=/dev/ttyS1 |
| |
| runcmd: |
| - systemctl daemon-reload |
| - systemctl --no-block start preloader-test.service |