vitess.io/vitess@v0.16.2/go/vt/vttablet/filelogger/filelogger_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 filelogger 18 19 import ( 20 "context" 21 "os" 22 "path" 23 "testing" 24 "time" 25 26 "vitess.io/vitess/go/streamlog" 27 "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv" 28 ) 29 30 // TestFileLog sends a stream of five query records to the plugin, and verifies that they are logged. 31 func TestFileLog(t *testing.T) { 32 dir := t.TempDir() 33 34 logPath := path.Join(dir, "test.log") 35 logger, err := Init(logPath) 36 defer logger.Stop() 37 if err != nil { 38 t.Fatalf("error setting up file logger: %v", err) 39 } 40 41 ctx := context.Background() 42 43 log1 := &tabletenv.LogStats{ 44 Ctx: ctx, 45 OriginalSQL: "test 1", 46 } 47 log1.AddRewrittenSQL("test 1 PII", time.Time{}) 48 log1.MysqlResponseTime = 0 49 tabletenv.StatsLogger.Send(log1) 50 51 log2 := &tabletenv.LogStats{ 52 Ctx: ctx, 53 OriginalSQL: "test 2", 54 } 55 log2.AddRewrittenSQL("test 2 PII", time.Time{}) 56 log2.MysqlResponseTime = 0 57 tabletenv.StatsLogger.Send(log2) 58 59 // Allow time for propagation 60 for i := 0; i < 10; i++ { 61 time.Sleep(10 * time.Millisecond) 62 63 want := "\t\t\t''\t''\t0001-01-01 00:00:00.000000\t0001-01-01 00:00:00.000000\t0.000000\t\t\"test 1\"\tmap[]\t1\t\"test 1 PII\"\tmysql\t0.000000\t0.000000\t0\t0\t0\t\"\"\t\n\t\t\t''\t''\t0001-01-01 00:00:00.000000\t0001-01-01 00:00:00.000000\t0.000000\t\t\"test 2\"\tmap[]\t1\t\"test 2 PII\"\tmysql\t0.000000\t0.000000\t0\t0\t0\t\"\"\t\n" 64 contents, _ := os.ReadFile(logPath) 65 got := string(contents) 66 if want == got { 67 return 68 } 69 // Last iteration. 70 if i == 9 { 71 t.Errorf("streamlog file: want %q got %q", want, got) 72 } 73 } 74 } 75 76 // TestFileLog sends a stream of five query records to the plugin, and verifies that they are logged. 77 func TestFileLogRedacted(t *testing.T) { 78 streamlog.SetRedactDebugUIQueries(true) 79 defer func() { 80 streamlog.SetRedactDebugUIQueries(false) 81 }() 82 83 dir := t.TempDir() 84 85 logPath := path.Join(dir, "test.log") 86 logger, err := Init(logPath) 87 defer logger.Stop() 88 if err != nil { 89 t.Fatalf("error setting up file logger: %v", err) 90 } 91 92 ctx := context.Background() 93 94 log1 := &tabletenv.LogStats{ 95 Ctx: ctx, 96 OriginalSQL: "test 1", 97 } 98 log1.AddRewrittenSQL("test 1 PII", time.Time{}) 99 log1.MysqlResponseTime = 0 100 tabletenv.StatsLogger.Send(log1) 101 102 log2 := &tabletenv.LogStats{ 103 Ctx: ctx, 104 OriginalSQL: "test 2", 105 } 106 log2.AddRewrittenSQL("test 2 PII", time.Time{}) 107 log2.MysqlResponseTime = 0 108 tabletenv.StatsLogger.Send(log2) 109 110 // Allow time for propagation 111 time.Sleep(10 * time.Millisecond) 112 113 want := "\t\t\t''\t''\t0001-01-01 00:00:00.000000\t0001-01-01 00:00:00.000000\t0.000000\t\t\"test 1\"\t\"[REDACTED]\"\t1\t\"[REDACTED]\"\tmysql\t0.000000\t0.000000\t0\t0\t0\t\"\"\t\n\t\t\t''\t''\t0001-01-01 00:00:00.000000\t0001-01-01 00:00:00.000000\t0.000000\t\t\"test 2\"\t\"[REDACTED]\"\t1\t\"[REDACTED]\"\tmysql\t0.000000\t0.000000\t0\t0\t0\t\"\"\t\n" 114 contents, _ := os.ReadFile(logPath) 115 got := string(contents) 116 if want != string(got) { 117 t.Errorf("streamlog file: want %q got %q", want, got) 118 } 119 }