Skip to content

Commit

Permalink
Merged pull request influxdata#1698 from influxdata/md-issue#1697
Browse files Browse the repository at this point in the history
Fix panic with MQTT toml configuration generation
  • Loading branch information
nathanielc committed Nov 28, 2017
2 parents bb35bdc + 9041625 commit 3402077
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Bugfixes

- [#1250](https://github.com/influxdata/kapacitor/issues/1250): Fix VictorOps "data" field being a string instead of actual JSON.
- [#1697](https://github.com/influxdata/kapacitor/issues/1697): Fix panic with MQTT toml configuration generation.

## v1.4.0-rc1 [2017-11-09]

Expand Down
29 changes: 16 additions & 13 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6948,11 +6948,14 @@ func TestServer_UpdateConfig(t *testing.T) {
{
section: "mqtt",
setDefaults: func(c *server.Config) {
c.MQTT = mqtt.Configs{mqtt.Config{
Name: "default",
URL: "tcp://mqtt.example.com:1883",
NewClientF: mqtttest.NewClient,
}}
cfg := &mqtt.Config{
Name: "default",
URL: "tcp://mqtt.example.com:1883",
}
cfg.SetNewClientF(mqtttest.NewClient)
c.MQTT = mqtt.Configs{
*cfg,
}
},
element: "default",
expDefaultSection: client.ConfigSection{
Expand Down Expand Up @@ -8867,15 +8870,15 @@ func TestServer_AlertHandlers(t *testing.T) {
setup: func(c *server.Config, ha *client.TopicHandler) (context.Context, error) {
cc := new(mqtttest.ClientCreator)
ctxt := context.WithValue(nil, "clientCreator", cc)

c.MQTT = mqtt.Configs{
mqtt.Config{
Enabled: true,
Name: "test",
URL: "tcp://mqtt.example.com:1883",
NewClientF: cc.NewClient,
},
cfg := &mqtt.Config{
Enabled: true,
Name: "test",
URL: "tcp://mqtt.example.com:1883",
}

cfg.SetNewClientF(cc.NewClient)

c.MQTT = mqtt.Configs{*cfg}
return ctxt, nil
},
result: func(ctxt context.Context) error {
Expand Down
15 changes: 11 additions & 4 deletions services/mqtt/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ type Config struct {
Username string `toml:"username" override:"username"`
Password string `toml:"password" override:"password,redact"`

// NewClientF is a function that returns a client for a given config.
NewClientF func(c Config) (Client, error) `toml:"-" override:"-"`
// newClientF is a function that returns a client for a given config.
// It is used exclusively for testing.
newClientF func(c Config) (Client, error) `override:"-"`
}

// SetNewClientF sets the newClientF on a Config.
// It is used exclusively for testing.
func (c *Config) SetNewClientF(fn func(c Config) (Client, error)) {
c.newClientF = fn
}

func NewConfig() Config {
Expand All @@ -52,8 +59,8 @@ func (c Config) Validate() error {
// NewClient creates a new client based off this configuration.
func (c Config) NewClient() (Client, error) {
newC := newClient
if c.NewClientF != nil {
newC = c.NewClientF
if c.newClientF != nil {
newC = c.newClientF
}
return newC(c)
}
Expand Down

0 comments on commit 3402077

Please sign in to comment.