Skip to content

Commit

Permalink
tests for config
Browse files Browse the repository at this point in the history
  • Loading branch information
erikstmartin committed Jun 13, 2012
1 parent e8c7d1b commit 4862f7a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 7 deletions.
17 changes: 10 additions & 7 deletions skylib/config.go
Expand Up @@ -12,6 +12,7 @@ import (
"flag" "flag"
"log" "log"
"time" "time"
"os"
) )


type BindAddr struct { type BindAddr struct {
Expand All @@ -37,16 +38,18 @@ type ClientConfig struct {
} }


func GetServiceConfigFromFlags() *ServiceConfig { func GetServiceConfigFromFlags() *ServiceConfig {
flagset := flag.NewFlagSet("config", flag.ContinueOnError)

var ( var (
bindPort *int = flag.Int("port", 9999, "tcp port to listen") bindPort *int = flagset.Int("port", 9999, "tcp port to listen")
bindAddr *string = flag.String("address", "127.0.0.1", "address to bind") bindAddr *string = flagset.String("address", "127.0.0.1", "address to bind")
region *string = flag.String("region", "unknown", "region service is located in") region *string = flagset.String("region", "unknown", "region service is located in")
doozer *string = flag.String("doozer", "127.0.0.1:8046", "initial doozer instance to connect to") doozer *string = flagset.String("doozer", "127.0.0.1:8046", "initial doozer instance to connect to")
doozerBoot *string = flag.String("doozerboot", "127.0.0.1:8046", "initial doozer instance to connect to") doozerBoot *string = flagset.String("doozerboot", "127.0.0.1:8046", "initial doozer instance to connect to")
doozerDiscover *bool = flag.Bool("autodiscover", true, "auto discover new doozer instances") doozerDiscover *bool = flagset.Bool("autodiscover", true, "auto discover new doozer instances")
) )


flag.Parse() flagset.Parse(os.Args[1:])


return &ServiceConfig{ return &ServiceConfig{
Region: *region, Region: *region,
Expand Down
66 changes: 66 additions & 0 deletions skylib/config_test.go
@@ -0,0 +1,66 @@
package skylib

import (
"testing"
"os"
)

func TestGetServiceConfigFromFlags(t *testing.T) {
os.Args = []string {"test", "--port=1234", "--address=localhost", "--region=TestRegion", "--doozer=localhost:8046", "--doozerboot=localhost:1232", "--autodiscover=true"}

config := GetServiceConfigFromFlags()

if config.ServiceAddr.IPAddress != "localhost" {
t.Error("Address not set through flag")
}

if config.ServiceAddr.Port != 1234 {
t.Error("Port not set through flag")
}

if config.Region != "TestRegion" {
t.Error("Region not set through flag")
}

if config.DoozerConfig.Uri != "localhost:8046" {
t.Error("DoozerUri not set through flag")
}

if config.DoozerConfig.BootUri != "localhost:1232" {
t.Error("DoozerBootUri not set through flag")
}

if config.DoozerConfig.AutoDiscover != true {
t.Error("DoozerAutoDiscover not set through flag")
}
}

func TestGetServiceConfigFromFlagsDefaults(t *testing.T) {
os.Args = []string {"test"}

config := GetServiceConfigFromFlags()

if config.ServiceAddr.IPAddress != "127.0.0.1" {
t.Error("Address not set to default value")
}

if config.ServiceAddr.Port != 9999 {
t.Error("Port not set to default value")
}

if config.Region != "unknown" {
t.Error("Region not set to default value")
}

if config.DoozerConfig.Uri != "127.0.0.1:8046" {
t.Error("DoozerUri not set to default value")
}

if config.DoozerConfig.BootUri != "127.0.0.1:8046" {
t.Error("DoozerBootUri not set to default value")
}

if config.DoozerConfig.AutoDiscover != true {
t.Error("DoozerAutoDiscover not set to default value")
}
}

0 comments on commit 4862f7a

Please sign in to comment.