diff --git a/docs/fields-configuration.md b/docs/fields-configuration.md index f65e305..68b812f 100644 --- a/docs/fields-configuration.md +++ b/docs/fields-configuration.md @@ -22,8 +22,11 @@ For each config entry the following fields are available: - `"after_n"`: resets the counter after a specific number of iterations. - `probability` *required when strategy is "probabilistic"*: an integer between 1 and 100 representing the percentage chance of reset for each generated value. - `reset_after_n` *required when strategy is "after_n"*: an integer specifying the number of values to generate before resetting the counter. - -Note: The `counter_reset` configuration is only applicable when `counter` is set to `true`. +- `formatting_pattern` *optional (applicable to `string` type fields)*: a string that defines a pattern for generating formatted string values. The pattern can include static text and placeholders that will be replaced with random values. Multiple options can be provided, separated by `|`, from which one will be randomly selected for each generated value. Available placeholders are: + - `{string}`: replaced with a random noun + - `{ipv4}`: replaced with a random IPv4 address + - `{ipv6}`: replaced with a random IPv6 address + - `{port}`: replaced with a random port number between 1024 and 65535 - `period` *optional (`date` type only)*: values will be evenly generated between `time.Now()` and `time.Now().Add(period)`, where period is expressed as `time.Duration`. It accepts also a negative duration: in this case values will be evenly generated between `time.Now().Add(period)` and `time.Now()`. If both `period` and at least one of `range.from` or `range.to` settings are defined an error will be returned and the generator will stop. - `object_keys` *optional (`object` type only)*: list of field names to generate in a object field type; if not specified a random number of field names will be generated in the object filed type - `value` *optional*: hardcoded value to set for the field (any `cardinality` will be ignored) diff --git a/pkg/genlib/config/config.go b/pkg/genlib/config/config.go index c8eaf9b..e82256a 100644 --- a/pkg/genlib/config/config.go +++ b/pkg/genlib/config/config.go @@ -39,16 +39,17 @@ type Config struct { } type ConfigField struct { - Name string `config:"name"` - Fuzziness float64 `config:"fuzziness"` - Range Range `config:"range"` - Cardinality int `config:"cardinality"` - Period time.Duration `config:"period"` - Enum []string `config:"enum"` - ObjectKeys []string `config:"object_keys"` - Value any `config:"value"` - Counter bool `config:"counter"` - CounterReset *CounterReset `config:"counter_reset"` + Name string `config:"name"` + Fuzziness float64 `config:"fuzziness"` + Range Range `config:"range"` + Cardinality int `config:"cardinality"` + Period time.Duration `config:"period"` + Enum []string `config:"enum"` + ObjectKeys []string `config:"object_keys"` + Value any `config:"value"` + Counter bool `config:"counter"` + CounterReset *CounterReset `config:"counter_reset"` + FormattingPattern string `config:"formatting_pattern"` } const ( diff --git a/pkg/genlib/generator_interface.go b/pkg/genlib/generator_interface.go index 45e718f..842c266 100644 --- a/pkg/genlib/generator_interface.go +++ b/pkg/genlib/generator_interface.go @@ -10,6 +10,7 @@ import ( "errors" "fmt" "math" + "math/rand" "regexp" "strconv" "strings" @@ -98,6 +99,35 @@ func newGenState() *genState { } } +// replacePattern replaces placeholders in a pattern with random data. +func replacePattern(pattern string) string { + options := strings.Split(pattern, "|") + chosenOption := options[rand.Intn(len(options))] + + replacements := map[string]func() string{ + "{string}": func() string { + return randomdata.Noun() + }, + "{ipv4}": func() string { + return randomdata.IpV4Address() + }, + "{ipv6}": func() string { + return randomdata.IpV6Address() + }, + "{port}": func() string { + return fmt.Sprintf("%d", randomdata.Number(1024, 65535)) + }, + } + + for placeholder, replacementFunc := range replacements { + if strings.Contains(chosenOption, placeholder) { + chosenOption = strings.Replace(chosenOption, placeholder, replacementFunc(), -1) + } + } + + return chosenOption +} + func bindField(cfg Config, field Field, fieldMap map[string]any, withReturn bool) error { // Check for hardcoded field value if len(field.Value) > 0 { @@ -842,6 +872,17 @@ func bindConstantKeywordWithReturn(field Field, fieldMap map[string]any) error { } func bindKeywordWithReturn(fieldCfg ConfigField, field Field, fieldMap map[string]any) error { + if fieldCfg.FormattingPattern != "" { + var emitF emitF + emitF = func(state *genState) any { + res := replacePattern(fieldCfg.FormattingPattern) + return res + } + + fieldMap[field.Name] = emitF + return nil + } + if len(fieldCfg.Enum) > 0 { var emitF emitF emitF = func(state *genState) any { diff --git a/pkg/genlib/generator_with_text_template_test.go b/pkg/genlib/generator_with_text_template_test.go index 5e7999b..697ad8f 100644 --- a/pkg/genlib/generator_with_text_template_test.go +++ b/pkg/genlib/generator_with_text_template_test.go @@ -5,6 +5,7 @@ import ( "fmt" "math/rand" "net" + "regexp" "strconv" "strings" "testing" @@ -818,6 +819,104 @@ func Test_FieldLongCounterResetAfterN5WithTextTemplate(t *testing.T) { } } +func Test_FieldKeywordFormattingPatternPathWithTextTemplate(t *testing.T) { + fld := Field{ + Name: "path", + Type: FieldTypeKeyword, + } + + template := []byte(`{{$path := generate "path"}}{"path":"{{$path}}"}`) + configYaml := []byte(`fields: +- name: path + cardinality: 25 + formatting_pattern: "/home/{string}/{string}/{string}"`) + t.Logf("with template: %s", string(template)) + + cfg, err := config.LoadConfigFromYaml(configYaml) + if err != nil { + t.Fatal(err) + } + + g := makeGeneratorWithTextTemplate(t, cfg, []Field{fld}, template, 10) + + var buf bytes.Buffer + + pathRegex := regexp.MustCompile(`^/home/[^/]+/[^/]+/[^/]+$`) + + nSpins := int64(10) + + for i := int64(0); i < nSpins; i++ { + if err := g.Emit(&buf); err != nil { + t.Fatal(err) + } + + m := unmarshalJSONT[string](t, buf.Bytes()) + buf.Reset() + + if len(m) != 1 { + t.Errorf("Expected map size 1, got %d", len(m)) + } + + v, ok := m[fld.Name] + if !ok { + t.Errorf("Missing key %v", fld.Name) + } + + if !pathRegex.MatchString(v) { + t.Errorf("Generated path %v does not match expected format", v) + } + } +} + +func Test_FieldKeywordFormattingPatternHostIPWithTextTemplate(t *testing.T) { + fld := Field{ + Name: "hostIP", + Type: FieldTypeKeyword, + } + + template := []byte(`{{$hostIP := generate "hostIP"}}{"hostIP":"{{$hostIP}}"}`) + configYaml := []byte(`fields: +- name: hostIP + cardinality: 25 + formatting_pattern: "{ipv4}:{port}|{ipv6}"`) + t.Logf("with template: %s", string(template)) + + cfg, err := config.LoadConfigFromYaml(configYaml) + if err != nil { + t.Fatal(err) + } + + g := makeGeneratorWithTextTemplate(t, cfg, []Field{fld}, template, 10) + + var buf bytes.Buffer + + ipRegex := regexp.MustCompile(`^((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5})|(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}))$`) + + nSpins := int64(10) + + for i := int64(0); i < nSpins; i++ { + if err := g.Emit(&buf); err != nil { + t.Fatal(err) + } + + m := unmarshalJSONT[string](t, buf.Bytes()) + buf.Reset() + + if len(m) != 1 { + t.Errorf("Expected map size 1, got %d", len(m)) + } + + v, ok := m[fld.Name] + if !ok { + t.Errorf("Missing key %v", fld.Name) + } + + if !ipRegex.MatchString(v) { + t.Errorf("Generated pattern %v does not match expected format", v) + } + } +} + func Test_FieldFloatsWithTextTemplate(t *testing.T) { _testNumericWithTextTemplate[float64](t, FieldTypeDouble) _testNumericWithTextTemplate[float32](t, FieldTypeFloat)