func TestTailsLogsFor(t *testing.T) { expectedMessage := messagetesthelpers.MarshalledLogMessage(t, "My message", "my-app-id") websocketEndpoint := func(conn *websocket.Conn) { conn.Write(expectedMessage) conn.Close() } websocketServer := httptest.NewTLSServer(websocket.Handler(websocketEndpoint)) defer websocketServer.Close() var redirectEndpoint = func(writer http.ResponseWriter, request *http.Request) { assert.Equal(t, request.URL.Path, "/tail/") assert.Equal(t, request.URL.RawQuery, "app=my-app-guid") assert.Equal(t, request.Method, "GET") assert.Contains(t, request.Header.Get("Authorization"), "BEARER my_access_token") writer.Header().Set("Location", strings.Replace(websocketServer.URL, "https", "wss", 1)) writer.WriteHeader(http.StatusFound) } http.HandleFunc("/", redirectEndpoint) go http.ListenAndServe(":"+LOGGREGATOR_REDIRECTOR_PORT, nil) redirectServer := httptest.NewTLSServer(http.HandlerFunc(redirectEndpoint)) defer redirectServer.Close() gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) app := cf.Application{Name: "my-app", Guid: "my-app-guid"} config := &configuration.Configuration{AccessToken: "BEARER my_access_token", Target: "http://localhost"} loggregatorHostResolver := func(hostname string) string { return hostname } logsRepo := NewLoggregatorLogsRepository(config, gateway, loggregatorHostResolver) connected := false onConnect := func() { connected = true } tailedMessages := []*logmessage.LogMessage{} onMessage := func(message *logmessage.LogMessage) { tailedMessages = append(tailedMessages, message) } logsRepo.TailLogsFor(app, onConnect, onMessage) assert.Equal(t, len(tailedMessages), 1) actualMessage, err := proto.Marshal(tailedMessages[0]) assert.NoError(t, err) assert.Equal(t, actualMessage, expectedMessage) assert.True(t, connected) }
func TestEndtoEndMessageShouldNotWork(t *testing.T) { receivedChan := make(chan []byte) ws, _, _ := testhelpers.AddWSSink(t, receivedChan, "8083", "/tail/?app=myApp") defer ws.Close() time.Sleep(50 * time.Millisecond) connection, err := net.Dial("udp", "localhost:3456") expectedMessageString := "Some Data" expectedMessage := messagetesthelpers.MarshalledLogMessage(t, expectedMessageString, "myApp") _, err = connection.Write(expectedMessage) assert.NoError(t, err) select { case _ = <-receivedChan: t.Error("Message should have been dropped") case <-time.After(2 * time.Second): //success } }
func TestRecentLogsFor(t *testing.T) { expectedMessage := messagetesthelpers.MarshalledLogMessage(t, "My message", "my-app-id") message, err := logmessage.ParseMessage(expectedMessage) assert.NoError(t, err) ts := httptest.NewTLSServer(http.HandlerFunc(recentLogsEndpoint(message))) defer ts.Close() app := cf.Application{Name: "my-app", Guid: "my-app-guid"} config := &configuration.Configuration{AccessToken: "BEARER my_access_token", Target: ts.URL} gateway := net.NewCloudControllerGateway(&testhelpers.FakeAuthenticator{}) loggregatorHostResolver := func(hostname string) string { return hostname } logsRepo := NewLoggregatorLogsRepository(config, gateway, loggregatorHostResolver) logs, err := logsRepo.RecentLogsFor(app) assert.NoError(t, err) assert.Equal(t, len(logs), 1) actualMessage, err := proto.Marshal(logs[0]) assert.NoError(t, err) assert.Equal(t, actualMessage, expectedMessage) }
func TestFromLogMessageProtobuffer(t *testing.T) { message := testhelpers.MarshalledLogMessage(t, "message", "my_app_id") appid, err := FromProtobufferMessage(message) assert.NoError(t, err) assert.Equal(t, "my_app_id", appid) }