github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/enginepb/hide_sensitive_test.go (about) 1 // Copyright 2022 PingCAP, Inc. 2 // 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package enginepb 15 16 import ( 17 "testing" 18 19 "github.com/pingcap/log" 20 "github.com/pingcap/tiflow/pkg/logutil" 21 "github.com/stretchr/testify/require" 22 "go.uber.org/zap" 23 "go.uber.org/zap/zapcore" 24 "go.uber.org/zap/zaptest" 25 ) 26 27 var ( 28 expectOutput = `:\\\"password: \\\\\\\"******\\\\\\\"\\\\nssl-ca-bytes: \\\"******\\\"\\\\nssl-key-bytes: \\\"******\\\"\\\\nssl-cert-bytes: \\\"******\\\"` 29 config = `password: "random-password" 30 ssl-ca-bytes: -----BEGIN CERTIFICATE----- 31 random1 32 random1-random2 33 random1-random2-random3 34 -----END CERTIFICATE----- 35 ssl-key-bytes: '-----BEGIN PRIVATE KEY----- 36 random1 37 random1-random2-random3 38 random1-random2 39 -----END PRIVATE KEY-----' 40 ssl-cert-bytes: "-----BEGIN CERTIFICATE REQUEST----- 41 random1-random2-random3 42 random1-random2 43 random1 44 -----END CERTIFICATE REQUEST-----"` 45 ) 46 47 func TestJobConfig(t *testing.T) { 48 job := &Job{ 49 Id: "test_job", 50 Config: []byte(config), 51 } 52 c := &CreateJobRequest{ 53 TenantId: "test_tenant", 54 ProjectId: "test_project", 55 Job: job, 56 } 57 58 var buffer zaptest.Buffer 59 err := logutil.InitLogger(&logutil.Config{Level: "info"}, logutil.WithOutputWriteSyncer(&buffer)) 60 require.NoError(t, err) 61 62 log.Info("test CreateJobRequest", zap.Any("c", c)) 63 require.Contains(t, buffer.String(), expectOutput) 64 buffer.Reset() 65 66 log.Info("test Job", zap.Any("job", job)) 67 require.Contains(t, buffer.String(), expectOutput) 68 } 69 70 func TestConfig(t *testing.T) { 71 configs := []zapcore.ObjectMarshaler{ 72 &PreDispatchTaskRequest{TaskConfig: []byte(config)}, 73 &QueryMetaStoreResponse{Config: []byte(config)}, 74 &QueryStorageConfigResponse{Config: []byte(config)}, 75 } 76 77 var buffer zaptest.Buffer 78 err := logutil.InitLogger(&logutil.Config{Level: "info"}, logutil.WithOutputWriteSyncer(&buffer)) 79 require.NoError(t, err) 80 81 for _, c := range configs { 82 log.Info("test config", zap.Any("c", c)) 83 require.Contains(t, buffer.String(), expectOutput) 84 buffer.Reset() 85 } 86 }