From 8fe4561fe2273622379eb95b2fb3211c11ec6115 Mon Sep 17 00:00:00 2001 From: Jay Pipes Date: Wed, 3 Jul 2024 08:06:53 -0400 Subject: [PATCH] remove Scenario.evalPlugins Adds a new `Spec.Plugin` field and stores a pointer to the plugin that successfully parsed the test spec. Signed-off-by: Jay Pipes --- api/spec.go | 2 ++ internal/testutil/plugin/bar/plugin.go | 7 +++++- internal/testutil/plugin/foo/plugin.go | 7 +++++- plugin/exec/parse_test.go | 1 + plugin/exec/plugin.go | 13 +++++++--- scenario/parse.go | 5 +--- scenario/parse_test.go | 8 ++++++ scenario/run.go | 35 +++++++++++++++----------- 8 files changed, 54 insertions(+), 24 deletions(-) diff --git a/api/spec.go b/api/spec.go index 9a2bdc0..8d52df3 100644 --- a/api/spec.go +++ b/api/spec.go @@ -28,6 +28,8 @@ var ( // output or behaviour. All gdt plugins have their own Spec structs that // inherit from this base struct. type Spec struct { + // Plugin is a pointer to the plugin that successfully parsed the test spec + Plugin Plugin `yaml:"-"` // Defaults contains the parsed defaults for the Spec. These are injected // by the scenario during parse. Defaults *Defaults `yaml:"-"` diff --git a/internal/testutil/plugin/bar/plugin.go b/internal/testutil/plugin/bar/plugin.go index bfcd43c..9f334a1 100644 --- a/internal/testutil/plugin/bar/plugin.go +++ b/internal/testutil/plugin/bar/plugin.go @@ -14,8 +14,13 @@ import ( "gopkg.in/yaml.v3" ) +var ( + // this is just for testing purposes... + PluginRef = &Plugin{} +) + func init() { - plugin.Register(&Plugin{}) + plugin.Register(PluginRef) } type Defaults struct { diff --git a/internal/testutil/plugin/foo/plugin.go b/internal/testutil/plugin/foo/plugin.go index b62c5d5..180fd9c 100644 --- a/internal/testutil/plugin/foo/plugin.go +++ b/internal/testutil/plugin/foo/plugin.go @@ -15,8 +15,13 @@ import ( "gopkg.in/yaml.v3" ) +var ( + // this is just for testing purposes... + PluginRef = &Plugin{} +) + func init() { - plugin.Register(&Plugin{}) + plugin.Register(PluginRef) } type InnerDefaults struct { diff --git a/plugin/exec/parse_test.go b/plugin/exec/parse_test.go index c79b419..fa739bb 100644 --- a/plugin/exec/parse_test.go +++ b/plugin/exec/parse_test.go @@ -53,6 +53,7 @@ func TestSimpleCommand(t *testing.T) { expTests := []api.Evaluable{ &gdtexec.Spec{ Spec: api.Spec{ + Plugin: gdtexec.PluginRef, Index: 0, Defaults: &api.Defaults{}, }, diff --git a/plugin/exec/plugin.go b/plugin/exec/plugin.go index 87fac20..3a7d03a 100644 --- a/plugin/exec/plugin.go +++ b/plugin/exec/plugin.go @@ -11,6 +11,15 @@ import ( gdtplugin "github.com/gdt-dev/gdt/plugin" ) +var ( + // this is just for testing purposes... + PluginRef = &plugin{} +) + +func init() { + gdtplugin.Register(PluginRef) +} + var ( DefaultTimeout = "10s" ) @@ -20,10 +29,6 @@ func OverrideDefaultTimeout(d string) { DefaultTimeout = d } -func init() { - gdtplugin.Register(Plugin()) -} - const ( pluginName = "exec" ) diff --git a/scenario/parse.go b/scenario/parse.go index 4f34698..8a1e486 100644 --- a/scenario/parse.go +++ b/scenario/parse.go @@ -82,8 +82,6 @@ func (s *Scenario) UnmarshalYAML(node *yaml.Node) error { s.Defaults = defaults } } - // We store a lookup to the parsing plugin for each parsed test spec - evalPlugins := map[int]api.Plugin{} for i := 0; i < len(node.Content); i += 2 { keyNode := node.Content[i] if keyNode.Kind != yaml.ScalarNode { @@ -116,10 +114,10 @@ func (s *Scenario) UnmarshalYAML(node *yaml.Node) error { } return err } + base.Plugin = plugin sp.SetBase(base) s.Tests = append(s.Tests, sp) parsed = true - evalPlugins[idx] = plugin break } } @@ -161,6 +159,5 @@ func (s *Scenario) UnmarshalYAML(node *yaml.Node) error { } } } - s.evalPlugins = evalPlugins return nil } diff --git a/scenario/parse_test.go b/scenario/parse_test.go index 2cf041e..493fc80 100644 --- a/scenario/parse_test.go +++ b/scenario/parse_test.go @@ -236,6 +236,7 @@ func TestKnownSpec(t *testing.T) { expTests := []api.Evaluable{ &foo.Spec{ Spec: api.Spec{ + Plugin: foo.PluginRef, Index: 0, Name: "bar", Defaults: expSpecDefaults, @@ -244,6 +245,7 @@ func TestKnownSpec(t *testing.T) { }, &foo.Spec{ Spec: api.Spec{ + Plugin: foo.PluginRef, Index: 1, Description: "Bazzy Bizzy", Defaults: expSpecDefaults, @@ -272,6 +274,7 @@ func TestMultipleSpec(t *testing.T) { expTests := []api.Evaluable{ &foo.Spec{ Spec: api.Spec{ + Plugin: foo.PluginRef, Index: 0, Defaults: &api.Defaults{}, }, @@ -279,6 +282,7 @@ func TestMultipleSpec(t *testing.T) { }, &bar.Spec{ Spec: api.Spec{ + Plugin: bar.PluginRef, Index: 1, Defaults: &api.Defaults{}, }, @@ -340,6 +344,7 @@ func TestEnvExpansion(t *testing.T) { expTests := []api.Evaluable{ &foo.Spec{ Spec: api.Spec{ + Plugin: foo.PluginRef, Index: 0, Name: "$NOT_EXPANDED", Defaults: expSpecDefaults, @@ -348,6 +353,7 @@ func TestEnvExpansion(t *testing.T) { }, &foo.Spec{ Spec: api.Spec{ + Plugin: foo.PluginRef, Index: 1, Description: "Bazzy Bizzy", Defaults: expSpecDefaults, @@ -406,6 +412,7 @@ func TestScenarioDefaults(t *testing.T) { expTests := []api.Evaluable{ &foo.Spec{ Spec: api.Spec{ + Plugin: foo.PluginRef, Index: 0, Defaults: expSpecDefaults, Timeout: &api.Timeout{ @@ -416,6 +423,7 @@ func TestScenarioDefaults(t *testing.T) { }, &foo.Spec{ Spec: api.Spec{ + Plugin: foo.PluginRef, Index: 1, Defaults: expSpecDefaults, }, diff --git a/scenario/run.go b/scenario/run.go index 5b38588..d843f75 100644 --- a/scenario/run.go +++ b/scenario/run.go @@ -41,12 +41,6 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error { defer fix.Stop(ctx) } } - var rterr error - var scDefaults *Defaults - scDefaultsAny, found := s.Defaults[DefaultsKey] - if found { - scDefaults = scDefaultsAny.(*Defaults) - } // If the test author has specified any pre-flight checks in the `skip-if` // collection, evaluate those first and if any failed, skip the scenario's // tests. @@ -63,6 +57,10 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error { return nil } } + + var err error + scDefaults := s.getScenarioDefaults() + t.Run(s.Title(), func(t *testing.T) { ctx = gdtcontext.PushTrace(ctx, s.Title()) defer func() { @@ -86,7 +84,7 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error { specCtx = gdtcontext.PopTrace(specCtx) } - plugin := s.evalPlugins[idx] + plugin := sb.Plugin rt := getRetry(specCtx, scDefaults, plugin, spec) @@ -109,15 +107,14 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error { select { case <-specCtx.Done(): - t.Fatalf("assertion failed: timeout exceeded (%s)", to.After) popTracer() - specCancel() + t.Fatalf("assertion failed: timeout exceeded (%s)", to.After) break case runres := <-ch: res = runres.r - rterr = runres.err + err = runres.err } - if rterr != nil { + if err != nil { popTracer() specCancel() break @@ -134,14 +131,14 @@ func (s *Scenario) Run(ctx context.Context, t *testing.T) error { if res.HasData() { ctx = gdtcontext.StorePriorRun(ctx, res.Data()) } + popTracer() + specCancel() for _, fail := range res.Failures() { t.Fatal(fail) } - popTracer() - specCancel() } }) - return rterr + return err } type runSpecRes struct { @@ -375,3 +372,13 @@ func getRetry( } return nil } + +// getScenarioDefaults returns the Defaults parsed from the scenario's YAML +// file's `defaults` field, or nil if none were specified. +func (s *Scenario) getScenarioDefaults() *Defaults { + scDefaultsAny, found := s.Defaults[DefaultsKey] + if found { + return scDefaultsAny.(*Defaults) + } + return nil +}