Skip to content

Commit dd35ff4

Browse files
committed
Add tests to deployment and on push, update makefile with e2e build
1 parent 90d9d32 commit dd35ff4

File tree

7 files changed

+137
-10
lines changed

7 files changed

+137
-10
lines changed

.github/workflows/docker-image.yml

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
11
name: Docker Image CI
22

3-
on:
4-
push:
5-
branches: ["main"]
3+
on: ["push"]
64

75
jobs:
6+
e2e_tests:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
fail-fast: true
10+
steps:
11+
- uses: actions/checkout@v4
12+
with:
13+
fetch-depth: 0
14+
fetch-tags: true
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v4
18+
with:
19+
go-version: "1.23.1"
20+
21+
- name: Update repos
22+
run: sudo apt update
23+
24+
- name: Install mingw for windows dll tests
25+
run: sudo apt install -y gcc-mingw-w64
26+
27+
- name: Make integration tests
28+
run: yes | make e2e
29+
30+
- name: Run tests
31+
working-directory: e2e
32+
run: ./e2e
33+
834
build:
35+
needs: test
36+
if: github.ref == 'refs/heads/main'
937
runs-on: ubuntu-latest
1038
strategy:
1139
fail-fast: true

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ e2e/downloads
1515
e2e/cache
1616
e2e/keys
1717
e2e/data.db
18-
e2e/id_ed25519
18+
e2e/id_ed25519
19+
e2e/watch.log
20+
e2e/authorized_controllee_keys

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ release: .generate_keys
3434

3535
e2e: .generate_keys
3636
go build -ldflags="github.com/NHAS/reverse_ssh/e2e.Version=$(shell git describe --tags)" -o e2e ./...
37+
cp internal/client/keys/private_key.pub e2e/authorized_controllee_keys
3738

3839
client: .generate_keys
3940
go build $(BUILD_FLAGS) -ldflags="$(LDFLAGS_RELEASE)" -o bin ./cmd/client

e2e/basics.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package main
22

3-
import (
4-
"os"
5-
)
3+
import "log"
64

7-
func basics(serverLog *os.File) {
5+
func basics() {
6+
log.Println("Starting basic tests")
87
conditionExec("version", Version, 0, "", 0)
98
conditionExec("ls", "No RSSH clients connected", 1, "", 0)
109
conditionExec("who", user, 0, "", 0)
10+
log.Println("Finished basic tests")
1111
}

e2e/client_tests.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"log"
6+
"os"
7+
"os/exec"
8+
"time"
9+
)
10+
11+
func clientTests() {
12+
log.Println("Starting client tests")
13+
kill := runPrecompiledClient()
14+
defer kill()
15+
16+
conditionExec("ls", "127.0.0.1:", 0, "", 0)
17+
conditionExec("exec -y * echo hello", "hello", 0, "", 0)
18+
19+
log.Println("Ending client tests")
20+
21+
}
22+
23+
func runPrecompiledClient() func() {
24+
cmd := exec.Command("./client", "--foreground", "-d", listenAddr)
25+
26+
r, w, err := os.Pipe()
27+
if err != nil {
28+
log.Fatal("failed to create for client pipe: ", err)
29+
}
30+
31+
cmd.Stdout = io.MultiWriter(os.Stdout, w)
32+
cmd.Stderr = io.MultiWriter(os.Stdout, w)
33+
34+
err = cmd.Start()
35+
if err != nil {
36+
log.Fatal("failed to start client:", err)
37+
}
38+
serverLog = r
39+
40+
time.Sleep(1 * time.Second)
41+
42+
return func() {
43+
if cmd.Process != nil {
44+
cmd.Process.Kill()
45+
}
46+
}
47+
}

e2e/link_tests.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
)
7+
8+
func linkTests() {
9+
log.Println("Starting link tests")
10+
11+
conditionExec("link --name linuxbin", "linuxbin", 0, "", 0)
12+
conditionExec("link --goos linux --shared-object --name sharedlinux", "sharedlinux", 0, "", 0)
13+
14+
conditionExec("link --goos windows --name windowsbin", "windowsbin", 0, "", 0)
15+
conditionExec("link --goos windows --shared-object --name windowsdll", "windowsdll", 0, "", 0)
16+
17+
resp, err := http.Get("http://" + listenAddr + "/linuxbin")
18+
if err != nil {
19+
log.Fatal("failed to fetch linuxbin: ", err)
20+
}
21+
resp.Body.Close()
22+
if resp.StatusCode != 200 {
23+
log.Fatal("should have returned 200 for created linux binary, instead got: ", resp.Status)
24+
}
25+
26+
resp, err = http.Get("http://" + listenAddr + "/windowsdll")
27+
if err != nil {
28+
log.Fatal("failed to fetch windowsdll: ", err)
29+
}
30+
resp.Body.Close()
31+
if resp.StatusCode != 200 {
32+
log.Fatal("should have returned 200 for created windowsdll, instead got: ", resp.Status)
33+
}
34+
35+
log.Println("Ending link tests")
36+
37+
}

e2e/main.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func main() {
6262
log.Fatalf("Missing required files: %v", missingFiles)
6363
}
6464

65+
reset()
66+
6567
kill := runServer()
6668
defer kill()
6769

@@ -82,7 +84,10 @@ func main() {
8284
}
8385
defer client.Close()
8486

85-
basics(serverLog)
87+
// integration tests
88+
basics()
89+
clientTests()
90+
linkTests()
8691

8792
log.Println("All passed!")
8893
}
@@ -98,7 +103,7 @@ func conditionExec(command, expectedOutput string, exitCode int, serverLogExpect
98103
if err != nil {
99104

100105
if exitError, ok := err.(*ssh.ExitError); !ok || (ok && exitCode != exitError.ExitStatus()) {
101-
log.Fatalf("Failed to execute command %q: %v", command, err)
106+
log.Fatalf("Failed to execute command %q: %v: %q", command, err, output)
102107
}
103108
}
104109

@@ -164,3 +169,10 @@ func runServer() func() {
164169
}
165170
}
166171
}
172+
173+
func reset() {
174+
os.RemoveAll("./cache")
175+
os.RemoveAll("./downloads")
176+
os.RemoveAll("./keys")
177+
os.RemoveAll("./data.db")
178+
}

0 commit comments

Comments
 (0)