This file is indexed.

/usr/share/gocode/src/github.com/Sirupsen/logrus/hooks/airbrake/airbrake_test.go is in golang-github-sirupsen-logrus-dev 0.8.7-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package airbrake

import (
	"encoding/xml"
	"net/http"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/Sirupsen/logrus"
)

type notice struct {
	Error NoticeError `xml:"error"`
}
type NoticeError struct {
	Class   string `xml:"class"`
	Message string `xml:"message"`
}

type customErr struct {
	msg string
}

func (e *customErr) Error() string {
	return e.msg
}

const (
	testAPIKey    = "abcxyz"
	testEnv       = "development"
	expectedClass = "*airbrake.customErr"
	expectedMsg   = "foo"
	unintendedMsg = "Airbrake will not see this string"
)

var (
	noticeError = make(chan NoticeError, 1)
)

// TestLogEntryMessageReceived checks if invoking Logrus' log.Error
// method causes an XML payload containing the log entry message is received
// by a HTTP server emulating an Airbrake-compatible endpoint.
func TestLogEntryMessageReceived(t *testing.T) {
	log := logrus.New()
	ts := startAirbrakeServer(t)
	defer ts.Close()

	hook := NewHook(ts.URL, testAPIKey, "production")
	log.Hooks.Add(hook)

	log.Error(expectedMsg)

	select {
	case received := <-noticeError:
		if received.Message != expectedMsg {
			t.Errorf("Unexpected message received: %s", received.Message)
		}
	case <-time.After(time.Second):
		t.Error("Timed out; no notice received by Airbrake API")
	}
}

// TestLogEntryMessageReceived confirms that, when passing an error type using
// logrus.Fields, a HTTP server emulating an Airbrake endpoint receives the
// error message returned by the Error() method on the error interface
// rather than the logrus.Entry.Message string.
func TestLogEntryWithErrorReceived(t *testing.T) {
	log := logrus.New()
	ts := startAirbrakeServer(t)
	defer ts.Close()

	hook := NewHook(ts.URL, testAPIKey, "production")
	log.Hooks.Add(hook)

	log.WithFields(logrus.Fields{
		"error": &customErr{expectedMsg},
	}).Error(unintendedMsg)

	select {
	case received := <-noticeError:
		if received.Message != expectedMsg {
			t.Errorf("Unexpected message received: %s", received.Message)
		}
		if received.Class != expectedClass {
			t.Errorf("Unexpected error class: %s", received.Class)
		}
	case <-time.After(time.Second):
		t.Error("Timed out; no notice received by Airbrake API")
	}
}

// TestLogEntryWithNonErrorTypeNotReceived confirms that, when passing a
// non-error type using logrus.Fields, a HTTP server emulating an Airbrake
// endpoint receives the logrus.Entry.Message string.
//
// Only error types are supported when setting the 'error' field using
// logrus.WithFields().
func TestLogEntryWithNonErrorTypeNotReceived(t *testing.T) {
	log := logrus.New()
	ts := startAirbrakeServer(t)
	defer ts.Close()

	hook := NewHook(ts.URL, testAPIKey, "production")
	log.Hooks.Add(hook)

	log.WithFields(logrus.Fields{
		"error": expectedMsg,
	}).Error(unintendedMsg)

	select {
	case received := <-noticeError:
		if received.Message != unintendedMsg {
			t.Errorf("Unexpected message received: %s", received.Message)
		}
	case <-time.After(time.Second):
		t.Error("Timed out; no notice received by Airbrake API")
	}
}

func startAirbrakeServer(t *testing.T) *httptest.Server {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var notice notice
		if err := xml.NewDecoder(r.Body).Decode(&notice); err != nil {
			t.Error(err)
		}
		r.Body.Close()

		noticeError <- notice.Error
	}))

	return ts
}