From fa03953bc8528e90f44262de5613043118f1e615 Mon Sep 17 00:00:00 2001 From: Benjamin Isinger Date: Sun, 27 Aug 2023 11:17:13 +0200 Subject: [PATCH] Fix validation for empty check runs Fixes bug introduced by homeport/duct-tape-resource#31 that did not allow no/op In configuration Validates check config before running execute --- internal/dtr/check.go | 13 +++++++++++++ internal/dtr/common.go | 17 ++++++++++------- internal/dtr/in_test.go | 20 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/internal/dtr/check.go b/internal/dtr/check.go index ec6501e..c5d1622 100644 --- a/internal/dtr/check.go +++ b/internal/dtr/check.go @@ -21,6 +21,7 @@ package dtr import ( + "fmt" "io" ) @@ -30,6 +31,11 @@ func Check(in io.Reader) (CheckResult, error) { return nil, err } + err = validateCheckConfig(config.Source.Check) + if err != nil { + return nil, err + } + output, err := execute(config.Source.Check) if err != nil { return nil, err @@ -52,3 +58,10 @@ func Check(in io.Reader) (CheckResult, error) { return result, nil } + +func validateCheckConfig(c Custom) error { + if c.Run == "" { + return fmt.Errorf("run command not specified. Bailing out") + } + return nil +} diff --git a/internal/dtr/common.go b/internal/dtr/common.go index 1f0670b..f352b40 100644 --- a/internal/dtr/common.go +++ b/internal/dtr/common.go @@ -104,10 +104,6 @@ func execute(entry Custom) ([]string, error) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - if entry.Run == "" { - return nil, fmt.Errorf("run command not specified. Bailing out") - } - if entry.Before != "" { before := command(ctx, entry.Env, entry.Before, os.Stderr) if err := before.Run(); err != nil { @@ -116,9 +112,16 @@ func execute(entry Custom) ([]string, error) { } var outStream bytes.Buffer - run := command(ctx, entry.Env, entry.Run, &outStream) - if err := run.Run(); err != nil { - return nil, fmt.Errorf("failure while running run command: %w", err) + // User does not always need to define Run + // e.g. user just implements Check but not In to trigger jobs + // In this scenario a user would still use get on the resource. Then In would be executed. + // For this scenario we would still like to allow no-op In runs + // Therefore we can skip command execution if nothing is defined + if entry.Run != "" { + run := command(ctx, entry.Env, entry.Run, &outStream) + if err := run.Run(); err != nil { + return nil, fmt.Errorf("failure while running run command: %w", err) + } } var result []string diff --git a/internal/dtr/in_test.go b/internal/dtr/in_test.go index 65a09b4..8e75fc8 100644 --- a/internal/dtr/in_test.go +++ b/internal/dtr/in_test.go @@ -59,4 +59,24 @@ var _ = Describe("In", func() { })) }) }) + + Context("empty/no-op configuration", func() { + It("should not fail", func() { + _, err := In(feed(Config{})) + + Expect(err).NotTo(HaveOccurred()) + }) + + It("should return given version", func() { + version := Version{"ref": "foobar"} + result, err := In(feed(Config{ + Version: version, + })) + + Expect(err).NotTo(HaveOccurred()) + Expect(result).To(Equal(InOutResult{ + Version: version, + })) + }) + }) })