Skip to content

Commit

Permalink
Merge pull request kubernetes#2229 from marseel/add_rand_data
Browse files Browse the repository at this point in the history
Add RandData template function
  • Loading branch information
k8s-ci-robot authored Jan 24, 2023
2 parents 01d6e63 + aa13a17 commit 1e848d1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
12 changes: 12 additions & 0 deletions clusterloader2/pkg/config/template_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func GetFuncs(fsys fs.FS) template.FuncMap {
"Mod": mod,
"MultiplyFloat": multiplyFloat,
"MultiplyInt": multiplyInt,
"RandData": randData,
"RandInt": randInt,
"RandIntRange": randIntRange,
"Seq": seq,
Expand Down Expand Up @@ -101,6 +102,17 @@ func toFloat64(val interface{}) float64 {
panic(fmt.Sprintf("cannot cast %v to float64", val))
}

// randData returns pseudo-random string of i length.
func randData(i interface{}) string {
const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
typedI := int(toFloat64(i))
b := make([]byte, typedI)
for i := range b {
b[i] = alphabet[rand.Intn(len(alphabet))]
}
return string(b)
}

// randInt returns pseudo-random int in [0, i].
func randInt(i interface{}) int {
typedI := int(toFloat64(i))
Expand Down
41 changes: 41 additions & 0 deletions clusterloader2/pkg/config/template_functions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"testing"
)

func TestTemplateRandData(t *testing.T) {
tests := []struct {
length int
}{
{
length: 1,
},
{
length: 100,
},
}

for _, tt := range tests {
data := randData(tt.length)
if tt.length != len(data) {
t.Errorf("wanted: %d, got: %d", tt.length, len(data))
}
}
}

0 comments on commit 1e848d1

Please sign in to comment.