func TestKeepAliveWithMultipleAZs(t *testing.T) { keepAliveChan1 := make(chan []byte) keepAliveChan2 := make(chan []byte) go KeepAliveServer("localhost:62032", keepAliveChan1) go KeepAliveServer("localhost:62033", keepAliveChan2) proxy := NewProxy( "localhost:62034", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62032"}), hasher.NewHasher([]string{"localhost:62033"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() WaitForServerStart("62032", "/") KeepAliveClient(t, "62034", "/?app=myServerApp1") select { case data := <-keepAliveChan1: assert.Equal(t, string(data), "keep alive") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } select { case data := <-keepAliveChan2: assert.Equal(t, string(data), "keep alive") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } }
func TestProxyingWithMultipleAZs(t *testing.T) { go Server("localhost:62027", "Hello World from the server 1 - AZ1", 1) go Server("localhost:62028", "Hello World from the server 2 - AZ1", 1) go Server("localhost:62029", "Hello World from the server 1 - AZ2", 1) go Server("localhost:62030", "Hello World from the server 2 - AZ2", 1) proxy := NewProxy( "localhost:62031", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62027", "localhost:62028"}), hasher.NewHasher([]string{"localhost:62029", "localhost:62030"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() WaitForServerStart("62027", "/") receivedChan := Client(t, "62031", "/?app=0") messages := "" for message := range receivedChan { messages = messages + string(message) } assert.Contains(t, messages, "Hello World from the server 1 - AZ1") assert.Contains(t, messages, "Hello World from the server 1 - AZ2") receivedChan = Client(t, "62031", "/?app=1") messages = "" for message := range receivedChan { messages = messages + string(message) } assert.Contains(t, messages, "Hello World from the server 2 - AZ1") assert.Contains(t, messages, "Hello World from the server 2 - AZ2") }
func TestProxyWhenAuthorizationFailsThroughQueryParams(t *testing.T) { proxy := NewProxy( "localhost:62062", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62032"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() time.Sleep(time.Millisecond * 50) config, err := websocket.NewConfig("ws://localhost:62061/?app=myApp&authorization="+url.QueryEscape(testhelpers.INVALID_AUTHENTICATION_TOKEN), "http://localhost") assert.NoError(t, err) receivedChan := ClientWithAuth(t, "62062", "/?app=myApp", config) select { case data := <-receivedChan: messagetesthelpers.AssertProtoBufferMessageEquals(t, "Error: Invalid authorization", data) case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } _, stillOpen := <-receivedChan assert.False(t, stillOpen) }
func TestProxyWithoutAuthorization(t *testing.T) { proxy := NewProxy( "localhost:62061", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62032"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() time.Sleep(time.Millisecond * 50) config, err := websocket.NewConfig("ws://localhost:62061/?app=myApp", "http://localhost") assert.NoError(t, err) receivedChan := ClientWithAuth(t, "62061", "/?app=myApp", config) select { case data := <-receivedChan: messagetesthelpers.AssertProtoBufferMessageEquals(t, "Error: Authorization not provided", data) case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } _, stillOpen := <-receivedChan assert.False(t, stillOpen) }
func TestProxyingWithHashingBetweenServers(t *testing.T) { go Server("localhost:62024", "Hello World from the server 1", 1) go Server("localhost:62025", "Hello World from the server 2", 1) proxy := NewProxy( "localhost:62026", []*hasher.Hasher{hasher.NewHasher([]string{"localhost:62024", "localhost:62025"})}, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() WaitForServerStart("62024", "/") receivedChan := Client(t, "62026", "/?app=0") select { case data := <-receivedChan: assert.Equal(t, string(data), "Hello World from the server 1") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } receivedChan = Client(t, "62026", "/?app=1") select { case data := <-receivedChan: assert.Equal(t, string(data), "Hello World from the server 2") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } }
func TestThatItIgnoresBadMessages(t *testing.T) { listener, dataChannel := agentlistener.NewAgentListener("localhost:9996", logger) go listener.Start() loggregatorServers := []string{"localhost:9996"} hasher := hasher.NewHasher(loggregatorServers) r, err := NewRouter("localhost:3455", hasher, newCfConfig(), logger) assert.NoError(t, err) go r.Start(logger) time.Sleep(50 * time.Millisecond) lc := loggregatorclient.NewLoggregatorClient("localhost:3455", logger, loggregatorclient.DefaultBufferSize) lc.Send([]byte("This is poorly formatted")) logEmitter, _ := emitter.NewEmitter("localhost:3455", "ROUTER", "42", "secret", logger) logEmitter.Emit("my_awesome_app", "Hello World") received := <-dataChannel receivedEnvelope := &logmessage.LogEnvelope{} proto.Unmarshal(received, receivedEnvelope) assert.Equal(t, receivedEnvelope.GetLogMessage().GetAppId(), "my_awesome_app") assert.Equal(t, string(receivedEnvelope.GetLogMessage().GetMessage()), "Hello World") }
func TestThatItWorksWithTwoLoggregators(t *testing.T) { listener1, dataChan1 := agentlistener.NewAgentListener("localhost:9998", logger) go listener1.Start() listener2, dataChan2 := agentlistener.NewAgentListener("localhost:9997", logger) go listener2.Start() loggregatorServers := []string{"localhost:9998", "localhost:9997"} hasher := hasher.NewHasher(loggregatorServers) rt, err := NewRouter("localhost:3457", hasher, newCfConfig(), logger) assert.NoError(t, err) go rt.Start(logger) time.Sleep(50 * time.Millisecond) logEmitter, _ := emitter.NewEmitter("localhost:3457", "ROUTER", "42", "secret", logger) logEmitter.Emit("2", "My message") receivedData := <-dataChan1 receivedEnvelope := &logmessage.LogEnvelope{} proto.Unmarshal(receivedData, receivedEnvelope) assert.Equal(t, receivedEnvelope.GetLogMessage().GetAppId(), "2") assert.Equal(t, string(receivedEnvelope.GetLogMessage().GetMessage()), "My message") logEmitter.Emit("1", "Another message") receivedData = <-dataChan2 receivedEnvelope = &logmessage.LogEnvelope{} proto.Unmarshal(receivedData, receivedEnvelope) assert.Equal(t, receivedEnvelope.GetLogMessage().GetAppId(), "1") assert.Equal(t, string(receivedEnvelope.GetLogMessage().GetMessage()), "Another message") }
func makeIncomingRouter(config *Config, logger *gosteno.Logger) *trafficcontroller.Router { serversForZone := config.Loggregators[config.Zone] servers := make([]string, len(serversForZone)) for index, server := range serversForZone { logger.Debugf("Incoming Router Startup: Forwarding messages from source to loggregator server [%v] at %v", index, net.JoinHostPort(server, strconv.FormatUint(uint64(config.IncomingPort), 10))) servers[index] = net.JoinHostPort(serversForZone[index], strconv.FormatUint(uint64(config.IncomingPort), 10)) } logger.Debugf("Incoming Router Startup: Loggregator Servers in the zone %s: %v", config.Zone, servers) h := hasher.NewHasher(servers) logger.Debugf("Incoming Router Startup: Hashed Loggregator Server in the zone: %v", h.LoggregatorServers()) logger.Debugf("Incoming Router Startup: Going to start incoming router on %v", config.Host) router, err := trafficcontroller.NewRouter(config.Host, h, config.Config, logger) if err != nil { panic(err) } return router }
func makeHashers(loggregators map[string][]string, outgoingPort uint32, logger *gosteno.Logger) []*hasher.Hasher { counter := 0 hashers := make([]*hasher.Hasher, 0, len(loggregators)) for _, servers := range loggregators { logger.Debugf("Output Proxy Startup: Hashing servers: %v Length: %d", servers, len(servers)) if len(servers) == 0 { continue } for index, server := range servers { logger.Debugf("Output Proxy Startup: Forwarding messages to client from loggregator server [%v] at %v", index, net.JoinHostPort(server, strconv.FormatUint(uint64(outgoingPort), 10))) servers[index] = net.JoinHostPort(server, strconv.FormatUint(uint64(outgoingPort), 10)) } hashers = hashers[:(counter + 1)] hashers[counter] = hasher.NewHasher(servers) counter++ } return hashers }
func TestProxyingWithTwoMessages(t *testing.T) { go Server("localhost:62020", "Hello World from the server", 2) proxy := NewProxy( "localhost:62021", []*hasher.Hasher{hasher.NewHasher([]string{"localhost:62020"})}, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() WaitForServerStart("62020", "/") receivedChan := Client(t, "62021", "/?app=myApp") messages := "" for message := range receivedChan { messages = messages + string(message) } assert.Contains(t, messages, "Hello World from the serverHello World from the server") }
func TestProxyingWithAuthThroughQueryParam(t *testing.T) { go Server("localhost:62038", "Hello World from the server", 1) proxy := NewProxy( "localhost:62022", []*hasher.Hasher{hasher.NewHasher([]string{"localhost:62038"})}, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() WaitForServerStart("62038", "/") receivedChan := ClientWithQueryParamAuth(t, "62022", "/?app=myApp") select { case data := <-receivedChan: assert.Equal(t, string(data), "Hello World from the server") case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } }
func TestThatItWorksWithOneLoggregator(t *testing.T) { listener, dataChannel := agentlistener.NewAgentListener("localhost:9999", logger) go listener.Start() loggregatorServers := []string{"localhost:9999"} hasher := hasher.NewHasher(loggregatorServers) r, err := NewRouter("localhost:3456", hasher, newCfConfig(), logger) assert.NoError(t, err) go r.Start(logger) time.Sleep(50 * time.Millisecond) logEmitter, _ := emitter.NewEmitter("localhost:3456", "ROUTER", "42", "secret", logger) logEmitter.Emit("my_awesome_app", "Hello World") received := <-dataChannel receivedEnvelope := &logmessage.LogEnvelope{} proto.Unmarshal(received, receivedEnvelope) assert.Equal(t, receivedEnvelope.GetLogMessage().GetAppId(), "my_awesome_app") assert.Equal(t, string(receivedEnvelope.GetLogMessage().GetMessage()), "Hello World") }
func TestProxyWhenLogTargetisinvalid(t *testing.T) { proxy := NewProxy( "localhost:62060", []*hasher.Hasher{ hasher.NewHasher([]string{"localhost:62032"}), }, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() time.Sleep(time.Millisecond * 50) receivedChan := Client(t, "62060", "/invalid_target") select { case data := <-receivedChan: messagetesthelpers.AssertProtoBufferMessageEquals(t, "Error: Invalid target", data) case <-time.After(1 * time.Second): t.Error("Did not receive response within one second") } _, stillOpen := <-receivedChan assert.False(t, stillOpen) }
testhelpers "trafficcontroller_testhelpers" ) var _ = Describe("OutputProxyMultipleHasher", func() { var hashers []hasher.Hasher var fls []*fakeListener var count int var ts *httptest.Server BeforeEach(func() { count = 0 hashers = []hasher.Hasher{ hasher.NewHasher([]string{"localhost:62038"}), hasher.NewHasher([]string{"localhost:62039"}), } fls = []*fakeListener{ &fakeListener{messageChan: make(chan []byte, 1), expectedHost: "ws://" + hashers[0].LoggregatorServers()[0] + "/tail/?app=myApp"}, &fakeListener{messageChan: make(chan []byte, 1), expectedHost: "ws://" + hashers[1].LoggregatorServers()[0] + "/tail/?app=myApp"}, } proxy := trafficcontroller.NewProxy( hashers, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) trafficcontroller.NewWebsocketListener = func() listener.Listener {
f.WebsocketConnection.Close() } var _ = Describe("OutPutProxy", func() { var fph *fakeProxyHandler var hashers []*hasher.Hasher var PORT = "62022" BeforeEach(func() { fph = &fakeProxyHandler{callParams: make([][]interface{}, 0)} NewProxyHandlerProvider = func(ws *websocket.Conn, logger *gosteno.Logger) websocketHandler { fph.WebsocketConnection = ws return fph } hashers = []*hasher.Hasher{hasher.NewHasher([]string{"localhost:62038"})} proxy := NewProxy( "localhost:"+PORT, hashers, testhelpers.SuccessfulAuthorizer, loggertesthelper.Logger(), ) go proxy.Start() }) Context("Auth Params", func() { It("Should Authenticate with Auth Query Params", func() { ClientWithQueryParamAuth(PORT, "/?app=myApp", testhelpers.VALID_AUTHENTICATION_TOKEN) callParams := fph.CallParams() Expect(callParams).To(HaveLen(1))
AfterEach(func() { fakeServer.Close() }) It("Proxies multiple messages", func() { fakeClient := &fakeClient{} handler := NewProxyHandler( fakeClient, loggertesthelper.Logger(), ) handler.HandleWebSocket( "appId", "/dump", []*hasher.Hasher{hasher.NewHasher([]string{fakeServer.Listener.Addr().String()})}) Expect(fakeClient.ReceivedMessageTypes()).To(HaveLen(2)) Expect(fakeClient.ReceivedMessages()).To(Equal([]string{"Message1", "Message2"})) Expect(fakeClient.ReceivedMessageTypes()).To(ContainElement(websocket.BinaryMessage)) }) It("Uses the Correct Request URI", func() { handler := NewProxyHandler( &fakeClient{}, loggertesthelper.Logger(), ) handler.HandleWebSocket( "appId",
package hasher_test import ( "crypto/rand" "encoding/hex" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "trafficcontroller/hasher" ) var _ = Describe("Hasher", func() { It("should panic when not seeded with servers", func() { Expect(func() { hasher.NewHasher([]string{}) }).To(Panic()) }) Describe("LoggregatorServers", func() { It("should return one server", func() { loggregatorServer := []string{"10.10.0.16:9998"} h := hasher.NewHasher(loggregatorServer) Expect(h.LoggregatorServers()).To(Equal(loggregatorServer)) }) It("should return all servers", func() { loggregatorServers := []string{"10.10.0.16:9998", "10.10.0.17:9997"} h := hasher.NewHasher(loggregatorServers) Expect(h.LoggregatorServers()).To(Equal(loggregatorServers)) })