Skip to content

Commit

Permalink
test: make tests reproducible locally
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <[email protected]>
  • Loading branch information
alegrey91 committed Feb 15, 2025
1 parent 72ef42c commit 294f2e2
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 26 deletions.
25 changes: 5 additions & 20 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,9 @@ jobs:
make build-static-libbpfgo
make build-bpf
- name: Run Unit-Test
- name: Run unit tests
run: |
mkdir /tmp/unit/
# test packages excluding the ones with libbpfgo
go test \
-cover \
-v \
$(go list ./... | grep -v "github.com/alegrey91/harpoon$" | grep -v ebpf | grep -v cmd) \
-skip TestHarpoon \
-args -test.gocoverdir=/tmp/unit/
make unit-tests
- name: Upload cover profiles
uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # ratchet:actions/upload-artifact@v4
Expand Down Expand Up @@ -82,18 +75,10 @@ jobs:
run: |
make build -C tests/testcases/example-app/
- name: Run integration test
- name: Run integration tests
run: |
mkdir -p /tmp/integration
# we have to run integration tests one-by-one
# otherwhise they will run in parallel.
# since harpoon apply network forwards, these could
# interact with each other and make the test fail.
go test \
-exec sudo \
-cover \
-v main_test.go \
-args -test.gocoverdir=/tmp/integration/
make integration-tests
- name: Upload cover profiles
uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # ratchet:actions/upload-artifact@v4
with:
Expand Down
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,31 @@ ifdef GITHUB_REF_NAME
docker push ${IMAGE_NAME}:latest
endif

unit-tests:
mkdir -p /tmp/unit/
go test \
-cover \
-v \
$(shell go list ./... | grep -v "github.com/alegrey91/harpoon$$" | grep -v ebpf | grep -v cmd) \
-skip TestHarpoon \
-args -test.gocoverdir=/tmp/unit/

integration-tests:
mkdir -p /tmp/integration
go test \
-exec sudo \
-cover \
-v main_test.go \
-args -test.gocoverdir=/tmp/integration/

create-bin-dir:
mkdir -p ${BINARY_DIR}

create-output-dir:
mkdir -p ${OUTPUT_DIR}

install:
cp ${BINARY_DIR}/${BINARY_NAME} /usr/sbin/
cp ${BINARY_DIR}/${BINARY_NAME} /usr/local/bin

clean:
rm -rf ${OUTPUT_DIR}
Expand Down
8 changes: 4 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ func TestHarpoon(t *testing.T) {
//Cmds: customCommands(),
RequireExplicitExec: true,
Setup: func(env *testscript.Env) error {
existingDir := filepath.Join("tests", "testcases")
destDir := filepath.Join(env.WorkDir, "testcases")
// Copy the directory to the test environment
err := copyDir(existingDir, destDir)
// copy test cases into test env
testCasesSourceDir := filepath.Join("tests", "testcases")
testCasesDestDir := filepath.Join(env.WorkDir, "testcases")
err := copyDir(testCasesSourceDir, testCasesDestDir)
if err != nil {
return err
}
Expand Down
8 changes: 7 additions & 1 deletion tests/integration.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ stdout 'write'
exec harpoon capture -f main.main -l -- ./bin/example-app coin
stderr 'libbpf: license of ebpf.o is GPL'

# verify all the syscalls from different goroutines are traced
exec harpoon capture -f main.main -l -- ./bin/example-app goroutines
stdout 'write'
stdout 'gettid'
stdout 'read'
stdout 'sync'

exec harpoon capture -f main.main -i 2 -- ./bin/example-app ten
stdout 'write'
stdout 'nanosleep'
Expand Down Expand Up @@ -164,7 +171,6 @@ symbolsOrigins:
"nanosleep",
"newfstatat",
"openat",
"pipe2",
"prctl",
"pread64",
"prlimit64",
Expand Down
39 changes: 39 additions & 0 deletions tests/testcases/example-app/cmd/goroutines.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"

"github.com/alegrey91/seccomp-test-coverage/pkg/randomic"
)

// goroutinesCmd represents the coin command
var goroutinesCmd = &cobra.Command{
Use: "goroutines",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
randomic.RunGoroutines()
},
}

func init() {
rootCmd.AddCommand(goroutinesCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// goroutinesCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// goroutinesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
10 changes: 10 additions & 0 deletions tests/testcases/example-app/harpoon-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
symbolsOrigins:
- testBinaryPath: /tmp/harpoon/__pkg_randomic.test
symbols:
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.RockPaperScissors
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.ThrowDice
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.FlipCoin
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.DoSomethingSpecial
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.DoNothing

23 changes: 23 additions & 0 deletions tests/testcases/example-app/pkg/randomic/randomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package randomic
import (
"fmt"
"math/rand"
"sync"
"syscall"
"time"
)
Expand Down Expand Up @@ -70,3 +71,25 @@ func DoForTenSec() {
func DoNothing() bool {
return true
}

func RunGoroutines() {
var wg sync.WaitGroup
wg.Add(4)
go func() {
defer wg.Done()
fmt.Println("hello")
}()
go func() {
defer wg.Done()
syscall.Gettid()
}()
go func() {
defer wg.Done()
syscall.Read(10, []byte("aaaaaaa"))
}()
go func() {
defer wg.Done()
syscall.Sync()
}()
wg.Wait()
}

0 comments on commit 294f2e2

Please sign in to comment.