Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

123 rader
3.1 KiB

  1. /*
  2. Copyright 2016 Google LLC
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // This file contains only basic checks. The fake is effectively tested by the
  14. // logging client unit tests.
  15. package testing
  16. import (
  17. "testing"
  18. "time"
  19. "github.com/golang/protobuf/proto"
  20. tspb "github.com/golang/protobuf/ptypes/timestamp"
  21. logpb "google.golang.org/genproto/googleapis/logging/v2"
  22. grpc "google.golang.org/grpc"
  23. )
  24. func TestNewServer(t *testing.T) {
  25. // Confirm that we can create and use a working gRPC server.
  26. addr, err := NewServer()
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. conn, err := grpc.Dial(addr, grpc.WithInsecure())
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. // Avoid "connection is closing; please retry" message from gRPC.
  35. time.Sleep(300 * time.Millisecond)
  36. conn.Close()
  37. }
  38. func TestParseFilter(t *testing.T) {
  39. for _, test := range []struct {
  40. filter string
  41. want string
  42. wantErr bool
  43. }{
  44. {"", "", false},
  45. {"logName = syslog", "syslog", false},
  46. {"logname = syslog", "", true},
  47. {"logName = 'syslog'", "", true},
  48. {"logName == syslog", "", true},
  49. } {
  50. got, err := parseFilter(test.filter)
  51. if err != nil {
  52. if !test.wantErr {
  53. t.Errorf("%q: got %v, want no error", test.filter, err)
  54. }
  55. continue
  56. }
  57. if test.wantErr {
  58. t.Errorf("%q: got no error, want one", test.filter)
  59. continue
  60. }
  61. if got != test.want {
  62. t.Errorf("%q: got %q, want %q", test.filter, got, test.want)
  63. }
  64. }
  65. }
  66. func TestSortEntries(t *testing.T) {
  67. entries := []*logpb.LogEntry{
  68. /* 0 */ {Timestamp: &tspb.Timestamp{Seconds: 30}},
  69. /* 1 */ {Timestamp: &tspb.Timestamp{Seconds: 10}},
  70. /* 2 */ {Timestamp: &tspb.Timestamp{Seconds: 20}, InsertId: "b"},
  71. /* 3 */ {Timestamp: &tspb.Timestamp{Seconds: 20}, InsertId: "a"},
  72. /* 4 */ {Timestamp: &tspb.Timestamp{Seconds: 20}, InsertId: "c"},
  73. }
  74. for _, test := range []struct {
  75. orderBy string
  76. want []int // slice of index into entries; nil == error
  77. }{
  78. {"", []int{1, 3, 2, 4, 0}},
  79. {"timestamp asc", []int{1, 3, 2, 4, 0}},
  80. {"timestamp desc", []int{0, 4, 2, 3, 1}},
  81. {"something else", nil},
  82. } {
  83. got := make([]*logpb.LogEntry, len(entries))
  84. copy(got, entries)
  85. err := sortEntries(got, test.orderBy)
  86. if err != nil {
  87. if test.want != nil {
  88. t.Errorf("%q: got %v, want nil error", test.orderBy, err)
  89. }
  90. continue
  91. }
  92. want := make([]*logpb.LogEntry, len(entries))
  93. for i, j := range test.want {
  94. want[i] = entries[j]
  95. }
  96. if !logEntriesEqual(got, want) {
  97. t.Errorf("%q: got %v, want %v", test.orderBy, got, want)
  98. }
  99. }
  100. }
  101. func logEntriesEqual(a, b []*logpb.LogEntry) bool {
  102. if len(a) != len(b) {
  103. return false
  104. }
  105. for i, aa := range a {
  106. if !proto.Equal(aa, b[i]) {
  107. return false
  108. }
  109. }
  110. return true
  111. }