Skip to content
Browse files

tests for config

  • Loading branch information...
1 parent e8c7d1b commit 4862f7a5a749d8e85fbc03b9e8afd88e4c630a09 @erikstmartin erikstmartin committed Jun 13, 2012
Showing with 76 additions and 7 deletions.
  1. +10 −7 skylib/config.go
  2. +66 −0 skylib/config_test.go
View
17 skylib/config.go
@@ -12,6 +12,7 @@ import (
"flag"
"log"
"time"
+ "os"
)
type BindAddr struct {
@@ -37,16 +38,18 @@ type ClientConfig struct {
}
func GetServiceConfigFromFlags() *ServiceConfig {
+ flagset := flag.NewFlagSet("config", flag.ContinueOnError)
+
var (
- bindPort *int = flag.Int("port", 9999, "tcp port to listen")
- bindAddr *string = flag.String("address", "127.0.0.1", "address to bind")
- region *string = flag.String("region", "unknown", "region service is located in")
- doozer *string = flag.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")
- doozerDiscover *bool = flag.Bool("autodiscover", true, "auto discover new doozer instances")
+ bindPort *int = flagset.Int("port", 9999, "tcp port to listen")
+ bindAddr *string = flagset.String("address", "127.0.0.1", "address to bind")
+ region *string = flagset.String("region", "unknown", "region service is located in")
+ doozer *string = flagset.String("doozer", "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 = flagset.Bool("autodiscover", true, "auto discover new doozer instances")
)
- flag.Parse()
+ flagset.Parse(os.Args[1:])
return &ServiceConfig{
Region: *region,
View
66 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.
Something went wrong with that request. Please try again.