Skip to content

Commit

Permalink
Merge pull request #3 from opsfactory/tests/coverage
Browse files Browse the repository at this point in the history
Tests and fixes by @fntlnz
  • Loading branch information
atosatto authored Jul 14, 2016
2 parents b0f086e + 4efd752 commit c065501
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 33 deletions.
12 changes: 10 additions & 2 deletions cmd/kappa/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"github.com/opsfactory/kappa/config"
"github.com/opsfactory/kappa/container/backend"
"github.com/opsfactory/kappa/engine"
"github.com/opsfactory/kappa/version"

Expand All @@ -22,6 +23,7 @@ func main() {
app.Version = version.FullVersion()
app.Author = "@opsfactory"
app.Usage = "Native docker autoscaling for the most popular orchestration frameworks."

app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, C",
Expand All @@ -32,6 +34,7 @@ func main() {
Usage: "Enable debug logging",
},
}

app.Before = func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
Expand All @@ -45,21 +48,26 @@ func main() {

return nil
}

app.Action = func(ctx *cli.Context) error {
log.Infof("Reading config from %s.", configFile)
cfg, err := config.NewConfigFromFile(configFile)
if err != nil {
log.Fatalf("Unexpected error parsing configuration: %v", err)
}

eng, err := engine.NewEngine(cfg)
b, err := backend.NewBackend(cfg.Backend, cfg.BackendConfig)
if err != nil {
log.Fatalf("Unexpected error starting Kappa with the given configuration: %v", err)
log.Fatalf("Unable to create backend %s: %v", cfg.Backend, err)
return err
}

eng := engine.NewEngine(b)
return eng.Run()
}

if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}

}
6 changes: 5 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ func parse(y []byte) (Config, error) {
return c, nil
}

func NewConfigFromByteArray(config []byte) (Config, error) {
return parse(config)
}

func NewConfigFromFile(file string) (Config, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
log.Fatalf("Unable to read config file %s: %v.", file, err)
return Config{}, nil
}
return parse(data)
return NewConfigFromByteArray(data)
}

func (c Config) Print() {
Expand Down
8 changes: 6 additions & 2 deletions container/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type Action struct {
}

func (a Action) String() string {
return fmt.Sprintf("Action{Container: %s, Command: %s, Unit: %d}",
a.Container, a.Command, a.Unit)
return fmt.Sprintf(
"Action{Container: %s, Command: %s, Unit: %d}",
a.Container,
a.Command,
a.Unit,
)
}
43 changes: 43 additions & 0 deletions container/action/action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package action

import (
"testing"

"github.com/opsfactory/kappa/container"
"github.com/opsfactory/kappa/container/label"
)

func TestActionString(t *testing.T) {
ct := container.NewContainer()
ct.Name = "john"
ct.Backend = container.MesosBackend
ct.Replicas = []string{}
ct.NumReplicas = 0
ct.DesiredReplicas = 2
lbls := label.NewLabelContainer()
lbls.Max = "10"
lbls.Min = "1"
lbls.Rate = "1"
lbls.Metric = "mymetric"
ct.Labels = lbls

command := ScaleUp
var unit ActionUnit
unit = 1
a := Action{
Container: &ct,
Command: command,
Unit: unit,
}

estr := "Action{Container: Container{Name: john, Labels: LabelContainer{Min: 1, Max: 10, Rate: 1, Metric: mymetric}, Replicas: [], NumReplicas: 0, DesiredReplicas: 2, Backend: mesos}, Command: ScaleUP, Unit: 1}"

res := a.String()
if res != estr {
t.Errorf(
"Expected string and Action string does not match:\nExpected:%s\nGiven:%s",
estr,
res,
)
}
}
4 changes: 2 additions & 2 deletions container/backend/docker/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func startHandlerBuilder(d *Docker, ech chan<- kappaevent.Event) handlerFunc {
return
}
log.Debugf("[Docker][EventHandler][Start] ID: %s Name: %s Labels: %s", c.Replicas[0], c.Name, c.Labels)
ech <- kappaevent.NewContainerStartEvent(&c)
ech <- kappaevent.NewContainerStartEvent(c)
}
}

Expand All @@ -26,6 +26,6 @@ func dieHandlerBuilder(d *Docker, ech chan<- kappaevent.Event) handlerFunc {
return
}
log.Debugf("[Docker][EventHandler][Die] ID: %s Name: %s", c.Replicas[0], c.Name)
ech <- kappaevent.NewContainerDieEvent(&c)
ech <- kappaevent.NewContainerDieEvent(c)
}
}
24 changes: 10 additions & 14 deletions container/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
)

type Event struct {
Container *container.Container
Container container.Container
Type EventType
}

Expand All @@ -23,20 +23,16 @@ func (ev Event) String() string {
ev.Container, ev.Type)
}

func NewEvent() Event {
return Event{}
func newEvent(c container.Container, t EventType) Event {
return Event{
Container: c,
Type: t,
}
}

func NewContainerStartEvent(c *container.Container) Event {
e := NewEvent()
e.Container = c
e.Type = ContainerStart
return e
func NewContainerStartEvent(c container.Container) Event {
return newEvent(c, ContainerStart)
}

func NewContainerDieEvent(c *container.Container) Event {
e := NewEvent()
e.Container = c
e.Type = ContainerDie
return e
func NewContainerDieEvent(c container.Container) Event {
return newEvent(c, ContainerDie)
}
25 changes: 25 additions & 0 deletions container/event/event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package event

import (
"testing"

"github.com/opsfactory/kappa/container"
)

func TestNewContainerStartEvent(t *testing.T) {
c := container.NewContainer()
ev := NewContainerStartEvent(c)

if ev.Type != ContainerStart {
t.Error("event type should be `Start`, not: ", ev.Type)
}
}

func TestNewContainerDieEvent(t *testing.T) {
c := container.NewContainer()
ev := NewContainerDieEvent(c)

if ev.Type != ContainerDie {
t.Error("event type should be `Die`, not: ", ev.Type)
}
}
19 changes: 7 additions & 12 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@ package engine

import (
log "github.com/Sirupsen/logrus"
"github.com/opsfactory/kappa/config"
"github.com/opsfactory/kappa/container/action"
"github.com/opsfactory/kappa/container/backend"
"github.com/opsfactory/kappa/container/event"
)

type Engine struct {
cfg config.Config
backend backend.Backend
}

func NewEngine(cfg config.Config) (*Engine, error) {
b, err := backend.NewBackend(cfg.Backend, cfg.BackendConfig)
if err != nil {
log.Fatalf("Unable to create backend %s: %v", cfg.Backend, err)
return nil, err
}

return &Engine{cfg: cfg, backend: b}, nil
func NewEngine(b backend.Backend) *Engine {
return &Engine{backend: b}
}

func (e Engine) Run() error {
Expand All @@ -40,8 +32,11 @@ func (e Engine) Run() error {
return nil
}

func (e Engine) handleEvent(eventsChan <-chan event.Event,
actionsChan <-chan action.Action, errChan chan<- error) {
func (e Engine) handleEvent(
eventsChan <-chan event.Event,
actionsChan <-chan action.Action,
errChan chan<- error,
) {
for ev := range eventsChan {
log.Infof("[EVENT] %s", ev)
}
Expand Down
31 changes: 31 additions & 0 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package engine

import (
"fmt"
"testing"

"github.com/opsfactory/kappa/container/action"
"github.com/opsfactory/kappa/container/event"
)

type stubBackend struct {
}

func (b stubBackend) Monitor(eventsChan chan<- event.Event, errChan chan<- error) {
errChan <- fmt.Errorf("I am meant to interrupt")
}

func (b stubBackend) Exec(eventsChan chan<- action.Action, errChan chan<- error) {

}

// TODO: update this tests once the actual Run function is in the process of being implemented
func TestEngineRun(t *testing.T) {
b := stubBackend{}
e := NewEngine(b)
res := e.Run()

if res == nil {
t.Error("An error was expected")
}
}

0 comments on commit c065501

Please sign in to comment.