Skip to content

Commit ecc3374

Browse files
committed
fix scenario.WithDefaults()
Adds a new `api.DefaultsHandler` interface that plugins must now implement. This `api.DefaultsHandler` inherits from `yaml.Unmarshaler` and exposes another `Merge()` method that accepts a map of variable override defaults received from `scenario.WithDefaults()` and merges those values with whatever the plugin returns from its `Defaults()` method. Issue #68 Signed-off-by: Jay Pipes <[email protected]>
1 parent 5d4fea4 commit ecc3374

File tree

10 files changed

+41
-10
lines changed

10 files changed

+41
-10
lines changed

api/plugin.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,23 @@ type PluginInfo struct {
2323
Retry *Retry
2424
}
2525

26+
type DefaultsHandler interface {
27+
yaml.Unmarshaler
28+
// Merge merges the supplies map of key/value combinations with the set of
29+
// handled defaults for the plugin. The supplied key/value map will NOT be
30+
// unpacked from its top-most plugin named element. So, for example, the
31+
// kube plugin should expect to get a map that looks like
32+
// "kube:namespace:<namespace>" and not "namespace:<namespace>".
33+
Merge(map[string]any)
34+
}
35+
2636
// Plugin is the driver interface for different types of gdt tests.
2737
type Plugin interface {
2838
// Info returns a struct that describes what the plugin does
2939
Info() PluginInfo
3040
// Defaults returns a YAML Unmarshaler types that the plugin knows how
3141
// to parse its defaults configuration with.
32-
Defaults() yaml.Unmarshaler
42+
Defaults() DefaultsHandler
3343
// Specs returns a list of YAML Unmarshaler types that the plugin knows
3444
// how to parse.
3545
Specs() []Evaluable

context/context_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type fooDefaults struct {
2121
Foo string `yaml:"foo"`
2222
}
2323

24+
func (d *fooDefaults) Merge(map[string]any) {}
25+
2426
func (d *fooDefaults) UnmarshalYAML(node *yaml.Node) error {
2527
return nil
2628
}
@@ -62,7 +64,7 @@ func (p *fooPlugin) Info() api.PluginInfo {
6264
}
6365
}
6466

65-
func (p *fooPlugin) Defaults() yaml.Unmarshaler {
67+
func (p *fooPlugin) Defaults() api.DefaultsHandler {
6668
return &fooDefaults{}
6769
}
6870

internal/testutil/plugin/bar/plugin.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Defaults struct {
2828
Foo string `yaml:"bar"`
2929
}
3030

31+
func (d *Defaults) Merge(map[string]any) {}
32+
3133
func (d *Defaults) UnmarshalYAML(node *yaml.Node) error {
3234
return nil
3335
}
@@ -98,7 +100,7 @@ func (p *Plugin) Info() api.PluginInfo {
98100
}
99101
}
100102

101-
func (p *Plugin) Defaults() yaml.Unmarshaler {
103+
func (p *Plugin) Defaults() api.DefaultsHandler {
102104
return &Defaults{}
103105
}
104106

internal/testutil/plugin/failer/plugin.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Defaults struct {
2828
InnerDefaults
2929
}
3030

31+
func (d *Defaults) Merge(map[string]any) {}
32+
3133
func (d *Defaults) UnmarshalYAML(node *yaml.Node) error {
3234
if node.Kind != yaml.MappingNode {
3335
return parse.ExpectedMapAt(node)
@@ -129,7 +131,7 @@ func (p *Plugin) Info() api.PluginInfo {
129131
}
130132
}
131133

132-
func (p *Plugin) Defaults() yaml.Unmarshaler {
134+
func (p *Plugin) Defaults() api.DefaultsHandler {
133135
return &Defaults{}
134136
}
135137

internal/testutil/plugin/foo/plugin.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ type Defaults struct {
3333
InnerDefaults `yaml:",inline"`
3434
}
3535

36+
func (d *Defaults) Merge(map[string]any) {}
37+
3638
func (d *Defaults) UnmarshalYAML(node *yaml.Node) error {
3739
if node.Kind != yaml.MappingNode {
3840
return parse.ExpectedMapAt(node)
@@ -136,7 +138,7 @@ func (p *Plugin) Info() api.PluginInfo {
136138
}
137139
}
138140

139-
func (p *Plugin) Defaults() yaml.Unmarshaler {
141+
func (p *Plugin) Defaults() api.DefaultsHandler {
140142
return &Defaults{}
141143
}
142144

internal/testutil/plugin/priorrun/plugin.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const PriorRunDataKey = "priorrun"
2424

2525
type Defaults struct{}
2626

27+
func (d *Defaults) Merge(map[string]any) {}
28+
2729
func (d *Defaults) UnmarshalYAML(node *yaml.Node) error {
2830
return nil
2931
}
@@ -116,7 +118,7 @@ func (p *Plugin) Info() api.PluginInfo {
116118
}
117119
}
118120

119-
func (p *Plugin) Defaults() yaml.Unmarshaler {
121+
func (p *Plugin) Defaults() api.DefaultsHandler {
120122
return &Defaults{}
121123
}
122124

plugin/exec/defaults.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ type Defaults struct {
1616
execDefaults
1717
}
1818

19+
// Merge merges the supplies map of key/value combinations with the set of
20+
// handled defaults for the plugin. The supplied key/value map will NOT be
21+
// unpacked from its top-most plugin named element. So, for example, the
22+
// kube plugin should expect to get a map that looks like
23+
// "kube:namespace:<namespace>" and not "namespace:<namespace>".
24+
func (d *Defaults) Merge(map[string]any) {}
25+
1926
func (d *Defaults) UnmarshalYAML(node *yaml.Node) error {
2027
if node.Kind != yaml.MappingNode {
2128
return parse.ExpectedMapAt(node)

plugin/exec/plugin.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package exec
66

77
import (
8-
"gopkg.in/yaml.v3"
9-
108
"github.com/gdt-dev/core/api"
119
gdtplugin "github.com/gdt-dev/core/plugin"
1210
)
@@ -44,7 +42,7 @@ func (p *plugin) Info() api.PluginInfo {
4442
}
4543
}
4644

47-
func (p *plugin) Defaults() yaml.Unmarshaler {
45+
func (p *plugin) Defaults() api.DefaultsHandler {
4846
return &Defaults{}
4947
}
5048

plugin/registry_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type fooDefaults struct {
1818
Foo string `yaml:"foo"`
1919
}
2020

21+
func (d *fooDefaults) Merge(map[string]any) {}
22+
2123
func (d *fooDefaults) UnmarshalYAML(node *yaml.Node) error {
2224
return nil
2325
}
@@ -59,7 +61,7 @@ func (p *fooPlugin) Info() api.PluginInfo {
5961
}
6062
}
6163

62-
func (p *fooPlugin) Defaults() yaml.Unmarshaler {
64+
func (p *fooPlugin) Defaults() api.DefaultsHandler {
6365
return &fooDefaults{}
6466
}
6567

scenario/parse.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (s *Scenario) UnmarshalYAML(node *yaml.Node) error {
8181
if err := valNode.Decode(plugDefaults); err != nil {
8282
return err
8383
}
84+
// The user may have used scenario.WithDefaults() so we need to
85+
// merge anything we got from WithDefaults with anything we
86+
// parsed from the plugins.
87+
plugDefaults.Merge(s.Defaults)
8488
defaults[p.Info().Name] = plugDefaults
8589
}
8690
// The scenario may have its own defaults as well, so we stash

0 commit comments

Comments
 (0)