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