• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rom8726 / chaoskit / 19555280984

21 Nov 2025 12:05AM UTC coverage: 44.974% (-0.6%) from 45.584%
19555280984

push

github

rom8726
Introduce enhanced chaos testing framework with new APIs, options, and examples.

0 of 78 new or added lines in 1 file covered. (0.0%)

4 existing lines in 2 files now uncovered.

2049 of 4556 relevant lines covered (44.97%)

745.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/testing/testing.go
1
package testing
2

3
import (
4
        "context"
5
        "fmt"
6

7
        "github.com/rom8726/chaoskit"
8
)
9

10
// TestingT is an interface that matches testing.T and similar types
11
type TestingT interface {
12
        Errorf(format string, args ...interface{})
13
        FailNow()
14
        Helper()
15
}
16

17
// ChaosTestOption configures chaos testing
18
type ChaosTestOption func(*chaosTestConfig)
19

20
type chaosTestConfig struct {
21
        repeat         int
22
        failurePolicy  chaoskit.FailurePolicy
23
        executorOpts   []chaoskit.ExecutorOption
24
        skipReport     bool
25
        reportToStderr bool
26
}
27

28
// WithRepeat sets the number of times to repeat the test scenario
NEW
29
func WithRepeat(n int) ChaosTestOption {
×
NEW
30
        return func(c *chaosTestConfig) {
×
NEW
31
                c.repeat = n
×
NEW
32
        }
×
33
}
34

35
// WithFailurePolicy sets how to handle failures (FailFast or ContinueOnFailure)
NEW
36
func WithFailurePolicy(policy chaoskit.FailurePolicy) ChaosTestOption {
×
NEW
37
        return func(c *chaosTestConfig) {
×
NEW
38
                c.failurePolicy = policy
×
NEW
39
        }
×
40
}
41

42
// WithExecutorOptions passes options to the underlying executor
NEW
43
func WithExecutorOptions(opts ...chaoskit.ExecutorOption) ChaosTestOption {
×
NEW
44
        return func(c *chaosTestConfig) {
×
NEW
45
                c.executorOpts = append(c.executorOpts, opts...)
×
NEW
46
        }
×
47
}
48

49
// WithoutReport skips printing the report after execution
NEW
50
func WithoutReport() ChaosTestOption {
×
NEW
51
        return func(c *chaosTestConfig) {
×
NEW
52
                c.skipReport = true
×
NEW
53
        }
×
54
}
55

56
// WithReportToStderr prints the report to stderr instead of stdout
NEW
57
func WithReportToStderr() ChaosTestOption {
×
NEW
58
        return func(c *chaosTestConfig) {
×
NEW
59
                c.reportToStderr = true
×
NEW
60
        }
×
61
}
62

63
// WithChaos creates a chaos test function that uses the full ChaosKit framework.
64
// It creates a scenario using ScenarioBuilder, runs it with an Executor, and validates results.
65
//
66
// The builderFn receives a pre-initialized ScenarioBuilder with the target already set.
67
// You should add steps, injectors, and validators to the builder.
68
//
69
// Usage:
70
//
71
//        func TestWithChaos(t *testing.T) {
72
//            target := &MyTarget{}
73
//
74
//            chaoskit.WithChaos(t, "name", target, func(s *chaoskit.ScenarioBuilder) *chaoskit.ScenarioBuilder {
75
//                return s.
76
//                    Step("step1", func(ctx context.Context, target chaoskit.Target) error {
77
//                        // Your test logic
78
//                        return DoSomething()
79
//                    }).
80
//                    Inject("delay", injectors.RandomDelay(10*time.Millisecond, 50*time.Millisecond)).
81
//                    Assert("goroutines", validators.GoroutineLimit(100))
82
//            }, WithRepeat(10))()
83
//        }
84
func WithChaos(
85
        t TestingT,
86
        name string,
87
        target chaoskit.Target,
88
        builderFn func(*chaoskit.ScenarioBuilder) *chaoskit.ScenarioBuilder,
89
        opts ...ChaosTestOption,
NEW
90
) func() {
×
NEW
91
        return func() {
×
NEW
92
                t.Helper()
×
93

×
NEW
94
                // Apply options
×
NEW
95
                config := &chaosTestConfig{
×
NEW
96
                        repeat:        1,
×
NEW
97
                        failurePolicy: chaoskit.FailFast,
×
NEW
98
                }
×
NEW
99
                for _, opt := range opts {
×
NEW
100
                        opt(config)
×
UNCOV
101
                }
×
102

103
                // Create scenario builder
NEW
104
                builder := chaoskit.NewScenario(name).WithTarget(target)
×
NEW
105

×
NEW
106
                // Let user configure the scenario
×
NEW
107
                builder = builderFn(builder)
×
NEW
108

×
NEW
109
                // Set repeat count
×
NEW
110
                builder = builder.Repeat(config.repeat)
×
NEW
111

×
NEW
112
                // Build scenario
×
NEW
113
                scenario := builder.Build()
×
NEW
114

×
NEW
115
                // Create executor with options
×
NEW
116
                executorOpts := append(
×
NEW
117
                        []chaoskit.ExecutorOption{chaoskit.WithFailurePolicy(config.failurePolicy)},
×
NEW
118
                        config.executorOpts...,
×
NEW
119
                )
×
NEW
120
                executor := chaoskit.NewExecutor(executorOpts...)
×
NEW
121

×
NEW
122
                // Run scenario
×
NEW
123
                ctx := context.Background()
×
NEW
124
                if err := executor.Run(ctx, scenario); err != nil {
×
NEW
125
                        t.Errorf("chaos test failed: %v", err)
×
NEW
126

×
NEW
127
                        // Print report on failure
×
NEW
128
                        if !config.skipReport {
×
NEW
129
                                report := executor.Reporter().GenerateReport()
×
NEW
130
                                if logger, ok := t.(interface{ Logf(string, ...interface{}) }); ok {
×
NEW
131
                                        logger.Logf("\n%s", report)
×
UNCOV
132
                                }
×
133
                        }
134

NEW
135
                        t.FailNow()
×
NEW
136
                        return
×
137
                }
138

139
                // Print report on success (if not skipped)
NEW
140
                if !config.skipReport {
×
NEW
141
                        report := executor.Reporter().GenerateReport()
×
NEW
142
                        if logger, ok := t.(interface{ Logf(string, ...interface{}) }); ok {
×
NEW
143
                                logger.Logf("\n%s", report)
×
NEW
144
                        }
×
145
                }
146
        }
147
}
148

149
// WithChaosSimple is a simplified version that takes steps, injectors, and validators directly.
150
// This is useful when you don't need the full builder flexibility.
151
//
152
// Usage:
153
//
154
//        func TestSimpleChaos(t *testing.T) {
155
//            target := &MyTarget{}
156
//            steps := []chaoskit.StepFunc{
157
//                func(ctx context.Context, target chaoskit.Target) error {
158
//                    return DoSomething()
159
//                },
160
//            }
161
//            injectors := []chaoskit.Injector{
162
//                injectors.RandomDelay(10*time.Millisecond, 50*time.Millisecond),
163
//            }
164
//            validators := []chaoskit.Validator{
165
//                validators.GoroutineLimit(100),
166
//            }
167
//
168
//            chaoskit.WithChaosSimple(t, name, target, steps, injectors, validators, WithRepeat(10))()
169
//        }
170
func WithChaosSimple(
171
        t TestingT,
172
        name string,
173
        target chaoskit.Target,
174
        steps []func(context.Context, chaoskit.Target) error,
175
        injectors []chaoskit.Injector,
176
        validators []chaoskit.Validator,
177
        opts ...ChaosTestOption,
NEW
178
) func() {
×
NEW
179
        return WithChaos(t, name, target, func(s *chaoskit.ScenarioBuilder) *chaoskit.ScenarioBuilder {
×
NEW
180
                // Add steps
×
NEW
181
                for i, stepFn := range steps {
×
NEW
182
                        s = s.Step(fmt.Sprintf("step-%d", i+1), stepFn)
×
NEW
183
                }
×
184

185
                // Add injectors
NEW
186
                for i, inj := range injectors {
×
NEW
187
                        s = s.Inject(fmt.Sprintf("injector-%d", i+1), inj)
×
NEW
188
                }
×
189

190
                // Add validators
NEW
191
                for i, val := range validators {
×
NEW
192
                        s = s.Assert(fmt.Sprintf("validator-%d", i+1), val)
×
NEW
193
                }
×
194

NEW
195
                return s
×
196
        }, opts...)
197
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc