Skip to content

Commit

Permalink
Refactor testing to use light config definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Castell committed Aug 24, 2018
1 parent e5cb765 commit ab311af
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 254 deletions.
50 changes: 27 additions & 23 deletions cmd/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,7 @@ It also runs a web UI.`,
// The returned value is the exit code.
// protoc -I proto/ proto/executor.proto --go_out=plugins=grpc:dkron/
RunE: func(cmd *cobra.Command, args []string) error {
legacyConfig()

// Make sure we clean up any managed plugins at the end of this
p := &Plugins{}
if err := p.DiscoverPlugins(); err != nil {
log.Fatal(err)
}
plugins := &dkron.Plugins{
Processors: p.Processors,
Executors: p.Executors,
}

agent = dkron.NewAgent(config, plugins)
if err := agent.Start(); err != nil {
return err
}

exit := handleSignals()
if exit != 0 {
return fmt.Errorf("Exit status: %d", exit)
}

return nil
return agentRun(args...)
},
}

Expand All @@ -67,6 +45,32 @@ func init() {
viper.BindPFlags(agentCmd.Flags())
}

func agentRun(args ...string) error {
legacyConfig()

// Make sure we clean up any managed plugins at the end of this
p := &Plugins{}
if err := p.DiscoverPlugins(); err != nil {
log.Fatal(err)
}
plugins := &dkron.Plugins{
Processors: p.Processors,
Executors: p.Executors,
}

agent = dkron.NewAgent(config, plugins)
if err := agent.Start(); err != nil {
return err
}

exit := handleSignals()
if exit != 0 {
return fmt.Errorf("Exit status: %d", exit)
}

return nil
}

// handleSignals blocks until we get an exit-causing signal
func handleSignals() int {
signalCh := make(chan os.Signal, 4)
Expand Down
50 changes: 12 additions & 38 deletions cmd/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ package cmd
import (
"os"
"testing"
"time"

"github.com/hashicorp/serf/testutil"
"github.com/mitchellh/cli"
)

var (
Expand All @@ -22,44 +18,22 @@ func getEnvWithDefault() string {
return ea
}

func TestAgentCommandRun(t *testing.T) {
shutdownCh := make(chan struct{})
defer close(shutdownCh)

ui := new(cli.MockUi)
a := &AgentCommand{
Ui: ui,
ShutdownCh: shutdownCh,
}

args := []string{
"-bind-addr", testutil.GetBindAddr().String(),
"-log-level", logLevel,
func Test_unmarshalTags(t *testing.T) {
tagPairs := []string{
"tag1=val1",
"tag2=val2",
}

resultCh := make(chan int)
go func() {
resultCh <- a.Run(args)
}()
tags, err := unmarshalTags(tagPairs)

time.Sleep(2 * time.Second)

// Verify it runs "forever"
select {
case <-resultCh:
t.Fatalf("ended too soon, err: %s", ui.ErrorWriter.String())
case <-time.After(50 * time.Millisecond):
if err != nil {
t.Fatalf("err: %s", err)
}

// Send a shutdown request
shutdownCh <- struct{}{}

select {
case code := <-resultCh:
if code != 0 {
t.Fatalf("bad code: %d", code)
}
case <-time.After(50 * time.Millisecond):
t.Fatalf("timeout")
if v, ok := tags["tag1"]; !ok || v != "val1" {
t.Fatalf("bad: %v", tags)
}
if v, ok := tags["tag2"]; !ok || v != "val2" {
t.Fatalf("bad: %v", tags)
}
}
34 changes: 34 additions & 0 deletions cmd/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"bytes"
"testing"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/victorcoder/dkron/dkron"
)

func TestReadConfigTags(t *testing.T) {
viper.Reset()
viper.SetConfigType("yaml")
var jsonConfig = []byte(`
tags:
- foo: bar
`)
viper.ReadConfig(bytes.NewBuffer(jsonConfig))
config := &dkron.Config{}
viper.Unmarshal(config)
t.Log(config.Tags)
assert.Equal(t, "bar", config.Tags["foo"])

viper.Set("tag", []string{"monthy=python"})
viper.Unmarshal(config)
assert.NotContains(t, config.Tags, "foo")
assert.Contains(t, config.Tags, "monthy")
assert.Equal(t, "python", config.Tags["monthy"])

config = &dkron.Config{Tags: map[string]string{"t1": "v1", "t2": "v2"}}
assert.Equal(t, "v1", config.Tags["t1"])
assert.Equal(t, "v2", config.Tags["t2"])
}
125 changes: 46 additions & 79 deletions dkron/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

var (
logLevel = "error"
logLevel = "debug"
etcdAddr = getEnvWithDefault()
)

Expand Down Expand Up @@ -46,17 +46,17 @@ func TestAgentCommand_runForElection(t *testing.T) {
}
}

args := []string{
"-bind-addr", a1Addr,
"-join", a2Addr,
"-node-name", a1Name,
"-server",
"-log-level", logLevel,
}
c := DefaultConfig()
c.BindAddr = a1Addr
c.StartJoin = []string{a2Addr}
c.NodeName = a1Name
c.Server = true
c.LogLevel = logLevel

c := NewConfig(args)
a1 := NewAgent(c, nil)
a1.Start()
if err := a1.Start(); err != nil {
t.Fatal(err)
}

// Wait for the first agent to start and set itself as leader
kv1, err := watchOrDie(client, "dkron/leader")
Expand All @@ -68,15 +68,13 @@ func TestAgentCommand_runForElection(t *testing.T) {
assert.Equal(t, a1Name, leaderA1)

// Start another agent
args2 := []string{
"-bind-addr", a2Addr,
"-join", a1Addr + ":8946",
"-node-name", a2Name,
"-server",
"-log-level", logLevel,
}
c = DefaultConfig()
c.BindAddr = a2Addr
c.StartJoin = []string{a1Addr + ":8946"}
c.NodeName = a2Name
c.Server = true
c.LogLevel = logLevel

c = NewConfig(args2)
a2 := NewAgent(c, nil)
a2.Start()

Expand Down Expand Up @@ -124,32 +122,26 @@ func Test_processFilteredNodes(t *testing.T) {
a1Addr := testutil.GetBindAddr().String()
a2Addr := testutil.GetBindAddr().String()

args := []string{
"-bind-addr", a1Addr,
"-join", a2Addr,
"-node-name", "test1",
"-server",
"-tag", "role=test",
"-log-level", logLevel,
}
c := DefaultConfig()
c.BindAddr = a1Addr
c.StartJoin = []string{a2Addr}
c.NodeName = "a1Name"
c.Server = true
c.LogLevel = logLevel

c := NewConfig(args)
a1 := NewAgent(c, nil)
a1.Start()

time.Sleep(2 * time.Second)

// Start another agent
args2 := []string{
"-bind-addr", a2Addr,
"-join", a1Addr,
"-node-name", "test2",
"-server",
"-tag", "role=test",
"-log-level", logLevel,
}
c = DefaultConfig()
c.BindAddr = a2Addr
c.StartJoin = []string{a1Addr + ":8946"}
c.NodeName = "a2Name"
c.Server = true
c.LogLevel = logLevel

c = NewConfig(args2)
a2 := NewAgent(c, nil)
a2.Start()

Expand All @@ -176,37 +168,15 @@ func Test_processFilteredNodes(t *testing.T) {
a2.Stop()
}

func Test_UnmarshalTags(t *testing.T) {
tagPairs := []string{
"tag1=val1",
"tag2=val2",
}

tags, err := UnmarshalTags(tagPairs)

if err != nil {
t.Fatalf("err: %s", err)
}

if v, ok := tags["tag1"]; !ok || v != "val1" {
t.Fatalf("bad: %v", tags)
}
if v, ok := tags["tag2"]; !ok || v != "val2" {
t.Fatalf("bad: %v", tags)
}
}

func TestEncrypt(t *testing.T) {
args := []string{
"-bind-addr", testutil.GetBindAddr().String(),
"-node-name", "test1",
"-server",
"-tag", "role=test",
"-encrypt", "kPpdjphiipNSsjd4QHWbkA==",
"-log-level", logLevel,
}
c := DefaultConfig()
c.BindAddr = testutil.GetBindAddr().String()
c.NodeName = "test1"
c.Server = true
c.Tags = map[string]string{"role": "test"}
c.EncryptKey = "kPpdjphiipNSsjd4QHWbkA=="
c.LogLevel = logLevel

c := NewConfig(args)
a := NewAgent(c, nil)
a.Start()

Expand All @@ -219,15 +189,13 @@ func TestEncrypt(t *testing.T) {
func Test_getRPCAddr(t *testing.T) {
a1Addr := testutil.GetBindAddr()

args := []string{
"-bind-addr", a1Addr.String() + ":5000",
"-node-name", "test1",
"-server",
"-tag", "role=test",
"-log-level", logLevel,
}
c := DefaultConfig()
c.BindAddr = a1Addr.String() + ":5000"
c.NodeName = "test1"
c.Server = true
c.Tags = map[string]string{"role": "test"}
c.LogLevel = logLevel

c := NewConfig(args)
a := NewAgent(c, nil)
a.Start()

Expand All @@ -242,13 +210,12 @@ func Test_getRPCAddr(t *testing.T) {

func TestAgentConfig(t *testing.T) {
advAddr := testutil.GetBindAddr().String()
args := []string{
"-bind-addr", testutil.GetBindAddr().String(),
"-advertise-addr", advAddr,
"-log-level", logLevel,
}

c := NewConfig(args)
c := DefaultConfig()
c.BindAddr = testutil.GetBindAddr().String()
c.AdvertiseAddr = advAddr
c.LogLevel = logLevel

a := NewAgent(c, nil)
a.Start()

Expand Down
16 changes: 7 additions & 9 deletions dkron/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ import (
)

func setupAPITest(t *testing.T) (a *Agent) {
args := []string{
"-bind-addr", testutil.GetBindAddr().String(),
"-http-addr", "127.0.0.1:8090",
"-node-name", "test",
"-server",
"-log-level", logLevel,
"-keyspace", "dkron-test",
}
c := DefaultConfig()
c.BindAddr = testutil.GetBindAddr().String()
c.HTTPAddr = "127.0.0.1:8090"
c.NodeName = "test"
c.Server = true
c.LogLevel = logLevel
c.Keyspace = "dkron-test"

c := NewConfig(args)
a = NewAgent(c, nil)
a.Start()

Expand Down
Loading

0 comments on commit ab311af

Please sign in to comment.