Sfoglia il codice sorgente

cgroupv2: add test for reexec

Miao Wang 4 anni fa
parent
commit
f8b6ea9c4e
4 ha cambiato i file con 79 aggiunte e 2 eliminazioni
  1. 16 1
      .github/workflows/tunasync.yml
  2. 1 0
      go.mod
  3. 2 0
      go.sum
  4. 60 1
      worker/cgroup_test.go

+ 16 - 1
.github/workflows/tunasync.yml

@@ -71,6 +71,20 @@ jobs:
       run: |
         make build-test-worker
         sudo cgexec -g "*:/" bash -c "echo 0 > /sys/fs/cgroup/systemd/tasks; exec sudo -u $USER env USECURCGROUP=1 TERM=xterm-256color cgexec -g cpu,memory:tunasync ./worker.test -test.v=true -test.coverprofile profile2.cov -test.run TestCgroup"
+        touch /tmp/dummy_exec
+        chmod +x /tmp/dummy_exec
+        run_test_reexec (){
+          case="$1"
+          shift
+          argv0="$1"
+          shift
+          (TESTREEXEC="$case" TERM=xterm-256color exec -a "$argv0" ./worker.test -test.v=true -test.coverprofile "profile5_$case.cov" -test.run TestReexec -- "$@")
+        }
+        run_test_reexec 1 tunasync-exec __dummy__
+        run_test_reexec 2 tunasync-exec /tmp/dummy_exec
+        run_test_reexec 3 tunasync-exec /tmp/dummy_exec 3< <(echo -n "abrt")
+        run_test_reexec 4 tunasync-exec /tmp/dummy_exec 3< <(echo -n "cont")
+        run_test_reexec 5 tunasync-exec2
 
     - name: Set up Docker Buildx
       uses: docker/setup-buildx-action@v1
@@ -209,7 +223,8 @@ jobs:
         CUSER="$(id --user --name)"
         "${HOME}/go/bin/gocovmerge" profile.cov profile2.cov \
           "umlrootfs/home/${CUSER}/profile3.cov" \
-          "umlrootfs/home/${CUSER}/profile4.cov" > profile-all.cov
+          "umlrootfs/home/${CUSER}/profile4.cov" \
+          profile5_*.cov > profile-all.cov
 
     - name: Convert coverage to lcov
       uses: jandelgado/gcov2lcov-action@v1.0.0

+ 1 - 0
go.mod

@@ -26,6 +26,7 @@ require (
 	github.com/pkg/profile v1.4.0
 	github.com/ryszard/goskiplist v0.0.0-20150312221310-2dfbae5fcf46
 	github.com/sirupsen/logrus v1.8.1 // indirect
+	github.com/smartystreets/assertions v1.2.0 // indirect
 	github.com/smartystreets/goconvey v1.6.4
 	github.com/syndtr/goleveldb v1.0.0
 	github.com/urfave/cli v1.22.3

+ 2 - 0
go.sum

@@ -173,6 +173,8 @@ github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE
 github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
 github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
+github.com/smartystreets/assertions v1.2.0 h1:42S6lae5dvLc7BrLu/0ugRtcFVjoJNMC/N3yZFZkDFs=
+github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
 github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
 github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
 github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=

+ 60 - 1
worker/cgroup_test.go

@@ -3,6 +3,7 @@ package worker
 import (
 	"io/ioutil"
 	"os"
+	"os/exec"
 	"path/filepath"
 	"strconv"
 	"strings"
@@ -19,7 +20,65 @@ import (
 )
 
 func init() {
-	reexec.Init()
+	_, testReexec := os.LookupEnv("TESTREEXEC")
+	if ! testReexec {
+		reexec.Init()
+	}
+}
+
+func TestReexec(t *testing.T) {
+	testCase, testReexec := os.LookupEnv("TESTREEXEC")
+	if ! testReexec {
+		return
+	}
+	for len(os.Args) > 1 {
+		thisArg := os.Args[1]
+		os.Args = append([]string{os.Args[0]}, os.Args[2:]...)
+		if thisArg == "--" {
+			break
+		}
+	}
+	switch testCase {
+		case "1":
+			Convey("Reexec should panic when command not found", t, func(ctx C){
+				So(func(){
+					reexec.Init()
+				}, ShouldPanicWith, exec.ErrNotFound)
+			})
+		case "2":
+			Convey("Reexec should run when fd 3 is not open", t, func(ctx C){
+				So((func() error{
+					pipe := os.NewFile(3, "pipe")
+					if pipe == nil {
+						return errors.New("pipe is nil")
+					} else {
+						_, err := pipe.Stat()
+						return err
+					}
+				})(), ShouldNotBeNil)
+				So(func(){
+					reexec.Init()
+				}, ShouldPanicWith, syscall.ENOEXEC)
+			})
+		case "3":
+			Convey("Reexec should fail when fd 3 is sent with abrt cmd", t, func(ctx C){
+				So(func(){
+					reexec.Init()
+				}, ShouldPanicWith, "Exited on request")
+			})
+		case "4":
+			Convey("Reexec should run when fd 3 is sent with cont cmd", t, func(ctx C){
+				So(func(){
+					reexec.Init()
+				}, ShouldPanicWith, syscall.ENOEXEC)
+			})
+		case "5":
+			Convey("Reexec should not be triggered when argv[0] is not reexec", t, func(ctx C){
+				So(func(){
+					reexec.Init()
+				}, ShouldNotPanic)
+			})
+	}
 }
 
 func TestCgroup(t *testing.T) {