github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/logs/console_tailed_logs_outputter/console_tailed_logs_outputter_test.go (about)

     1  package console_tailed_logs_outputter_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"time"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  	"github.com/onsi/gomega/gbytes"
    11  
    12  	"github.com/cloudfoundry-incubator/ltc/logs/console_tailed_logs_outputter"
    13  	"github.com/cloudfoundry-incubator/ltc/logs/fake_log_reader"
    14  	"github.com/cloudfoundry-incubator/ltc/logs/reserved_app_ids"
    15  	"github.com/cloudfoundry-incubator/ltc/terminal"
    16  	"github.com/cloudfoundry-incubator/ltc/terminal/colors"
    17  	"github.com/cloudfoundry-incubator/ltc/test_helpers"
    18  	"github.com/cloudfoundry/sonde-go/events"
    19  )
    20  
    21  var _ = Describe("ConsoleTailedLogsOutputter", func() {
    22  	var (
    23  		outputBuffer               *gbytes.Buffer
    24  		terminalUI                 terminal.UI
    25  		logReader                  *fake_log_reader.FakeLogReader
    26  		consoleTailedLogsOutputter *console_tailed_logs_outputter.ConsoleTailedLogsOutputter
    27  	)
    28  
    29  	buildLogMessage := func(sourceType, sourceInstance string, timestamp time.Time, message []byte) *events.LogMessage {
    30  		unixTime := timestamp.UnixNano()
    31  		return &events.LogMessage{
    32  			Message:        message,
    33  			Timestamp:      &unixTime,
    34  			SourceType:     &sourceType,
    35  			SourceInstance: &sourceInstance,
    36  		}
    37  	}
    38  
    39  	BeforeEach(func() {
    40  		outputBuffer = gbytes.NewBuffer()
    41  		terminalUI = terminal.NewUI(nil, outputBuffer, nil)
    42  		logReader = fake_log_reader.NewFakeLogReader()
    43  		consoleTailedLogsOutputter = console_tailed_logs_outputter.NewConsoleTailedLogsOutputter(terminalUI, logReader)
    44  	})
    45  
    46  	Describe("OutputTailedLogs", func() {
    47  		It("Tails logs", func() {
    48  			now := time.Now()
    49  			logReader.AddLog(buildLogMessage("RTR", "1", now, []byte("First log")))
    50  			logReader.AddError(errors.New("First Error"))
    51  
    52  			go consoleTailedLogsOutputter.OutputTailedLogs("my-app-guid")
    53  
    54  			Eventually(logReader.GetAppGuid).Should(Equal("my-app-guid"))
    55  
    56  			logOutputBufferString := fmt.Sprintf("%s [%s|%s] First log\n", colors.Cyan(now.Format("01/02 15:04:05.00")), colors.Yellow("RTR"), colors.Yellow("1"))
    57  			Eventually(outputBuffer).Should(test_helpers.Say(logOutputBufferString))
    58  			Eventually(outputBuffer).Should(test_helpers.Say("First Error\n"))
    59  		})
    60  	})
    61  
    62  	Describe("OutputDebugLogs", func() {
    63  		It("tails logs with pretty formatting", func() {
    64  			now := time.Now()
    65  			logReader.AddLog(buildLogMessage("rep", "cell-1", now, []byte("First log")))
    66  			logReader.AddError(errors.New("First Error"))
    67  
    68  			go consoleTailedLogsOutputter.OutputDebugLogs(true)
    69  
    70  			Eventually(logReader.GetAppGuid).Should(Equal(reserved_app_ids.LatticeDebugLogStreamAppId))
    71  
    72  			Eventually(outputBuffer).Should(test_helpers.Say("rep"))
    73  			Eventually(outputBuffer).Should(test_helpers.Say("cell-1"))
    74  			Eventually(outputBuffer).Should(test_helpers.Say(now.Format("01/02 15:04:05.00")))
    75  			Eventually(outputBuffer).Should(test_helpers.Say("First log"))
    76  			Eventually(outputBuffer).Should(test_helpers.Say("First Error\n"))
    77  		})
    78  
    79  		It("tails logs without pretty formatting", func() {
    80  			now := time.Now()
    81  			logReader.AddLog(buildLogMessage("rep", "cell-1", now, []byte("First log")))
    82  			logReader.AddError(errors.New("First Error"))
    83  
    84  			go consoleTailedLogsOutputter.OutputDebugLogs(false)
    85  
    86  			Eventually(logReader.GetAppGuid).Should(Equal(reserved_app_ids.LatticeDebugLogStreamAppId))
    87  
    88  			Eventually(outputBuffer).Should(test_helpers.Say(now.Format("01/02 15:04:05.00")))
    89  			Eventually(outputBuffer).Should(test_helpers.Say("rep"))
    90  			Eventually(outputBuffer).Should(test_helpers.Say("cell-1"))
    91  			Eventually(outputBuffer).Should(test_helpers.Say("First log\n"))
    92  			Eventually(outputBuffer).Should(test_helpers.Say("First Error\n"))
    93  		})
    94  	})
    95  
    96  	Describe("StopOutputting", func() {
    97  		It("stops outputting logs", func() {
    98  			go consoleTailedLogsOutputter.OutputTailedLogs("my-app-guid")
    99  
   100  			consoleTailedLogsOutputter.StopOutputting()
   101  			Expect(logReader.IsLogTailStopped()).To(BeTrue())
   102  		})
   103  	})
   104  })