github.com/kubeshop/testkube@v1.17.23/pkg/utils/utils_test.go (about) 1 package utils 2 3 import ( 4 "bytes" 5 "strings" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestRoundDuration(t *testing.T) { 13 t.Run("round duration should round to default 100ms", func(t *testing.T) { 14 15 //given 16 d := time.Duration(99111111111) 17 18 // when 19 rounded := RoundDuration(d) 20 21 // then 22 assert.Equal(t, "1m39.11s", rounded.String()) 23 }) 24 25 t.Run("round duration should round to given value", func(t *testing.T) { 26 27 //given 28 d := time.Duration(99111111111) 29 30 // when 31 rounded := RoundDuration(d, time.Minute) 32 33 // then 34 assert.Equal(t, "2m0s", rounded.String()) 35 }) 36 } 37 38 func TestSanitizeName(t *testing.T) { 39 t.Parallel() 40 41 t.Run("name should not be changed", func(t *testing.T) { 42 t.Parallel() 43 //given 44 name := "abc-123" 45 46 // when 47 sanized := SanitizeName(name) 48 49 // then 50 assert.Equal(t, "abc-123", sanized) 51 }) 52 53 t.Run("name should be shorted", func(t *testing.T) { 54 t.Parallel() 55 //given 56 name := "abc" + strings.Repeat("0123456789", 10) 57 58 // when 59 sanized := SanitizeName(name) 60 61 // then 62 assert.Equal(t, "abc"+strings.Repeat("0123456789", 6), sanized) 63 }) 64 65 t.Run("name should be sanitized", func(t *testing.T) { 66 t.Parallel() 67 //given 68 name := "@#$%!abc()~+123{}<>;" 69 70 // when 71 sanized := SanitizeName(name) 72 73 // then 74 assert.Equal(t, "abc-123", sanized) 75 }) 76 } 77 78 func TestNewTemplate(t *testing.T) { 79 t.Parallel() 80 81 t.Run("sprig functions should be available", func(t *testing.T) { 82 t.Parallel() 83 //given 84 template := `{{ default "foo" .Bar }}` 85 86 // when 87 tpl, err := NewTemplate("test").Parse(template) 88 assert.NoError(t, err) 89 var result bytes.Buffer 90 assert.NoError(t, tpl.Execute(&result, nil)) 91 92 // then 93 assert.Equal(t, "foo", result.String()) 94 }) 95 }