Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

allow specifying the charset to use with the RandomString template function #785

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pkg/templates/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ const (
)

// stolen from https://github.com/replicatedhq/replicated/blob/8ce3ed40436e38b8089387d103623dbe09bbf1c0/pkg/commands/random.go#L22
func (ctx *StaticCtx) RandomString(length uint64) string {
func (ctx *StaticCtx) RandomString(length uint64, providedCharset ...string) string {
debug := log.With(level.Debug(ctx.Logger), "func", "random")
debug.Log("event", "start")
regExp, err := syntax.Parse(DefaultCharset, syntax.Perl)

charset := DefaultCharset
if len(providedCharset) >= 1 {
charset = providedCharset[0]
}
regExp, err := syntax.Parse(charset, syntax.Perl)
if err != nil {
debug.Log("event", "regexParse.fail", "err", err)
return ""
Expand Down
77 changes: 77 additions & 0 deletions pkg/templates/random_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package templates

import (
"fmt"
"regexp"
"testing"
"unicode/utf8"

"github.com/replicatedhq/ship/pkg/testing/logger"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGenerateRandomString(t *testing.T) {
Expand All @@ -14,3 +19,75 @@ func TestGenerateRandomString(t *testing.T) {
assert.Regexp(t, DefaultCharset, str)

}

func TestGenerateRandomStringTemplates(t *testing.T) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function TestGenerateRandomStringTemplates has 61 lines of code (exceeds 50 allowed). Consider refactoring.

tests := []struct {
name string
length int
outputRegex string
templateString string
}{
{
name: "DefaultCharset",
length: 100,
outputRegex: DefaultCharset,
templateString: `{{repl RandomString 100}}`,
},
{
name: "ExplicitDefaultCharset",
length: 100,
outputRegex: DefaultCharset,
templateString: fmt.Sprintf("{{repl RandomString 100 %q}}", DefaultCharset),
},
{
name: "lowercase",
length: 100,
outputRegex: "[a-z]",
templateString: `{{repl RandomString 100 "[a-z]"}}`,
},
{
name: "very restricted charset aB",
length: 100,
outputRegex: "[aB]",
templateString: `{{repl RandomString 100 "[aB]"}}`,
},
{
name: "not lowercase",
length: 1000,
outputRegex: "[^a-z]",
templateString: `{{repl RandomString 1000 "[^a-z]"}}`,
},
{
name: "base64",
length: 1000,
outputRegex: "[A-Za-z0-9+/]",
templateString: `{{repl RandomString 1000 "[A-Za-z0-9+/]"}}`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := require.New(t)

builderBuilder := &BuilderBuilder{
Logger: &logger.TestLogger{T: t},
Viper: viper.New(),
}

builder := builderBuilder.NewBuilder(
builderBuilder.NewStaticContext(),
)

outputString, err := builder.String(tt.templateString)
req.NoError(err)

req.Equal(utf8.RuneCountInString(outputString), tt.length, "%q should have length %d", outputString, tt.length)

tt.outputRegex = fmt.Sprintf("^%s+$", tt.outputRegex)

matcher := regexp.MustCompile(tt.outputRegex)

req.True(matcher.MatchString(outputString), "String %q must match %q", outputString, tt.outputRegex)
})
}
}