-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo_hijack_suite_test.go
102 lines (84 loc) · 1.83 KB
/
go_hijack_suite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package gohijack
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/mitchellh/mapstructure"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/u2386/go-hijack/runtime"
)
//go:noinline
func this_is_for_test(i int) string { return fmt.Sprint(i) }
func TestGoHijack(t *testing.T) {
_ = this_is_for_test(0)
RegisterFailHandler(Fail)
RunSpecs(t, "GoHijack Suite")
}
var _ = Describe("Test UDS Listener", func() {
Context("Listen on uds", func() {
var (
ctx context.Context
cancel context.CancelFunc
err error
)
BeforeEach(func() {
u := &uds{
Addr: UDSAddress,
}
ctx, cancel = context.WithCancel(context.Background())
ch := make(chan error, 1)
go func() {
select {
case <-ctx.Done():
case ch <- u.Run(ctx):
}
}()
select {
case err = <-ch:
case <-time.After(500 * time.Millisecond):
}
})
AfterEach(func() {
if _, err := os.Stat(UDSAddress); err == nil {
if err := os.RemoveAll(UDSAddress); err != nil {
fmt.Fprintf(os.Stderr, "unexcepted error:%s", err)
}
}
})
It("creates a sock", func() {
Expect(err).Should(BeNil())
_, err = os.Stat(UDSAddress)
Expect(err).Should(BeNil())
cancel()
time.Sleep(500 * time.Millisecond)
_, err = os.Stat(UDSAddress)
Expect(err).Should(HaveOccurred())
})
})
})
var _ = Describe("Test Parser", func() {
Context("Test Json Parser", func() {
var (
point runtime.HijackPoint
)
BeforeEach(func() {
parser := JsonParser()
m := parser.Parse(`
{
"func":"this_is_for_test",
"action":"delay",
"val": 10
}
`)
mapstructure.Decode(m, &point)
})
It("should parse successfully", func() {
Expect(point).ShouldNot(BeNil())
Expect(point.Func).To(BeEquivalentTo("this_is_for_test"))
Expect(point.Action).To(BeEquivalentTo("delay"))
})
})
})