github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/api/logs/log_cache_message_test.go (about)

     1  package logs_test
     2  
     3  import (
     4  	"time"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/cf/api/logs"
     8  	"code.cloudfoundry.org/cli/cf/api/logs/logsfakes"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("log messages", func() {
    14  
    15  	var (
    16  		fakeColorLogger *logsfakes.FakeColorLogger
    17  		logCacheMessage *logs.LogCacheMessage
    18  	)
    19  
    20  	// TODO: review if that is the only way to initialize variables of these types
    21  
    22  	message := *sharedaction.NewLogMessage(
    23  		"some-message",
    24  		"OUT",
    25  		time.Unix(0, 0),
    26  		"APP/PROC/WEB",
    27  		"0",
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		fakeColorLogger = new(logsfakes.FakeColorLogger)
    32  		message = *sharedaction.NewLogMessage(
    33  			"some-message",
    34  			"OUT",
    35  			time.Unix(0, 0),
    36  			"APP/PROC/WEB",
    37  			"0",
    38  		)
    39  		logCacheMessage = logs.NewLogCacheMessage(fakeColorLogger, message)
    40  
    41  		fakeColorLogger.LogStdoutColorCalls(func(data string) string {
    42  			return data
    43  		})
    44  
    45  		fakeColorLogger.LogStderrColorCalls(func(data string) string {
    46  			return data
    47  		})
    48  
    49  		fakeColorLogger.LogSysHeaderColorCalls(func(data string) string {
    50  			return data
    51  		})
    52  
    53  	})
    54  
    55  	Describe("ToSimpleLog", func() {
    56  		It("returns the message", func() {
    57  			Expect(logCacheMessage.ToSimpleLog()).To(Equal("some-message"))
    58  		})
    59  	})
    60  
    61  	Describe("GetSourceName", func() {
    62  		It("returns the source name", func() {
    63  			// TODO: reconsider whether this should return SourceType or SourceInstance
    64  			Expect(logCacheMessage.GetSourceName()).To(Equal("APP/PROC/WEB"))
    65  		})
    66  	})
    67  
    68  	Describe("ToLog", func() {
    69  		Context("timezone handling", func() {
    70  			It("reflects timezone in log output", func() {
    71  				Expect(logCacheMessage.ToLog(time.FixedZone("TST", 1*60*60))).To(ContainSubstring("1970-01-01T01:00:00.00+0100"))
    72  			})
    73  		})
    74  
    75  		Context("sourceID sets the logHeader format", func() {
    76  			It("omits sourceID from the header when sourceID is empty", func() {
    77  				message = *sharedaction.NewLogMessage(
    78  					"some-message",
    79  					"OUT",
    80  					time.Unix(0, 0),
    81  					"APP/PROC/WEB",
    82  					"",
    83  				)
    84  				logCacheMessage = logs.NewLogCacheMessage(fakeColorLogger, message)
    85  
    86  				Expect(logCacheMessage.ToLog(time.UTC)).To(ContainSubstring("[APP/PROC/WEB]"))
    87  
    88  			})
    89  			It("includes sourceID to the header when sourceID is not empty", func() {
    90  				Expect(logCacheMessage.ToLog(time.UTC)).To(ContainSubstring("[APP/PROC/WEB/0]"))
    91  			})
    92  		})
    93  		Context("trimming of message text", func() {
    94  			It("trims newlines from the end of the message", func() {
    95  				message = *sharedaction.NewLogMessage(
    96  					"some-message\r\n",
    97  					"OUT",
    98  					time.Unix(0, 0),
    99  					"APP/PROC/WEB",
   100  					"",
   101  				)
   102  				logCacheMessage = logs.NewLogCacheMessage(fakeColorLogger, message)
   103  				Expect(logCacheMessage.ToLog(time.UTC)).To(HaveSuffix("some-message"), "invisible characters present")
   104  			})
   105  		})
   106  		Context("padding calculation", func() {
   107  			Context("with a short source type empty source instance id", func() {
   108  				It("prepending output with long padding", func() {
   109  					message = *sharedaction.NewLogMessage(
   110  						"some-message",
   111  						"OUT",
   112  						time.Unix(0, 0),
   113  						"STG",
   114  						"",
   115  					)
   116  					logCacheMessage = logs.NewLogCacheMessage(fakeColorLogger, message)
   117  					Expect(logCacheMessage.ToLog(time.UTC)).To(Equal("1970-01-01T00:00:00.00+0000 [STG]        OUT some-message"))
   118  				})
   119  			})
   120  
   121  			Context("with a short source type and an instance id", func() {
   122  				It("prepending output with medium padding", func() {
   123  					message = *sharedaction.NewLogMessage(
   124  						"some-message",
   125  						"OUT",
   126  						time.Unix(0, 0),
   127  						"STG",
   128  						"1",
   129  					)
   130  					logCacheMessage = logs.NewLogCacheMessage(fakeColorLogger, message)
   131  					Expect(logCacheMessage.ToLog(time.UTC)).To(Equal("1970-01-01T00:00:00.00+0000 [STG/1]      OUT some-message"))
   132  				})
   133  			})
   134  
   135  			Context("with a long source type and a source instance id", func() {
   136  				It("prepending output with no padding", func() {
   137  					Expect(logCacheMessage.ToLog(time.UTC)).To(Equal("1970-01-01T00:00:00.00+0000 [APP/PROC/WEB/0]OUT some-message"))
   138  				})
   139  			})
   140  		})
   141  
   142  		Context("multi-line log messages", func() {
   143  			It("splits the message into multiple lines", func() {
   144  				message = *sharedaction.NewLogMessage(
   145  					"some-message1\nsome-message2",
   146  					"OUT",
   147  					time.Unix(0, 0),
   148  					"STG",
   149  					"1",
   150  				)
   151  				logCacheMessage = logs.NewLogCacheMessage(fakeColorLogger, message)
   152  				Expect(logCacheMessage.ToLog(time.UTC)).To(Equal("1970-01-01T00:00:00.00+0000 [STG/1]      OUT some-message1\n" +
   153  					"                                         some-message2"))
   154  			})
   155  		})
   156  
   157  		Context("handles both STDERR and STDOUT", func() {
   158  			It("correctly colors the STDOUT log message", func() {
   159  				message = *sharedaction.NewLogMessage(
   160  					"some-message",
   161  					"OUT",
   162  					time.Unix(0, 0),
   163  					"STG",
   164  					"1",
   165  				)
   166  				logCacheMessage = logs.NewLogCacheMessage(fakeColorLogger, message)
   167  				fakeColorLogger.LogSysHeaderColorReturns("colorized-header")
   168  				fakeColorLogger.LogStdoutColorReturns("colorized-message")
   169  				Expect(logCacheMessage.ToLog(time.UTC)).To(Equal("colorized-header      colorized-message"))
   170  				Expect(fakeColorLogger.LogSysHeaderColorCallCount()).To(Equal(1))
   171  				Expect(fakeColorLogger.LogStdoutColorCallCount()).To(Equal(1))
   172  				Expect(fakeColorLogger.LogStderrColorCallCount()).To(Equal(0))
   173  			})
   174  			It("correctly colors the STDERR log message", func() {
   175  				message = *sharedaction.NewLogMessage(
   176  					"some-message",
   177  					"ERR",
   178  					time.Unix(0, 0),
   179  					"STG",
   180  					"1",
   181  				)
   182  				logCacheMessage = logs.NewLogCacheMessage(fakeColorLogger, message)
   183  				fakeColorLogger.LogSysHeaderColorReturns("colorized-header")
   184  				fakeColorLogger.LogStderrColorReturns("colorized-message")
   185  				Expect(logCacheMessage.ToLog(time.UTC)).To(Equal("colorized-header      colorized-message"))
   186  				Expect(fakeColorLogger.LogSysHeaderColorCallCount()).To(Equal(1))
   187  				Expect(fakeColorLogger.LogStdoutColorCallCount()).To(Equal(0))
   188  				Expect(fakeColorLogger.LogStderrColorCallCount()).To(Equal(1))
   189  			})
   190  		})
   191  
   192  		It("correctly formats the log", func() {
   193  			Expect(logCacheMessage.ToLog(time.UTC)).To(Equal("1970-01-01T00:00:00.00+0000 [APP/PROC/WEB/0]OUT some-message"))
   194  		})
   195  	})
   196  })