Skip to content

Commit 86211ec

Browse files
committed
havoc examples
1 parent 5e13d77 commit 86211ec

File tree

2 files changed

+293
-0
lines changed

2 files changed

+293
-0
lines changed

havoc/examples/example_test.go

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
package examples
2+
3+
import (
4+
"context"
5+
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
6+
"github.com/rs/zerolog"
7+
"github.com/rs/zerolog/log"
8+
"github.com/smartcontractkit/chainlink-testing-framework/havoc"
9+
"github.com/stretchr/testify/require"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/utils/ptr"
12+
"os"
13+
"sigs.k8s.io/controller-runtime/pkg/client"
14+
"testing"
15+
"time"
16+
)
17+
18+
const (
19+
Namespace = "janitor"
20+
)
21+
22+
func defaultLogger() zerolog.Logger {
23+
return log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Level(zerolog.TraceLevel)
24+
}
25+
26+
type NodeLatenciesConfig struct {
27+
Description string
28+
Latency time.Duration
29+
FromLabelKey string
30+
FromLabelValues []string
31+
ToLabelKey string
32+
ToLabelValues []string
33+
ExperimentTotalDuration time.Duration
34+
ExperimentCreateDelay time.Duration
35+
}
36+
37+
func nodeLatencies(client client.Client, l zerolog.Logger, cfg NodeLatenciesConfig) (*havoc.Schedule, error) {
38+
return havoc.NewSchedule(havoc.ScheduleOpts{
39+
Description: cfg.Description,
40+
DelayCreate: cfg.ExperimentCreateDelay,
41+
Duration: cfg.ExperimentTotalDuration,
42+
Logger: &l,
43+
Object: &v1alpha1.Schedule{
44+
TypeMeta: metav1.TypeMeta{
45+
Kind: "Schedule",
46+
APIVersion: "chaos-mesh.org/v1alpha1",
47+
},
48+
ObjectMeta: metav1.ObjectMeta{
49+
Name: "latencies",
50+
Namespace: Namespace,
51+
},
52+
Spec: v1alpha1.ScheduleSpec{
53+
Schedule: "*/1 * * * *",
54+
ConcurrencyPolicy: v1alpha1.ForbidConcurrent,
55+
Type: v1alpha1.ScheduleTypeNetworkChaos,
56+
HistoryLimit: 10,
57+
ScheduleItem: v1alpha1.ScheduleItem{
58+
EmbedChaos: v1alpha1.EmbedChaos{
59+
NetworkChaos: &v1alpha1.NetworkChaosSpec{
60+
Action: v1alpha1.DelayAction,
61+
PodSelector: v1alpha1.PodSelector{
62+
Mode: v1alpha1.AllMode,
63+
Selector: v1alpha1.PodSelectorSpec{
64+
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
65+
Namespaces: []string{Namespace},
66+
ExpressionSelectors: v1alpha1.LabelSelectorRequirements{
67+
{
68+
Operator: "In",
69+
Key: cfg.FromLabelKey,
70+
Values: cfg.FromLabelValues,
71+
},
72+
},
73+
},
74+
},
75+
},
76+
Duration: ptr.To[string]((30 * time.Second).String()),
77+
Direction: v1alpha1.From,
78+
Target: &v1alpha1.PodSelector{
79+
Selector: v1alpha1.PodSelectorSpec{
80+
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
81+
Namespaces: []string{Namespace},
82+
ExpressionSelectors: v1alpha1.LabelSelectorRequirements{
83+
{
84+
Operator: "In",
85+
Key: cfg.ToLabelKey,
86+
Values: cfg.ToLabelValues,
87+
},
88+
},
89+
},
90+
},
91+
Mode: v1alpha1.AllMode,
92+
},
93+
TcParameter: v1alpha1.TcParameter{
94+
Delay: &v1alpha1.DelaySpec{
95+
Latency: cfg.Latency.String(),
96+
Correlation: "100",
97+
Jitter: "0ms",
98+
},
99+
},
100+
},
101+
},
102+
},
103+
},
104+
},
105+
Client: client,
106+
})
107+
}
108+
109+
type NodeRebootsConfig struct {
110+
Description string
111+
LabelKey string
112+
LabelValues []string
113+
ExperimentTotalDuration time.Duration
114+
ExperimentCreateDelay time.Duration
115+
}
116+
117+
func reboots(client client.Client, l zerolog.Logger, cfg NodeRebootsConfig) (*havoc.Schedule, error) {
118+
return havoc.NewSchedule(havoc.ScheduleOpts{
119+
Description: cfg.Description,
120+
DelayCreate: cfg.ExperimentCreateDelay,
121+
Duration: cfg.ExperimentTotalDuration,
122+
Logger: &l,
123+
Object: &v1alpha1.Schedule{
124+
TypeMeta: metav1.TypeMeta{
125+
Kind: "Schedule",
126+
APIVersion: "chaos-mesh.org/v1alpha1",
127+
},
128+
ObjectMeta: metav1.ObjectMeta{
129+
Name: "reboots",
130+
Namespace: Namespace,
131+
},
132+
Spec: v1alpha1.ScheduleSpec{
133+
Schedule: "*/1 * * * *",
134+
ConcurrencyPolicy: v1alpha1.ForbidConcurrent,
135+
Type: v1alpha1.ScheduleTypePodChaos,
136+
HistoryLimit: 10,
137+
ScheduleItem: v1alpha1.ScheduleItem{
138+
EmbedChaos: v1alpha1.EmbedChaos{
139+
PodChaos: &v1alpha1.PodChaosSpec{
140+
Action: v1alpha1.PodFailureAction,
141+
ContainerSelector: v1alpha1.ContainerSelector{
142+
PodSelector: v1alpha1.PodSelector{
143+
Mode: v1alpha1.AllMode,
144+
Selector: v1alpha1.PodSelectorSpec{
145+
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
146+
Namespaces: []string{Namespace},
147+
ExpressionSelectors: v1alpha1.LabelSelectorRequirements{
148+
{
149+
Operator: "In",
150+
Key: cfg.LabelKey,
151+
Values: cfg.LabelValues,
152+
},
153+
},
154+
},
155+
},
156+
},
157+
},
158+
Duration: ptr.To[string]("40s"),
159+
},
160+
},
161+
},
162+
},
163+
},
164+
Client: client,
165+
})
166+
}
167+
168+
func TestChaos(t *testing.T) {
169+
l := defaultLogger()
170+
c, err := havoc.NewChaosMeshClient()
171+
require.NoError(t, err)
172+
173+
rebootsChaos, err := reboots(c, l, NodeRebootsConfig{
174+
Description: "reboot nodes",
175+
LabelKey: "app.kubernetes.io/instance",
176+
LabelValues: []string{"janitor"},
177+
ExperimentTotalDuration: 1 * time.Minute,
178+
})
179+
require.NoError(t, err)
180+
latenciesChaos, err := nodeLatencies(c, l, NodeLatenciesConfig{
181+
Description: "network issues",
182+
Latency: 300 * time.Millisecond,
183+
FromLabelKey: "app.kubernetes.io/instance",
184+
FromLabelValues: []string{"janitor"},
185+
ToLabelKey: "app.kubernetes.io/instance",
186+
ToLabelValues: []string{"janitor"},
187+
ExperimentTotalDuration: 2 * time.Minute,
188+
ExperimentCreateDelay: 1 * time.Minute,
189+
})
190+
require.NoError(t, err)
191+
192+
_ = rebootsChaos
193+
_ = latenciesChaos
194+
195+
chaosList := []havoc.ChaosEntity{
196+
rebootsChaos,
197+
latenciesChaos,
198+
}
199+
200+
for _, chaos := range chaosList {
201+
chaos.AddListener(havoc.NewChaosLogger(l))
202+
//chaos.AddListener(havoc.NewSingleLineGrafanaAnnotator(cfg.GrafanaURL, cfg.GrafanaToken, cfg.GrafanaDashboardUID))
203+
exists, err := havoc.ChaosObjectExists(chaos.GetObject(), c)
204+
require.NoError(t, err)
205+
require.False(t, exists, "chaos object already exists: %s. Delete it before starting the test", chaos.GetChaosName())
206+
chaos.Create(context.Background())
207+
}
208+
t.Cleanup(func() {
209+
for _, chaos := range chaosList {
210+
chaos.Delete(context.Background())
211+
}
212+
})
213+
214+
// your load test comes here !
215+
time.Sleep(3 * time.Minute)
216+
}

havoc/examples/go.mod

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
module github.com/smartcontractkit/chainlink-testing-framework/havoc/examples
2+
3+
go 1.23.4
4+
5+
require (
6+
github.com/smartcontractkit/chainlink-testing-framework/havoc v1.50.2
7+
github.com/stretchr/testify v1.10.0
8+
)
9+
10+
require (
11+
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
12+
github.com/beorn7/perks v1.0.1 // indirect
13+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
14+
github.com/chaos-mesh/chaos-mesh/api v0.0.0-20240821051457-da69c6d9617a // indirect
15+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
16+
github.com/docker/go-units v0.5.0 // indirect
17+
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
18+
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
19+
github.com/fsnotify/fsnotify v1.7.0 // indirect
20+
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
21+
github.com/go-logr/logr v1.4.2 // indirect
22+
github.com/go-openapi/jsonpointer v0.20.0 // indirect
23+
github.com/go-openapi/jsonreference v0.20.2 // indirect
24+
github.com/go-openapi/swag v0.22.4 // indirect
25+
github.com/go-resty/resty/v2 v2.11.0 // indirect
26+
github.com/gogo/protobuf v1.3.2 // indirect
27+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
28+
github.com/golang/protobuf v1.5.4 // indirect
29+
github.com/google/gnostic-models v0.6.8 // indirect
30+
github.com/google/go-cmp v0.6.0 // indirect
31+
github.com/google/gofuzz v1.2.0 // indirect
32+
github.com/google/uuid v1.6.0 // indirect
33+
github.com/grafana/grafana-foundation-sdk/go v0.0.0-20240326122733-6f96a993222b // indirect
34+
github.com/imdario/mergo v0.3.16 // indirect
35+
github.com/josharian/intern v1.0.0 // indirect
36+
github.com/json-iterator/go v1.1.12 // indirect
37+
github.com/mailru/easyjson v0.7.7 // indirect
38+
github.com/mattn/go-colorable v0.1.13 // indirect
39+
github.com/mattn/go-isatty v0.0.19 // indirect
40+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
41+
github.com/modern-go/reflect2 v1.0.2 // indirect
42+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
43+
github.com/pkg/errors v0.9.1 // indirect
44+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
45+
github.com/prometheus/client_golang v1.19.1 // indirect
46+
github.com/prometheus/client_model v0.6.1 // indirect
47+
github.com/prometheus/common v0.55.0 // indirect
48+
github.com/prometheus/procfs v0.15.1 // indirect
49+
github.com/robfig/cron/v3 v3.0.1 // indirect
50+
github.com/rs/zerolog v1.33.0 // indirect
51+
github.com/smartcontractkit/chainlink-testing-framework/lib/grafana v1.50.0 // indirect
52+
github.com/spf13/pflag v1.0.5 // indirect
53+
github.com/x448/float16 v0.8.4 // indirect
54+
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
55+
golang.org/x/net v0.26.0 // indirect
56+
golang.org/x/oauth2 v0.21.0 // indirect
57+
golang.org/x/sys v0.22.0 // indirect
58+
golang.org/x/term v0.22.0 // indirect
59+
golang.org/x/text v0.16.0 // indirect
60+
golang.org/x/time v0.6.0 // indirect
61+
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
62+
google.golang.org/protobuf v1.34.2 // indirect
63+
gopkg.in/inf.v0 v0.9.1 // indirect
64+
gopkg.in/yaml.v2 v2.4.0 // indirect
65+
gopkg.in/yaml.v3 v3.0.1 // indirect
66+
k8s.io/api v0.31.1 // indirect
67+
k8s.io/apiextensions-apiserver v0.31.0 // indirect
68+
k8s.io/apimachinery v0.31.1 // indirect
69+
k8s.io/client-go v0.31.1 // indirect
70+
k8s.io/klog/v2 v2.130.1 // indirect
71+
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
72+
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
73+
sigs.k8s.io/controller-runtime v0.19.0 // indirect
74+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
75+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
76+
sigs.k8s.io/yaml v1.4.0 // indirect
77+
)

0 commit comments

Comments
 (0)