func startAppWithInstancesAndErrors(t *testing.T, app cf.Application, instances [][]cf.ApplicationInstance, errorCodes []string) (ui *testterm.FakeUI, appRepo *testapi.FakeApplicationRepository, reqFactory *testreq.FakeReqFactory) { token, err := testconfig.CreateAccessTokenWithTokenInfo(configuration.TokenInfo{ Username: "******", }) assert.NoError(t, err) config := &configuration.Configuration{ Space: cf.Space{Name: "my-space"}, Organization: cf.Organization{Name: "my-org"}, AccessToken: token, ApplicationStartTimeout: 2, } appRepo = &testapi.FakeApplicationRepository{ FindByNameApp: app, GetInstancesResponses: instances, GetInstancesErrorCodes: errorCodes, } currentTime := time.Now() messageType := logmessage.LogMessage_ERR sourceType := logmessage.LogMessage_DEA logMessage1 := logmessage.LogMessage{ Message: []byte("Log Line 1"), AppId: proto.String(app.Guid), MessageType: &messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } logMessage2 := logmessage.LogMessage{ Message: []byte("Log Line 2"), AppId: proto.String(app.Guid), MessageType: &messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } logRepo := &testapi.FakeLogsRepository{ TailLogMessages: []logmessage.LogMessage{ logMessage1, logMessage2, }, } args := []string{"my-app"} reqFactory = &testreq.FakeReqFactory{Application: app} ui = callStart(args, config, reqFactory, appRepo, logRepo) return }
func TestLogsTailsTheAppLogs(t *testing.T) { app := cf.Application{Name: "my-app", Guid: "my-app-guid"} currentTime := time.Now() messageType := logmessage.LogMessage_ERR deaSourceType := logmessage.LogMessage_DEA deaSourceId := "42" deaLogMessage := logmessage.LogMessage{ Message: []byte("Log Line 1"), AppId: proto.String("my-app"), MessageType: &messageType, SourceType: &deaSourceType, SourceId: &deaSourceId, Timestamp: proto.Int64(currentTime.UnixNano()), } logs := []logmessage.LogMessage{deaLogMessage} reqFactory, logsRepo := getLogsDependencies() reqFactory.Application = app logsRepo.TailLogMessages = logs ui := callLogs(t, []string{"my-app"}, reqFactory, logsRepo) assert.Equal(t, reqFactory.ApplicationName, "my-app") assert.Equal(t, app, logsRepo.AppLogged) assert.Equal(t, len(ui.Outputs), 2) assert.Contains(t, ui.Outputs[0], "Connected, tailing logs for app") assert.Contains(t, ui.Outputs[0], "my-app") assert.Contains(t, ui.Outputs[0], "my-org") assert.Contains(t, ui.Outputs[0], "my-space") assert.Contains(t, ui.Outputs[0], "my-user") assert.Contains(t, ui.Outputs[1], "Log Line 1") }
func (proxy *Proxy) isAuthorized(appId, authToken string, clientAddress string) (bool, *logmessage.LogMessage) { newLogMessage := func(message []byte) *logmessage.LogMessage { currentTime := time.Now() messageType := logmessage.LogMessage_ERR return &logmessage.LogMessage{ Message: message, AppId: proto.String(appId), MessageType: &messageType, SourceName: proto.String("LGR"), Timestamp: proto.Int64(currentTime.UnixNano()), } } if appId == "" { message := fmt.Sprintf("HttpServer: Did not accept sink connection with invalid app id: %s.", clientAddress) proxy.logger.Warn(message) return false, newLogMessage([]byte("Error: Invalid target")) } if authToken == "" { message := fmt.Sprintf("HttpServer: Did not accept sink connection from %s without authorization.", clientAddress) proxy.logger.Warnf(message) return false, newLogMessage([]byte("Error: Authorization not provided")) } if !proxy.authorize(authToken, appId, proxy.logger) { message := fmt.Sprintf("HttpServer: Auth token [%s] not authorized to access appId [%s].", authToken, appId) proxy.logger.Warn(message) return false, newLogMessage([]byte("Error: Invalid authorization")) } return true, nil }
func NewHttpStop(req *http.Request, statusCode int, contentLength int64, peerType events.PeerType, requestId *uuid.UUID) *events.HttpStop { httpStop := &events.HttpStop{ Timestamp: proto.Int64(time.Now().UnixNano()), Uri: proto.String(fmt.Sprintf("%s%s", req.Host, req.URL.Path)), RequestId: NewUUID(requestId), PeerType: &peerType, StatusCode: proto.Int(statusCode), ContentLength: proto.Int64(contentLength), } if applicationId, err := uuid.ParseHex(req.Header.Get("X-CF-ApplicationID")); err == nil { httpStop.ApplicationId = NewUUID(applicationId) } return httpStop }
func NewHttpStart(req *http.Request, peerType events.PeerType, requestId *uuid.UUID) *events.HttpStart { httpStart := &events.HttpStart{ Timestamp: proto.Int64(time.Now().UnixNano()), RequestId: NewUUID(requestId), PeerType: &peerType, Method: events.HttpStart_Method(events.HttpStart_Method_value[req.Method]).Enum(), Uri: proto.String(fmt.Sprintf("%s%s", req.Host, req.URL.Path)), RemoteAddress: proto.String(req.RemoteAddr), UserAgent: proto.String(req.UserAgent()), } if applicationId, err := uuid.ParseHex(req.Header.Get("X-CF-ApplicationID")); err == nil { httpStart.ApplicationId = NewUUID(applicationId) } if instanceIndex, err := strconv.Atoi(req.Header.Get("X-CF-InstanceIndex")); err == nil { httpStart.InstanceIndex = proto.Int(instanceIndex) } if instanceId := req.Header.Get("X-CF-InstanceID"); instanceId != "" { httpStart.InstanceId = &instanceId } return httpStart }
// Increment fetches the value for key, and assuming the value is an // "integer" type, increments it by inc and stores the new value. The // newly incremented value is returned. func (mvcc *MVCC) Increment(key Key, timestamp proto.Timestamp, txn *proto.Transaction, inc int64) (int64, error) { // Handle check for non-existence of key. In order to detect // the potential write intent by another concurrent transaction // with a newer timestamp, we need to use the max timestamp // while reading. value, err := mvcc.Get(key, proto.MaxTimestamp, txn) if err != nil { return 0, err } var int64Val int64 // If the value exists, verify it's an integer type not a byte slice. if value != nil { if value.Bytes != nil || value.Integer == nil { return 0, util.Errorf("cannot increment key %q which already has a generic byte value: %+v", key, *value) } int64Val = value.GetInteger() } // Check for overflow and underflow. if encoding.WillOverflow(int64Val, inc) { return 0, util.Errorf("key %q with value %d incremented by %d results in overflow", key, int64Val, inc) } if inc == 0 { return int64Val, nil } r := int64Val + inc value = &proto.Value{Integer: gogoproto.Int64(r)} value.InitChecksum(key) return r, mvcc.Put(key, timestamp, *value, txn) }
func TestValueBothBytesAndIntegerSet(t *testing.T) { k := []byte("key") v := Value{Bytes: []byte("a"), Integer: gogoproto.Int64(0)} if err := v.Verify(k); err == nil { t.Error("expected error with both byte slice and integer fields set") } }
func ExampleCompile() { a := &test.NinOptNative{ Field4: proto.Int64(1234), Field7: proto.Int32(123), } fp1, err := fieldpath.NewInt64Path("test", "NinOptNative", test.ThetestDescription(), "Field4") if err != nil { panic(err) } fp2, err := fieldpath.NewSint32Path("test", "NinOptNative", test.ThetestDescription(), "Field7") if err != nil { panic(err) } buf, err := proto.Marshal(a) if err != nil { panic(err) } u1 := fieldpath.NewInt64Unmarshaler(fp1, &handler64{}) u2 := fieldpath.NewSint32Unmarshaler(fp2, &handler32{}) c := fieldpath.Compile(u1, u2) err = c.Unmarshal(buf) if err != nil { panic(err) } // Output: // 1234 // 123 }
func TestMVCCPutWithBadValue(t *testing.T) { mvcc := createTestMVCC(t) badValue := proto.Value{Bytes: []byte("a"), Integer: gogoproto.Int64(1)} err := mvcc.Put(testKey1, makeTS(0, 0), badValue, nil) if err == nil { t.Fatal("expected an error putting a value with both byte slice and integer components") } }
func newTestMessage() *pb.MyMessage { msg := &pb.MyMessage{ Count: proto.Int32(42), Name: proto.String("Dave"), Quote: proto.String(`"I didn't want to go."`), Pet: []string{"bunny", "kitty", "horsey"}, Inner: &pb.InnerMessage{ Host: proto.String("footrest.syd"), Port: proto.Int32(7001), Connected: proto.Bool(true), }, Others: []*pb.OtherMessage{ { Key: proto.Int64(0xdeadbeef), Value: []byte{1, 65, 7, 12}, }, { Weight: proto.Float32(6.022), Inner: &pb.InnerMessage{ Host: proto.String("lesha.mtv"), Port: proto.Int32(8002), }, }, }, Bikeshed: pb.MyMessage_BLUE.Enum(), Somegroup: &pb.MyMessage_SomeGroup{ GroupField: proto.Int32(8), }, // One normally wouldn't do this. // This is an undeclared tag 13, as a varint (wire type 0) with value 4. XXX_unrecognized: []byte{13<<3 | 0, 4}, } ext := &pb.Ext{ Data: proto.String("Big gobs for big rats"), } if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil { panic(err) } greetings := []string{"adg", "easy", "cow"} if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil { panic(err) } // Add an unknown extension. We marshal a pb.Ext, and fake the ID. b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")}) if err != nil { panic(err) } b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...) proto.SetRawExtension(msg, 201, b) // Extensions can be plain fields, too, so let's test that. b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19) proto.SetRawExtension(msg, 202, b) return msg }
func TestLogsOutputsRecentLogs(t *testing.T) { app := cf.Application{Name: "my-app", Guid: "my-app-guid"} currentTime := time.Now() messageType := logmessage.LogMessage_ERR sourceType := logmessage.LogMessage_DEA logMessage1 := logmessage.LogMessage{ Message: []byte("Log Line 1"), AppId: proto.String("my-app"), MessageType: &messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } logMessage2 := logmessage.LogMessage{ Message: []byte("Log Line 2"), AppId: proto.String("my-app"), MessageType: &messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } recentLogs := []logmessage.LogMessage{ logMessage1, logMessage2, } reqFactory, logsRepo := getLogsDependencies() reqFactory.Application = app logsRepo.RecentLogs = recentLogs ui := callLogs(t, []string{"--recent", "my-app"}, reqFactory, logsRepo) assert.Equal(t, reqFactory.ApplicationName, "my-app") assert.Equal(t, app, logsRepo.AppLogged) assert.Equal(t, len(ui.Outputs), 3) assert.Contains(t, ui.Outputs[0], "Connected, dumping recent logs for app") assert.Contains(t, ui.Outputs[0], "my-app") assert.Contains(t, ui.Outputs[0], "my-org") assert.Contains(t, ui.Outputs[0], "my-space") assert.Contains(t, ui.Outputs[0], "my-user") assert.Contains(t, ui.Outputs[1], "Log Line 1") assert.Contains(t, ui.Outputs[2], "Log Line 2") }
func makeLogMessage(appId, message, sourceType, sourceInstance string, messageType events.LogMessage_MessageType) *events.LogMessage { return &events.LogMessage{ Message: []byte(message), AppId: proto.String(appId), MessageType: &messageType, SourceType: &sourceType, SourceInstance: &sourceInstance, Timestamp: proto.Int64(time.Now().UnixNano()), } }
func (ls *loggingStream) listen() { newLogMessage := func(message []byte) *logmessage.LogMessage { currentTime := time.Now() sourceType := logmessage.LogMessage_WARDEN_CONTAINER return &logmessage.LogMessage{ Message: message, AppId: proto.String(ls.inst.applicationId), DrainUrls: ls.inst.drainUrls, MessageType: &ls.messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } } socket := func(messageType logmessage.LogMessage_MessageType) (net.Conn, error) { return net.Dial("unix", filepath.Join(ls.inst.identifier(), socketName(messageType))) } go func() { connection, err := socket(ls.messageType) if err != nil { ls.logger.Errorf("Error while dialing into socket %s, %s", ls.messageType, err) return } defer func() { connection.Close() ls.logger.Infof("Stopped reading from socket %s", ls.messageType) }() buffer := make([]byte, bufferSize) for { readCount, err := connection.Read(buffer) if readCount == 0 && err != nil { ls.logger.Infof("Error while reading from socket %s, %s", ls.messageType, err) break } ls.logger.Debugf("Read %d bytes from instance socket %s", readCount, ls.messageType) atomic.AddUint64(ls.messagesReceived, 1) atomic.AddUint64(ls.bytesReceived, uint64(readCount)) ls.loggregatorClient.IncLogStreamRawByteCount(uint64(readCount)) data, err := proto.Marshal(newLogMessage(buffer[0:readCount])) if err != nil { ls.logger.Errorf("Error marshalling log message: %s", err) } ls.loggregatorClient.IncLogStreamPbByteCount(uint64(len(data))) ls.loggregatorClient.Send(data) ls.logger.Debugf("Sent %d bytes to loggregator client", readCount) runtime.Gosched() } }() }
func TestLogsTailsTheAppLogs(t *testing.T) { app := cf.Application{Name: "my-app", Guid: "my-app-guid"} /////////////// currentTime := time.Now() messageType := logmessage.LogMessage_ERR sourceType := logmessage.LogMessage_DEA logMessage1 := &logmessage.LogMessage{ Message: []byte("Log Line 1"), AppId: proto.String("my-app"), MessageType: &messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } logMessage2 := &logmessage.LogMessage{ Message: []byte("Log Line 2"), AppId: proto.String("my-app"), MessageType: &messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } ///////////////// logs := []*logmessage.LogMessage{ logMessage1, logMessage2, } reqFactory, logsRepo := getDefaultLogsDependencies() reqFactory.Application = app logsRepo.TailLogMessages = logs ui := callLogs([]string{"my-app"}, reqFactory, logsRepo) assert.Equal(t, reqFactory.ApplicationName, "my-app") assert.Equal(t, app, logsRepo.AppLogged) assert.Equal(t, len(ui.Outputs), 3) assert.Contains(t, ui.Outputs[0], "Connected") assert.Contains(t, ui.Outputs[1], "Log Line 1") assert.Contains(t, ui.Outputs[2], "Log Line 2") }
func TestRecentLogsWithAppName(t *testing.T) { config := &configuration.Configuration{} app := cf.Application{Name: "my-app", Guid: "my-app-guid"} appRepo := &testhelpers.FakeApplicationRepository{AppByName: app} /////////////// currentTime := time.Now() messageType := logmessage.LogMessage_ERR sourceType := logmessage.LogMessage_DEA logMessage1 := &logmessage.LogMessage{ Message: []byte("Log Line 1"), AppId: proto.String("my-app"), MessageType: &messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } logMessage2 := &logmessage.LogMessage{ Message: []byte("Log Line 2"), AppId: proto.String("my-app"), MessageType: &messageType, SourceType: &sourceType, Timestamp: proto.Int64(currentTime.UnixNano()), } ///////////////// recentLogs := []*logmessage.LogMessage{ logMessage1, logMessage2, } logsRepo := &testhelpers.FakeLogsRepository{RecentLogs: recentLogs} reqFactory := &testhelpers.FakeReqFactory{Application: app} ui := callRecentLogs([]string{"my-app"}, config, reqFactory, appRepo, logsRepo) assert.Equal(t, reqFactory.ApplicationName, "my-app") assert.Equal(t, app, logsRepo.AppLogged) assert.Equal(t, len(ui.Outputs), 2) assert.Contains(t, ui.Outputs[0], "Log Line 1") assert.Contains(t, ui.Outputs[1], "Log Line 2") }
func NewNoaaLogMessage(msgText, appGuid, sourceName string, timestamp time.Time) *events.LogMessage { messageType := events.LogMessage_ERR return &events.LogMessage{ Message: []byte(msgText), AppId: proto.String(appGuid), MessageType: &messageType, SourceType: proto.String(sourceName), Timestamp: proto.Int64(timestamp.UnixNano()), } }
func makeLogMessage(message string, timestamp int64) *logmessage.LogMessage { messageType := logmessage.LogMessage_OUT sourceName := "DEA" return &logmessage.LogMessage{ Message: []byte(message), AppId: proto.String("my-app-guid"), MessageType: &messageType, SourceName: &sourceName, Timestamp: proto.Int64(timestamp), } }
func TestValueChecksumWithInteger(t *testing.T) { k := []byte("key") testValues := []int64{0, 1, -1, math.MinInt64, math.MaxInt64} for _, i := range testValues { v := Value{Integer: gogoproto.Int64(i)} v.InitChecksum(k) if err := v.Verify(k); err != nil { t.Error(err) } // Try a different key; should fail. if err := v.Verify([]byte("key2")); err == nil { t.Error("expected checksum verification failure on different key") } // Mess with value. v.Integer = gogoproto.Int64(i + 1) if err := v.Verify(k); err == nil { t.Error("expected checksum verification failure on different value") } } }
func (e *LoggregatorEmitter) newLogMessage(appId, message string, mt events.LogMessage_MessageType) *events.LogMessage { currentTime := time.Now() return &events.LogMessage{ Message: []byte(message), AppId: proto.String(appId), MessageType: &mt, SourceInstance: &e.sId, Timestamp: proto.Int64(currentTime.UnixNano()), SourceType: &e.sn, } }
func generateLogMessage(messageString string, appId *string) *logmessage.LogMessage { messageType := logmessage.LogMessage_ERR currentTime := time.Now() logMessage := &logmessage.LogMessage{ Message: []byte(messageString), AppId: appId, MessageType: &messageType, SourceName: proto.String("LGR"), Timestamp: proto.Int64(currentTime.UnixNano()), } return logMessage }
func createMessage(message string, timestamp int64) *events.Envelope { if timestamp == 0 { timestamp = time.Now().UnixNano() } logMessageType := events.LogMessage_OUT logMessage := &events.LogMessage{ Message: []byte(message), MessageType: &logMessageType, AppId: proto.String("my-app-guid"), SourceType: proto.String("DEA"), Timestamp: proto.Int64(timestamp), } eventType := events.Envelope_LogMessage return &events.Envelope{ LogMessage: logMessage, EventType: &eventType, Origin: proto.String("fake-origin-1"), Timestamp: proto.Int64(timestamp), } }
func NewLogMessage(messageType events.LogMessage_MessageType, messageString, appId, sourceType string) *events.LogMessage { currentTime := time.Now() logMessage := &events.LogMessage{ Message: []byte(messageString), AppId: &appId, MessageType: &messageType, SourceType: proto.String(sourceType), Timestamp: proto.Int64(currentTime.UnixNano()), } return logMessage }
func NewLogMessageWithSourceName(t *testing.T, messageString, sourceName, appId string) *LogMessage { currentTime := time.Now() messageType := LogMessage_OUT protoMessage := &LogMessage{ Message: []byte(messageString), AppId: proto.String(appId), MessageType: &messageType, Timestamp: proto.Int64(currentTime.UnixNano()), SourceName: proto.String(sourceName), } return protoMessage }
func generateLogMessage(messageString, appId string, messageType logmessage.LogMessage_MessageType, sourceName, sourceId string) *logmessage.LogMessage { currentTime := time.Now() logMessage := &logmessage.LogMessage{ Message: []byte(messageString), AppId: proto.String(appId), MessageType: &messageType, SourceName: proto.String(sourceName), SourceId: proto.String(sourceId), Timestamp: proto.Int64(currentTime.UnixNano()), } return logMessage }
func generateLogMessage(messageString string, appId string) []byte { messageType := logmessage.LogMessage_ERR currentTime := time.Now() logMessage := &logmessage.LogMessage{ Message: []byte(messageString), AppId: proto.String(appId), MessageType: &messageType, SourceName: proto.String("LGR"), Timestamp: proto.Int64(currentTime.UnixNano()), } msg, _ := proto.Marshal(logMessage) return msg }
func NewLogMessage(msgText, appGuid, sourceName string, timestamp time.Time) (msg *logmessage.Message) { messageType := logmessage.LogMessage_ERR logMsg := logmessage.LogMessage{ Message: []byte(msgText), AppId: proto.String(appGuid), MessageType: &messageType, SourceName: proto.String(sourceName), Timestamp: proto.Int64(timestamp.UnixNano()), } data, _ := proto.Marshal(&logMsg) msg, _ = logmessage.ParseMessage(data) return }
func marshalledLogMessageWithTime(messageString string, timestamp int64) []byte { messageType := logmessage.LogMessage_OUT sourceName := "DEA" protoMessage := &logmessage.LogMessage{ Message: []byte(messageString), AppId: proto.String("my-app-guid"), MessageType: &messageType, SourceName: &sourceName, Timestamp: proto.Int64(timestamp), } message, err := proto.Marshal(protoMessage) Expect(err).ToNot(HaveOccurred()) return message }
func GenerateMessage(messageType LogMessage_MessageType, messageString, appId, sourceName string) (*Message, error) { currentTime := time.Now() logMessage := &LogMessage{ Message: []byte(messageString), AppId: &appId, MessageType: &messageType, SourceName: proto.String(sourceName), Timestamp: proto.Int64(currentTime.UnixNano()), } lmBytes, err := proto.Marshal(logMessage) if err != nil { return nil, err } return NewMessage(logMessage, lmBytes), nil }
func marshalledLogMessageWithTime(t *testing.T, messageString string, timestamp int64) []byte { messageType := logmessage.LogMessage_OUT sourceName := "DEA" protoMessage := &logmessage.LogMessage{ Message: []byte(messageString), AppId: proto.String("my-app-guid"), MessageType: &messageType, SourceName: &sourceName, Timestamp: proto.Int64(timestamp), } message, err := proto.Marshal(protoMessage) assert.NoError(t, err) return message }
func createMessage(message string, timestamp int64) *logmessage.LogMessage { messageType := logmessage.LogMessage_OUT sourceName := "DEA" if timestamp == 0 { timestamp = time.Now().UnixNano() } return &logmessage.LogMessage{ Message: []byte(message), AppId: proto.String("my-app-guid"), MessageType: &messageType, SourceName: &sourceName, Timestamp: proto.Int64(timestamp), } }