Skip to content

Commit

Permalink
helper to determine if streams require max bytes set
Browse files Browse the repository at this point in the history
Signed-off-by: R.I.Pienaar <[email protected]>
  • Loading branch information
ripienaar committed Apr 14, 2022
1 parent 013aa6e commit 89a5ef4
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/dustin/go-humanize v1.0.0
github.com/google/go-cmp v0.5.7
github.com/klauspost/compress v1.15.1
github.com/nats-io/nats-server/v2 v2.7.5-0.20220413010444-e06e0a247fe7
github.com/nats-io/nats-server/v2 v2.7.5-0.20220414055341-ec3f9258afde
github.com/nats-io/nats.go v1.14.1-0.20220412004736-c75dfd54b52c
github.com/nats-io/nuid v1.0.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/nats-io/jwt/v2 v2.2.1-0.20220330180145-442af02fd36a h1:lem6QCvxR0Y28g
github.com/nats-io/jwt/v2 v2.2.1-0.20220330180145-442af02fd36a/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k=
github.com/nats-io/nats-server/v2 v2.7.5-0.20220413010444-e06e0a247fe7 h1:YlPFbUQzUuX3bCc7ko+anK9bTamRUPddJ38LuLpybTU=
github.com/nats-io/nats-server/v2 v2.7.5-0.20220413010444-e06e0a247fe7/go.mod h1:5vic7C58BFEVltiZhs7Kq81q2WcEPhJPsmNv1FOrdv0=
github.com/nats-io/nats-server/v2 v2.7.5-0.20220414055341-ec3f9258afde h1:xq3o1kmoAg8eHpS7ijxs9RAYwL9ifrFtzoK4P9M/WBs=
github.com/nats-io/nats-server/v2 v2.7.5-0.20220414055341-ec3f9258afde/go.mod h1:5vic7C58BFEVltiZhs7Kq81q2WcEPhJPsmNv1FOrdv0=
github.com/nats-io/nats.go v1.14.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nats.go v1.14.1-0.20220412004736-c75dfd54b52c h1:6jrqGDg/avM6mFiY4SvqG+4cf8z5mM6NJcpFLBz21ds=
github.com/nats-io/nats.go v1.14.1-0.20220412004736-c75dfd54b52c/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
Expand Down
20 changes: 20 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ func (m *Manager) JetStreamAccountInfo() (info *api.JetStreamAccountStats, err e
return resp.JetStreamAccountStats, nil
}

// IsStreamMaxBytesRequired determines if the JetStream account requires streams to set a byte limit
func (m *Manager) IsStreamMaxBytesRequired() (bool, error) {
nfo, err := m.JetStreamAccountInfo()
if err != nil {
return false, err
}

if nfo.Limits.MaxBytesRequired {
return true, nil
}

for _, t := range nfo.Tiers {
if t.Limits.MaxBytesRequired {
return true, nil
}
}

return false, nil
}

func (m *Manager) jsonRequest(subj string, req interface{}, response interface{}) (err error) {
var body []byte

Expand Down
71 changes: 71 additions & 0 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"testing"
"time"
Expand All @@ -36,6 +37,7 @@ func withJSCluster(t *testing.T, cb func(*testing.T, []*natsd.Server, *nats.Conn
if err != nil {
t.Fatalf("temp dir could not be made: %s", err)
}
defer os.RemoveAll(d)

var (
servers []*natsd.Server
Expand Down Expand Up @@ -114,6 +116,44 @@ func withJSCluster(t *testing.T, cb func(*testing.T, []*natsd.Server, *nats.Conn
}
}

func withNatsServerWithConfig(t *testing.T, cfile string, cb func(*testing.T, *natsd.Server)) {
t.Helper()

d, err := ioutil.TempDir("", "jstest")
if err != nil {
t.Fatalf("temp dir could not be made: %s", err)
}
defer os.RemoveAll(d)

af, err := filepath.Abs(cfile)
if err != nil {
t.Fatalf("absolute path failed: %v", err)
}

opts, err := natsd.ProcessConfigFile(af)
if err != nil {
t.Fatalf("config file failed: %v", err)
}

opts.StoreDir = d
opts.Port = -1
opts.Host = "localhost"
opts.LogFile = "/dev/stdout"
opts.Trace = true

s, err := natsd.NewServer(opts)
if err != nil {
t.Fatal("server start failed: ", err)
}

go s.Start()
if !s.ReadyForConnections(10 * time.Second) {
t.Error("nats server did not start")
}

cb(t, s)
}

func startJSServer(t *testing.T) (*natsd.Server, *nats.Conn, *jsm.Manager) {
t.Helper()

Expand Down Expand Up @@ -154,6 +194,37 @@ func startJSServer(t *testing.T) (*natsd.Server, *nats.Conn, *jsm.Manager) {
return s, nc, mgr
}

func TestIsStreamBytesRequired(t *testing.T) {
withNatsServerWithConfig(t, "testdata/bytes_required.cfg", func(t *testing.T, srv *natsd.Server) {
cases := []struct {
user string
required bool
}{
{"other", false},
{"a", true},
}

for _, tc := range cases {
t.Run(fmt.Sprintf("User_%s", tc.user), func(t *testing.T) {
nc, err := nats.Connect(srv.ClientURL(), nats.UserInfo(tc.user, "b"))
if err != nil {
t.Fatalf("connection failed: %v", err)
}

mgr, _ := jsm.New(nc)

required, err := mgr.IsStreamMaxBytesRequired()
if err != nil {
t.Fatalf("failed: %v", err)
}
if required != tc.required {
t.Fatalf("Expected it to be %t got %t", tc.required, required)
}
})
}
})
}

func TestJetStreamEnabled(t *testing.T) {
srv, nc, mgr := startJSServer(t)
defer srv.Shutdown()
Expand Down
20 changes: 20 additions & 0 deletions testdata/bytes_required.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
jetstream: {
store: /tmp
}
accounts {
OTHER: {
users: [{user: other, password: b}]
jetstream {
max_bytes_required: false
}

}

HR: {
jetstream {
max_bytes_required: true
}

users: [{user: a, password: b}]
}
}

0 comments on commit 89a5ef4

Please sign in to comment.