vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/txlogz_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tabletserver 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "strings" 23 "testing" 24 "time" 25 26 "vitess.io/vitess/go/vt/vttablet/tabletserver/tx" 27 28 "vitess.io/vitess/go/vt/callerid" 29 30 "vitess.io/vitess/go/streamlog" 31 "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" 32 ) 33 34 func testNotRedacted(t *testing.T, r *httptest.ResponseRecorder) { 35 if strings.Contains(r.Body.String(), "redacted") { 36 t.Errorf("/debug/txlogz unexpectedly redacted") 37 } 38 } 39 40 func testRedacted(t *testing.T, r *httptest.ResponseRecorder) { 41 if !strings.Contains(r.Body.String(), "redacted") { 42 t.Errorf("/debug/txlogz unexpectedly not redacted") 43 } 44 } 45 46 func testHandler(req *http.Request, t *testing.T) { 47 // Test with redactions off to start 48 streamlog.SetRedactDebugUIQueries(false) 49 50 response := httptest.NewRecorder() 51 tabletenv.TxLogger.Send("test msg") 52 txlogzHandler(response, req) 53 if !strings.Contains(response.Body.String(), "error") { 54 t.Fatalf("should show an error page since transaction log format is invalid.") 55 } 56 txConn := &StatefulConnection{ 57 ConnID: 123456, 58 txProps: &tx.Properties{ 59 EffectiveCaller: callerid.NewEffectiveCallerID("effective-caller", "component", "subcomponent"), 60 ImmediateCaller: callerid.NewImmediateCallerID("immediate-caller"), 61 StartTime: time.Now(), 62 Conclusion: "unknown", 63 Queries: []string{"select * from test"}, 64 }, 65 } 66 txConn.txProps.EndTime = txConn.txProps.StartTime 67 response = httptest.NewRecorder() 68 tabletenv.TxLogger.Send(txConn) 69 txlogzHandler(response, req) 70 testNotRedacted(t, response) 71 txConn.txProps.EndTime = txConn.txProps.StartTime.Add(time.Duration(2) * time.Second) 72 response = httptest.NewRecorder() 73 tabletenv.TxLogger.Send(txConn) 74 txlogzHandler(response, req) 75 testNotRedacted(t, response) 76 txConn.txProps.EndTime = txConn.txProps.StartTime.Add(time.Duration(500) * time.Millisecond) 77 response = httptest.NewRecorder() 78 tabletenv.TxLogger.Send(txConn) 79 txlogzHandler(response, req) 80 testNotRedacted(t, response) 81 82 // Test with redactions on 83 streamlog.SetRedactDebugUIQueries(true) 84 txlogzHandler(response, req) 85 testRedacted(t, response) 86 87 // Reset to default redaction state 88 streamlog.SetRedactDebugUIQueries(false) 89 } 90 91 func TestTxlogzHandler(t *testing.T) { 92 req, _ := http.NewRequest("GET", "/txlogz?timeout=0&limit=10", nil) 93 testHandler(req, t) 94 } 95 96 func TestTxlogzHandlerWithNegativeTimeout(t *testing.T) { 97 req, _ := http.NewRequest("GET", "/txlogz?timeout=-1&limit=10", nil) 98 testHandler(req, t) 99 } 100 101 func TestTxlogzHandlerWithLargeLimit(t *testing.T) { 102 req, _ := http.NewRequest("GET", "/txlogz?timeout=0&limit=10000000", nil) 103 testHandler(req, t) 104 }