run.sh 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #!/bin/sh
  2. set -e
  3. if [ "$(uname -s)" = "Darwin" ]; then
  4. export PATH="$(dirname $(brew list gnu-getopt | grep "bin/getopt$")):$PATH"
  5. fi
  6. AYA_SOURCE_DIR="$(realpath $(dirname $0)/..)"
  7. # Temporary directory for tests to use.
  8. AYA_TMPDIR="${AYA_SOURCE_DIR}/.tmp"
  9. # Directory for VM images
  10. AYA_IMGDIR=${AYA_TMPDIR}
  11. if [ -z "${AYA_BUILD_TARGET}" ]; then
  12. AYA_BUILD_TARGET=$(rustc -vV | sed -n 's|host: ||p')
  13. fi
  14. AYA_HOST_ARCH=$(uname -m)
  15. if [ "${AYA_HOST_ARCH}" = "arm64" ]; then
  16. AYA_HOST_ARCH="aarch64"
  17. fi
  18. if [ -z "${AYA_GUEST_ARCH}" ]; then
  19. AYA_GUEST_ARCH="${AYA_HOST_ARCH}"
  20. fi
  21. if [ "${AYA_GUEST_ARCH}" = "aarch64" ]; then
  22. if [ -z "${AARCH64_UEFI}" ]; then
  23. AARCH64_UEFI="$(brew list qemu -1 -v | grep edk2-aarch64-code.fd)"
  24. fi
  25. fi
  26. if [ -z "$AYA_MUSL_TARGET" ]; then
  27. AYA_MUSL_TARGET=${AYA_GUEST_ARCH}-unknown-linux-musl
  28. fi
  29. # Test Image
  30. if [ -z "${AYA_TEST_IMAGE}" ]; then
  31. AYA_TEST_IMAGE="fedora38"
  32. fi
  33. case "${AYA_TEST_IMAGE}" in
  34. fedora*) AYA_SSH_USER="fedora";;
  35. centos*) AYA_SSH_USER="centos";;
  36. esac
  37. download_images() {
  38. mkdir -p "${AYA_IMGDIR}"
  39. case $1 in
  40. fedora37)
  41. if [ ! -f "${AYA_IMGDIR}/fedora37.${AYA_GUEST_ARCH}.qcow2" ]; then
  42. IMAGE="Fedora-Cloud-Base-37-1.7.${AYA_GUEST_ARCH}.qcow2"
  43. IMAGE_URL="https://download.fedoraproject.org/pub/fedora/linux/releases/37/Cloud/${AYA_GUEST_ARCH}/images"
  44. echo "Downloading: ${IMAGE}, this may take a while..."
  45. curl -o "${AYA_IMGDIR}/fedora37.${AYA_GUEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  46. fi
  47. ;;
  48. fedora38)
  49. if [ ! -f "${AYA_IMGDIR}/fedora38.${AYA_GUEST_ARCH}.qcow2" ]; then
  50. IMAGE="Fedora-Cloud-Base-38_Beta-1.3.${AYA_GUEST_ARCH}.qcow2"
  51. IMAGE_URL="https://fr2.rpmfind.net/linux/fedora/linux/releases/test/38_Beta/Cloud/${AYA_GUEST_ARCH}/images"
  52. echo "Downloading: ${IMAGE}, this may take a while..."
  53. curl -o "${AYA_IMGDIR}/fedora38.${AYA_GUEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  54. fi
  55. ;;
  56. centos8)
  57. if [ ! -f "${AYA_IMGDIR}/centos8.${AYA_GUEST_ARCH}.qcow2" ]; then
  58. IMAGE="CentOS-8-GenericCloud-8.4.2105-20210603.0.${AYA_GUEST_ARCH}.qcow2"
  59. IMAGE_URL="https://cloud.centos.org/centos/8/${AYA_GUEST_ARCH}/images"
  60. echo "Downloading: ${IMAGE}, this may take a while..."
  61. curl -o "${AYA_IMGDIR}/centos8.${AYA_GUEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  62. fi
  63. ;;
  64. *)
  65. echo "$1 is not a recognized image name"
  66. return 1
  67. ;;
  68. esac
  69. }
  70. start_vm() {
  71. download_images "${AYA_TEST_IMAGE}"
  72. # prepare config
  73. cat > "${AYA_TMPDIR}/metadata.yaml" <<EOF
  74. instance-id: iid-local01
  75. local-hostname: test
  76. EOF
  77. if [ ! -f "${AYA_TMPDIR}/test_rsa" ]; then
  78. ssh-keygen -t rsa -b 4096 -f "${AYA_TMPDIR}/test_rsa" -N "" -C "" -q
  79. pub_key=$(cat "${AYA_TMPDIR}/test_rsa.pub")
  80. fi
  81. if [ ! -f "${AYA_TMPDIR}/ssh_config" ]; then
  82. cat > "${AYA_TMPDIR}/ssh_config" <<EOF
  83. StrictHostKeyChecking=no
  84. UserKnownHostsFile=/dev/null
  85. GlobalKnownHostsFile=/dev/null
  86. EOF
  87. fi
  88. cat > "${AYA_TMPDIR}/user-data.yaml" <<EOF
  89. #cloud-config
  90. ssh_authorized_keys:
  91. - ${pub_key}
  92. EOF
  93. $AYA_SOURCE_DIR/test/cloud-localds "${AYA_TMPDIR}/seed.img" "${AYA_TMPDIR}/user-data.yaml" "${AYA_TMPDIR}/metadata.yaml"
  94. case "${AYA_GUEST_ARCH}" in
  95. x86_64)
  96. QEMU=qemu-system-x86_64
  97. machine="q35"
  98. cpu="qemu64"
  99. nr_cpus="$(nproc --all)"
  100. if [ "${AYA_HOST_ARCH}" = "${AYA_GUEST_ARCH}" ]; then
  101. if [ -c /dev/kvm ]; then
  102. machine="${machine},accel=kvm"
  103. cpu="host"
  104. elif [ "$(uname -s)" = "Darwin" ]; then
  105. machine="${machine},accel=hvf"
  106. cpu="host"
  107. fi
  108. fi
  109. ;;
  110. aarch64)
  111. QEMU=qemu-system-aarch64
  112. machine="virt"
  113. cpu="cortex-a57"
  114. uefi="-drive file=${AARCH64_UEFI},if=pflash,format=raw,readonly=on"
  115. if [ "${AYA_HOST_ARCH}" = "${AYA_GUEST_ARCH}" ]; then
  116. if [ -c /dev/kvm ]; then
  117. machine="${machine},accel=kvm"
  118. cpu="host"
  119. nr_cpus="$(nproc --all)"
  120. elif [ "$(uname -s)" = "Darwin" ]; then
  121. machine="${machine},accel=hvf,highmem=off"
  122. cpu="cortex-a72"
  123. # nrpoc --all on apple silicon returns the two extra fancy
  124. # cores and then qemu complains that nr_cpus > actual_cores
  125. nr_cpus=8
  126. fi
  127. fi
  128. ;;
  129. *)
  130. echo "${AYA_GUEST_ARCH} is not supported"
  131. return 1
  132. ;;
  133. esac
  134. if [ ! -f "${AYA_IMGDIR}/vm.qcow2" ]; then
  135. echo "Creating VM image"
  136. qemu-img create -F qcow2 -f qcow2 -o backing_file="${AYA_IMGDIR}/${AYA_TEST_IMAGE}.${AYA_GUEST_ARCH}.qcow2" "${AYA_IMGDIR}/vm.qcow2" || return 1
  137. CACHED_VM=0
  138. else
  139. echo "Reusing existing VM image"
  140. CACHED_VM=1
  141. fi
  142. $QEMU \
  143. -machine "${machine}" \
  144. -cpu "${cpu}" \
  145. -m 3G \
  146. -smp "${nr_cpus}" \
  147. -display none \
  148. -monitor none \
  149. -daemonize \
  150. -pidfile "${AYA_TMPDIR}/vm.pid" \
  151. -device virtio-net-pci,netdev=net0 \
  152. -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  153. $uefi \
  154. -drive if=virtio,format=qcow2,file="${AYA_IMGDIR}/vm.qcow2" \
  155. -drive if=virtio,format=raw,file="${AYA_TMPDIR}/seed.img" || return 1
  156. trap cleanup_vm EXIT
  157. echo "Waiting for SSH on port 2222..."
  158. retry=0
  159. max_retries=300
  160. 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
  161. retry=$((retry+1))
  162. if [ ${retry} -gt ${max_retries} ]; then
  163. echo "Unable to connect to VM"
  164. return 1
  165. fi
  166. sleep 1
  167. done
  168. echo "VM launched"
  169. exec_vm uname -a
  170. echo "Enabling testing repositories"
  171. exec_vm sudo dnf config-manager --set-enabled updates-testing
  172. exec_vm sudo dnf config-manager --set-enabled updates-testing-modular
  173. echo "Installing dependencies"
  174. exec_vm sudo dnf install -qy bpftool llvm llvm-devel clang clang-devel zlib-devel
  175. exec_vm 'curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
  176. -y --profile minimal --default-toolchain nightly --component rust-src --component clippy'
  177. exec_vm 'echo source ~/.cargo/env >> ~/.bashrc'
  178. exec_vm cargo install bpf-linker --no-default-features
  179. }
  180. scp_vm() {
  181. local=$1
  182. remote=$(basename "$1")
  183. scp -q -F "${AYA_TMPDIR}/ssh_config" \
  184. -i "${AYA_TMPDIR}/test_rsa" \
  185. -P 2222 "${local}" \
  186. "${AYA_SSH_USER}@localhost:${remote}"
  187. }
  188. rsync_vm() {
  189. rsync -a -e "ssh -p 2222 -F ${AYA_TMPDIR}/ssh_config -i ${AYA_TMPDIR}/test_rsa" $1 $AYA_SSH_USER@localhost:
  190. }
  191. exec_vm() {
  192. ssh -q -F "${AYA_TMPDIR}/ssh_config" \
  193. -i "${AYA_TMPDIR}/test_rsa" \
  194. -p 2222 \
  195. ${AYA_SSH_USER}@localhost \
  196. "$@"
  197. }
  198. stop_vm() {
  199. if [ -f "${AYA_TMPDIR}/vm.pid" ]; then
  200. echo "Stopping VM forcefully"
  201. kill -9 "$(cat "${AYA_TMPDIR}/vm.pid")"
  202. rm "${AYA_TMPDIR}/vm.pid"
  203. fi
  204. }
  205. cleanup_vm() {
  206. stop_vm
  207. if [ "$?" != "0" ]; then
  208. rm -f "${AYA_IMGDIR}/vm.qcow2"
  209. fi
  210. }
  211. start_vm
  212. trap cleanup_vm EXIT
  213. # make sure we always use fresh sources (also see comment at the end)
  214. exec_vm "rm -rf aya/*"
  215. rsync_vm "--exclude=target --exclude=.tmp $AYA_SOURCE_DIR"
  216. exec_vm "cd aya; cargo xtask integration-test"
  217. # we rm and sync but it doesn't seem to work reliably - I guess we could sleep a
  218. # few seconds after but ain't nobody got time for that. Instead we also rm
  219. # before rsyncing.
  220. exec_vm "rm -rf aya/*; sync"