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.
 
 
 

114 lines
3.0 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. "errors"
  17. "strings"
  18. "testing"
  19. "cloud.google.com/go/internal/testutil"
  20. gax "github.com/googleapis/gax-go"
  21. "golang.org/x/net/context"
  22. "google.golang.org/api/option"
  23. erpb "google.golang.org/genproto/googleapis/devtools/clouderrorreporting/v1beta1"
  24. )
  25. type fakeReportErrorsClient struct {
  26. req *erpb.ReportErrorEventRequest
  27. fail bool
  28. doneCh chan struct{}
  29. }
  30. func (c *fakeReportErrorsClient) ReportErrorEvent(ctx context.Context, req *erpb.ReportErrorEventRequest, _ ...gax.CallOption) (*erpb.ReportErrorEventResponse, error) {
  31. defer func() {
  32. close(c.doneCh)
  33. }()
  34. if c.fail {
  35. return nil, errors.New("request failed")
  36. }
  37. c.req = req
  38. return &erpb.ReportErrorEventResponse{}, nil
  39. }
  40. func (c *fakeReportErrorsClient) Close() error {
  41. return nil
  42. }
  43. func newFakeReportErrorsClient() *fakeReportErrorsClient {
  44. c := &fakeReportErrorsClient{}
  45. c.doneCh = make(chan struct{})
  46. return c
  47. }
  48. func newTestClient(c *fakeReportErrorsClient) *Client {
  49. newClient = func(ctx context.Context, opts ...option.ClientOption) (client, error) {
  50. return c, nil
  51. }
  52. t, err := NewClient(context.Background(), testutil.ProjID(), Config{
  53. ServiceName: "myservice",
  54. ServiceVersion: "v1.0",
  55. })
  56. if err != nil {
  57. panic(err)
  58. }
  59. return t
  60. }
  61. func commonChecks(t *testing.T, req *erpb.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. }
  75. func TestReport(t *testing.T) {
  76. fc := newFakeReportErrorsClient()
  77. c := newTestClient(fc)
  78. c.Report(Entry{Error: errors.New("error")})
  79. <-fc.doneCh
  80. r := fc.req
  81. if r == nil {
  82. t.Fatalf("got no error report, expected one")
  83. }
  84. commonChecks(t, r, "errorreporting.TestReport")
  85. }
  86. func TestReportSync(t *testing.T) {
  87. ctx := context.Background()
  88. fc := newFakeReportErrorsClient()
  89. c := newTestClient(fc)
  90. if err := c.ReportSync(ctx, Entry{Error: errors.New("error")}); err != nil {
  91. t.Fatalf("cannot upload errors: %v", err)
  92. }
  93. <-fc.doneCh
  94. r := fc.req
  95. if r == nil {
  96. t.Fatalf("got no error report, expected one")
  97. }
  98. commonChecks(t, r, "errorreporting.TestReport")
  99. }