Skip to content

Commit

Permalink
Add force action test case
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mizutani committed Feb 2, 2024
1 parent 67390ae commit 7c6a3d1
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
37 changes: 37 additions & 0 deletions pkg/chain/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/m-mizutani/alertchain/pkg/infra/logging"
"github.com/m-mizutani/alertchain/pkg/infra/memory"
"github.com/m-mizutani/alertchain/pkg/infra/policy"
"github.com/m-mizutani/goerr"
"github.com/m-mizutani/gt"
)

Expand Down Expand Up @@ -332,3 +333,39 @@ func TestGlobalAttrRaceCondition(t *testing.T) {
gt.V(t, v.Value).Equal(float64(threadNum))
})
}

func TestForceAction(t *testing.T) {
var alertData any

alertPolicy := gt.R1(policy.New(
policy.WithPackage("alert"),
policy.WithFile("testdata/force_action/alert.rego"),
policy.WithReadFile(read),
)).NoError(t)

actionPolicy := gt.R1(policy.New(
policy.WithPackage("action"),
policy.WithFile("testdata/force_action/action.rego"),
policy.WithReadFile(read),
)).NoError(t)

calledStep := map[float64]bool{}
mock := func(ctx *model.Context, alert model.Alert, args model.ActionArgs) (any, error) {
step := gt.Cast[float64](t, args["step"])
calledStep[step] = true
return nil, goerr.New("force action should not be called")
}

c := gt.R1(chain.New(
core.WithPolicyAlert(alertPolicy),
core.WithPolicyAction(actionPolicy),
core.WithExtraAction("mock", mock),
)).NoError(t)

ctx := model.NewContext()

_ = gt.R1(c.HandleAlert(ctx, "my_alert", alertData)).Error(t)
gt.True(t, calledStep[1])
gt.True(t, calledStep[2])
gt.False(t, calledStep[3])
}
31 changes: 31 additions & 0 deletions pkg/chain/testdata/force_action/action.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package action

run[job] {
input.seq == 0
job := {
"id": "force_continue",
"uses": "mock",
"args": {"step": 1},
"force": true,
}
}

run[job] {
input.seq == 1
job := {
"id": "stop_by_error",
"uses": "mock",
"args": {"step": 2},
"force": false,
}
}

run[job] {
input.seq == 1
job := {
"id": "not_run",
"uses": "mock",
"args": {"step": 3},
"force": false,
}
}
7 changes: 7 additions & 0 deletions pkg/chain/testdata/force_action/alert.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package alert.my_alert

alert[msg] {
msg := {
"title": "global alert test",
}
}
9 changes: 8 additions & 1 deletion pkg/chain/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,14 @@ func (x *proc) evaluate(ctx *model.Context) error {
x.run = append(x.run, p)
result, err := x.executeAction(ctx, p, x.alert)
if err != nil {
return err
if !p.Force {
return err
}

utils.Logger().Warn("got error, but force run action",
slog.Any("action", p),
utils.ErrLog(err),
)
}

actionResult := model.ActionResult{
Expand Down
9 changes: 5 additions & 4 deletions pkg/domain/model/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ type ActionRunResponse struct {
}

type Action struct {
ID types.ActionID `json:"id"`
Name string `json:"name"`
Uses types.ActionName `json:"uses"`
Args ActionArgs `json:"args"`
ID types.ActionID `json:"id"`
Name string `json:"name"`
Uses types.ActionName `json:"uses"`
Args ActionArgs `json:"args"`
Force bool `json:"force"`
}

type ActionResult struct {
Expand Down

0 comments on commit 7c6a3d1

Please sign in to comment.