lib.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #!/bin/sh
  2. # Source the main regression test library if present
  3. [ -f "${RT_LIB}" ] && . "${RT_LIB}"
  4. # Temporary directory for tests to use.
  5. AYA_TMPDIR="${RT_PROJECT_ROOT}/_tmp"
  6. # Directory for VM images
  7. AYA_IMGDIR="${RT_PROJECT_ROOT}/_images"
  8. # Test Architecture
  9. if [ -z "${AYA_TEST_ARCH}" ]; then
  10. AYA_TEST_ARCH="$(uname -m)"
  11. fi
  12. # Test Image
  13. if [ -z "${AYA_TEST_IMAGE}" ]; then
  14. AYA_TEST_IMAGE="fedora35"
  15. fi
  16. case "${AYA_TEST_IMAGE}" in
  17. fedora*) AYA_SSH_USER="fedora";;
  18. centos*) AYA_SSH_USER="centos";;
  19. esac
  20. # compiles the ebpf program by using rust-script to create a temporary
  21. # cargo project in $(pwd)/ebpf. caller must add rm -rf ebpf to the clean_up
  22. # functionAYA_TEST_ARCH
  23. compile_ebpf() {
  24. file=$(basename "$1")
  25. dir=$(dirname "$1")
  26. base=$(echo "${file}" | cut -f1 -d '.')
  27. rm -rf "${dir}/ebpf"
  28. rust-script --pkg-path "${dir}/ebpf" --gen-pkg-only "$1"
  29. artifact=$(sed -n 's/^name = \"\(.*\)\"/\1/p' "${dir}/ebpf/Cargo.toml" | head -n1)
  30. mkdir -p "${dir}/.cargo"
  31. cat > "${dir}/.cargo/config.toml" << EOF
  32. [build]
  33. target = "bpfel-unknown-none"
  34. [unstable]
  35. build-std = ["core"]
  36. EOF
  37. cat >> "${dir}/ebpf/Cargo.toml" << EOF
  38. [workspace]
  39. members = []
  40. EOF
  41. # overwrite the rs file as rust-script adds a main fn
  42. cp "$1" "${dir}/ebpf/${file}"
  43. cargo build -q --manifest-path "${dir}/ebpf/Cargo.toml"
  44. mv "${dir}/ebpf/target/bpfel-unknown-none/debug/${artifact}" "${dir}/${base}.o"
  45. rm -rf "${dir}/.cargo"
  46. }
  47. # compiles the userspace program by using rust-script to create a temporary
  48. # cargo project in $(pwd)/user. caller must add rm -rf ebpf to the clean_up
  49. # function. this is required since the binary produced has to be run with
  50. # sudo to load an eBPF program
  51. compile_user() {
  52. file=$(basename "$1")
  53. dir=$(dirname "$1")
  54. base=$(echo "${file}" | cut -f1 -d '.')
  55. rm -rf "${dir}/user"
  56. rust-script --pkg-path "${dir}/user" --gen-pkg-only "$1"
  57. artifact=$(sed -n 's/^name = \"\(.*\)\"/\1/p' "${dir}/user/Cargo.toml" | head -n1)
  58. cat >> "${dir}/user/Cargo.toml" << EOF
  59. [workspace]
  60. members = []
  61. EOF
  62. cargo build -q --release --manifest-path "${dir}/user/Cargo.toml" --target=x86_64-unknown-linux-musl
  63. mv "${dir}/user/target/x86_64-unknown-linux-musl/release/${artifact}" "${dir}/${base}"
  64. }
  65. download_images() {
  66. mkdir -p "${AYA_IMGDIR}"
  67. case $1 in
  68. fedora35)
  69. if [ ! -f "${AYA_IMGDIR}/fedora35.${AYA_TEST_ARCH}.qcow2" ]; then
  70. IMAGE="Fedora-Cloud-Base-35-1.2.${AYA_TEST_ARCH}.qcow2"
  71. IMAGE_URL="https://download.fedoraproject.org/pub/fedora/linux/releases/35/Cloud/${AYA_TEST_ARCH}/images"
  72. echo "Downloading: ${IMAGE}, this may take a while..."
  73. curl -o "${AYA_IMGDIR}/fedora35.${AYA_TEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  74. fi
  75. ;;
  76. centos8)
  77. if [ ! -f "${AYA_IMGDIR}/centos8.${AYA_TEST_ARCH}.qcow2" ]; then
  78. IMAGE="CentOS-8-GenericCloud-8.4.2105-20210603.0.${AYA_TEST_ARCH}.qcow2"
  79. IMAGE_URL="https://cloud.centos.org/centos/8/${AYA_TEST_ARCH}/images"
  80. echo "Downloading: ${IMAGE}, this may take a while..."
  81. curl -o "${AYA_IMGDIR}/centos8.${AYA_TEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  82. fi
  83. ;;
  84. *)
  85. echo "$1 is not a recognized image name"
  86. return 1
  87. ;;
  88. esac
  89. }
  90. start_vm() {
  91. download_images "${AYA_TEST_IMAGE}"
  92. # prepare config
  93. cat > "${AYA_TMPDIR}/metadata.yaml" <<EOF
  94. instance-id: iid-local01
  95. local-hostname: test
  96. EOF
  97. if [ ! -f "${AYA_TMPDIR}/test_rsa" ]; then
  98. ssh-keygen -t rsa -b 4096 -f "${AYA_TMPDIR}/test_rsa" -N "" -C "" -q
  99. pub_key=$(cat "${AYA_TMPDIR}/test_rsa.pub")
  100. cat > "${AYA_TMPDIR}/user-data.yaml" <<EOF
  101. #cloud-config
  102. ssh_authorized_keys:
  103. - ${pub_key}
  104. EOF
  105. fi
  106. if [ ! -f "${AYA_TMPDIR}/ssh_config" ]; then
  107. cat > "${AYA_TMPDIR}/ssh_config" <<EOF
  108. StrictHostKeyChecking=no
  109. UserKnownHostsFile=/dev/null
  110. GlobalKnownHostsFile=/dev/null
  111. EOF
  112. fi
  113. cloud-localds "${AYA_TMPDIR}/seed.img" "${AYA_TMPDIR}/user-data.yaml" "${AYA_TMPDIR}/metadata.yaml"
  114. case "${AYA_TEST_ARCH}" in
  115. x86_64)
  116. QEMU=qemu-system-x86_64
  117. machine="q35"
  118. cpu="qemu64"
  119. if [ "$(uname -m)" = "${AYA_TEST_ARCH}" ]; then
  120. if [ -c /dev/kvm ]; then
  121. machine="${machine},accel=kvm"
  122. cpu="host"
  123. elif [ "$(uname -s)" = "Darwin" ]; then
  124. machine="${machine},accel=hvf"
  125. cpu="host"
  126. fi
  127. fi
  128. ;;
  129. aarch64)
  130. QEMU=qemu-system-aarch64
  131. machine="virt"
  132. cpu="cortex-a57"
  133. if [ "$(uname -m)" = "${AYA_TEST_ARCH}" ]; then
  134. if [ -c /dev/kvm ]; then
  135. machine="${machine},accel=kvm"
  136. cpu="host"
  137. elif [ "$(uname -s)" = "Darwin" ]; then
  138. machine="${machine},accel=hvf"
  139. cpu="host"
  140. fi
  141. fi
  142. ;;
  143. *)
  144. echo "${AYA_TEST_ARCH} is not supported"
  145. return 1
  146. ;;
  147. esac
  148. 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
  149. $QEMU \
  150. -machine "${machine}" \
  151. -cpu "${cpu}" \
  152. -m 2G \
  153. -display none \
  154. -monitor none \
  155. -daemonize \
  156. -pidfile "${AYA_TMPDIR}/vm.pid" \
  157. -device virtio-net-pci,netdev=net0 \
  158. -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  159. -drive if=virtio,format=qcow2,file="${AYA_TMPDIR}/vm.qcow2" \
  160. -drive if=virtio,format=raw,file="${AYA_TMPDIR}/seed.img" || return 1
  161. trap cleanup_vm EXIT
  162. echo "Waiting for SSH on port 2222..."
  163. retry=0
  164. max_retries=300
  165. 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
  166. retry=$((retry+1))
  167. if [ ${retry} -gt ${max_retries} ]; then
  168. echo "Unable to connect to VM"
  169. return 1
  170. fi
  171. sleep 1
  172. done
  173. echo "VM launched, installing dependencies"
  174. exec_vm sudo dnf install -qy bpftool
  175. }
  176. scp_vm() {
  177. local=$1
  178. scp -q -F "${AYA_TMPDIR}/ssh_config" \
  179. -i "${AYA_TMPDIR}/test_rsa" \
  180. -P 2222 "${local}" \
  181. "${AYA_SSH_USER}@localhost:${local}"
  182. }
  183. exec_vm() {
  184. ssh -q -F "${AYA_TMPDIR}/ssh_config" \
  185. -i "${AYA_TMPDIR}/test_rsa" \
  186. -p 2222 \
  187. ${AYA_SSH_USER}@localhost \
  188. "$@"
  189. }
  190. stop_vm() {
  191. if [ -f "${AYA_TMPDIR}/vm.pid" ]; then
  192. echo "Stopping VM forcefully"
  193. kill -9 "$(cat "${AYA_TMPDIR}/vm.pid")"
  194. rm "${AYA_TMPDIR}/vm.pid"
  195. fi
  196. rm -f "${AYA_TMPDIR}/vm.qcow2"
  197. }
  198. cleanup_vm() {
  199. if [ "$?" != "0" ]; then
  200. stop_vm
  201. fi
  202. }