Skip to content

Commit 73525d4

Browse files
committed
more cases, add default params, error when no uniq labels
1 parent acfb620 commit 73525d4

File tree

4 files changed

+202
-79
lines changed

4 files changed

+202
-79
lines changed

framework/cmd/main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Be aware that any TODO requires your attention before your run the final test!
5454
&cli.StringFlag{
5555
Name: "name",
5656
Aliases: []string{"n"},
57-
Value: "TestLoadChaos",
57+
Value: "TestGeneratedLoadChaos",
5858
Usage: "Test suite name",
5959
},
6060
&cli.StringFlag{
@@ -81,6 +81,18 @@ Be aware that any TODO requires your attention before your run the final test!
8181
Value: "app.kubernetes.io/instance",
8282
Usage: "Default unique pod key, read more here: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/",
8383
},
84+
&cli.StringFlag{
85+
Name: "latency-ms",
86+
Aliases: []string{"l"},
87+
Value: "300",
88+
Usage: "Default latency for delay experiments in milliseconds",
89+
},
90+
&cli.StringFlag{
91+
Name: "jitter-ms",
92+
Aliases: []string{"j"},
93+
Value: "100",
94+
Usage: "Default jitter for delay experiments in milliseconds",
95+
},
8496
},
8597
Action: func(c *cli.Context) error {
8698
if c.Args().Len() == 0 {
@@ -89,6 +101,8 @@ Be aware that any TODO requires your attention before your run the final test!
89101
ns := c.Args().First()
90102
testSuiteName := c.String("name")
91103
podLabelKey := c.String("pod-label-key")
104+
latencyMs := c.Int("latency-ms")
105+
jitterMs := c.Int("jitter-ms")
92106
outputDir := c.String("output-dir")
93107
moduleName := c.String("module")
94108
includeWorkload := c.Bool("workload")
@@ -107,6 +121,8 @@ Be aware that any TODO requires your attention before your run the final test!
107121
cg, err := wasp.NewLoadTestGenBuilder(k8sClient, ns).
108122
TestSuiteName(testSuiteName).
109123
UniqPodLabelKey(podLabelKey).
124+
Latency(latencyMs).
125+
Jitter(jitterMs).
110126
Workload(includeWorkload).
111127
OutputDir(outputDir).
112128
GoModName(moduleName).

infra/chaosmesh-playground/dummy-cluster.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ apiVersion: apps/v1
600600
kind: Deployment
601601
metadata:
602602
name: production-app-that-must-be-safe
603+
labels:
604+
app.kubernetes.io/instance: production-app
603605
spec:
604606
selector:
605607
matchLabels:

wasp/tmpl_generator.go

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,18 @@ var _ TestCodeGenerator = (*LoadTestCodegen)(nil)
4242

4343
const (
4444
DefaultTestSuiteName = "TestGeneratedLoadChaos"
45+
DefaultUniqLabel = "app.kubernetes.io/instance"
4546
DefaultTestModule = "main"
4647
)
4748

4849
/* Templates */
4950

5051
const (
51-
// TODO: remove this replace after merge
5252
GoModTemplate = `module {{.ModuleName}}
5353
5454
go 1.25
55+
56+
replace github.com/smartcontractkit/chainlink-testing-framework/wasp => ../../../wasp/
5557
`
5658

5759
// TableTestTmpl is a load/chaos table test template
@@ -222,8 +224,8 @@ require.NoError(t, err)`
222224
Namespace: cfg.Chaos.Namespace,
223225
LabelKey: "{{.LabelKey}}",
224226
LabelValues: []string{"{{.LabelValue}}"},
225-
Latency: 400 * time.Millisecond,
226-
Jitter: 20 * time.Millisecond,
227+
Latency: {{.LatencyMs}} * time.Millisecond,
228+
Jitter: {{.JitterMs}} * time.Millisecond,
227229
Correlation: "0",
228230
InjectionDuration: f.MustParseDuration(cfg.Chaos.ExperimentInjectionDuration),
229231
})
@@ -264,6 +266,8 @@ type PodFailParams struct {
264266
type PodDelayParams struct {
265267
LabelKey string
266268
LabelValue string
269+
LatencyMs int
270+
JitterMs int
267271
}
268272

269273
// K8sPodsInterface defines the interface for Kubernetes interactions
@@ -314,12 +318,14 @@ func (m *MockK8s) GetPods(ctx context.Context, namespace string) (*corev1.PodLis
314318
type LoadTestBuilder struct {
315319
namespace string
316320
testSuiteName string
317-
k8sClient K8sPodsInterface
318321
pods []corev1.Pod
319322
uniqPodLabelKey string
323+
latencyMs int
324+
jitterMs int
320325
includeWorkload bool
321326
outputDir string
322327
moduleName string
328+
k8sClient K8sPodsInterface
323329
}
324330

325331
// LoadTestCodegen is a load test code generator that creates workload and chaos experiments
@@ -331,7 +337,8 @@ type LoadTestCodegen struct {
331337
func NewLoadTestGenBuilder(client K8sPodsInterface, namespace string) *LoadTestBuilder {
332338
return &LoadTestBuilder{
333339
namespace: namespace,
334-
testSuiteName: DefaultGenName,
340+
testSuiteName: DefaultTestSuiteName,
341+
uniqPodLabelKey: DefaultUniqLabel,
335342
k8sClient: client,
336343
includeWorkload: false,
337344
outputDir: ".",
@@ -348,6 +355,18 @@ func (g *LoadTestBuilder) TestSuiteName(n string) *LoadTestBuilder {
348355
return g
349356
}
350357

358+
// Latency sets default latency for delay experiments
359+
func (g *LoadTestBuilder) Latency(l int) *LoadTestBuilder {
360+
g.latencyMs = l
361+
return g
362+
}
363+
364+
// Jitter sets default jitter for delay experiments
365+
func (g *LoadTestBuilder) Jitter(j int) *LoadTestBuilder {
366+
g.jitterMs = j
367+
return g
368+
}
369+
351370
// UniqPodLabelKey K8s Pod label key that uniqely identifies Pod for chaos experiment
352371
// read more here https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/
353372
func (g *LoadTestBuilder) UniqPodLabelKey(k string) *LoadTestBuilder {
@@ -426,7 +445,7 @@ func (g *LoadTestCodegen) Write() error {
426445
return err
427446
}
428447
goModPath := filepath.Join(g.cfg.outputDir, "go.mod")
429-
if err := os.WriteFile(goModPath, []byte(goModContent), 0600); err != nil {
448+
if err := os.WriteFile(goModPath, []byte(goModContent), 0o600); err != nil {
430449
return fmt.Errorf("failed to write go.mod: %w", err)
431450
}
432451

@@ -436,7 +455,7 @@ func (g *LoadTestCodegen) Write() error {
436455
return err
437456
}
438457
testPath := filepath.Join(g.cfg.outputDir, "chaos_test.go")
439-
if err := os.WriteFile(testPath, []byte(testContent), 0600); err != nil {
458+
if err := os.WriteFile(testPath, []byte(testContent), 0o600); err != nil {
440459
return fmt.Errorf("failed to write test file: %w", err)
441460
}
442461
currentDir, err := os.Getwd()
@@ -500,6 +519,12 @@ func (g *LoadTestCodegen) GenerateLoadTest() (string, string) {
500519
func (g *LoadTestCodegen) GenerateTestCases() ([]TestCaseParams, error) {
501520
var testCases []TestCaseParams
502521

522+
for _, pod := range g.cfg.pods {
523+
if _, ok := pod.Labels[g.cfg.uniqPodLabelKey]; !ok {
524+
return nil, fmt.Errorf("pod %s doesn't have uniq label key %s", pod.Name, g.cfg.uniqPodLabelKey)
525+
}
526+
}
527+
503528
// Pod failures
504529
for _, pod := range g.cfg.pods {
505530
r, err := render(PodFailTmpl, PodFailParams{
@@ -520,6 +545,8 @@ func (g *LoadTestCodegen) GenerateTestCases() ([]TestCaseParams, error) {
520545
r, err := render(PodDelayTmpl, PodDelayParams{
521546
LabelKey: g.cfg.uniqPodLabelKey,
522547
LabelValue: pod.Labels[g.cfg.uniqPodLabelKey],
548+
LatencyMs: g.cfg.latencyMs,
549+
JitterMs: g.cfg.jitterMs,
523550
})
524551
if err != nil {
525552
return nil, err

0 commit comments

Comments
 (0)