| #cloud-config |
| # |
| # Copyright 2021 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 |
| |
| trap 'fail exiting due to errors' EXIT |
| |
| fail() { |
| echo "TestFail: $@" |
| } |
| |
| testWorkdirClean() { |
| if [[ -d "/var/lib/.cos-customizer" ]]; then |
| echo "/var/lib/.cos-customizer exists" |
| echo "testWorkdirClean fail" |
| RESULT="fail" |
| return |
| fi |
| echo "testWorkdirClean pass" |
| } |
| |
| testInstallPackageKubernetes() { |
| expectedFiles=("/home/kubernetes/bin/kubeadm" "/home/kubernetes/bin/kubectl" "/home/kubernetes/bin/kubelet") |
| filesNotFound=() |
| for file in ${expectedFiles[*]}; do |
| if [[ ! -f $file ]]; then |
| filesNotFound+=($file) |
| fi |
| done |
| if [[ ${filesNotFound[@]} -gt 0 ]]; then |
| echo "testInstallPackageKubernetes failed" |
| RESULT="fail" |
| return |
| fi |
| echo "testInstallPackageKubernetes pass" |
| } |
| |
| testPreloadContainer() { |
| if [[ "$(docker images -q calico/node 2> /dev/null)" == "" ]]; then |
| echo "testPreloadContainer failed" |
| RESULT="fail" |
| return |
| fi |
| echo "testPreloadContainer passed" |
| } |
| |
| main() { |
| RESULT="pass" |
| testWorkdirClean |
| testInstallPackageKubernetes |
| testPreloadContainer |
| |
| 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 |