|
| 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 | +} |
0 commit comments