forked from k8sgateway/k8sgateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nats.go
179 lines (149 loc) · 3.15 KB
/
nats.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package services
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/onsi/ginkgo"
)
type NatsStreamingFactory struct {
nats string
tmpdir string
}
func downloadNats(destDir string) (string, error) {
// get us some natsss
// TODO (celsosantos): Add ARM64 support
natsurl := fmt.Sprintf("https://github.com/nats-io/nats-streaming-server/releases/download/v0.9.0/nats-streaming-server-v0.9.0-%s-amd64.zip",
runtime.GOOS)
resp, err := http.Get(natsurl)
if err != nil {
return "", err
}
var buffer bytes.Buffer
if resp.Body == nil {
return "", errors.New("no body")
}
defer resp.Body.Close()
io.Copy(&buffer, resp.Body)
rederat := bytes.NewReader(buffer.Bytes())
// unzip
r, err := zip.NewReader(rederat, int64(rederat.Len()))
if err != nil {
return "", err
}
for _, f := range r.File {
if strings.HasSuffix(f.Name, "nats-streaming-server") {
natsfile := filepath.Join(destDir, "nats-streaming-server")
natsout, err := os.OpenFile(natsfile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return "", err
}
defer natsout.Close()
rc, err := f.Open()
if err != nil {
return "", err
}
defer rc.Close()
_, err = io.Copy(natsout, rc)
if err != nil {
return "", err
}
return natsfile, nil
}
}
return "", errors.New("nats file not found")
}
func NewNatsStreamingFactory() (*NatsStreamingFactory, error) {
nats := os.Getenv("NATS_SERVER")
tmpdir := ""
if nats == "" {
nats = "nats-streaming-server"
_, err := exec.LookPath(nats)
if err != nil {
tmpdir, err = ioutil.TempDir(os.Getenv("HELPER_TMP"), "nats")
if err != nil {
return nil, err
}
nats, err = downloadNats(tmpdir)
if err != nil {
os.RemoveAll(tmpdir)
return nil, err
}
}
}
return &NatsStreamingFactory{
nats: nats,
tmpdir: tmpdir,
}, nil
}
func (gf *NatsStreamingFactory) NewNatsStreamingInstance() (*NatsStreamingInstance, error) {
tmpdir, err := ioutil.TempDir(os.Getenv("HELPER_TMP"), "nats")
if err != nil {
return nil, err
}
gi := &NatsStreamingInstance{
nats: gf.nats,
tmpdir: tmpdir,
}
if err != nil {
return nil, err
}
return gi, nil
}
func (gf *NatsStreamingFactory) Clean() error {
if gf == nil {
return nil
}
if gf.tmpdir != "" {
os.RemoveAll(gf.tmpdir)
}
return nil
}
type NatsStreamingInstance struct {
nats string
tmpdir string
cmd *exec.Cmd
}
func (nsi *NatsStreamingInstance) NatsPort() uint32 {
return 4222
}
func (nsi *NatsStreamingInstance) ClusterId() string {
return "test-cluster"
}
func (nsi *NatsStreamingInstance) Run() error {
var cmd *exec.Cmd
natsargs := []string{"-DV"}
cmd = exec.Command(nsi.nats, natsargs...)
cmd.Dir = nsi.tmpdir
cmd.Stdout = ginkgo.GinkgoWriter
cmd.Stderr = ginkgo.GinkgoWriter
err := cmd.Start()
if err != nil {
return err
}
time.Sleep(time.Second / 3)
nsi.cmd = cmd
return nil
}
func (nsi *NatsStreamingInstance) Clean() error {
if nsi == nil {
return nil
}
if nsi.cmd != nil {
nsi.cmd.Process.Kill()
nsi.cmd.Wait()
}
if nsi.tmpdir != "" {
defer os.RemoveAll(nsi.tmpdir)
}
return nil
}