Skip to content

Commit 696e1b1

Browse files
committed
WIP: load/chaos codegen
1 parent 7f15b3a commit 696e1b1

23 files changed

+3461
-219
lines changed

book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [CLI](./framework/cli.md)
1313
- [Configuration](./framework/configuration.md)
1414
- [Debugging Tests](framework/components/debug.md)
15+
- [Generating Tests](framework/generate.md)
1516
- [Creating your own components](./developing/developing_components.md)
1617
- [Exposing Components](framework/components/state.md)
1718
- [Asserting Logs](./developing/asserting_logs.md)

book/src/framework/generate.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generating Tests
2+
3+
Some types of tests may require a lot of boilerplate and configurability, for example load and chaos tests.
4+
5+
We provide code generation tool that help you to start with best practes in such cases.
6+
7+
All generators have `-h` or `--help` flag, please read the docs!

framework/cmd/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ blockscout/services/blockscout-db-data
22
blockscout/services/logs
33
blockscout/services/redis-data
44
blockscout/services/stats-db-data
5+
wasp-self-test/
6+
wasp-test/

framework/cmd/main.go

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/urfave/cli/v2"
1212

1313
"github.com/smartcontractkit/chainlink-testing-framework/framework"
14+
"github.com/smartcontractkit/chainlink-testing-framework/wasp"
1415
)
1516

1617
func main() {
@@ -19,6 +20,111 @@ func main() {
1920
Usage: "Chainlink Testing Framework CLI",
2021
UsageText: "'ctf' is a useful utility that can:\n- clean up test docker containers\n- modify test files\n- create a local observability stack with Grafana/Loki/Pyroscope",
2122
Commands: []*cli.Command{
23+
{
24+
Name: "gen",
25+
Aliases: []string{"g"},
26+
Usage: "Generates various test templates",
27+
Subcommands: []*cli.Command{
28+
{
29+
Name: "load",
30+
Aliases: []string{"l"},
31+
Usage: "Generates a load/chaos test template for Kubernetes namespace",
32+
Description: `Scans a Kubernetes namespace and generates load testing templates for discovered services.
33+
34+
Prerequisites:
35+
36+
Connect to K8s and don't forget to switch context first:
37+
kubectl config use-context <your_ctx>
38+
By default test sends data to a local CTF stack, see //TODO comments to change that, spin up the stack:
39+
ctf obs up
40+
41+
Usage:
42+
43+
Generate basic kill/latency tests:
44+
ctf gen k8s-load my-namespace
45+
With workload:
46+
ctf gen k8s-load -w my-namespace
47+
With workload and name:
48+
ctf gen k8s-load -w -n TestSomething my-namespace
49+
50+
Be aware that any TODO requires your attention before your run the final test!
51+
`,
52+
ArgsUsage: "--workload --name $name --output-dir $dir --module $go_mod_name [NAMESPACE]",
53+
Flags: []cli.Flag{
54+
&cli.StringFlag{
55+
Name: "name",
56+
Aliases: []string{"n"},
57+
Value: "TestLoadChaos",
58+
Usage: "Test suite name",
59+
},
60+
&cli.StringFlag{
61+
Name: "output-dir",
62+
Aliases: []string{"o"},
63+
Value: "wasp-test",
64+
Usage: "Output directory for generated files",
65+
},
66+
&cli.StringFlag{
67+
Name: "module",
68+
Aliases: []string{"m"},
69+
Value: "github.com/smartcontractkit/chainlink-testing-framework/wasp-test",
70+
Usage: "Go module name for generated project",
71+
},
72+
&cli.BoolFlag{
73+
Name: "workload",
74+
Aliases: []string{"w"},
75+
Value: false,
76+
Usage: "Include workload generation in tests",
77+
},
78+
&cli.StringFlag{
79+
Name: "pod-label-key",
80+
Aliases: []string{"k"},
81+
Value: "app.kubernetes.io/instance",
82+
Usage: "Default unique pod key, read more here: https://kubernetes.io/docs/concepts/overview/working-with-objects/common-labels/",
83+
},
84+
},
85+
Action: func(c *cli.Context) error {
86+
if c.Args().Len() == 0 {
87+
return fmt.Errorf("Kubernetes namespace argument is required")
88+
}
89+
ns := c.Args().First()
90+
testSuiteName := c.String("name")
91+
podLabelKey := c.String("pod-label-key")
92+
outputDir := c.String("output-dir")
93+
moduleName := c.String("module")
94+
includeWorkload := c.Bool("workload")
95+
framework.L.Info().
96+
Str("SuiteName", testSuiteName).
97+
Str("OutputDir", outputDir).
98+
Str("GoModuleName", moduleName).
99+
Bool("Workload", includeWorkload).
100+
Msg("Generating load&chaos test template")
101+
102+
k8sClient, err := wasp.NewK8s()
103+
if err != nil {
104+
return fmt.Errorf("failed to create K8s client")
105+
}
106+
107+
cg, err := wasp.NewLoadTestGenBuilder(k8sClient, ns).
108+
TestSuiteName(testSuiteName).
109+
UniqPodLabelKey(podLabelKey).
110+
Workload(includeWorkload).
111+
OutputDir(outputDir).
112+
GoModName(moduleName).
113+
Build()
114+
if err != nil {
115+
return fmt.Errorf("failed to create codegen: %w", err)
116+
}
117+
if err := cg.Read(); err != nil {
118+
return fmt.Errorf("failed to scan namespace: %w", err)
119+
}
120+
if err := cg.Write(); err != nil {
121+
return fmt.Errorf("failed to generate module: %w", err)
122+
}
123+
return nil
124+
},
125+
},
126+
},
127+
},
22128
{
23129
Name: "config",
24130
Aliases: []string{"c"},
@@ -206,7 +312,7 @@ func PrettyPrintTOML(inputFile string, outputFile string) error {
206312
}
207313

208314
//nolint
209-
err = os.WriteFile(outputFile, []byte(dumpData), 0644)
315+
err = os.WriteFile(outputFile, []byte(dumpData), 0o644)
210316
if err != nil {
211317
return fmt.Errorf("error writing to output file: %v", err)
212318
}

0 commit comments

Comments
 (0)