Skip to content

Commit a42551a

Browse files
committed
Added tests for TLS
1 parent ae94224 commit a42551a

File tree

7 files changed

+126
-36
lines changed

7 files changed

+126
-36
lines changed

Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
GOLANGCI_LINT = $(GOPATH)/bin/golangci-lint
2-
GOLANGCI_LINT_VERSION = 1.56.2
2+
GOLANGCI_LINT_VERSION = v1.61.0
33

44
$(GOLANGCI_LINT): ## Download Go linter
55
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOPATH)/bin $(GOLANGCI_LINT_VERSION)
66

7-
.PHONY: validate
8-
validate: lint test
7+
.PHONY: ci
8+
ci: tidy lint test
99
go mod tidy && git diff --exit-code
1010

1111
.PHONY: lint
1212
lint: $(GOLANGCI_LINT) ## Run Go linter
1313
$(GOLANGCI_LINT) run -v ./...
14-
#$(GOLANGCI_LINT) run -v -c .golangci.yml ./...
14+
15+
.PHONY: tidy
16+
tidy:
17+
go mod tidy && git diff --exit-code
1518

1619
.PHONY: test
1720
test:

cluster/cluster_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ func TestStartMultipleInstances(t *testing.T) {
3333

3434
assert.Equal(t, 2, len(cluster.ListPeers()))
3535
assert.Equal(t, 2, len(cluster.ListDaemons()))
36-
err = cluster.Shutdown(context.Background())
37-
require.NoError(t, err)
36+
require.NoError(t, cluster.Shutdown(context.Background()))
3837
}
3938

4039
func TestRestart(t *testing.T) {
@@ -45,8 +44,7 @@ func TestRestart(t *testing.T) {
4544
assert.Equal(t, 2, len(cluster.ListDaemons()))
4645
err = cluster.Restart(context.Background())
4746
require.NoError(t, err)
48-
err = cluster.Shutdown(context.Background())
49-
require.NoError(t, err)
47+
require.NoError(t, cluster.Shutdown(context.Background()))
5048
}
5149

5250
func TestStartOneInstance(t *testing.T) {
@@ -55,8 +53,7 @@ func TestStartOneInstance(t *testing.T) {
5553

5654
assert.Equal(t, 1, len(cluster.ListPeers()))
5755
assert.Equal(t, 1, len(cluster.ListDaemons()))
58-
err = cluster.Shutdown(context.Background())
59-
require.NoError(t, err)
56+
require.NoError(t, cluster.Shutdown(context.Background()))
6057
}
6158

6259
func TestStartMultipleDaemons(t *testing.T) {
@@ -74,13 +71,13 @@ func TestStartMultipleDaemons(t *testing.T) {
7471
assert.Contains(t, daemons[1].ListenAddress(), ":2222")
7572
assert.Contains(t, cluster.DaemonAt(0).ListenAddress(), ":1111")
7673
assert.Contains(t, cluster.DaemonAt(1).ListenAddress(), ":2222")
77-
err = cluster.Shutdown(context.Background())
78-
require.NoError(t, err)
74+
require.NoError(t, cluster.Shutdown(context.Background()))
7975
}
8076

8177
func TestStartWithInvalidPeer(t *testing.T) {
8278
err := cluster.StartWith(context.Background(), []peer.Info{{Address: "1111"}}, groupcache.Options{})
8379
assert.Error(t, err)
8480
assert.Nil(t, cluster.ListPeers())
8581
assert.Nil(t, cluster.ListDaemons())
82+
require.NoError(t, cluster.Shutdown(context.Background()))
8683
}

cmd/server/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func main() {
6262
defer cancel()
6363
err := i.SetPeers(ctx, peers)
6464
if err != nil {
65-
log.Fatalf(err.Error())
65+
log.Fatal(err.Error())
6666
}
6767

6868
group, err := i.NewGroup("cache1", 64<<20, groupcache.GetterFunc(
@@ -81,7 +81,7 @@ func main() {
8181
},
8282
))
8383
if err != nil {
84-
log.Fatalf(err.Error())
84+
log.Fatal(err.Error())
8585
}
8686

8787
mux := http.NewServeMux()

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module github.com/groupcache/groupcache-go/v3
22

3-
go 1.21
3+
go 1.21.7
4+
5+
toolchain go1.23.1
46

57
require (
8+
github.com/kapetan-io/tackle v0.10.0
69
github.com/maypok86/otter v1.2.0
710
github.com/segmentio/fasthash v1.0.3
8-
github.com/stretchr/testify v1.8.1
11+
github.com/stretchr/testify v1.9.0
912
golang.org/x/net v0.22.0
1013
google.golang.org/protobuf v1.28.1
1114
)

go.sum

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
32
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43
github.com/dolthub/maphash v0.1.0 h1:bsQ7JsF4FkkWyrP3oCnFJgrCUAFbFf3kOl4L/QxPDyQ=
@@ -8,19 +7,16 @@ github.com/gammazero/deque v0.2.1/go.mod h1:LFroj8x4cMYCukHJDbxFCkT+r9AndaJnFMuZ
87
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
98
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
109
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
10+
github.com/kapetan-io/tackle v0.10.0 h1:EsNnlIYE8eoGxZhNyI1m91cDGGVDNsOru25V811MS00=
11+
github.com/kapetan-io/tackle v0.10.0/go.mod h1:E7MpdJUog4MvyKkWtQyX8UjFe5tL4SHQ44ZGk+zDBM8=
1112
github.com/maypok86/otter v1.2.0 h1:djwBBNpp9+dyzBTY0zscIG+pyAQVXRRRMbzztf8iJ4U=
1213
github.com/maypok86/otter v1.2.0/go.mod h1:mKLfoI7v1HOmQMwFgX4QkRk23mX6ge3RDvjdHOWG4R4=
1314
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1415
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1516
github.com/segmentio/fasthash v1.0.3 h1:EI9+KE1EwvMLBWwjpRDc+fEM+prwxDYbslddQGtrmhM=
1617
github.com/segmentio/fasthash v1.0.3/go.mod h1:waKX8l2N8yckOgmSsXJi7x1ZfdKZ4x7KRMzBtS3oedY=
17-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
18-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
19-
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
20-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
21-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
22-
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
23-
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
18+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
19+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2420
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
2521
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
2622
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -31,6 +27,5 @@ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175
3127
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
3228
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3329
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
34-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3530
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3631
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

transport/http_transport_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,17 @@ func TestHttpTransport(t *testing.T) {
5757
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
5858
}),
5959
}))
60+
defer func() { _ = cluster.Shutdown(context.Background()) }()
6061

61-
newGetter := func(idx int) groupcache.Getter {
62-
return groupcache.GetterFunc(func(ctx context.Context, key string, dest transport.Sink) error {
63-
if _, err := http.Get(ts.URL); err != nil {
64-
t.Logf("HTTP request from getter failed with '%s'", err)
65-
}
66-
return dest.SetString(strconv.Itoa(idx)+":"+key, time.Time{})
67-
})
68-
}
69-
70-
// Create a group for each instance in the cluster
62+
// Create a group on each instance in the cluster
7163
for idx, d := range cluster.ListDaemons() {
72-
_, err := d.GetInstance().NewGroup(groupName, 1<<20, newGetter(idx))
64+
_, err := d.GetInstance().NewGroup(groupName, 1<<20,
65+
groupcache.GetterFunc(func(ctx context.Context, key string, dest transport.Sink) error {
66+
if _, err := http.Get(ts.URL); err != nil {
67+
t.Logf("HTTP request from getter failed with '%s'", err)
68+
}
69+
return dest.SetString(strconv.Itoa(idx)+":"+key, time.Time{})
70+
}))
7371
require.NoError(t, err)
7472
}
7573

transport/tls_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package transport_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/groupcache/groupcache-go/v3"
7+
"github.com/groupcache/groupcache-go/v3/cluster"
8+
"github.com/groupcache/groupcache-go/v3/transport"
9+
"github.com/groupcache/groupcache-go/v3/transport/pb"
10+
"github.com/kapetan-io/tackle/autotls"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
"io"
14+
"log/slog"
15+
"net/http"
16+
"net/http/httptest"
17+
"strconv"
18+
"strings"
19+
"testing"
20+
"time"
21+
)
22+
23+
func TestTLS(t *testing.T) {
24+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
25+
defer cancel()
26+
27+
// AutoGenerate TLS certs
28+
conf := autotls.Config{AutoTLS: true}
29+
require.NoError(t, autotls.Setup(&conf))
30+
31+
// Start a 2 node cluster with TLS
32+
err := cluster.Start(context.Background(), 2,
33+
groupcache.Options{
34+
Transport: transport.NewHttpTransport(transport.HttpTransportOptions{
35+
TLSConfig: conf.ServerTLS,
36+
Client: &http.Client{
37+
Transport: &http.Transport{
38+
TLSClientConfig: conf.ClientTLS,
39+
},
40+
},
41+
}),
42+
Logger: slog.New(slog.NewTextHandler(io.Discard, nil)),
43+
})
44+
require.NoError(t, err)
45+
46+
assert.Equal(t, 2, len(cluster.ListPeers()))
47+
assert.Equal(t, 2, len(cluster.ListDaemons()))
48+
49+
// Start a http server to count the number of non cached hits
50+
var serverHits int
51+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52+
_, _ = fmt.Fprintln(w, "Hello")
53+
serverHits++
54+
}))
55+
defer ts.Close()
56+
57+
// Create a group on each instance in the cluster
58+
for idx, d := range cluster.ListDaemons() {
59+
_, err := d.GetInstance().NewGroup(groupName, 1<<20,
60+
groupcache.GetterFunc(func(ctx context.Context, key string, dest transport.Sink) error {
61+
if _, err := http.Get(ts.URL); err != nil {
62+
t.Logf("HTTP request from getter failed with '%s'", err)
63+
}
64+
return dest.SetString(strconv.Itoa(idx)+":"+key, time.Time{})
65+
}))
66+
require.NoError(t, err)
67+
}
68+
69+
// Create new transport with the client TLS config
70+
tr := transport.NewHttpTransport(transport.HttpTransportOptions{
71+
TLSConfig: conf.ServerTLS,
72+
Client: &http.Client{
73+
Transport: &http.Transport{
74+
TLSClientConfig: conf.ClientTLS,
75+
},
76+
},
77+
})
78+
79+
// Create a new client to the first peer in the cluster
80+
c, err := tr.NewClient(ctx, cluster.PeerAt(0))
81+
require.NoError(t, err)
82+
83+
// Each new key should result in a new hit to the test server
84+
for _, key := range testKeys(100) {
85+
var resp pb.GetResponse
86+
require.NoError(t, getRequest(ctx, c, groupName, key, &resp))
87+
88+
// The value should be in the format `instance:key`
89+
assert.True(t, strings.HasSuffix(string(resp.Value), ":"+key))
90+
}
91+
assert.Equal(t, 100, serverHits)
92+
93+
require.NoError(t, cluster.Shutdown(context.Background()))
94+
}

0 commit comments

Comments
 (0)