From 1dbd2dec0027e0deefb102d31e5406707e3be0e7 Mon Sep 17 00:00:00 2001 From: chrsblck Date: Sun, 31 Jan 2016 02:30:09 -0800 Subject: [PATCH] Improve config handling - Add constants for default values - Comment out config for vars that will be defaulted in main ~/redisbeat.yml - Add test yaml file under tests/ - Add Config unit tests --- beat/config.go | 22 +++++++++++++++ beat/redisbeat.go | 34 +++++++++++------------ beat/redisbeat_test.go | 63 +++++++++++++++++++++++++++++++++++++++++- redisbeat.yml | 34 +++++++++++------------ tests/redisbeat.yml | 58 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+), 35 deletions(-) create mode 100644 tests/redisbeat.yml diff --git a/beat/config.go b/beat/config.go index 715d5f7..456522a 100644 --- a/beat/config.go +++ b/beat/config.go @@ -1,5 +1,27 @@ package beat +import "time" + +const ( + DEFAULT_PERIOD time.Duration = 10 * time.Second + DEFAULT_HOST string = "localhost" + DEFAULT_PORT int = 6379 + DEFAULT_NETWORK string = "tcp" + DEFAULT_MAX_CONN int = 10 + DEFAULT_AUTH_REQUIRED bool = false + DEFAULT_AUTH_REQUIRED_PASS string = "" + DEFAULT_STATS_SERVER bool = true + DEFAULT_STATS_CLIENT bool = true + DEFAULT_STATS_MEMORY bool = true + DEFAULT_STATS_PERSISTENCE bool = true + DEFAULT_STATS_STATS bool = true + DEFAULT_STATS_REPLICATION bool = true + DEFAULT_STATS_CPU bool = true + DEFAULT_STATS_COMMAND bool = true + DEFAULT_STATS_CLUSTER bool = true + DEFAULT_STATS_KEYSPACE bool = true +) + type RedisConfig struct { Period *int64 Host *string diff --git a/beat/redisbeat.go b/beat/redisbeat.go index 22c77bf..94d0a17 100644 --- a/beat/redisbeat.go +++ b/beat/redisbeat.go @@ -56,103 +56,103 @@ func (rb *Redisbeat) Config(b *beat.Beat) error { if rb.RbConfig.Input.Period != nil { rb.period = time.Duration(*rb.RbConfig.Input.Period) * time.Second } else { - rb.period = 1 * time.Second + rb.period = DEFAULT_PERIOD } if rb.RbConfig.Input.Host != nil { rb.host = *rb.RbConfig.Input.Host } else { - rb.host = "localhost" + rb.host = DEFAULT_HOST } if rb.RbConfig.Input.Port != nil { rb.port = *rb.RbConfig.Input.Port } else { - rb.port = 6379 + rb.port = DEFAULT_PORT } if rb.RbConfig.Input.Network != nil { rb.network = *rb.RbConfig.Input.Network } else { - rb.network = "tcp" + rb.network = DEFAULT_NETWORK } if rb.RbConfig.Input.MaxConn != nil { rb.maxConn = *rb.RbConfig.Input.MaxConn } else { - rb.maxConn = 10 + rb.maxConn = DEFAULT_MAX_CONN } if rb.RbConfig.Input.Auth.Required != nil { rb.auth = *rb.RbConfig.Input.Auth.Required } else { - rb.auth = false + rb.auth = DEFAULT_AUTH_REQUIRED } if rb.RbConfig.Input.Auth.RequiredPass != nil { rb.pass = *rb.RbConfig.Input.Auth.RequiredPass } else { - rb.pass = "" + rb.pass = DEFAULT_AUTH_REQUIRED_PASS } if rb.RbConfig.Input.Stats.Server != nil { rb.serverStats = *rb.RbConfig.Input.Stats.Server } else { - rb.serverStats = true + rb.serverStats = DEFAULT_STATS_SERVER } if rb.RbConfig.Input.Stats.Clients != nil { rb.clientsStats = *rb.RbConfig.Input.Stats.Clients } else { - rb.clientsStats = true + rb.clientsStats = DEFAULT_STATS_CLIENT } if rb.RbConfig.Input.Stats.Memory != nil { rb.memoryStats = *rb.RbConfig.Input.Stats.Memory } else { - rb.memoryStats = true + rb.memoryStats = DEFAULT_STATS_MEMORY } if rb.RbConfig.Input.Stats.Persistence != nil { rb.persistenceStats = *rb.RbConfig.Input.Stats.Persistence } else { - rb.persistenceStats = true + rb.persistenceStats = DEFAULT_STATS_PERSISTENCE } if rb.RbConfig.Input.Stats.Stats != nil { rb.statsStats = *rb.RbConfig.Input.Stats.Stats } else { - rb.statsStats = true + rb.statsStats = DEFAULT_STATS_STATS } if rb.RbConfig.Input.Stats.Replication != nil { rb.replicationStats = *rb.RbConfig.Input.Stats.Replication } else { - rb.replicationStats = true + rb.replicationStats = DEFAULT_STATS_REPLICATION } if rb.RbConfig.Input.Stats.Cpu != nil { rb.cpuStats = *rb.RbConfig.Input.Stats.Cpu } else { - rb.cpuStats = true + rb.cpuStats = DEFAULT_STATS_CPU } if rb.RbConfig.Input.Stats.Commandstats != nil { rb.commandStats = *rb.RbConfig.Input.Stats.Commandstats } else { - rb.commandStats = true + rb.commandStats = DEFAULT_STATS_COMMAND } if rb.RbConfig.Input.Stats.Cluster != nil { rb.clusterStats = *rb.RbConfig.Input.Stats.Cluster } else { - rb.clusterStats = true + rb.clusterStats = DEFAULT_STATS_CLUSTER } if rb.RbConfig.Input.Stats.Keyspace != nil { rb.keyspaceStats = *rb.RbConfig.Input.Stats.Keyspace } else { - rb.keyspaceStats = true + rb.keyspaceStats = DEFAULT_STATS_KEYSPACE } logp.Debug("redisbeat", "Init redisbeat") diff --git a/beat/redisbeat_test.go b/beat/redisbeat_test.go index 4820937..d03eb92 100644 --- a/beat/redisbeat_test.go +++ b/beat/redisbeat_test.go @@ -1,11 +1,72 @@ package beat import ( + "os" "testing" + "time" + beat "github.com/elastic/beats/libbeat/beat" + cfg "github.com/elastic/beats/libbeat/cfgfile" "github.com/stretchr/testify/assert" ) +func TestDefaultConfig(t *testing.T) { + // modify cfgfile's default location + os.Args = []string{"../redisbeat.yaml"} + cfg.ChangeDefaultCfgfileFlag("redisbeat") + + rb := New() + err := rb.Config(&beat.Beat{}) + assert.Nil(t, err) + + assert.Equal(t, DEFAULT_PERIOD, rb.period, "Default time period should be %v", DEFAULT_PERIOD) + assert.Equal(t, DEFAULT_HOST, rb.host, "Default host should be %v", DEFAULT_HOST) + assert.Equal(t, DEFAULT_PORT, rb.port, "Default port should be %v", DEFAULT_PORT) + assert.Equal(t, DEFAULT_NETWORK, rb.network, "Default network should be %v", DEFAULT_NETWORK) + assert.Equal(t, DEFAULT_MAX_CONN, rb.maxConn, "Default max connections should be %v", DEFAULT_MAX_CONN) + assert.Equal(t, DEFAULT_AUTH_REQUIRED, rb.auth, "Default auth required should be %v", DEFAULT_AUTH_REQUIRED) + assert.Equal(t, DEFAULT_AUTH_REQUIRED_PASS, rb.pass, "Default auth required pass should be %v", DEFAULT_AUTH_REQUIRED_PASS) + assert.Equal(t, DEFAULT_STATS_SERVER, rb.serverStats, "Default server stats should be %v", DEFAULT_STATS_SERVER) + assert.Equal(t, DEFAULT_STATS_CLIENT, rb.clientsStats, "Default client stats should be %v", DEFAULT_STATS_CLIENT) + assert.Equal(t, DEFAULT_STATS_MEMORY, rb.memoryStats, "Default memory stats should be %v", DEFAULT_STATS_MEMORY) + assert.Equal(t, DEFAULT_STATS_PERSISTENCE, rb.persistenceStats, "Default persistence stats should be %v", DEFAULT_STATS_PERSISTENCE) + assert.Equal(t, DEFAULT_STATS_STATS, rb.statsStats, "Default stats stats should be %v", DEFAULT_STATS_STATS) + assert.Equal(t, DEFAULT_STATS_REPLICATION, rb.replicationStats, "Default replication stats should be %v", DEFAULT_STATS_REPLICATION) + assert.Equal(t, DEFAULT_STATS_CPU, rb.cpuStats, "Default cpu stats should be %v", DEFAULT_STATS_CPU) + assert.Equal(t, DEFAULT_STATS_COMMAND, rb.commandStats, "Default command stats should be %v", DEFAULT_STATS_COMMAND) + assert.Equal(t, DEFAULT_STATS_CLUSTER, rb.clusterStats, "Default cluster stats should be %v", DEFAULT_STATS_CLUSTER) + assert.Equal(t, DEFAULT_STATS_KEYSPACE, rb.keyspaceStats, "Default keyspace stats should be %v", DEFAULT_STATS_KEYSPACE) +} + +func TestModifiedConfig(t *testing.T) { + // modify cfgfile's default location + os.Args = []string{"../tests/redisbeat.yml"} + cfg.ChangeDefaultCfgfileFlag("redisbeat") + + rb := New() + err := rb.Config(&beat.Beat{}) + assert.Nil(t, err) + + expectedTime := 5 * time.Second + assert.Equal(t, expectedTime, rb.period, "Configured time period should be %v", expectedTime) + assert.Equal(t, "redis.testing.fake", rb.host, "Configured host should be %v", "redis.testing.fake") + assert.Equal(t, 9736, rb.port, "Configured port should be %v", 9736) + assert.Equal(t, "udp", rb.network, "Configured network should be %v", "udp") + assert.Equal(t, 5, rb.maxConn, "Configured max connections should be %v", 5) + assert.Equal(t, true, rb.auth, "Configured auth required should be %v", true) + assert.Equal(t, "p@ssw0rd", rb.pass, "Configured auth required pass should be %v", "p@ssw0rd") + assert.Equal(t, true, rb.serverStats, "Configured server stats should be %v", true) + assert.Equal(t, false, rb.clientsStats, "Configured client stats should be %v", false) + assert.Equal(t, false, rb.memoryStats, "Configured memory stats should be %v", false) + assert.Equal(t, false, rb.persistenceStats, "Configured persistence stats should be %v", false) + assert.Equal(t, false, rb.statsStats, "Configured stats stats should be %v", false) + assert.Equal(t, false, rb.replicationStats, "Configured replication stats should be %v", false) + assert.Equal(t, false, rb.cpuStats, "Configured cpu stats should be %v", false) + assert.Equal(t, false, rb.commandStats, "Configured command stats should be %v", false) + assert.Equal(t, false, rb.clusterStats, "Configured cluster stats should be %v", false) + assert.Equal(t, false, rb.keyspaceStats, "Configured keyspace stats should be %v", false) +} + func TestConvertReplyToMap(t *testing.T) { testReplyString := "# Server\r\nredis_version:3.0.0\r\nredis_mode:standalone\r\nmultiplexing_api:epoll\r\n" replyMap, err := convertReplyToMap(testReplyString) @@ -13,4 +74,4 @@ func TestConvertReplyToMap(t *testing.T) { assert.Equal(t, "3.0.0", replyMap["redis_version"], "Redis version should be 3.0.0") assert.Equal(t, "standalone", replyMap["redis_mode"], "Redis mode should be standalone") assert.Equal(t, "epoll", replyMap["multiplexing_api"], "Redis multiplexing api should be epoll") -} \ No newline at end of file +} diff --git a/redisbeat.yml b/redisbeat.yml index a1292ca..47c67d0 100644 --- a/redisbeat.yml +++ b/redisbeat.yml @@ -3,58 +3,58 @@ ############################# Input ############################################ input: # In seconds, defines how often to read server statistics - period: 10 + #period: 10 # Host of redis server, default is localhost - host: "localhost" + #host: "localhost" # Port the redis server is listening on, default is 6379 - port: 6379 + #port: 6379 # Network type, default is tcp - network: "tcp" + #network: "tcp" # Max connections for redis pool, default is 10 - maxconn: 10 + #maxconn: 10 # Authentication config auth: # default is false - required: true + #required: true # default is empty string - required_pass: "p@ssw0rd" + #required_pass: "p@ssw0rd" # Statistics to collect (all enabled by default) stats: # server information - server: true + #server: true # clients information - clients: true + #clients: true # memory information - memory: true + #memory: true # persistence information - persistence: true + #persistence: true # stats information - stats: true + #stats: true # replication information - replication: true + #replication: true # cpu information - cpu: true + #cpu: true # commandstats information - commandstats: true + #commandstats: true # cluster information - cluster: true + #cluster: true # keyspace information - keyspace: true + #keyspace: true ############################################################################### ############################# Libbeat Config ################################## # Base config file used by all other beats for using libbeat features diff --git a/tests/redisbeat.yml b/tests/redisbeat.yml new file mode 100644 index 0000000..1e9386f --- /dev/null +++ b/tests/redisbeat.yml @@ -0,0 +1,58 @@ +################### Redisbeat Test Config ######################### + +############################# Input ############################################ +input: + # In seconds, defines how often to read server statistics + period: 5 + + # Host of redis server, default is localhost + host: "redis.testing.fake" + + # Port the redis server is listening on, default is 6379 + port: 9736 + + # Network type, default is tcp + network: "udp" + + # Max connections for redis pool, default is 10 + maxconn: 5 + + # Authentication config + auth: + # default is false + required: true + # default is empty string + required_pass: "p@ssw0rd" + + # Statistics to collect (all enabled by default) + stats: + # server information + server: true + + # clients information + clients: false + + # memory information + memory: false + + # persistence information + persistence: false + + # stats information + stats: false + + # replication information + replication: false + + # cpu information + cpu: false + + # commandstats information + commandstats: false + + # cluster information + cluster: false + + # keyspace information + keyspace: false +