Skip to content

Commit cd7e268

Browse files
committed
fix: minor NPEs; added test
1 parent 1091f70 commit cd7e268

6 files changed

Lines changed: 102 additions & 6 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ dist/
3131
.vproxy.conf
3232
t/
3333
*.pprof
34+
.history

Makefile

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11

22
SHELL := bash
33

4+
# Build the vproxy binary (using go build)
45
build: clean
56
go build ./bin/vproxy
7+
echo "built ./vproxy"
68

9+
# Build a snapshot (using goreleaser)
710
snapshot: clean
811
goreleaser release --snapshot --clean
912

13+
# Install the generated homebrew formula into local homebrew tap
1014
install-formula: snapshot
11-
cp -a dist/homebrew/Formula/*.rb /usr/local/Homebrew/Library/Taps/jittering/homebrew-kegs/Formula/
15+
[ -z "$$HOMEBREW_PREFIX" ] && echo "HOMEBREW_PREFIX is not set" && exit 1
16+
cp -a dist/homebrew/Formula/*.rb $${HOMEBREW_PREFIX}/Library/Taps/jittering/homebrew-kegs/Formula/
1217

18+
# Build for linux (x64) (for testing only, release uses goreleaser)
1319
build-linux:
1420
GOOS=linux go build -o vproxy-linux-x64 ./bin/vproxy/
1521

22+
# Build for mac (x64) (for testing only, release uses goreleaser)
1623
build-mac:
1724
GOOS=darwin go build -o vproxy-macos-x64 ./bin/vproxy/
1825

26+
# Build for windows (x64) (for testing only, release uses goreleaser)
1927
build-windows:
2028
GOOS=windows go build -o vproxy-windows-x64 ./bin/vproxy/
2129

30+
# Release using goreleaser
2231
release: clean
2332
goreleaser release --clean
2433

34+
# Check goreleaser config and generated homebrew formula style for errors
2535
check-style:
2636
goreleaser check
2737
goreleaser --snapshot --skip-validate --clean
@@ -30,6 +40,7 @@ check-style:
3040
| grep -v Enabled | grep -v '#' | grep -v '^$$' | tr ':\n' ','); \
3141
brew style --display-cop-names --except-cops="$${cops}" ./dist/*.rb;
3242

43+
## Build and install into homebrew bin path, restart service
3344
build-brew:
3445
go build -ldflags \
3546
"-X main.version=snapshot \
@@ -38,12 +49,27 @@ build-brew:
3849
-X main.builtBy=$$(whoami)" \
3950
-o vproxy ./bin/vproxy/
4051

41-
sudo mv vproxy /opt/homebrew/opt/vproxy/bin/vproxy
52+
sudo mv vproxy $${HOMEBREW_PREFIX}/opt/vproxy/bin/vproxy
4253
sudo pkill -f 'vproxy daemon'
4354

55+
# Clean build artifacts
4456
clean:
4557
rm -f ./vproxy*
4658
rm -rf ./dist/
4759

60+
# Build and install to /usr/local/bin
4861
install: build
4962
sudo cp -a ./vproxy /usr/local/bin/vproxy
63+
64+
help: ## Show make target help
65+
@(grep -A1 '^#' $(MAKEFILE_LIST) \
66+
| grep -v '^--$$' \
67+
| while IFS= read -r line1 && IFS= read -r line2; do \
68+
if [[ "$$line2" =~ ^[a-zA-Z_-]+:.*$$ ]]; then \
69+
echo "$$line2 #$$line1"; \
70+
fi; \
71+
done; \
72+
grep -hE '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST)) \
73+
| sort \
74+
| awk '!seen[$$1]++' \
75+
| awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

daemon.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ func testListener(addr string) {
108108
l.Close()
109109
}
110110

111+
func (d *Daemon) Shutdown() {
112+
if d.enableHTTP() && d.httpListener != nil {
113+
d.httpListener.Close()
114+
}
115+
if d.enableTLS() && d.httpsListener != nil {
116+
d.httpsListener.Close()
117+
}
118+
}
119+
111120
// Run the daemon service. Does not return until the service is killed.
112121
func (d *Daemon) Run() {
113122
d.httpAddr = fmt.Sprintf("%s:%d", d.listenHost, d.httpPort)

daemon_test.go

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,52 @@
11
package vproxy
22

33
import (
4+
"fmt"
45
"net/http"
56
"net/http/httptest"
7+
"os"
68
"strings"
79
"testing"
10+
11+
"github.com/stretchr/testify/assert"
812
)
913

14+
var temp = ""
15+
16+
func setup() error {
17+
var err error
18+
temp, err = os.MkdirTemp("", "vproxy")
19+
if err != nil {
20+
return err
21+
}
22+
fmt.Println("using temp dir:", temp)
23+
os.Setenv("CERT_PATH", temp)
24+
os.Setenv("CAROOT_PATH", temp)
25+
err = InitTrustStore()
26+
if err != nil {
27+
return err
28+
}
29+
return nil
30+
}
31+
32+
func teardown() {
33+
os.RemoveAll(temp)
34+
}
35+
36+
func TestMain(m *testing.M) {
37+
err := setup()
38+
if err != nil {
39+
fmt.Println("setup error:", err)
40+
os.Exit(1)
41+
}
42+
43+
code := m.Run()
44+
45+
teardown()
46+
47+
os.Exit(code)
48+
}
49+
1050
func TestListClients(t *testing.T) {
1151
request, _ := http.NewRequest("GET", "/events/next/", nil)
1252
response := httptest.NewRecorder()
@@ -15,7 +55,7 @@ func TestListClients(t *testing.T) {
1555
lh := NewLoggedHandler(vhostMux)
1656

1757
// start daemon
18-
d := NewDaemon(lh, "127.0.0.1", 80, 443)
58+
d := NewDaemon(lh, "127.0.0.1", 0, 0)
1959

2060
d.listClients(response, request)
2161
if response.Code != http.StatusOK {
@@ -35,4 +75,20 @@ func TestListClients(t *testing.T) {
3575
if !strings.Contains(res, "test.local.com") {
3676
t.Fatalf("list does not contain test.local.com:\n%s", response.Body.String())
3777
}
78+
d.Shutdown()
79+
}
80+
81+
func TestAddRemoveVhost(t *testing.T) {
82+
83+
vhostMux := CreateVhostMux([]string{}, true)
84+
lh := NewLoggedHandler(vhostMux)
85+
d := NewDaemon(lh, "", 0, 0)
86+
87+
r := httptest.NewRecorder()
88+
d.addVhost("foo:8000", r)
89+
assert.Equal(t, 1, len(lh.vhostMux.Servers))
90+
91+
v := d.loggedHandler.GetVhost("foo")
92+
d.doRemoveVhost(v, r)
93+
assert.Equal(t, 0, len(lh.vhostMux.Servers))
3894
}

logged_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (lh *LoggedHandler) CreateTLSConfig() *tls.Config {
8787
}
8888

8989
// build cn and return
90-
cfg.BuildNameToCertificate()
90+
// cfg.BuildNameToCertificate()
9191
return cfg
9292
}
9393

vhost.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@ func (v *Vhost) BufferAsString() string {
167167
}
168168

169169
func (v *Vhost) Close() {
170-
close(v.logChan)
171-
v.logRing.Clear()
170+
if v.logChan != nil {
171+
close(v.logChan)
172+
}
173+
if v.logRing != nil {
174+
v.logRing.Clear()
175+
}
172176
}
173177

174178
func (v *Vhost) populateLogBuffer() {

0 commit comments

Comments
 (0)