Skip to content

Commit

Permalink
Merge pull request ManageIQ#250 from kbrock/more_validations
Browse files Browse the repository at this point in the history
Validate Catcher Next
  • Loading branch information
agrare committed Jul 17, 2024
2 parents 1b1cae1 + a7e46bd commit 8d86b56
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/floe/workflow/catcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Catcher

attr_reader :error_equals, :next, :result_path, :name

def initialize(_workflow, name, payload)
def initialize(workflow, name, payload)
@name = name
@payload = payload

Expand All @@ -17,6 +17,14 @@ def initialize(_workflow, name, payload)
@result_path = ReferencePath.new(payload.fetch("ResultPath", "$"))

missing_field_error!("ErrorEquals") if !@error_equals.kind_of?(Array) || @error_equals.empty?
validate_state_next!(workflow)
end

private

def validate_state_next!(workflow)
missing_field_error!("Next") if @next.nil?
invalid_field_error!("Next", @next, "is not found in \"States\"") if @next && !workflow_state?(@next, workflow)
end
end
end
Expand Down
19 changes: 17 additions & 2 deletions spec/workflow/states/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,16 @@
end

context "with a Catch" do
let(:catchers) { [{"ErrorEquals" => ["States.ALL"], "Next" => "FailState"}] }

let(:workflow) do
make_workflow(
ctx, {
"State" => {
"Type" => "Task",
"Resource" => resource,
"Retry" => [{"ErrorEquals" => ["States.Timeout"]}],
"Catch" => [{"ErrorEquals" => ["States.ALL"], "Next" => "FailState"}],
"Catch" => catchers,
"Next" => "SuccessState"
},
"FailState" => {"Type" => "Succeed"},
Expand Down Expand Up @@ -389,13 +391,14 @@

describe "Catch" do
context "with specific errors" do
let(:catchers) { [{"ErrorEquals" => ["States.Timeout"], "Next" => "FirstState"}] }
let(:workflow) do
make_workflow(
ctx, {
"State" => {
"Type" => "Task",
"Resource" => resource,
"Catch" => [{"ErrorEquals" => ["States.Timeout"], "Next" => "FirstState"}],
"Catch" => catchers,
"Next" => "SuccessState"
},
"FirstState" => {"Type" => "Succeed"},
Expand All @@ -404,6 +407,18 @@
)
end

context "with invalid next" do
let(:catchers) { [{"ErrorEquals" => ["States.Timeout"], "Next" => "MissingState"}] }

it { expect { workflow }.to raise_error(Floe::InvalidWorkflowError, "States.State.Catch.0 field \"Next\" value \"MissingState\" is not found in \"States\"") }
end

context "with missing next" do
let(:catchers) { [{"ErrorEquals" => ["States.Timeout"]}] }

it { expect { workflow }.to raise_error(Floe::InvalidWorkflowError, "States.State.Catch.0 does not have required field \"Next\"") }
end

it "catches the exception" do
expect_run_async(input, :output => "States.Timeout", :success => false)

Expand Down
4 changes: 2 additions & 2 deletions spec/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@

describe "#wait_until" do
it "reads when the workflow will be ready to continue" do
workflow = make_workflow(ctx, {"FirstState" => {"Type" => "Wait", "Seconds" => 10, "End" => true, "Next" => "SuccessState"}, "SuccessState" => {"Type" => "Succeed"}})
workflow = make_workflow(ctx, {"FirstState" => {"Type" => "Wait", "Seconds" => 10, "Next" => "SuccessState"}, "SuccessState" => {"Type" => "Succeed"}})
workflow.run_nonblock

expect(workflow.wait_until).to be_within(1).of(Time.now.utc + 10)
Expand All @@ -298,7 +298,7 @@

describe "#waiting?" do
it "reads when the workflow will be ready to continue" do
workflow = make_workflow(ctx, {"FirstState" => {"Type" => "Wait", "Seconds" => 10, "End" => true, "Next" => "SuccessState"}, "SuccessState" => {"Type" => "Succeed"}})
workflow = make_workflow(ctx, {"FirstState" => {"Type" => "Wait", "Seconds" => 10, "Next" => "SuccessState"}, "SuccessState" => {"Type" => "Succeed"}})
workflow.run_nonblock

expect(workflow.waiting?).to be_truthy
Expand Down

0 comments on commit 8d86b56

Please sign in to comment.