Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions pkg/cli/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ var validateCmd = &cobra.Command{
os.Exit(1)
}

if err := cfg.Validate(); err != nil {
pterm.Error.Printf("Config is invalid: %v\n", err)
os.Exit(1)
}

// LoadConfig implicitly calls Validate(), so no need to call it again.
pterm.Success.Println("Configuration is valid.")

fmt.Printf("\nSummary:\n")
Expand Down
19 changes: 17 additions & 2 deletions pkg/engine/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (m *ResourceChaosManager) ClearAll() {

type ActionHandler func(ctx context.Context, client ContainerRuntime, target string, spec config.ActionSpec) (*ContainerInfo, error)

var ActionHandlers = map[string]ActionHandler{
var actionHandlers = map[string]ActionHandler{
"stop": actionStop,
"restart": actionRestart,
"pause": actionPause,
Expand All @@ -58,6 +58,21 @@ var ActionHandlers = map[string]ActionHandler{
"limit_memory": actionLimitMemory,
}

// GetSupportedActions returns a list of all action names supported by the engine.
func GetSupportedActions() []string {
actions := make([]string, 0, len(actionHandlers))
for k := range actionHandlers {
actions = append(actions, k)
}
return actions
}

// GetActionHandler returns the handler for a given action name.
func GetActionHandler(name string) (ActionHandler, bool) {
handler, ok := actionHandlers[name]
return handler, ok
}

func actionStop(ctx context.Context, client ContainerRuntime, target string, spec config.ActionSpec) (*ContainerInfo, error) {
return client.StopContainer(ctx, target, 10)
}
Expand Down Expand Up @@ -110,7 +125,7 @@ func actionLimitMemory(ctx context.Context, client ContainerRuntime, target stri
}

func Dispatch(ctx context.Context, action config.ActionSpec, client ContainerRuntime, target string) (*ContainerInfo, error) {
handler, ok := ActionHandlers[action.Name]
handler, ok := actionHandlers[action.Name]
if !ok {
return nil, fmt.Errorf("unknown action '%s'", action.Name)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestActionHandlersMapExists(t *testing.T) {
}

for _, action := range expectedActions {
if _, ok := ActionHandlers[action]; !ok {
if _, ok := GetActionHandler(action); !ok {
t.Errorf("Action %q not found in ActionHandlers", action)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var metadataDenyList = []string{
// allowPrivateNetworks controls whether probes can reach private network ranges.
// In chaos engineering, probes legitimately target local containers, so private
// networks are allowed by default. Only cloud metadata endpoints are always blocked.
var allowPrivateNetworks = true
const allowPrivateNetworks = true

// validateProbeURL checks the target URL against the deny-list to prevent SSRF attacks.
// Cloud metadata endpoints (169.254.169.254) are always blocked.
Expand Down
4 changes: 2 additions & 2 deletions tests/engine/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestActionHandlersMapExists(t *testing.T) {
}

for _, action := range expectedActions {
if _, ok := engine.ActionHandlers[action]; !ok {
if _, ok := engine.GetActionHandler(action); !ok {
t.Errorf("Action %q not found in ActionHandlers", action)
}
}
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestDispatchWithValidAction(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler, ok := engine.ActionHandlers[tt.actionName]
handler, ok := engine.GetActionHandler(tt.actionName)

if tt.shouldExist && !ok {
t.Errorf("Action %q should exist in ActionHandlers", tt.actionName)
Expand Down
6 changes: 3 additions & 3 deletions tests/engine/chaos_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
func TestNewChaosEngine(t *testing.T) {
// Engine creation requires a real config, which needs proper setup
// For now, we test that action handlers exist
if len(engine.ActionHandlers) == 0 {
if len(engine.GetSupportedActions()) == 0 {
t.Error("ActionHandlers should not be empty")
}
}

func TestActionHandlersExist(t *testing.T) {
expectedCount := 7 // stop, restart, pause, delay, loss, limit_cpu, limit_memory
if len(engine.ActionHandlers) < expectedCount {
t.Errorf("Expected at least %d action handlers, got %d", expectedCount, len(engine.ActionHandlers))
if len(engine.GetSupportedActions()) < expectedCount {
t.Errorf("Expected at least %d action handlers, got %d", expectedCount, len(engine.GetSupportedActions()))
}
}

Expand Down
Loading