...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package statuscheck
17
18 import (
19 "time"
20
21 "github.com/go-logr/logr"
22 "github.com/pkg/errors"
23
24 "github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
25 )
26
27
28 type fakeHTTPExecutor struct {
29 logger logr.Logger
30
31 httpStatusCheck v1alpha1.HTTPStatusCheck
32 timeoutSeconds int
33 }
34
35 func (e *fakeHTTPExecutor) Do() (bool, string, error) {
36 return e.handle()
37 }
38
39 func (e *fakeHTTPExecutor) Type() string {
40 return "Fake-HTTP"
41 }
42
43 func newFakeExecutor(logger logr.Logger, statusCheck v1alpha1.StatusCheck) (Executor, error) {
44 var executor Executor
45 switch statusCheck.Spec.Type {
46 case v1alpha1.TypeHTTP:
47 if statusCheck.Spec.EmbedStatusCheck == nil || statusCheck.Spec.HTTPStatusCheck == nil {
48
49 return nil, errors.New("illegal status check, http should not be empty")
50 }
51 executor = &fakeHTTPExecutor{
52 logger: logger.WithName("fake-http-executor"),
53 httpStatusCheck: *statusCheck.Spec.HTTPStatusCheck,
54 timeoutSeconds: statusCheck.Spec.TimeoutSeconds,
55 }
56 default:
57 return nil, errors.New("unsupported type")
58 }
59 return executor, nil
60 }
61
62 func (e *fakeHTTPExecutor) handle() (bool, string, error) {
63 switch e.httpStatusCheck.RequestBody {
64 case "failure":
65 return false, "failure", nil
66 case "timeout":
67 time.Sleep(time.Duration(e.timeoutSeconds) * time.Second)
68 return false, "timeout", nil
69 default:
70 return true, "", nil
71 }
72 }
73