diff --git a/core/types/hammer_test.go b/core/types/hammer_test.go index 44a146fb..857697ee 100644 --- a/core/types/hammer_test.go +++ b/core/types/hammer_test.go @@ -235,7 +235,7 @@ func TestHammerEmptyScenarioItemID(t *testing.T) { }, } if err := h.Validate(); err == nil { - t.Errorf("TestHammerInvalidScenarioItemID errored") + t.Errorf("1- TestHammerEmptyScenarioItemID should be errored") } // Multi Scenario @@ -254,7 +254,34 @@ func TestHammerEmptyScenarioItemID(t *testing.T) { }, } if err := h.Validate(); err == nil { - t.Errorf("TestHammerInvalidScenarioItemID errored") + t.Errorf("2- TestHammerEmptyScenarioItemID should be errored") + } +} + +func TestHammerDuplicateScenarioItemID(t *testing.T) { + // Single Scenario + h := newDummyHammer() + h.Scenario = Scenario{ + Scenario: []ScenarioItem{ + { + ID: 1, + Protocol: SupportedProtocols[0], + Method: supportedProtocolMethods["HTTP"][1], + }, + { + ID: 2, + Protocol: SupportedProtocols[0], + Method: supportedProtocolMethods["HTTP"][1], + }, + { + ID: 2, + Protocol: SupportedProtocols[0], + Method: supportedProtocolMethods["HTTP"][1], + }, + }, + } + if err := h.Validate(); err == nil { + t.Errorf("TestHammerDuplicateScenarioItemID should be errored") } } diff --git a/core/types/scenario.go b/core/types/scenario.go index 08c35e1b..875511be 100644 --- a/core/types/scenario.go +++ b/core/types/scenario.go @@ -69,10 +69,16 @@ type Scenario struct { } func (s *Scenario) validate() error { + stepIds := make(map[int16]struct{}, len(s.Scenario)) for _, si := range s.Scenario { if err := si.validate(); err != nil { return err } + + if _, ok := stepIds[si.ID]; ok { + return fmt.Errorf("duplicate step id: %d", si.ID) + } + stepIds[si.ID] = struct{}{} } return nil } @@ -132,7 +138,7 @@ func (si *ScenarioItem) validate() error { return fmt.Errorf("unsupported Authentication Method (%s) For Protocol (%s) ", si.Auth.Type, si.Protocol) } if si.ID == 0 { - return fmt.Errorf("each scenario item should have an unique ID") + return fmt.Errorf("step ID should be greater than zero") } if si.Sleep != "" { sleep := strings.Split(si.Sleep, "-")