log_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2014 Google Inc. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package log
  5. import (
  6. "reflect"
  7. "testing"
  8. "time"
  9. "github.com/golang/protobuf/proto"
  10. pb "google.golang.org/appengine/internal/log"
  11. )
  12. func TestQueryToRequest(t *testing.T) {
  13. testCases := []struct {
  14. desc string
  15. query *Query
  16. want *pb.LogReadRequest
  17. }{
  18. {
  19. desc: "Empty",
  20. query: &Query{},
  21. want: &pb.LogReadRequest{
  22. AppId: proto.String("s~fake"),
  23. VersionId: []string{"v12"},
  24. },
  25. },
  26. {
  27. desc: "Versions",
  28. query: &Query{
  29. Versions: []string{"alpha", "backend:beta"},
  30. },
  31. want: &pb.LogReadRequest{
  32. AppId: proto.String("s~fake"),
  33. ModuleVersion: []*pb.LogModuleVersion{
  34. {
  35. VersionId: proto.String("alpha"),
  36. }, {
  37. ModuleId: proto.String("backend"),
  38. VersionId: proto.String("beta"),
  39. },
  40. },
  41. },
  42. },
  43. }
  44. for _, tt := range testCases {
  45. req, err := makeRequest(tt.query, "s~fake", "v12")
  46. if err != nil {
  47. t.Errorf("%s: got err %v, want nil", tt.desc, err)
  48. continue
  49. }
  50. if !proto.Equal(req, tt.want) {
  51. t.Errorf("%s request:\ngot %v\nwant %v", tt.desc, req, tt.want)
  52. }
  53. }
  54. }
  55. func TestProtoToRecord(t *testing.T) {
  56. // We deliberately leave ModuleId and other optional fields unset.
  57. p := &pb.RequestLog{
  58. AppId: proto.String("s~fake"),
  59. VersionId: proto.String("1"),
  60. RequestId: []byte("deadbeef"),
  61. Ip: proto.String("127.0.0.1"),
  62. StartTime: proto.Int64(431044244000000),
  63. EndTime: proto.Int64(431044724000000),
  64. Latency: proto.Int64(480000000),
  65. Mcycles: proto.Int64(7),
  66. Method: proto.String("GET"),
  67. Resource: proto.String("/app"),
  68. HttpVersion: proto.String("1.1"),
  69. Status: proto.Int32(418),
  70. ResponseSize: proto.Int64(1337),
  71. UrlMapEntry: proto.String("_go_app"),
  72. Combined: proto.String("apache log"),
  73. }
  74. // Sanity check that all required fields are set.
  75. if _, err := proto.Marshal(p); err != nil {
  76. t.Fatalf("proto.Marshal: %v", err)
  77. }
  78. want := &Record{
  79. AppID: "s~fake",
  80. ModuleID: "default",
  81. VersionID: "1",
  82. RequestID: []byte("deadbeef"),
  83. IP: "127.0.0.1",
  84. StartTime: time.Date(1983, 8, 29, 22, 30, 44, 0, time.UTC),
  85. EndTime: time.Date(1983, 8, 29, 22, 38, 44, 0, time.UTC),
  86. Latency: 8 * time.Minute,
  87. MCycles: 7,
  88. Method: "GET",
  89. Resource: "/app",
  90. HTTPVersion: "1.1",
  91. Status: 418,
  92. ResponseSize: 1337,
  93. URLMapEntry: "_go_app",
  94. Combined: "apache log",
  95. Finished: true,
  96. AppLogs: []AppLog{},
  97. }
  98. got := protoToRecord(p)
  99. // Coerce locations to UTC since otherwise they will be in local.
  100. got.StartTime, got.EndTime = got.StartTime.UTC(), got.EndTime.UTC()
  101. if !reflect.DeepEqual(got, want) {
  102. t.Errorf("protoToRecord:\ngot: %v\nwant: %v", got, want)
  103. }
  104. }