Skip to content

Commit

Permalink
Add tests with multiple brokers to manager_test.go
Browse files Browse the repository at this point in the history
We were not testing some important scenarios of the brokers.Manager
(e.g. starting/ending session on the correct broker if multiple are
configured, not triggering autodiscovery when configuredBrokers != nil).

This commit adds test cases to cover those scenarios.
  • Loading branch information
denisonbarbosa committed Sep 15, 2023
1 parent d2d12ec commit f30209b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
48 changes: 38 additions & 10 deletions internal/brokers/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ var (

func TestNewManager(t *testing.T) {
tests := map[string]struct {
cfgDir string
noBus bool
cfgDir string
configuredBrokers []string
noBus bool

wantErr bool
}{
"Creates all brokers when config dir has only valid brokers": {cfgDir: "valid_brokers"},
"Creates without autodiscovery when configuredBrokers is set": {cfgDir: "valid_brokers", configuredBrokers: []string{"valid_2"}},
"Creates only correct brokers when config dir has valid and invalid brokers": {cfgDir: "mixed_brokers"},
"Creates only local broker when config dir has only invalid ones": {cfgDir: "invalid_brokers"},
"Creates only local broker when config dir does not exist": {cfgDir: "does/not/exist"},
Expand All @@ -41,7 +43,7 @@ func TestNewManager(t *testing.T) {
t.Setenv("DBUS_SYSTEM_BUS_ADDRESS", "/dev/null")
}

got, err := brokers.NewManager(context.Background(), nil, brokers.WithRootDir(brokerCfgs), brokers.WithCfgDir(tc.cfgDir))
got, err := brokers.NewManager(context.Background(), tc.configuredBrokers, brokers.WithRootDir(brokerCfgs), brokers.WithCfgDir(tc.cfgDir))
if tc.wantErr {
require.Error(t, err, "NewManager should return an error, but did not")
return
Expand Down Expand Up @@ -171,9 +173,12 @@ func TestNewSession(t *testing.T) {
brokerID string
username string

configuredBrokers []string

wantErr bool
}{
"Successfully start a new session": {username: "success"},
"Successfully start a new session": {username: "success"},
"Successfully start a new session with the correct broker": {username: "success", configuredBrokers: []string{t.Name() + "_Broker1", t.Name() + "_Broker2"}},

"Error when broker does not exist": {brokerID: "does_not_exist", wantErr: true},
"Error when broker does not provide an ID": {username: "NS_no_id", wantErr: true},
Expand All @@ -185,8 +190,18 @@ func TestNewSession(t *testing.T) {
t.Parallel()

cfgDir := t.TempDir()
wantBroker := newBrokerForTests(t, cfgDir)
m, err := brokers.NewManager(context.Background(), nil, brokers.WithCfgDir(cfgDir))
if tc.configuredBrokers == nil {
tc.configuredBrokers = []string{strings.ReplaceAll(t.Name(), "/", "_")}
}

wantBroker := newBrokerForTests(t, cfgDir, tc.configuredBrokers[0])
if len(tc.configuredBrokers) > 1 {
for _, name := range tc.configuredBrokers[1:] {
newBrokerForTests(t, cfgDir, name)
}
}

m, err := brokers.NewManager(context.Background(), tc.configuredBrokers, brokers.WithCfgDir(cfgDir))
require.NoError(t, err, "Setup: could not create manager")

if tc.brokerID == "" {
Expand Down Expand Up @@ -226,9 +241,12 @@ func TestEndSession(t *testing.T) {
brokerID string
sessionID string

configuredBrokers []string

wantErr bool
}{
"Successfully end session": {sessionID: "success"},
"Successfully end session": {sessionID: "success"},
"Successfully end session on the correct broker": {sessionID: "success", configuredBrokers: []string{t.Name() + "_Broker1", t.Name() + "_Broker2"}},

"Error when broker does not exist": {brokerID: "does not exist", sessionID: "dont matter", wantErr: true},
"Error when ending session": {sessionID: "ES_error", wantErr: true},
Expand All @@ -239,12 +257,22 @@ func TestEndSession(t *testing.T) {
t.Parallel()

cfgDir := t.TempDir()
b := newBrokerForTests(t, cfgDir)
m, err := brokers.NewManager(context.Background(), nil, brokers.WithCfgDir(cfgDir))
if tc.configuredBrokers == nil {
tc.configuredBrokers = []string{strings.ReplaceAll(t.Name(), "/", "_")}
}

wantBroker := newBrokerForTests(t, cfgDir, tc.configuredBrokers[0])
if len(tc.configuredBrokers) > 1 {
for _, name := range tc.configuredBrokers[1:] {
newBrokerForTests(t, cfgDir, name)
}
}

m, err := brokers.NewManager(context.Background(), tc.configuredBrokers, brokers.WithCfgDir(cfgDir))
require.NoError(t, err, "Setup: could not create manager")

if tc.brokerID != "does not exist" {
m.SetBrokerForSession(&b, tc.sessionID)
m.SetBrokerForSession(&wantBroker, tc.sessionID)
}

err = m.EndSession(tc.sessionID)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- local
- Broker2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ID: BROKER_ID-success-session_id
Encryption Key: TestNewSession_Broker1_key

0 comments on commit f30209b

Please sign in to comment.