run.sh 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/sh
  2. set -e
  3. # Temporary directory for tests to use.
  4. AYA_TMPDIR="$(pwd)/.tmp"
  5. # Directory for VM images
  6. AYA_IMGDIR=${AYA_TMPDIR}
  7. # Test Architecture
  8. if [ -z "${AYA_TEST_ARCH}" ]; then
  9. AYA_TEST_ARCH="$(uname -m)"
  10. fi
  11. # Test Image
  12. if [ -z "${AYA_TEST_IMAGE}" ]; then
  13. AYA_TEST_IMAGE="fedora36"
  14. fi
  15. case "${AYA_TEST_IMAGE}" in
  16. fedora*) AYA_SSH_USER="fedora";;
  17. centos*) AYA_SSH_USER="centos";;
  18. esac
  19. download_images() {
  20. mkdir -p "${AYA_IMGDIR}"
  21. case $1 in
  22. fedora36)
  23. if [ ! -f "${AYA_IMGDIR}/fedora36.${AYA_TEST_ARCH}.qcow2" ]; then
  24. IMAGE="Fedora-Cloud-Base-36-1.5.${AYA_TEST_ARCH}.qcow2"
  25. IMAGE_URL="https://download.fedoraproject.org/pub/fedora/linux/releases/36/Cloud/${AYA_TEST_ARCH}/images"
  26. echo "Downloading: ${IMAGE}, this may take a while..."
  27. curl -o "${AYA_IMGDIR}/fedora36.${AYA_TEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  28. fi
  29. ;;
  30. centos8)
  31. if [ ! -f "${AYA_IMGDIR}/centos8.${AYA_TEST_ARCH}.qcow2" ]; then
  32. IMAGE="CentOS-8-GenericCloud-8.4.2105-20210603.0.${AYA_TEST_ARCH}.qcow2"
  33. IMAGE_URL="https://cloud.centos.org/centos/8/${AYA_TEST_ARCH}/images"
  34. echo "Downloading: ${IMAGE}, this may take a while..."
  35. curl -o "${AYA_IMGDIR}/centos8.${AYA_TEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  36. fi
  37. ;;
  38. *)
  39. echo "$1 is not a recognized image name"
  40. return 1
  41. ;;
  42. esac
  43. }
  44. start_vm() {
  45. download_images "${AYA_TEST_IMAGE}"
  46. # prepare config
  47. cat > "${AYA_TMPDIR}/metadata.yaml" <<EOF
  48. instance-id: iid-local01
  49. local-hostname: test
  50. EOF
  51. if [ ! -f "${AYA_TMPDIR}/test_rsa" ]; then
  52. ssh-keygen -t rsa -b 4096 -f "${AYA_TMPDIR}/test_rsa" -N "" -C "" -q
  53. pub_key=$(cat "${AYA_TMPDIR}/test_rsa.pub")
  54. cat > "${AYA_TMPDIR}/user-data.yaml" <<EOF
  55. #cloud-config
  56. ssh_authorized_keys:
  57. - ${pub_key}
  58. EOF
  59. fi
  60. if [ ! -f "${AYA_TMPDIR}/ssh_config" ]; then
  61. cat > "${AYA_TMPDIR}/ssh_config" <<EOF
  62. StrictHostKeyChecking=no
  63. UserKnownHostsFile=/dev/null
  64. GlobalKnownHostsFile=/dev/null
  65. EOF
  66. fi
  67. cloud-localds "${AYA_TMPDIR}/seed.img" "${AYA_TMPDIR}/user-data.yaml" "${AYA_TMPDIR}/metadata.yaml"
  68. case "${AYA_TEST_ARCH}" in
  69. x86_64)
  70. QEMU=qemu-system-x86_64
  71. machine="q35"
  72. cpu="qemu64"
  73. if [ "$(uname -m)" = "${AYA_TEST_ARCH}" ]; then
  74. if [ -c /dev/kvm ]; then
  75. machine="${machine},accel=kvm"
  76. cpu="host"
  77. elif [ "$(uname -s)" = "Darwin" ]; then
  78. machine="${machine},accel=hvf"
  79. cpu="host"
  80. fi
  81. fi
  82. ;;
  83. aarch64)
  84. QEMU=qemu-system-aarch64
  85. machine="virt"
  86. cpu="cortex-a57"
  87. if [ "$(uname -m)" = "${AYA_TEST_ARCH}" ]; then
  88. if [ -c /dev/kvm ]; then
  89. machine="${machine},accel=kvm"
  90. cpu="host"
  91. elif [ "$(uname -s)" = "Darwin" ]; then
  92. machine="${machine},accel=hvf"
  93. cpu="host"
  94. fi
  95. fi
  96. ;;
  97. *)
  98. echo "${AYA_TEST_ARCH} is not supported"
  99. return 1
  100. ;;
  101. esac
  102. qemu-img create -F qcow2 -f qcow2 -o backing_file="${AYA_IMGDIR}/${AYA_TEST_IMAGE}.${AYA_TEST_ARCH}.qcow2" "${AYA_TMPDIR}/vm.qcow2" || return 1
  103. $QEMU \
  104. -machine "${machine}" \
  105. -cpu "${cpu}" \
  106. -m 2G \
  107. -display none \
  108. -monitor none \
  109. -daemonize \
  110. -pidfile "${AYA_TMPDIR}/vm.pid" \
  111. -device virtio-net-pci,netdev=net0 \
  112. -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  113. -drive if=virtio,format=qcow2,file="${AYA_TMPDIR}/vm.qcow2" \
  114. -drive if=virtio,format=raw,file="${AYA_TMPDIR}/seed.img" || return 1
  115. trap cleanup_vm EXIT
  116. echo "Waiting for SSH on port 2222..."
  117. retry=0
  118. max_retries=300
  119. while ! ssh -q -F "${AYA_TMPDIR}/ssh_config" -o ConnectTimeout=1 -i "${AYA_TMPDIR}/test_rsa" ${AYA_SSH_USER}@localhost -p 2222 echo "Hello VM"; do
  120. retry=$((retry+1))
  121. if [ ${retry} -gt ${max_retries} ]; then
  122. echo "Unable to connect to VM"
  123. return 1
  124. fi
  125. sleep 1
  126. done
  127. echo "VM launched, installing dependencies"
  128. exec_vm sudo dnf install -qy bpftool
  129. }
  130. scp_vm() {
  131. local=$1
  132. remote=$(basename "$1")
  133. scp -q -F "${AYA_TMPDIR}/ssh_config" \
  134. -i "${AYA_TMPDIR}/test_rsa" \
  135. -P 2222 "${local}" \
  136. "${AYA_SSH_USER}@localhost:${remote}"
  137. }
  138. exec_vm() {
  139. ssh -q -F "${AYA_TMPDIR}/ssh_config" \
  140. -i "${AYA_TMPDIR}/test_rsa" \
  141. -p 2222 \
  142. ${AYA_SSH_USER}@localhost \
  143. "$@"
  144. }
  145. stop_vm() {
  146. if [ -f "${AYA_TMPDIR}/vm.pid" ]; then
  147. echo "Stopping VM forcefully"
  148. kill -9 "$(cat "${AYA_TMPDIR}/vm.pid")"
  149. rm "${AYA_TMPDIR}/vm.pid"
  150. fi
  151. rm -f "${AYA_TMPDIR}/vm.qcow2"
  152. }
  153. cleanup_vm() {
  154. if [ "$?" != "0" ]; then
  155. stop_vm
  156. fi
  157. }
  158. if [ -z "$1" ]; then
  159. echo "path to libbpf required"
  160. exit 1
  161. fi
  162. start_vm
  163. trap stop_vm EXIT
  164. cargo xtask build-integration-test --musl --libbpf-dir "$1"
  165. scp_vm ../target/x86_64-unknown-linux-musl/debug/integration-test
  166. exec_vm sudo ./integration-test