vitess.io/vitess@v0.16.2/go/vt/vtadmin/debug/debug_test.go (about) 1 /* 2 Copyright 2021 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 debug 18 19 import ( 20 "math/rand" 21 "testing" 22 "time" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 ) 27 28 func TestSanitizeString(t *testing.T) { 29 t.Parallel() 30 31 t.Run("empty string", func(t *testing.T) { 32 t.Parallel() 33 34 out := SanitizeString("") 35 assert.Equal(t, "", out) 36 }) 37 38 t.Run("non-empty strings", func(t *testing.T) { 39 t.Parallel() 40 41 letters := "abcdefghijklmnopqrstuvwxyz0123456789" 42 43 for i := 0; i < 10; i++ { 44 t.Run("", func(t *testing.T) { 45 t.Parallel() 46 47 length := rand.Intn(20) + 1 // [1, 21) 48 word := "" 49 for j := 0; j < length; j++ { 50 k := rand.Intn(len(letters)) 51 word += letters[k : k+1] 52 } 53 54 out := SanitizeString(word) 55 assert.Equal(t, sanitized, out) 56 }) 57 } 58 }) 59 } 60 61 func TestTimeToString(t *testing.T) { 62 t.Parallel() 63 64 for i := 0; i < 10; i++ { 65 t.Run("", func(t *testing.T) { 66 t.Parallel() 67 68 start := time.Now() 69 secondsoff := rand.Intn(60) 70 minutesoff := rand.Intn(60) 71 72 in := start.Add(time.Second*time.Duration(secondsoff) + time.Minute*time.Duration(minutesoff)) 73 out, err := time.Parse(time.RFC3339, TimeToString(in)) 74 require.NoError(t, err, "failed to round-trip a time through debug.TimeToString") 75 in = in.Truncate(time.Second) // RFC3339 does not include millis 76 assert.True(t, in.Equal(out), "round-trip changed the time value; in: %v, out: %v", in, out) 77 }) 78 } 79 }