...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package action
17
18 import (
19 "context"
20 "testing"
21
22 "github.com/pkg/errors"
23
24 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
25 )
26
27 type mockApplyError struct {
28 }
29
30 func (it *mockApplyError) Error() string {
31 return "mock apply error"
32 }
33
34 type mockRecoverError struct {
35 }
36
37 func (it mockRecoverError) Error() string {
38 return "mock recover error"
39 }
40
41 type chaosImplMustFailed struct {
42 }
43
44 func (it *chaosImplMustFailed) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
45 return v1alpha1.NotInjected, &mockApplyError{}
46
47 }
48
49 func (it *chaosImplMustFailed) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
50 return v1alpha1.Injected, mockRecoverError{}
51 }
52
53 func TestMultiplexer_passthroughsError(t *testing.T) {
54 type adHoc struct {
55 Backend *chaosImplMustFailed `action:"must-failed"`
56 }
57 multiplexer := NewMultiplexer(&adHoc{
58 Backend: &chaosImplMustFailed{},
59 })
60
61
62 chaos := v1alpha1.PodChaos{
63 Spec: v1alpha1.PodChaosSpec{
64 Action: "must-failed",
65 },
66 }
67 _, err := multiplexer.Apply(context.Background(), 0, []*v1alpha1.Record{}, &chaos)
68 applyError := &mockApplyError{}
69 if !errors.As(err, &applyError) {
70 t.Fatal("returned error is not mockApplyError")
71 }
72
73 _, err = multiplexer.Recover(context.Background(), 0, []*v1alpha1.Record{}, &chaos)
74 recoverError := mockRecoverError{}
75 if !errors.As(err, &recoverError) {
76 t.Fatal("returned error is not recoverError")
77 }
78 }
79
80 func TestMultiplexer_unhandledAction(t *testing.T) {
81 type adHoc struct {
82 Backend *chaosImplMustFailed `action:"must-failed"`
83 }
84 multiplexer := NewMultiplexer(&adHoc{
85
86 })
87 chaos := v1alpha1.PodChaos{
88 Spec: v1alpha1.PodChaosSpec{
89 Action: "not-exist",
90 },
91 }
92 _, err := multiplexer.Apply(context.Background(), 0, []*v1alpha1.Record{}, &chaos)
93 unknownAction := ErrorUnknownAction{}
94 if !errors.As(err, &unknownAction) {
95 t.Fatal("should not return error")
96 }
97
98 }
99