|
1 | | -## Havoc |
| 1 | +# Havoc |
2 | 2 |
|
3 | | -The `havoc` package is a Go library designed to facilitate chaos testing within Kubernetes environments using Chaos Mesh. It offers a structured way to define, execute, and manage chaos experiments as code, directly integrated into Go applications or testing suites. This package simplifies the creation and control of Chaos Mesh experiments, including network chaos, pod failures, and stress testing on Kubernetes clusters. |
| 3 | +[](https://pkg.go.dev/github.com/smartcontractkit/chainlink-testing-framework/havoc) |
4 | 4 |
|
5 | | -### Features |
| 5 | +The `havoc` package is designed to facilitate chaos testing within [Kubernetes](https://kubernetes.io/) environments using [Chaos Mesh](https://chaos-mesh.org/). It offers a structured way to define, execute, and manage chaos experiments as code, directly integrated into Go applications or testing suites, simplifying the creation and control of Chaos Mesh experiments. |
6 | 6 |
|
7 | | -- **Chaos Object Management:** Easily create, update, pause, resume, and delete chaos experiments using Go structures and methods. |
8 | | -- **Lifecycle Hooks:** Utilize chaos listeners to hook into lifecycle events of chaos experiments, such as creation, start, pause, resume, and finish. |
9 | | -- **Support for Various Chaos Experiments:** Create and manage different types of chaos experiments like NetworkChaos, IOChaos, StressChaos, PodChaos, and HTTPChaos. |
10 | | -- **Chaos Experiment Status Monitoring:** Monitor and react to the status of chaos experiments programmatically. |
| 7 | +## Features |
11 | 8 |
|
12 | | -### Installation |
| 9 | +- **Chaos Object Management:** Create, update, pause, resume, and delete chaos experiments using Go structures and methods. |
| 10 | +- **Lifecycle Hooks:** Utilize chaos listeners to hook into the lifecycle of chaos experiments. |
| 11 | +- **Different Experiments:** Create and manage different types of chaos experiments to affect network, IO, K8s pods, and more. |
| 12 | +- **Active Monitoring:** Monitor and react to the status of chaos experiments programmatically. |
13 | 13 |
|
14 | | -To use `havoc` in your project, ensure you have a Go environment setup. Then, install the package using go get: |
| 14 | +## Requirements |
15 | 15 |
|
16 | | -``` |
17 | | -go get -u github.com/smartcontractkit/chainlink-testing-framework/havoc |
18 | | -``` |
| 16 | +- [Go](https://go.dev/) |
| 17 | +- A Kubernetes cluster with [Chaos Mesh installed](https://chaos-mesh.org/docs/quick-start/) |
19 | 18 |
|
20 | | -Ensure your Kubernetes cluster is accessible and that you have Chaos Mesh installed and configured. |
| 19 | +## Active Monitoring |
21 | 20 |
|
22 | | -### Monitoring and Observability in Chaos Experiments |
| 21 | +`havoc` enhances chaos experiment observability through structured logging and Grafana annotations by implementing the [ChaosListener](https://pkg.go.dev/github.com/smartcontractkit/chainlink-testing-framework/havoc#ChaosListener) interface. |
23 | 22 |
|
24 | | -`havoc` enhances chaos experiment observability through structured logging and Grafana annotations, facilitated by implementing the ChaosListener interface. This approach allows for detailed monitoring, debugging, and visual representation of chaos experiments' impact. |
| 23 | +The [ChaosLogger](https://pkg.go.dev/github.com/smartcontractkit/chainlink-testing-framework/havoc#ChaosLogger) is the default implementation. It uses [zerolog](https://github.com/rs/zerolog) to provide structured, queryable logging of chaos events. It automatically logs key lifecycle events such as creation, start, pause, and termination of chaos experiments with detailed contextual information. |
25 | 24 |
|
26 | | -#### Structured Logging with ChaosLogger |
| 25 | +### Grafana Annotations |
27 | 26 |
|
28 | | -`ChaosLogger` leverages the zerolog library to provide structured, queryable logging of chaos events. It automatically logs key lifecycle events such as creation, start, pause, and termination of chaos experiments, including detailed contextual information. |
| 27 | +We recommend using [Grafana dashboards](https://grafana.com/) to monitor your chaos experiments, and provide the [SingleLineGrafanaAnnotator](https://pkg.go.dev/github.com/smartcontractkit/chainlink-testing-framework/havoc#SingleLineGrafanaAnnotator), a `ChaosListener` that annotates dashboards with chaos experiment events so you can see in real time what your chaos experiment is doing. |
29 | 28 |
|
30 | | -Instantiate `ChaosLogger` and register it as a listener to your chaos experiments: |
| 29 | +You can also use the [RangeGrafanaAnnotator](https://pkg.go.dev/github.com/smartcontractkit/chainlink-testing-framework/havoc#RangeGrafanaAnnotator) to show the full range of a chaos event's duration rather than a single line. |
31 | 30 |
|
32 | | -``` |
33 | | -logger := havoc.NewChaosLogger() |
34 | | -chaos.AddListener(logger) |
35 | | -``` |
36 | | - |
37 | | -### Default package logger |
38 | | - |
39 | | -`havoc/logger.go` contains default `Logger` instance for the package. |
40 | | - |
41 | | -#### Visual Monitoring with Grafana Annotations |
42 | | - |
43 | | -`SingleLineGrafanaAnnotator` is a `ChaosListener` that annotates Grafana dashboards with chaos experiment events. This visual representation helps correlate chaos events with their effects on system metrics and logs. |
44 | | - |
45 | | -Initialize `SingleLineGrafanaAnnotator` with your Grafana instance details and register it alongside `ChaosLogger`: |
46 | | - |
47 | | -```golang |
48 | | -annotator := havoc.NewSingleLineGrafanaAnnotator( |
49 | | - "http://grafana-instance.com", |
50 | | - "grafana-access-token", |
51 | | - "dashboard-uid", |
52 | | -) |
53 | | -chaos.AddListener(annotator) |
54 | | -``` |
55 | | - |
56 | | -### Creating a Chaos Experiment |
| 31 | +## Creating a Chaos Experiment |
57 | 32 |
|
58 | 33 | To create a chaos experiment, define the chaos object options, initialize a chaos experiment with NewChaos, and then call Create to start the experiment. |
59 | 34 |
|
60 | | -Here is an example of creating and starting a PodChaos experiment: |
61 | | - |
62 | | -```golang |
63 | | -package main |
64 | | - |
65 | | -import ( |
66 | | - "context" |
67 | | - "github.com/smartcontractkit/chainlink-testing-framework/havoc" |
68 | | - "github.com/chaos-mesh/chaos-mesh/api/v1alpha1" |
69 | | - "sigs.k8s.io/controller-runtime/pkg/client" |
70 | | - "time" |
71 | | -) |
72 | | - |
73 | | -func main() { |
74 | | - // Initialize dependencies |
75 | | - client, err := havoc.NewChaosMeshClient() |
76 | | - if err != nil { |
77 | | - panic(err) |
78 | | - } |
79 | | - logger := havoc.NewChaosLogger() |
80 | | - annotator := havoc.NewSingleLineGrafanaAnnotator( |
81 | | - "http://grafana-instance.com", |
82 | | - "grafana-access-token", |
83 | | - "dashboard-uid", |
84 | | - ) |
85 | | - |
86 | | - // Define chaos experiment |
87 | | - podChaos := &v1alpha1.PodChaos{ /* PodChaos spec */ } |
88 | | - chaos, err := havoc.NewChaos(havoc.ChaosOpts{ |
89 | | - Object: podChaos, |
90 | | - Description: "Pod failure example", |
91 | | - DelayCreate: 5 * time.Second, |
92 | | - Client: client, |
93 | | - }) |
94 | | - if err != nil { |
95 | | - panic(err) |
96 | | - } |
97 | | - |
98 | | - // Register listeners |
99 | | - chaos.AddListener(logger) |
100 | | - chaos.AddListener(annotator) |
101 | | - |
102 | | - // Start chaos experiment |
103 | | - chaos.Create(context.Background()) |
104 | | - |
105 | | - // Manage chaos lifecycle... |
106 | | -} |
107 | | -``` |
108 | | - |
109 | | -### Test Example |
110 | | - |
111 | | -```golang |
112 | | -func TestChaosDON(t *testing.T) { |
113 | | - testDuration := time.Minute * 60 |
114 | | - |
115 | | - // Load test config |
116 | | - cfg := &config.MercuryQAEnvChaos{} |
117 | | - |
118 | | - // Define chaos experiments and their schedule |
119 | | - |
120 | | - k8sClient, err := havoc.NewChaosMeshClient() |
121 | | - require.NoError(t, err) |
122 | | - |
123 | | - // Test 3.2: Disable 2 nodes simultaneously |
124 | | - |
125 | | - podFailureChaos4, err := k8s_chaos.MercuryPodChaosSchedule(k8s_chaos.MercuryScheduledPodChaosOpts{ |
126 | | - Name: "schedule-don-ocr-node-failure-4", |
127 | | - Description: "Disable 2 nodes (clc-ocr-mercury-arb-testnet-qa-nodes-3 and clc-ocr-mercury-arb-testnet-qa-nodes-4)", |
128 | | - DelayCreate: time.Minute * 0, |
129 | | - Duration: time.Minute * 20, |
130 | | - Namespace: cfg.ChaosNodeNamespace, |
131 | | - PodSelector: v1alpha1.PodSelector{ |
132 | | - Mode: v1alpha1.AllMode, |
133 | | - Selector: v1alpha1.PodSelectorSpec{ |
134 | | - GenericSelectorSpec: v1alpha1.GenericSelectorSpec{ |
135 | | - Namespaces: []string{cfg.ChaosNodeNamespace}, |
136 | | - ExpressionSelectors: v1alpha1.LabelSelectorRequirements{ |
137 | | - { |
138 | | - Key: "app.kubernetes.io/instance", |
139 | | - Operator: "In", |
140 | | - Values: []string{ |
141 | | - "clc-ocr-mercury-arb-testnet-qa-nodes-3", |
142 | | - "clc-ocr-mercury-arb-testnet-qa-nodes-4", |
143 | | - }, |
144 | | - }, |
145 | | - }, |
146 | | - }, |
147 | | - }, |
148 | | - }, |
149 | | - Client: k8sClient, |
150 | | - }) |
151 | | - require.NoError(t, err) |
152 | | - |
153 | | - // Test 3.3: Disable 3 nodes simultaneously |
154 | | - |
155 | | - podFailureChaos5, err := k8s_chaos.MercuryPodChaosSchedule(k8s_chaos.MercuryScheduledPodChaosOpts{ |
156 | | - Name: "schedule-don-ocr-node-failure-5", |
157 | | - Description: "Disable 3 nodes (clc-ocr-mercury-arb-testnet-qa-nodes-3, clc-ocr-mercury-arb-testnet-qa-nodes-4 and clc-ocr-mercury-arb-testnet-qa-nodes-5)", |
158 | | - DelayCreate: time.Minute * 40, |
159 | | - Duration: time.Minute * 20, |
160 | | - Namespace: cfg.ChaosNodeNamespace, |
161 | | - PodSelector: v1alpha1.PodSelector{ |
162 | | - Mode: v1alpha1.AllMode, |
163 | | - Selector: v1alpha1.PodSelectorSpec{ |
164 | | - GenericSelectorSpec: v1alpha1.GenericSelectorSpec{ |
165 | | - Namespaces: []string{cfg.ChaosNodeNamespace}, |
166 | | - ExpressionSelectors: v1alpha1.LabelSelectorRequirements{ |
167 | | - { |
168 | | - Key: "app.kubernetes.io/instance", |
169 | | - Operator: "In", |
170 | | - Values: []string{ |
171 | | - "clc-ocr-mercury-arb-testnet-qa-nodes-3", |
172 | | - "clc-ocr-mercury-arb-testnet-qa-nodes-4", |
173 | | - "clc-ocr-mercury-arb-testnet-qa-nodes-5", |
174 | | - }, |
175 | | - }, |
176 | | - }, |
177 | | - }, |
178 | | - }, |
179 | | - }, |
180 | | - Client: k8sClient, |
181 | | - }) |
182 | | - require.NoError(t, err) |
183 | | - |
184 | | - chaosList := []havoc.ChaosEntity{ |
185 | | - podFailureChaos4, |
186 | | - podFailureChaos5, |
187 | | - } |
188 | | - |
189 | | - for _, chaos := range chaosList { |
190 | | - chaos.AddListener(havoc.NewChaosLogger()) |
191 | | - chaos.AddListener(havoc.NewSingleLineGrafanaAnnotator(cfg.GrafanaURL, cfg.GrafanaToken, cfg.GrafanaDashboardUID)) |
192 | | - |
193 | | - // Fail the test if the chaos object already exists |
194 | | - exists, err := havoc.ChaosObjectExists(chaos.GetObject(), k8sClient) |
195 | | - require.NoError(t, err) |
196 | | - require.False(t, exists, "chaos object already exists: %s. Delete it before starting the test", chaos.GetChaosName()) |
197 | | - |
198 | | - chaos.Create(context.Background()) |
199 | | - } |
200 | | - |
201 | | - t.Cleanup(func() { |
202 | | - for _, chaos := range chaosList { |
203 | | - // Delete chaos object if it still exists |
204 | | - chaos.Delete(context.Background()) |
205 | | - } |
206 | | - }) |
207 | | - |
208 | | - // Simulate user activity/load for the duration of the chaos experiments |
209 | | - runUserLoad(t, cfg, testDuration) |
210 | | -} |
211 | | -``` |
| 35 | +See [this runnable example](https://pkg.go.dev/github.com/smartcontractkit/chainlink-testing-framework/havoc#ExampleNewChaos) of defining a chaos experiment. |
0 commit comments