summaryrefslogtreecommitdiffstats
path: root/vendor/google.golang.org/appengine/log/log_test.go
blob: 726468e2377264a7a0c35334caee8102d5bb5ecf (plain)
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
// Copyright 2014 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package log

import (
	"reflect"
	"testing"
	"time"

	"github.com/golang/protobuf/proto"

	pb "google.golang.org/appengine/internal/log"
)

func TestQueryToRequest(t *testing.T) {
	testCases := []struct {
		desc  string
		query *Query
		want  *pb.LogReadRequest
	}{
		{
			desc:  "Empty",
			query: &Query{},
			want: &pb.LogReadRequest{
				AppId:     proto.String("s~fake"),
				VersionId: []string{"v12"},
			},
		},
		{
			desc: "Versions",
			query: &Query{
				Versions: []string{"alpha", "backend:beta"},
			},
			want: &pb.LogReadRequest{
				AppId: proto.String("s~fake"),
				ModuleVersion: []*pb.LogModuleVersion{
					{
						VersionId: proto.String("alpha"),
					}, {
						ModuleId:  proto.String("backend"),
						VersionId: proto.String("beta"),
					},
				},
			},
		},
	}

	for _, tt := range testCases {
		req, err := makeRequest(tt.query, "s~fake", "v12")

		if err != nil {
			t.Errorf("%s: got err %v, want nil", tt.desc, err)
			continue
		}
		if !proto.Equal(req, tt.want) {
			t.Errorf("%s request:\ngot  %v\nwant %v", tt.desc, req, tt.want)
		}
	}
}

func TestProtoToRecord(t *testing.T) {
	// We deliberately leave ModuleId and other optional fields unset.
	p := &pb.RequestLog{
		AppId:        proto.String("s~fake"),
		VersionId:    proto.String("1"),
		RequestId:    []byte("deadbeef"),
		Ip:           proto.String("127.0.0.1"),
		StartTime:    proto.Int64(431044244000000),
		EndTime:      proto.Int64(431044724000000),
		Latency:      proto.Int64(480000000),
		Mcycles:      proto.Int64(7),
		Method:       proto.String("GET"),
		Resource:     proto.String("/app"),
		HttpVersion:  proto.String("1.1"),
		Status:       proto.Int32(418),
		ResponseSize: proto.Int64(1337),
		UrlMapEntry:  proto.String("_go_app"),
		Combined:     proto.String("apache log"),
	}
	// Sanity check that all required fields are set.
	if _, err := proto.Marshal(p); err != nil {
		t.Fatalf("proto.Marshal: %v", err)
	}
	want := &Record{
		AppID:        "s~fake",
		ModuleID:     "default",
		VersionID:    "1",
		RequestID:    []byte("deadbeef"),
		IP:           "127.0.0.1",
		StartTime:    time.Date(1983, 8, 29, 22, 30, 44, 0, time.UTC),
		EndTime:      time.Date(1983, 8, 29, 22, 38, 44, 0, time.UTC),
		Latency:      8 * time.Minute,
		MCycles:      7,
		Method:       "GET",
		Resource:     "/app",
		HTTPVersion:  "1.1",
		Status:       418,
		ResponseSize: 1337,
		URLMapEntry:  "_go_app",
		Combined:     "apache log",
		Finished:     true,
		AppLogs:      []AppLog{},
	}
	got := protoToRecord(p)
	// Coerce locations to UTC since otherwise they will be in local.
	got.StartTime, got.EndTime = got.StartTime.UTC(), got.EndTime.UTC()
	if !reflect.DeepEqual(got, want) {
		t.Errorf("protoToRecord:\ngot:  %v\nwant: %v", got, want)
	}
}