You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

176 lines
4.7 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package errorreporting
  15. import (
  16. "context"
  17. "errors"
  18. "strings"
  19. "testing"
  20. "time"
  21. "cloud.google.com/go/internal/testutil"
  22. gax "github.com/googleapis/gax-go/v2"
  23. "google.golang.org/api/option"
  24. pb "google.golang.org/genproto/googleapis/devtools/clouderrorreporting/v1beta1"
  25. )
  26. type fakeReportErrorsClient struct {
  27. req *pb.ReportErrorEventRequest
  28. fail bool
  29. doneCh chan struct{}
  30. }
  31. func (c *fakeReportErrorsClient) ReportErrorEvent(ctx context.Context, req *pb.ReportErrorEventRequest, _ ...gax.CallOption) (*pb.ReportErrorEventResponse, error) {
  32. defer close(c.doneCh)
  33. if c.fail {
  34. return nil, errors.New("request failed")
  35. }
  36. c.req = req
  37. return &pb.ReportErrorEventResponse{}, nil
  38. }
  39. func (c *fakeReportErrorsClient) Close() error {
  40. return nil
  41. }
  42. var defaultConfig = Config{
  43. ServiceName: "myservice",
  44. ServiceVersion: "v1.0",
  45. }
  46. func newFakeReportErrorsClient() *fakeReportErrorsClient {
  47. c := &fakeReportErrorsClient{}
  48. c.doneCh = make(chan struct{})
  49. return c
  50. }
  51. func newTestClient(c *fakeReportErrorsClient, cfg Config) *Client {
  52. newClient = func(ctx context.Context, opts ...option.ClientOption) (client, error) {
  53. return c, nil
  54. }
  55. t, err := NewClient(context.Background(), testutil.ProjID(), cfg)
  56. if err != nil {
  57. panic(err)
  58. }
  59. return t
  60. }
  61. func commonChecks(t *testing.T, req *pb.ReportErrorEventRequest, fn string) {
  62. if req.Event.ServiceContext.Service != "myservice" {
  63. t.Errorf("error report didn't contain service name")
  64. }
  65. if req.Event.ServiceContext.Version != "v1.0" {
  66. t.Errorf("error report didn't contain version name")
  67. }
  68. if !strings.Contains(req.Event.Message, "error") {
  69. t.Errorf("error report didn't contain message")
  70. }
  71. if !strings.Contains(req.Event.Message, fn) {
  72. t.Errorf("error report didn't contain stack trace")
  73. }
  74. if got, want := req.Event.Context.User, "user"; got != want {
  75. t.Errorf("got %q, want %q", got, want)
  76. }
  77. }
  78. func TestReport(t *testing.T) {
  79. fc := newFakeReportErrorsClient()
  80. c := newTestClient(fc, defaultConfig)
  81. c.Report(Entry{Error: errors.New("error"), User: "user"})
  82. c.Flush()
  83. <-fc.doneCh
  84. r := fc.req
  85. if r == nil {
  86. t.Fatalf("got no error report, expected one")
  87. }
  88. commonChecks(t, r, "errorreporting.TestReport")
  89. }
  90. func TestReportSync(t *testing.T) {
  91. ctx := context.Background()
  92. fc := newFakeReportErrorsClient()
  93. c := newTestClient(fc, defaultConfig)
  94. if err := c.ReportSync(ctx, Entry{Error: errors.New("error"), User: "user"}); err != nil {
  95. t.Fatalf("cannot upload errors: %v", err)
  96. }
  97. <-fc.doneCh
  98. r := fc.req
  99. if r == nil {
  100. t.Fatalf("got no error report, expected one")
  101. }
  102. commonChecks(t, r, "errorreporting.TestReport")
  103. }
  104. func TestOnError(t *testing.T) {
  105. fc := newFakeReportErrorsClient()
  106. fc.fail = true
  107. cfg := defaultConfig
  108. errc := make(chan error, 1)
  109. cfg.OnError = func(err error) { errc <- err }
  110. c := newTestClient(fc, cfg)
  111. c.Report(Entry{Error: errors.New("error")})
  112. c.Flush()
  113. <-fc.doneCh
  114. select {
  115. case err := <-errc:
  116. if err == nil {
  117. t.Error("got nil, want error")
  118. }
  119. case <-time.After(5 * time.Second):
  120. t.Error("timeout")
  121. }
  122. }
  123. func TestChopStack(t *testing.T) {
  124. for _, test := range []struct {
  125. name string
  126. in []byte
  127. expected string
  128. }{
  129. {
  130. name: "Report",
  131. in: []byte(` goroutine 39 [running]:
  132. runtime/debug.Stack()
  133. /gopath/runtime/debug/stack.go:24 +0x79
  134. cloud.google.com/go/errorreporting.(*Client).logInternal()
  135. /gopath/cloud.google.com/go/errorreporting/errors.go:259 +0x18b
  136. cloud.google.com/go/errorreporting.(*Client).Report()
  137. /gopath/cloud.google.com/go/errorreporting/errors.go:248 +0x4ed
  138. cloud.google.com/go/errorreporting.TestReport()
  139. /gopath/cloud.google.com/go/errorreporting/errors_test.go:137 +0x2a1
  140. testing.tRunner()
  141. /gopath/testing/testing.go:610 +0x81
  142. created by testing.(*T).Run
  143. /gopath/testing/testing.go:646 +0x2ec
  144. `),
  145. expected: ` goroutine 39 [running]:
  146. cloud.google.com/go/errorreporting.TestReport()
  147. /gopath/cloud.google.com/go/errorreporting/errors_test.go:137 +0x2a1
  148. testing.tRunner()
  149. /gopath/testing/testing.go:610 +0x81
  150. created by testing.(*T).Run
  151. /gopath/testing/testing.go:646 +0x2ec
  152. `,
  153. },
  154. } {
  155. out := chopStack(test.in)
  156. if out != test.expected {
  157. t.Errorf("case %q: chopStack(%q): got %q want %q", test.name, test.in, out, test.expected)
  158. }
  159. }
  160. }