Skip to content

Commit 294f2e2

Browse files
committed
test: make tests reproducible locally
Signed-off-by: Alessio Greggi <[email protected]>
1 parent 72ef42c commit 294f2e2

File tree

7 files changed

+106
-26
lines changed

7 files changed

+106
-26
lines changed

.github/workflows/tests.yaml

+5-20
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,9 @@ jobs:
3333
make build-static-libbpfgo
3434
make build-bpf
3535
36-
- name: Run Unit-Test
36+
- name: Run unit tests
3737
run: |
38-
mkdir /tmp/unit/
39-
# test packages excluding the ones with libbpfgo
40-
go test \
41-
-cover \
42-
-v \
43-
$(go list ./... | grep -v "github.com/alegrey91/harpoon$" | grep -v ebpf | grep -v cmd) \
44-
-skip TestHarpoon \
45-
-args -test.gocoverdir=/tmp/unit/
38+
make unit-tests
4639
4740
- name: Upload cover profiles
4841
uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # ratchet:actions/upload-artifact@v4
@@ -82,18 +75,10 @@ jobs:
8275
run: |
8376
make build -C tests/testcases/example-app/
8477
85-
- name: Run integration test
78+
- name: Run integration tests
8679
run: |
87-
mkdir -p /tmp/integration
88-
# we have to run integration tests one-by-one
89-
# otherwhise they will run in parallel.
90-
# since harpoon apply network forwards, these could
91-
# interact with each other and make the test fail.
92-
go test \
93-
-exec sudo \
94-
-cover \
95-
-v main_test.go \
96-
-args -test.gocoverdir=/tmp/integration/
80+
make integration-tests
81+
9782
- name: Upload cover profiles
9883
uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # ratchet:actions/upload-artifact@v4
9984
with:

Makefile

+18-1
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,31 @@ ifdef GITHUB_REF_NAME
9292
docker push ${IMAGE_NAME}:latest
9393
endif
9494

95+
unit-tests:
96+
mkdir -p /tmp/unit/
97+
go test \
98+
-cover \
99+
-v \
100+
$(shell go list ./... | grep -v "github.com/alegrey91/harpoon$$" | grep -v ebpf | grep -v cmd) \
101+
-skip TestHarpoon \
102+
-args -test.gocoverdir=/tmp/unit/
103+
104+
integration-tests:
105+
mkdir -p /tmp/integration
106+
go test \
107+
-exec sudo \
108+
-cover \
109+
-v main_test.go \
110+
-args -test.gocoverdir=/tmp/integration/
111+
95112
create-bin-dir:
96113
mkdir -p ${BINARY_DIR}
97114

98115
create-output-dir:
99116
mkdir -p ${OUTPUT_DIR}
100117

101118
install:
102-
cp ${BINARY_DIR}/${BINARY_NAME} /usr/sbin/
119+
cp ${BINARY_DIR}/${BINARY_NAME} /usr/local/bin
103120

104121
clean:
105122
rm -rf ${OUTPUT_DIR}

main_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ func TestHarpoon(t *testing.T) {
1414
//Cmds: customCommands(),
1515
RequireExplicitExec: true,
1616
Setup: func(env *testscript.Env) error {
17-
existingDir := filepath.Join("tests", "testcases")
18-
destDir := filepath.Join(env.WorkDir, "testcases")
19-
// Copy the directory to the test environment
20-
err := copyDir(existingDir, destDir)
17+
// copy test cases into test env
18+
testCasesSourceDir := filepath.Join("tests", "testcases")
19+
testCasesDestDir := filepath.Join(env.WorkDir, "testcases")
20+
err := copyDir(testCasesSourceDir, testCasesDestDir)
2121
if err != nil {
2222
return err
2323
}

tests/integration.txtar

+7-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ stdout 'write'
6969
exec harpoon capture -f main.main -l -- ./bin/example-app coin
7070
stderr 'libbpf: license of ebpf.o is GPL'
7171

72+
# verify all the syscalls from different goroutines are traced
73+
exec harpoon capture -f main.main -l -- ./bin/example-app goroutines
74+
stdout 'write'
75+
stdout 'gettid'
76+
stdout 'read'
77+
stdout 'sync'
78+
7279
exec harpoon capture -f main.main -i 2 -- ./bin/example-app ten
7380
stdout 'write'
7481
stdout 'nanosleep'
@@ -164,7 +171,6 @@ symbolsOrigins:
164171
"nanosleep",
165172
"newfstatat",
166173
"openat",
167-
"pipe2",
168174
"prctl",
169175
"pread64",
170176
"prlimit64",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright © 2023 NAME HERE <EMAIL ADDRESS>
3+
*/
4+
package cmd
5+
6+
import (
7+
"github.com/spf13/cobra"
8+
9+
"github.com/alegrey91/seccomp-test-coverage/pkg/randomic"
10+
)
11+
12+
// goroutinesCmd represents the coin command
13+
var goroutinesCmd = &cobra.Command{
14+
Use: "goroutines",
15+
Short: "A brief description of your command",
16+
Long: `A longer description that spans multiple lines and likely contains examples
17+
and usage of using your command. For example:
18+
19+
Cobra is a CLI library for Go that empowers applications.
20+
This application is a tool to generate the needed files
21+
to quickly create a Cobra application.`,
22+
Run: func(cmd *cobra.Command, args []string) {
23+
randomic.RunGoroutines()
24+
},
25+
}
26+
27+
func init() {
28+
rootCmd.AddCommand(goroutinesCmd)
29+
30+
// Here you will define your flags and configuration settings.
31+
32+
// Cobra supports Persistent Flags which will work for this command
33+
// and all subcommands, e.g.:
34+
// goroutinesCmd.PersistentFlags().String("foo", "", "A help for foo")
35+
36+
// Cobra supports local flags which will only run when this command
37+
// is called directly, e.g.:
38+
// goroutinesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
symbolsOrigins:
3+
- testBinaryPath: /tmp/harpoon/__pkg_randomic.test
4+
symbols:
5+
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.RockPaperScissors
6+
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.ThrowDice
7+
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.FlipCoin
8+
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.DoSomethingSpecial
9+
- github.com/alegrey91/seccomp-test-coverage/pkg/randomic.DoNothing
10+

tests/testcases/example-app/pkg/randomic/randomic.go

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package randomic
33
import (
44
"fmt"
55
"math/rand"
6+
"sync"
67
"syscall"
78
"time"
89
)
@@ -70,3 +71,25 @@ func DoForTenSec() {
7071
func DoNothing() bool {
7172
return true
7273
}
74+
75+
func RunGoroutines() {
76+
var wg sync.WaitGroup
77+
wg.Add(4)
78+
go func() {
79+
defer wg.Done()
80+
fmt.Println("hello")
81+
}()
82+
go func() {
83+
defer wg.Done()
84+
syscall.Gettid()
85+
}()
86+
go func() {
87+
defer wg.Done()
88+
syscall.Read(10, []byte("aaaaaaa"))
89+
}()
90+
go func() {
91+
defer wg.Done()
92+
syscall.Sync()
93+
}()
94+
wg.Wait()
95+
}

0 commit comments

Comments
 (0)