Skip to content

Commit

Permalink
havoc examples
Browse files Browse the repository at this point in the history
  • Loading branch information
skudasov committed Jan 28, 2025
1 parent 5e13d77 commit 86211ec
Show file tree
Hide file tree
Showing 2 changed files with 293 additions and 0 deletions.
216 changes: 216 additions & 0 deletions havoc/examples/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package examples

import (
"context"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/smartcontractkit/chainlink-testing-framework/havoc"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
"os"
"sigs.k8s.io/controller-runtime/pkg/client"
"testing"
"time"
)

const (
Namespace = "janitor"
)

func defaultLogger() zerolog.Logger {
return log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).Level(zerolog.TraceLevel)
}

type NodeLatenciesConfig struct {
Description string
Latency time.Duration
FromLabelKey string
FromLabelValues []string
ToLabelKey string
ToLabelValues []string
ExperimentTotalDuration time.Duration
ExperimentCreateDelay time.Duration
}

func nodeLatencies(client client.Client, l zerolog.Logger, cfg NodeLatenciesConfig) (*havoc.Schedule, error) {
return havoc.NewSchedule(havoc.ScheduleOpts{
Description: cfg.Description,
DelayCreate: cfg.ExperimentCreateDelay,
Duration: cfg.ExperimentTotalDuration,
Logger: &l,
Object: &v1alpha1.Schedule{
TypeMeta: metav1.TypeMeta{
Kind: "Schedule",
APIVersion: "chaos-mesh.org/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "latencies",
Namespace: Namespace,
},
Spec: v1alpha1.ScheduleSpec{
Schedule: "*/1 * * * *",
ConcurrencyPolicy: v1alpha1.ForbidConcurrent,
Type: v1alpha1.ScheduleTypeNetworkChaos,
HistoryLimit: 10,
ScheduleItem: v1alpha1.ScheduleItem{
EmbedChaos: v1alpha1.EmbedChaos{
NetworkChaos: &v1alpha1.NetworkChaosSpec{
Action: v1alpha1.DelayAction,
PodSelector: v1alpha1.PodSelector{
Mode: v1alpha1.AllMode,
Selector: v1alpha1.PodSelectorSpec{
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
Namespaces: []string{Namespace},
ExpressionSelectors: v1alpha1.LabelSelectorRequirements{
{
Operator: "In",
Key: cfg.FromLabelKey,
Values: cfg.FromLabelValues,
},
},
},
},
},
Duration: ptr.To[string]((30 * time.Second).String()),
Direction: v1alpha1.From,
Target: &v1alpha1.PodSelector{
Selector: v1alpha1.PodSelectorSpec{
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
Namespaces: []string{Namespace},
ExpressionSelectors: v1alpha1.LabelSelectorRequirements{
{
Operator: "In",
Key: cfg.ToLabelKey,
Values: cfg.ToLabelValues,
},
},
},
},
Mode: v1alpha1.AllMode,
},
TcParameter: v1alpha1.TcParameter{
Delay: &v1alpha1.DelaySpec{
Latency: cfg.Latency.String(),
Correlation: "100",
Jitter: "0ms",
},
},
},
},
},
},
},
Client: client,
})
}

type NodeRebootsConfig struct {
Description string
LabelKey string
LabelValues []string
ExperimentTotalDuration time.Duration
ExperimentCreateDelay time.Duration
}

func reboots(client client.Client, l zerolog.Logger, cfg NodeRebootsConfig) (*havoc.Schedule, error) {
return havoc.NewSchedule(havoc.ScheduleOpts{
Description: cfg.Description,
DelayCreate: cfg.ExperimentCreateDelay,
Duration: cfg.ExperimentTotalDuration,
Logger: &l,
Object: &v1alpha1.Schedule{
TypeMeta: metav1.TypeMeta{
Kind: "Schedule",
APIVersion: "chaos-mesh.org/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "reboots",
Namespace: Namespace,
},
Spec: v1alpha1.ScheduleSpec{
Schedule: "*/1 * * * *",
ConcurrencyPolicy: v1alpha1.ForbidConcurrent,
Type: v1alpha1.ScheduleTypePodChaos,
HistoryLimit: 10,
ScheduleItem: v1alpha1.ScheduleItem{
EmbedChaos: v1alpha1.EmbedChaos{
PodChaos: &v1alpha1.PodChaosSpec{
Action: v1alpha1.PodFailureAction,
ContainerSelector: v1alpha1.ContainerSelector{
PodSelector: v1alpha1.PodSelector{
Mode: v1alpha1.AllMode,
Selector: v1alpha1.PodSelectorSpec{
GenericSelectorSpec: v1alpha1.GenericSelectorSpec{
Namespaces: []string{Namespace},
ExpressionSelectors: v1alpha1.LabelSelectorRequirements{
{
Operator: "In",
Key: cfg.LabelKey,
Values: cfg.LabelValues,
},
},
},
},
},
},
Duration: ptr.To[string]("40s"),
},
},
},
},
},
Client: client,
})
}

func TestChaos(t *testing.T) {
l := defaultLogger()
c, err := havoc.NewChaosMeshClient()
require.NoError(t, err)

rebootsChaos, err := reboots(c, l, NodeRebootsConfig{
Description: "reboot nodes",
LabelKey: "app.kubernetes.io/instance",
LabelValues: []string{"janitor"},
ExperimentTotalDuration: 1 * time.Minute,
})
require.NoError(t, err)
latenciesChaos, err := nodeLatencies(c, l, NodeLatenciesConfig{
Description: "network issues",
Latency: 300 * time.Millisecond,
FromLabelKey: "app.kubernetes.io/instance",
FromLabelValues: []string{"janitor"},
ToLabelKey: "app.kubernetes.io/instance",
ToLabelValues: []string{"janitor"},
ExperimentTotalDuration: 2 * time.Minute,
ExperimentCreateDelay: 1 * time.Minute,
})
require.NoError(t, err)

_ = rebootsChaos
_ = latenciesChaos

chaosList := []havoc.ChaosEntity{
rebootsChaos,
latenciesChaos,
}

for _, chaos := range chaosList {
chaos.AddListener(havoc.NewChaosLogger(l))
//chaos.AddListener(havoc.NewSingleLineGrafanaAnnotator(cfg.GrafanaURL, cfg.GrafanaToken, cfg.GrafanaDashboardUID))
exists, err := havoc.ChaosObjectExists(chaos.GetObject(), c)
require.NoError(t, err)
require.False(t, exists, "chaos object already exists: %s. Delete it before starting the test", chaos.GetChaosName())
chaos.Create(context.Background())
}
t.Cleanup(func() {
for _, chaos := range chaosList {
chaos.Delete(context.Background())
}
})

// your load test comes here !
time.Sleep(3 * time.Minute)
}
77 changes: 77 additions & 0 deletions havoc/examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module github.com/smartcontractkit/chainlink-testing-framework/havoc/examples

go 1.23.4

require (
github.com/smartcontractkit/chainlink-testing-framework/havoc v1.50.2
github.com/stretchr/testify v1.10.0
)

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/chaos-mesh/chaos-mesh/api v0.0.0-20240821051457-da69c6d9617a // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-resty/resty/v2 v2.11.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grafana/grafana-foundation-sdk/go v0.0.0-20240326122733-6f96a993222b // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/rs/zerolog v1.33.0 // indirect
github.com/smartcontractkit/chainlink-testing-framework/lib/grafana v1.50.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.6.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.31.1 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/apimachinery v0.31.1 // indirect
k8s.io/client-go v0.31.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/controller-runtime v0.19.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

0 comments on commit 86211ec

Please sign in to comment.