-
Notifications
You must be signed in to change notification settings - Fork 211
/
fail.go
45 lines (38 loc) · 1.2 KB
/
fail.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package chaos
import (
"context"
chaos "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/spacemeshos/go-spacemesh/systest/testcontext"
)
func selectPods(pods []string) chaos.PodSelectorSpec {
return chaos.PodSelectorSpec{
GenericSelectorSpec: chaos.GenericSelectorSpec{
ExpressionSelectors: chaos.LabelSelectorRequirements{
apimetav1.LabelSelectorRequirement{
Key: "id",
Operator: apimetav1.LabelSelectorOpIn,
Values: pods,
},
},
},
}
}
// Teardown is returned by every chaos action and executed
// by the caller once chaos needs to be stopped.
type Teardown func(context.Context) error
// Fail the list of pods and prevents them from respawning until teardown is called.
func Fail(cctx *testcontext.Context, name string, pods ...string) (Teardown, error) {
fail := chaos.PodChaos{}
fail.Name = name
fail.Namespace = cctx.Namespace
fail.Spec.Action = chaos.PodFailureAction
fail.Spec.Mode = chaos.AllMode
fail.Spec.Selector = selectPods(pods)
if err := cctx.Generic.Create(cctx, &fail); err != nil {
return nil, err
}
return func(ctx context.Context) error {
return cctx.Generic.Delete(ctx, &fail)
}, nil
}