From 327ee32395dda0d69e85f99a866dee2e92a0f670 Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Wed, 9 Oct 2013 09:19:57 +0200 Subject: [PATCH] Fix same settings parse bug on replay side Added basic settings tests --- listener/settings_test.go | 30 +++++++++++++++++++++++++++- replay/replay.go | 7 +------ replay/settings.go | 15 +++++++++----- replay/settings_test.go | 42 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 replay/settings_test.go diff --git a/listener/settings_test.go b/listener/settings_test.go index dc7bca9..8488b01 100644 --- a/listener/settings_test.go +++ b/listener/settings_test.go @@ -4,6 +4,34 @@ import ( "testing" ) -func TestSettings(t *testing.T) { +func TestReplayAddressWithoutLimit(t *testing.T) { + settings := &ListenerSettings{ + ReplayAddress: "replay:1", + } + settings.Parse() + + if settings.ReplayAddress != "replay:1" { + t.Error("Address not match") + } + + if settings.ReplayLimit != 0 { + t.Error("Replay limit should be 0") + } +} + +func TestReplayAddressWithLimit(t *testing.T) { + settings := &ListenerSettings{ + ReplayAddress: "replay:1|10", + } + + settings.Parse() + + if settings.ReplayAddress != "replay:1" { + t.Error("Address not match") + } + + if settings.ReplayLimit != 10 { + t.Error("Replay limit should be 10") + } } diff --git a/replay/replay.go b/replay/replay.go index b65323b..3d531e4 100644 --- a/replay/replay.go +++ b/replay/replay.go @@ -72,12 +72,7 @@ func ParseRequest(data []byte) (request *http.Request, err error) { // Replay server listen to UDP traffic from Listeners // Each request processed by RequestFactory func Run() { - // Register Plugins - // Elasticsearch Plugin - if esp.Active { - esp.Init() - RegisterResponseAnalyzePlugin(&esp) - } + Settings.Parse() rm := NewReplayManager() diff --git a/replay/settings.go b/replay/settings.go index 4a46c10..6832258 100644 --- a/replay/settings.go +++ b/replay/settings.go @@ -20,9 +20,9 @@ type ReplaySettings struct { Port int Host string - Address string + Address string - ForwardAddress string + ForwardAddress string FileToReplyPath string @@ -69,9 +69,15 @@ func (r *ReplaySettings) ForwardedHosts() (hosts []*ForwardHost) { return } -// SetAddress with port, e.g.: 127.0.0.1:28020 -func (r *ReplaySettings) SetAddress() { +func (r *ReplaySettings) Parse() { r.Address = r.Host + ":" + strconv.Itoa(r.Port) + + // Register Plugins + // Elasticsearch Plugin + if esp.Active { + esp.Init() + RegisterResponseAnalyzePlugin(&esp) + } } func init() { @@ -90,7 +96,6 @@ func init() { flag.StringVar(&Settings.Host, "ip", defaultHost, "ip addresses to listen on") - Settings.SetAddress() flag.StringVar(&Settings.ForwardAddress, "f", defaultForwardAddress, "http address to forward traffic.\n\tYou can limit requests per second by adding `|num` after address.\n\tIf you have multiple addresses with different limits. For example: http://staging.example.com|100,http://dev.example.com|10") flag.StringVar(&Settings.FileToReplyPath, "file", "", "File to replay captured requests from") diff --git a/replay/settings_test.go b/replay/settings_test.go new file mode 100644 index 0000000..773ab3c --- /dev/null +++ b/replay/settings_test.go @@ -0,0 +1,42 @@ +package replay + +import ( + "testing" +) + +func TestAddress(t *testing.T) { + settings := &ReplaySettings{ + Host: "local", + Port: 2, + } + + settings.Parse() + + if settings.Address != "local:2" { + t.Error("Address not match") + } +} + +func TestForwardAddress(t *testing.T) { + settings := &ReplaySettings{ + Host: "local", + Port: 2, + ForwardAddress: "host1:1,host2:2|10", + } + + settings.Parse() + + forward_hosts := settings.ForwardedHosts() + + if len(forward_hosts) != 2 { + t.Error("Should have 2 forward hosts") + } + + if forward_hosts[0].Limit != 0 && forward_hosts[0].Url != "host1:1" { + t.Error("Host should be host1:1 with no limit") + } + + if forward_hosts[1].Limit != 10 && forward_hosts[0].Url != "host2:2" { + t.Error("Host should be host2:2 with 10 limit") + } +}