tunasync.yml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. name: tunasync
  2. on:
  3. push:
  4. branches: [ master ]
  5. pull_request:
  6. branches: [ master ]
  7. workflow_dispatch:
  8. jobs:
  9. build:
  10. name: Build
  11. runs-on: ubuntu-latest
  12. steps:
  13. - name: Check out code into the Go module directory
  14. uses: actions/checkout@v4
  15. - name: Set up Go
  16. uses: actions/setup-go@v5
  17. with:
  18. go-version: '^1.22'
  19. id: go
  20. - name: Get dependencies
  21. run: |
  22. go get -v -t -d ./cmd/tunasync
  23. go get -v -t -d ./cmd/tunasynctl
  24. - name: Build
  25. run: |
  26. make tunasync
  27. make tunasynctl
  28. - name: Keep artifacts
  29. uses: actions/upload-artifact@v4
  30. with:
  31. name: tunasync-bin
  32. path: build-linux-amd64/
  33. test:
  34. name: Test
  35. runs-on: ubuntu-latest
  36. services:
  37. registry:
  38. image: registry:2
  39. ports:
  40. - 5000:5000
  41. steps:
  42. - name: Setup test dependencies
  43. run: |
  44. sudo apt-get update
  45. sudo apt-get install -y cgroup-tools
  46. docker pull alpine:3.8
  47. - name: Check out code into the Go module directory
  48. uses: actions/checkout@v4
  49. - name: Set up Go
  50. uses: actions/setup-go@v5
  51. with:
  52. go-version: '^1.22'
  53. id: go
  54. - name: Run Unit tests.
  55. run: |
  56. go install github.com/wadey/gocovmerge@latest
  57. sudo systemd-run --service-type=oneshot --uid="$(id --user)" --pipe --wait \
  58. --property=Delegate=yes --setenv=USECURCGROUP=1 \
  59. --setenv=TERM=xterm-256color --same-dir \
  60. make test
  61. - name: Run Additional Unit tests.
  62. run: |
  63. make build-test-worker
  64. sudo mkdir /sys/fs/cgroup/tunasync
  65. sudo ./worker.test -test.v=true -test.coverprofile profile2.gcov -test.run TestCgroup
  66. sudo rmdir /sys/fs/cgroup/tunasync
  67. touch /tmp/dummy_exec
  68. chmod +x /tmp/dummy_exec
  69. run_test_reexec (){
  70. case="$1"
  71. shift
  72. argv0="$1"
  73. shift
  74. (TESTREEXEC="$case" TERM=xterm-256color exec -a "$argv0" ./worker.test -test.v=true -test.coverprofile "profile5_$case.gcov" -test.run TestReexec -- "$@")
  75. }
  76. run_test_reexec 1 tunasync-exec __dummy__
  77. run_test_reexec 2 tunasync-exec /tmp/dummy_exec
  78. run_test_reexec 3 tunasync-exec /tmp/dummy_exec 3< <(echo -n "abrt")
  79. run_test_reexec 4 tunasync-exec /tmp/dummy_exec 3< <(echo -n "cont")
  80. run_test_reexec 5 tunasync-exec2
  81. - name: Set up Docker Buildx
  82. uses: docker/setup-buildx-action@v3
  83. with:
  84. driver-opts: network=host
  85. - name: Cache Docker layers
  86. uses: actions/cache@v4
  87. if: github.event_name == 'push'
  88. with:
  89. path: /tmp/.buildx-cache
  90. key: ${{ runner.os }}-buildx-${{ github.sha }}
  91. restore-keys: |
  92. ${{ runner.os }}-buildx-
  93. - name: Cache Docker layers
  94. uses: actions/cache@v4
  95. if: github.event_name == 'pull_request'
  96. with:
  97. path: /tmp/.buildx-cache
  98. key: ${{ runner.os }}-pr-${{ github.event.pull_request.head.user.login }}-buildx-${{ github.sha }}
  99. restore-keys: |
  100. ${{ runner.os }}-pr-${{ github.event.pull_request.head.user.login }}-buildx-
  101. ${{ runner.os }}-buildx-
  102. - name: Cache Docker layers
  103. if: github.event_name != 'push' && github.event_name != 'pull_request'
  104. run: |
  105. echo "I do not know how to setup cache"
  106. exit -1
  107. - name: Prepare cache directory
  108. run: |
  109. mkdir -p /tmp/.buildx-cache
  110. - name: Build Docker image for uml rootfs
  111. uses: docker/build-push-action@v6
  112. with:
  113. context: .umlrootfs
  114. file: .umlrootfs/Dockerfile
  115. push: true
  116. tags: localhost:5000/umlrootfs
  117. cache-from: type=local,src=/tmp/.buildx-cache
  118. cache-to: type=local,dest=/tmp/.buildx-cache
  119. - name: Fetch and install uml package
  120. run: |
  121. sudo apt-get update
  122. sudo apt-get install -y debian-archive-keyring
  123. sudo ln -sf /usr/share/keyrings/debian-archive-keyring.gpg /etc/apt/trusted.gpg.d/
  124. echo "deb http://deb.debian.org/debian bullseye main" | sudo tee /etc/apt/sources.list.d/bullseye.list
  125. sudo apt-get update
  126. apt-get download user-mode-linux/bullseye
  127. sudo rm /etc/apt/sources.list.d/bullseye.list
  128. sudo apt-get update
  129. sudo mv user-mode-linux_*.deb /tmp/uml.deb
  130. sudo apt-get install --no-install-recommends -y /tmp/uml.deb
  131. sudo rm /tmp/uml.deb
  132. sudo apt-get install --no-install-recommends -y rsh-redone-client
  133. - name: Prepare uml environment
  134. run: |
  135. docker container create --name umlrootfs localhost:5000/umlrootfs
  136. sudo mkdir -p umlrootfs
  137. docker container export umlrootfs | sudo tar -xv -C umlrootfs
  138. docker container rm umlrootfs
  139. sudo cp -a --target-directory=umlrootfs/lib/ /usr/lib/uml/modules
  140. /bin/echo -e "127.0.0.1 localhost\n254.255.255.1 host" | sudo tee umlrootfs/etc/hosts
  141. sudo ip tuntap add dev umltap mode tap
  142. sudo ip addr add 254.255.255.1/24 dev umltap
  143. sudo ip link set umltap up
  144. - name: Start Uml
  145. run: |
  146. start_uml () {
  147. sudo bash -c 'linux root=/dev/root rootflags=/ rw rootfstype=hostfs mem=2G eth0=tuntap,umltap hostfs="$PWD/umlrootfs" con1=pts systemd.unified_cgroup_hierarchy=0 & pid=$!; echo "UMLINUX_PID=$pid" >> '"$GITHUB_ENV"
  148. }
  149. ( start_uml )
  150. started=0
  151. for i in $(seq 1 60); do
  152. if ping -c 1 -w 1 254.255.255.2; then
  153. started=1
  154. break
  155. fi
  156. done
  157. if [ "$started" != "1" ]; then
  158. echo "Failed to wait Umlinux online"
  159. exit 1
  160. fi
  161. - name: Prepare Uml Environment
  162. run: |
  163. CUSER="$(id --user --name)"
  164. CUID="$(id --user)"
  165. CGID="$(id --group)"
  166. sudo chroot umlrootfs bash --noprofile --norc -eo pipefail << EOF
  167. groupadd --gid "${CGID?}" "${CUSER?}"
  168. useradd --create-home --home-dir "/home/${CUSER}" --gid "${CGID?}" \
  169. --uid "${CUID?}" --shell "\$(which bash)" "${CUSER?}"
  170. EOF
  171. ln ./worker.test "umlrootfs/home/${CUSER}/worker.test"
  172. - name: Run Tests in Cgroupv1
  173. run: |
  174. CUSER="$(id --user --name)"
  175. sudo rsh 254.255.255.2 bash --noprofile --norc -eo pipefail << EOF
  176. exec 2>&1
  177. cd "/home/${CUSER}"
  178. lssubsys -am
  179. cgcreate -a "$CUSER" -t "$CUSER" -g cpu:tunasync
  180. cgcreate -a "$CUSER" -t "$CUSER" -g memory:tunasync
  181. TERM=xterm-256color ./worker.test -test.v=true -test.coverprofile \
  182. profile3.gcov -test.run TestCgroup
  183. cgexec -g "*:/" bash -c "echo 0 > /sys/fs/cgroup/systemd/tasks; exec sudo -u $CUSER env USECURCGROUP=1 TERM=xterm-256color cgexec -g cpu,memory:tunasync ./worker.test -test.v=true -test.coverprofile profile4.gcov -test.run TestCgroup"
  184. EOF
  185. - name: Stop Uml
  186. run: |
  187. sudo rsh 254.255.255.2 systemctl poweroff
  188. sleep 10
  189. if [ -e "/proc/$UMLINUX_PID" ]; then
  190. sleep 10
  191. if [ -e "/proc/$UMLINUX_PID" ]; then
  192. sudo kill -TERM "$UMLINUX_PID" || true
  193. sleep 1
  194. fi
  195. fi
  196. if [ -e "/proc/$UMLINUX_PID" ]; then
  197. sleep 10
  198. if [ -e "/proc/$UMLINUX_PID" ]; then
  199. sudo kill -KILL "$UMLINUX_PID" || true
  200. sleep 1
  201. fi
  202. fi
  203. - name: Combine coverage files
  204. run : |
  205. CUSER="$(id --user --name)"
  206. "${HOME}/go/bin/gocovmerge" profile.gcov profile2.gcov \
  207. "umlrootfs/home/${CUSER}/profile3.gcov" \
  208. "umlrootfs/home/${CUSER}/profile4.gcov" \
  209. profile5_*.gcov > merged.gcov
  210. # remove cmdline tools from coverage statistics
  211. grep -v "cmd/.*\.go" merged.gcov > profile-all.gcov
  212. - name: Convert coverage to lcov
  213. uses: jandelgado/gcov2lcov-action@v1
  214. with:
  215. infile: profile-all.gcov
  216. outfile: coverage.lcov
  217. - name: Coveralls
  218. uses: coverallsapp/github-action@v2
  219. with:
  220. github-token: ${{ secrets.github_token }}
  221. path-to-lcov: coverage.lcov