Skip to content

Commit

Permalink
Expose pod termination fault in JS API
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Chacin <[email protected]>
  • Loading branch information
pablochacin committed Oct 25, 2023
1 parent 9878e8d commit 3a0d652
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,36 @@ func (p *jsProtocolFaultInjector) InjectGrpcFaults(args ...goja.Value) {
}
}

// jsPodFaultInjector implements methods for injecting faults into Pods
type jsPodFaultInjector struct {
ctx context.Context
rt *goja.Runtime
disruptors.PodFaultInjector
}

// TerminatePods is a proxy method. Validates parameters and delegates to the Pod Fault Injector method
func (p *jsPodFaultInjector) TerminatePods(args ...goja.Value) {
if len(args) == 0 {
common.Throw(p.rt, fmt.Errorf("PodTermination fault is required"))
}

fault := disruptors.PodTerminationFault{}
err := convertValue(p.rt, args[0], &fault)
if err != nil {
common.Throw(p.rt, fmt.Errorf("invalid fault argument: %w", err))
}

// TODO: return list of pods terminated
_, err = p.PodFaultInjector.TerminatePods(p.ctx, fault)
if err != nil {
common.Throw(p.rt, fmt.Errorf("error injecting fault: %w", err))
}
}

type jsPodDisruptor struct {
jsDisruptor
jsProtocolFaultInjector
jsPodFaultInjector
}

// buildJsPodDisruptor builds a goja object that implements the PodDisruptor API
Expand All @@ -150,6 +177,11 @@ func buildJsPodDisruptor(
rt: rt,
ProtocolFaultInjector: disruptor,
},
jsPodFaultInjector: jsPodFaultInjector{
ctx: ctx,
rt: rt,
PodFaultInjector: disruptor,
},
}

return buildObject(rt, d)
Expand All @@ -158,6 +190,7 @@ func buildJsPodDisruptor(
type jsServiceDisruptor struct {
jsDisruptor
jsProtocolFaultInjector
jsPodFaultInjector
}

// buildJsServiceDisruptor builds a goja object that implements the ServiceDisruptor API
Expand All @@ -177,6 +210,11 @@ func buildJsServiceDisruptor(
rt: rt,
ProtocolFaultInjector: disruptor,
},
jsPodFaultInjector: jsPodFaultInjector{
ctx: ctx,
rt: rt,
PodFaultInjector: disruptor,
},
}

return buildObject(rt, d)
Expand Down
41 changes: 41 additions & 0 deletions pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,47 @@ func Test_JsPodDisruptor(t *testing.T) {
`,
expectError: true,
},
{
description: "Terminate Pods (integer count)",
script: `
const fault = {
count: 1
}
d.terminatePods(fault)
`,
expectError: false,
},
{
description: "Terminate Pods (percentage count)",
script: `
const fault = {
count: '100%'
}
d.terminatePods(fault)
`,
expectError: false,
},
{
description: "Terminate Pods (invalid percentage count)",
script: `
const fault = {
count: '100'
}
d.terminatePods(fault)
`,
expectError: true,
},
{
description: "Terminate Pods (missing argument)",
script: `
d.terminatePods()
`,
expectError: true,
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 3a0d652

Please sign in to comment.