Skip to content

Commit

Permalink
Implement the config package
Browse files Browse the repository at this point in the history
  • Loading branch information
marema31 committed Feb 21, 2019
1 parent 5542f5d commit 5f6217c
Show file tree
Hide file tree
Showing 10 changed files with 390 additions and 0 deletions.
37 changes: 37 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"github.com/spf13/viper"
)

// Default value
var defaults = map[string]interface{}{
"out_file": "/tmp/jocasta_stdout.log",
"out_maxsize": "0",
"out_backups": "0",
"err_file": "/tmp/jocasta_stderr.log",
"err_maxsize": "0",
"err_backups": "0",
}

// Config implements the config store of jocasta
type Config struct {
v *viper.Viper
}

// New initialize the config store
func New(path string, filename string) (*Config, error) {
v := viper.New()
for key, value := range defaults {
v.SetDefault(key, value)
}

v.SetConfigName(filename) // The file will be named [filename].json, [filename].yaml or [filename.toml]
v.AddConfigPath(path)
v.SetEnvPrefix("jocasta")
v.AutomaticEnv()
err := v.ReadInConfig()

config := &Config{v: v}
return config, err
}
21 changes: 21 additions & 0 deletions config/err.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

// Errfile return the filename for stdErr logs
func (c *Config) ErrFile() string {
return c.v.GetString("err_file")
}

// ErrMaxSize return the max size of stdstderr log file before rotation
func (c *Config) ErrMaxSize() (int, error) {
size, err := sizeFromString(c.v.GetString("err_maxsize"))
if err != nil {
return 0, err
}

return size, nil
}

// ErrBackups return the number of historical files for stdstderr logs
func (c *Config) ErrBackups() int {
return c.v.GetInt("err_backups")
}
81 changes: 81 additions & 0 deletions config/err_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package config

import (
"os"
"testing"
)

func TestErrDefaultValue(t *testing.T) {
defaultFile := "/tmp/jocasta_stderr.log"
defaultSize := 0
defaultBackups := 0

c, err := New("testdata", "empty")
if err != nil {
t.Errorf("Can not read the configuration file. err=%v", err)
}

maxsize, err := c.ErrMaxSize()
if err != nil {
t.Errorf("Can not retrieve err.maxsize. err=%v", err)
}
if maxsize != defaultSize {
t.Errorf("Default value for err.maxsize is not correct, should be %d and I receive %d", defaultSize, maxsize)
}

backups := c.ErrBackups()
if backups != defaultBackups {
t.Errorf("Default value for err.backups is not correct, should be %d and I receive %d", defaultBackups, backups)
}

file := c.ErrFile()
if file != defaultFile {
t.Errorf("Default value for err.file is not correct, should be %s and I receive %s", defaultFile, file)
}
}

func TestErrFromFile(t *testing.T) {
defaultFile := "/tmp/correct_err.log"
defaultSize := 2 * 1024 * 1024
defaultBackups := 3

c, err := New("testdata", "correct")
if err != nil {
t.Errorf("Can not read the configuration file. err=%v", err)
}

maxsize, err := c.ErrMaxSize()
if err != nil {
t.Errorf("Can not retrieve err.maxsize. err=%v", err)
}
if maxsize != defaultSize {
t.Errorf("Retrieved value for err.maxsize is not correct, should be %d and I receive %d", defaultSize, maxsize)
}

backups := c.ErrBackups()
if backups != defaultBackups {
t.Errorf("Retrieved value for err.backups is not correct, should be %d and I receive %d", defaultBackups, backups)
}

file := c.ErrFile()
if file != defaultFile {
t.Errorf("Retrieved value for err.file is not correct, should be %s and I receive %s", defaultFile, file)
}
}

func TestIncorrectErrSize(t *testing.T) {

str := "10KG"

os.Setenv("JOCASTA_ERR_MAXSIZE", str)
c, err := New("testdata", "empty")
if err != nil {
t.Errorf("Can not read the configuration file. err=%v", err)
}

maxsize, err := c.ErrMaxSize()
if err == nil {
t.Errorf("No error on invalid err.maxsize string. Calculated size=%d for '%s'", maxsize, str)
}

}
21 changes: 21 additions & 0 deletions config/out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

// Outfile return the filename for stdout logs
func (c *Config) OutFile() string {
return c.v.GetString("out_file")
}

// OutMaxSize return the max size of stdout log file before rotation
func (c *Config) OutMaxSize() (int, error) {
size, err := sizeFromString(c.v.GetString("out_maxsize"))
if err != nil {
return 0, err
}

return size, nil
}

// OutBackups return the number of historical files for stdout logs
func (c *Config) OutBackups() int {
return c.v.GetInt("out_backups")
}
99 changes: 99 additions & 0 deletions config/out_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package config

import (
"os"
"testing"
)

func TestOutDefaultValue(t *testing.T) {
defaultFile := "/tmp/jocasta_stdout.log"
defaultSize := 0
defaultBackups := 0

c, err := New("testdata", "empty")
if err != nil {
t.Errorf("Can not read the configuration file. err=%v", err)
}

maxsize, err := c.OutMaxSize()
if err != nil {
t.Errorf("Can not retrieve out.maxsize. err=%v", err)
}
if maxsize != defaultSize {
t.Errorf("Default value for out.maxsize is not correct, should be %d and I receive %d", defaultSize, maxsize)
}

backups := c.OutBackups()
if backups != defaultBackups {
t.Errorf("Default value for out.backups is not correct, should be %d and I receive %d", defaultBackups, backups)
}

file := c.OutFile()
if file != defaultFile {
t.Errorf("Default value for out.file is not correct, should be %s and I receive %s", defaultFile, file)
}
}

func TestOutRetrievedFromFile(t *testing.T) {
defaultFile := "/tmp/correct.log"
defaultSize := 1024 * 1024
defaultBackups := 2

c, err := New("testdata", "correct")
if err != nil {
t.Errorf("Can not read the configuration file. err=%v", err)
}

maxsize, err := c.OutMaxSize()
if err != nil {
t.Errorf("Can not retrieve out.maxsize. err=%v", err)
}
if maxsize != defaultSize {
t.Errorf("Retrieved value for out.maxsize is not correct, should be %d and I receive %d", defaultSize, maxsize)
}

backups := c.OutBackups()
if backups != defaultBackups {
t.Errorf("Retrieved value for out.backups is not correct, should be %d and I receive %d", defaultBackups, backups)
}

file := c.OutFile()
if file != defaultFile {
t.Errorf("Retrieved value for out.file is not correct, should be %s and I receive %s", defaultFile, file)
}
}

func TestOutEnv(t *testing.T) {

str := "dummy.log"

os.Setenv("JOCASTA_OUT_FILE", str)

c, err := New("testdata", "empty")
if err != nil {
t.Errorf("Can not read the configuration file. err=%v", err)
}

file := c.OutFile()
if file != str {
t.Errorf("Not able to retrieve out.file from environment variable . Found=%s for '%s'", file, str)
}

}

func TestIncorrectOutSize(t *testing.T) {

str := "10KG"

os.Setenv("JOCASTA_OUT_MAXSIZE", str)
c, err := New("testdata", "empty")
if err != nil {
t.Errorf("Can not read the configuration file. err=%v", err)
}

maxsize, err := c.OutMaxSize()
if err == nil {
t.Errorf("No error on invalid out.maxsize string. Calculated size=%d for '%s'", maxsize, str)
}

}
6 changes: 6 additions & 0 deletions config/testdata/correct.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
out_file: /tmp/correct.log
out_maxsize: 1MB
out_backups: 2
err_file: /tmp/correct_err.log
err_maxsize: 2MB
err_backups: 3
Empty file added config/testdata/empty.yaml
Empty file.
40 changes: 40 additions & 0 deletions config/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

import (
"math"
"strconv"
"strings"
)

func sizeFromString(str string) (int, error) {
var power float64
var strValue string

switch {
case strings.Contains(str, "TB"):
power = 4
strValue = str[:len(str)-2]
case strings.Contains(str, "GB"):
power = 3
strValue = str[:len(str)-2]
case strings.Contains(str, "MB"):
power = 2
strValue = str[:len(str)-2]
case strings.Contains(str, "KB"):
power = 1
strValue = str[:len(str)-2]
case strings.Contains(str, "B"):
power = 0
strValue = str[:len(str)-1]
default:
power = 0
strValue = str
}

value, err := strconv.Atoi(strValue)
if err != nil {
return 0, err
}

return value * int(math.Pow(1024, power)), nil
}
80 changes: 80 additions & 0 deletions config/tools_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package config

import (
"testing"
)

func TestIncorrectSize(t *testing.T) {

str := "10KG"

size, err := sizeFromString(str)
if err == nil {
t.Errorf("No error on invalid size string. Calculated size=%d for '%s'", size, str)
}

}

func TestCorrectSize(t *testing.T) {

str := "42"
waited := 42
size, err := sizeFromString(str)
if err != nil {
t.Errorf("Error while converting for '%s', err=%v", str, err)
}
if size != waited {
t.Errorf("Wrong conversion. Calculated size=%d for '%s', should be %d", size, str, waited)
}

str = "42B"
waited = 42
size, err = sizeFromString(str)
if err != nil {
t.Errorf("Error while converting for '%s', err=%v", str, err)
}
if size != waited {
t.Errorf("Wrong conversion. Calculated size=%d for '%s', should be %d", size, str, waited)
}

str = "42KB"
waited = 42 * 1024
size, err = sizeFromString(str)
if err != nil {
t.Errorf("Error while converting for '%s', err=%v", str, err)
}
if size != waited {
t.Errorf("Wrong conversion. Calculated size=%d for '%s', should be %d", size, str, waited)
}

str = "42MB"
waited = 42 * 1024 * 1024
size, err = sizeFromString(str)
if err != nil {
t.Errorf("Error while converting for '%s', err=%v", str, err)
}
if size != waited {
t.Errorf("Wrong conversion. Calculated size=%d for '%s', should be %d", size, str, waited)
}

str = "42GB"
waited = 42 * 1024 * 1024 * 1024
size, err = sizeFromString(str)
if err != nil {
t.Errorf("Error while converting for '%s', err=%v", str, err)
}
if size != waited {
t.Errorf("Wrong conversion. Calculated size=%d for '%s', should be %d", size, str, waited)
}

str = "42TB"
waited = 42 * 1024 * 1024 * 1024 * 1024
size, err = sizeFromString(str)
if err != nil {
t.Errorf("Error while converting for '%s', err=%v", str, err)
}
if size != waited {
t.Errorf("Wrong conversion. Calculated size=%d for '%s', should be %d", size, str, waited)
}

}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {

}

0 comments on commit 5f6217c

Please sign in to comment.