Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new random value injector options #171

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
70 changes: 68 additions & 2 deletions internal/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"

Expand All @@ -38,11 +39,18 @@ import (
)

var (
charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
charset = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
charsetLowercaseLetters = []rune("abcdefghijklmnopqrstuvwxyz")
charsetUppercaseLetters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
charsetLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
charsetAlphanumeric = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
charsetAlphanumericLower = []rune("abcdefghijklmnopqrstuvwxyz0123456789")

dataSourceRegex = regexp.MustCompile(`\${data\.(.*?)}`)
randomStrRegex = regexp.MustCompile(`\${Rand\.(.*?)}`)

defaultRandomStrLength = 8

caseDirectory = "case"
)

Expand Down Expand Up @@ -153,17 +161,44 @@ func (p *preparer) injectValues(manifestData string, dataSourceMap map[string]st
// Inject random strings
randomKeys := randomStrRegex.FindAllStringSubmatch(manifestData, -1)
for _, randomKey := range randomKeys {
switch randomKey[1] {
randKind, randStrLength := parseRandomKey(randomKey[1])
switch randKind {
case "RFC1123Subdomain":
r := generateRFC1123SubdomainCompatibleString()
manifestData = strings.Replace(manifestData, randomKey[0], r, 1)
case "Alphanumeric":
r := generateAlphanumericString(randStrLength)
manifestData = strings.Replace(manifestData, randomKey[0], r, 1)
case "AlphanumericLowercase":
r := generateAlphanumericLowercaseString(randStrLength)
manifestData = strings.Replace(manifestData, randomKey[0], r, 1)
case "LettersLowercase":
r := generateLowercaseLetterString(randStrLength)
manifestData = strings.Replace(manifestData, randomKey[0], r, 1)
case "LettersUppercase":
r := generateUppercaseLetterString(randStrLength)
manifestData = strings.Replace(manifestData, randomKey[0], r, 1)
case "Letters":
r := generateLetterString(randStrLength)
manifestData = strings.Replace(manifestData, randomKey[0], r, 1)
default:
continue
}
}
return manifestData
}

func parseRandomKey(randomKey string) (string, int) {
randStrLength := defaultRandomStrLength
randKind, randLength, found := strings.Cut(randomKey, "#")
if found {
if rsr, err := strconv.Atoi(randLength); err == nil && rsr > 0 {
randStrLength = rsr
}
}
return randKind, randStrLength
}

func generateRFC1123SubdomainCompatibleString() string {
rand.Seed(time.Now().UnixNano())
s := make([]rune, 8)
Expand All @@ -172,3 +207,34 @@ func generateRFC1123SubdomainCompatibleString() string {
}
return fmt.Sprintf("op-%s", string(s))
}

func generateStringWithCharset(charset []rune, length int) string {
if length < 1 {
length = defaultRandomStrLength
}
s := make([]rune, length)
for i := range s {
s[i] = charset[rand.Intn(len(charset))] //nolint:gosec // no need for crypto/rand here
}
return string(s)
}

func generateAlphanumericString(length int) string {
return generateStringWithCharset(charsetAlphanumeric, length)
}

func generateAlphanumericLowercaseString(length int) string {
return generateStringWithCharset(charsetAlphanumericLower, length)
}

func generateLowercaseLetterString(length int) string {
return generateStringWithCharset(charsetLowercaseLetters, length)
}

func generateUppercaseLetterString(length int) string {
return generateStringWithCharset(charsetUppercaseLetters, length)
}

func generateLetterString(length int) string {
return generateStringWithCharset(charsetLetters, length)
}
Loading