Skip to content

Commit

Permalink
[simulator][machine][config] Init reading config
Browse files Browse the repository at this point in the history
- Add machine simulator config struct, which supports: num, start, end, step
- add util function for read config file in test (TODO: don't know if it will have sideffect if is called multiple times)
  • Loading branch information
at15 committed Nov 21, 2016
1 parent e3e9261 commit 24e067d
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 5 deletions.
25 changes: 25 additions & 0 deletions pkg/config/machine_simulator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package config

import (
"time"

"github.com/spf13/viper"
)

// MachineSimulatorConfig defines the configurable property for the machine simulator
type MachineSimulatorConfig struct {
Num int
Start time.Time
End time.Time
Step int
}

// ReadMachineSimulatorConfigFromViper return a config struct using configuration in yml
func ReadMachineSimulatorConfigFromViper() *MachineSimulatorConfig {
c := &MachineSimulatorConfig{}
c.Num = viper.GetInt("simulator.machine.num")
c.Start = viper.GetTime("simulator.machine.start")
c.End = viper.GetTime("simulator.machine.end")
c.Step = viper.GetInt("simulator.machine.step")
return c
}
12 changes: 12 additions & 0 deletions pkg/config/machine_simulator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package config

import (
"testing"
"github.com/xephonhq/xephon-b/pkg/util"
)

func TestReadMachineSimulatorConfigFromViper(t *testing.T) {
util.ViperReadTestConfig()
c := ReadMachineSimulatorConfigFromViper()
t.Log(c)
}
4 changes: 2 additions & 2 deletions pkg/simulator/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/xephonhq/xephon-b/pkg/serialize"
)

var defaulMachineNumber int64 = 0
var defaultMachineNumber int64 = 0

type Machine struct {
Name string
Expand Down Expand Up @@ -109,7 +109,7 @@ func (ms *MachineSimulator) Start() {
}

func GenerateDefaultMachine() Machine {
num := atomic.AddInt64(&defaulMachineNumber, 1)
num := atomic.AddInt64(&defaultMachineNumber, 1)
return Machine{
Name: fmt.Sprintf("default-%d", num),
OS: "ubuntu16.04",
Expand Down
4 changes: 3 additions & 1 deletion pkg/util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"github.com/Sirupsen/logrus"
)

// Log util

// Logger is the default logger with info level
var Logger = logrus.New()

// Short name use in util package
var log = Logger.WithFields(logrus.Fields{
"pkg":"x.util",
"pkg": "x.util",
})

func init() {
Expand Down
24 changes: 24 additions & 0 deletions pkg/util/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package util

// Test util

import (
"path"
"runtime"

"github.com/spf13/viper"
)

// ViperReadTestConfig
func ViperReadTestConfig() {
// Get current file path https://coderwall.com/p/_fmbug/go-get-path-to-current-file
_, filename, _, ok := runtime.Caller(1)
if !ok {
log.Fatal("can't get current file path")
}

filePath := path.Join(path.Dir(filename), "../../xephon-b.yml")
log.Debug(filePath)
viper.SetConfigFile(filePath)
viper.ReadInConfig()
}
9 changes: 7 additions & 2 deletions xephon-b.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
ww# The example xephon-b configuration file
# The example xephon-b configuration file
# TODO: might be split to several config files like ansible
simulator:
machine:
num: 100
start:
# start time in ISO 8601
start: 1997-07-16T19:20:30.45+01:00
# end time in ISO 8601
end: 1997-07-16T19:20:31.45+01:00
# in second
step: 10

0 comments on commit 24e067d

Please sign in to comment.