Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Pérez-Aradros Herce committed Sep 13, 2018
1 parent 95299fb commit 5a3bf53
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 8 deletions.
5 changes: 3 additions & 2 deletions libbeat/common/reload/reload.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Register holds a registry of reloadable objects
var Register = newRegistry()
var Register = NewRegistry()

// ConfigWithMeta holds a pair of common.Config and optional metadata for it
type ConfigWithMeta struct {
Expand Down Expand Up @@ -54,7 +54,8 @@ type Registry struct {
confs map[string]Reloadable
}

func newRegistry() *Registry {
// NewRegistry initializes and returns a reload registry
func NewRegistry() *Registry {
return &Registry{
confsLists: make(map[string]ReloadableList),
confs: make(map[string]Reloadable),
Expand Down
8 changes: 4 additions & 4 deletions libbeat/common/reload/reload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (reloadableList) Reload(config []*ConfigWithMeta) error { return nil }

func RegisterReloadable(t *testing.T) {
obj := reloadable{}
r := newRegistry()
r := NewRegistry()

r.Register("my.reloadable", obj)

Expand All @@ -40,15 +40,15 @@ func RegisterReloadable(t *testing.T) {

func RegisterReloadableList(t *testing.T) {
objl := reloadableList{}
r := newRegistry()
r := NewRegistry()

r.RegisterList("my.reloadable", objl)

assert.Equal(t, objl, r.Get("my.reloadable"))
}

func TestRegisterNilFails(t *testing.T) {
r := newRegistry()
r := NewRegistry()

err := r.Register("name", nil)
assert.Error(t, err)
Expand All @@ -58,7 +58,7 @@ func TestRegisterNilFails(t *testing.T) {
}

func TestReRegisterFails(t *testing.T) {
r := newRegistry()
r := NewRegistry()

// two obj with the same name
err := r.Register("name", reloadable{})
Expand Down
8 changes: 6 additions & 2 deletions x-pack/libbeat/management/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func NewConfigManager(registry *reload.Registry, beatUUID uuid.UUID) (management
if err := c.Load(); err != nil {
return nil, errors.Wrap(err, "reading central management internal settings")
}
return NewConfigManagerWithConfig(c, registry, beatUUID)
}

// NewConfigManagerWithConfig returns a X-Pack Beats Central Management manager
func NewConfigManagerWithConfig(c *Config, registry *reload.Registry, beatUUID uuid.UUID) (management.ConfigManager, error) {
var client *api.Client
if c.Enabled {
var err error
Expand Down Expand Up @@ -159,7 +163,7 @@ func (cm *ConfigManager) apply() {
func (cm *ConfigManager) reload(t string, blocks []*api.ConfigBlock) {
cm.logger.Infof("Applying settings for %s", t)

if obj := reload.Register.Get(t); obj != nil {
if obj := cm.registry.Get(t); obj != nil {
// Single object
if len(blocks) != 1 {
cm.logger.Errorf("got an invalid number of configs for %s: %d, expected: 1", t, len(blocks))
Expand All @@ -174,7 +178,7 @@ func (cm *ConfigManager) reload(t string, blocks []*api.ConfigBlock) {
if err := obj.Reload(config); err != nil {
cm.logger.Error(err)
}
} else if obj := reload.Register.GetList(t); obj != nil {
} else if obj := cm.registry.GetList(t); obj != nil {
// List
var configs []*reload.ConfigWithMeta
for _, block := range blocks {
Expand Down
93 changes: 93 additions & 0 deletions x-pack/libbeat/management/manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package management

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/satori/go.uuid"
"github.com/stretchr/testify/assert"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/reload"
"github.com/elastic/beats/x-pack/libbeat/management/api"
)

type reloadable struct {
reloaded chan *reload.ConfigWithMeta
}

func (r *reloadable) Reload(c *reload.ConfigWithMeta) error {
r.reloaded <- c
return nil
}

func TestConfigManager(t *testing.T) {
registry := reload.NewRegistry()
id := uuid.NewV4()
accessToken := "footoken"
reloadable := reloadable{
reloaded: make(chan *reload.ConfigWithMeta, 1),
}
registry.MustRegister("test.block", &reloadable)

mux := http.NewServeMux()
i := 0
responses := []string{
// Initial load
`{"configuration_blocks":[{"type":"test.block","config":{"module":"apache2"}}]}`,

// No change, no reload
`{"configuration_blocks":[{"type":"test.block","config":{"module":"apache2"}}]}`,

// Changed, reload
`{"configuration_blocks":[{"type":"test.block","config":{"module":"system"}}]}`,
}
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, fmt.Sprintf("/api/beats/agent/%s/configuration", id), r.RequestURI)
fmt.Fprintf(w, responses[i])
i++
}))

server := httptest.NewServer(mux)

c, err := api.ConfigFromURL(server.URL)
if err != nil {
t.Fatal(err)
}

config := &Config{
Enabled: true,
Period: 100 * time.Millisecond,
Kibana: c,
AccessToken: accessToken,
}

manager, err := NewConfigManagerWithConfig(config, registry, id)
if err != nil {
t.Fatal(err)
}

manager.Start()

// On first reload we will get apache2 module
config1 := <-reloadable.reloaded
assert.Equal(t, &reload.ConfigWithMeta{
Config: common.MustNewConfigFrom(map[string]interface{}{
"module": "apache2",
}),
}, config1)

config2 := <-reloadable.reloaded
assert.Equal(t, &reload.ConfigWithMeta{
Config: common.MustNewConfigFrom(map[string]interface{}{
"module": "system",
}),
}, config2)
}

0 comments on commit 5a3bf53

Please sign in to comment.