2017-09-13 14:09:48 +02:00
|
|
|
// Copyright 2016 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package errorreporting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-10-22 10:07:36 +02:00
|
|
|
"cloud.google.com/go/internal/testutil"
|
|
|
|
|
2017-09-13 14:09:48 +02:00
|
|
|
gax "github.com/googleapis/gax-go"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
"google.golang.org/api/option"
|
|
|
|
erpb "google.golang.org/genproto/googleapis/devtools/clouderrorreporting/v1beta1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fakeReportErrorsClient struct {
|
2017-10-22 10:07:36 +02:00
|
|
|
req *erpb.ReportErrorEventRequest
|
|
|
|
fail bool
|
|
|
|
doneCh chan struct{}
|
2017-09-13 14:09:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeReportErrorsClient) ReportErrorEvent(ctx context.Context, req *erpb.ReportErrorEventRequest, _ ...gax.CallOption) (*erpb.ReportErrorEventResponse, error) {
|
2017-10-22 10:07:36 +02:00
|
|
|
defer func() {
|
|
|
|
close(c.doneCh)
|
|
|
|
}()
|
2017-09-13 14:09:48 +02:00
|
|
|
if c.fail {
|
|
|
|
return nil, errors.New("request failed")
|
|
|
|
}
|
|
|
|
c.req = req
|
|
|
|
return &erpb.ReportErrorEventResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *fakeReportErrorsClient) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-22 10:07:36 +02:00
|
|
|
func newFakeReportErrorsClient() *fakeReportErrorsClient {
|
|
|
|
c := &fakeReportErrorsClient{}
|
|
|
|
c.doneCh = make(chan struct{})
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2017-09-13 14:09:48 +02:00
|
|
|
func newTestClient(c *fakeReportErrorsClient) *Client {
|
2017-10-22 10:07:36 +02:00
|
|
|
newClient = func(ctx context.Context, opts ...option.ClientOption) (client, error) {
|
2017-09-13 14:09:48 +02:00
|
|
|
return c, nil
|
|
|
|
}
|
2017-10-22 10:07:36 +02:00
|
|
|
t, err := NewClient(context.Background(), testutil.ProjID(), Config{
|
|
|
|
ServiceName: "myservice",
|
|
|
|
ServiceVersion: "v1.0",
|
|
|
|
})
|
2017-09-13 14:09:48 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2017-10-22 10:07:36 +02:00
|
|
|
func commonChecks(t *testing.T, req *erpb.ReportErrorEventRequest, fn string) {
|
2017-09-13 14:09:48 +02:00
|
|
|
if req.Event.ServiceContext.Service != "myservice" {
|
|
|
|
t.Errorf("error report didn't contain service name")
|
|
|
|
}
|
2017-10-22 10:07:36 +02:00
|
|
|
if req.Event.ServiceContext.Version != "v1.0" {
|
2017-09-13 14:09:48 +02:00
|
|
|
t.Errorf("error report didn't contain version name")
|
|
|
|
}
|
2017-10-22 10:07:36 +02:00
|
|
|
if !strings.Contains(req.Event.Message, "error") {
|
2017-09-13 14:09:48 +02:00
|
|
|
t.Errorf("error report didn't contain message")
|
|
|
|
}
|
2017-10-22 10:07:36 +02:00
|
|
|
if !strings.Contains(req.Event.Message, fn) {
|
2017-09-13 14:09:48 +02:00
|
|
|
t.Errorf("error report didn't contain stack trace")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReport(t *testing.T) {
|
2017-10-22 10:07:36 +02:00
|
|
|
fc := newFakeReportErrorsClient()
|
2017-09-13 14:09:48 +02:00
|
|
|
c := newTestClient(fc)
|
2017-10-22 10:07:36 +02:00
|
|
|
c.Report(Entry{Error: errors.New("error")})
|
|
|
|
|
|
|
|
<-fc.doneCh
|
2017-09-13 14:09:48 +02:00
|
|
|
r := fc.req
|
|
|
|
if r == nil {
|
|
|
|
t.Fatalf("got no error report, expected one")
|
|
|
|
}
|
|
|
|
commonChecks(t, r, "errorreporting.TestReport")
|
|
|
|
}
|
2017-10-22 10:07:36 +02:00
|
|
|
func TestReportSync(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
fc := newFakeReportErrorsClient()
|
2017-09-13 14:09:48 +02:00
|
|
|
c := newTestClient(fc)
|
2017-10-22 10:07:36 +02:00
|
|
|
if err := c.ReportSync(ctx, Entry{Error: errors.New("error")}); err != nil {
|
|
|
|
t.Fatalf("cannot upload errors: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-fc.doneCh
|
2017-09-13 14:09:48 +02:00
|
|
|
r := fc.req
|
|
|
|
if r == nil {
|
|
|
|
t.Fatalf("got no error report, expected one")
|
|
|
|
}
|
2017-10-22 10:07:36 +02:00
|
|
|
commonChecks(t, r, "errorreporting.TestReport")
|
2017-09-13 14:09:48 +02:00
|
|
|
}
|