-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.go
100 lines (77 loc) · 1.86 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package ddosy
import (
"fmt"
"time"
)
func ValidateScheduleRequestWeb(req ScheduleRequestWeb) error {
if req.Endpoint == "" {
return fmt.Errorf("must provide endpoint")
}
if len(req.TrafficPatterns) == 0 {
return fmt.Errorf("must provide at least 1 traffic pattern")
}
for _, p := range req.TrafficPatterns {
if err := ValidateTrafficPatternWeb(p); err != nil {
return err
}
}
if len(req.LoadPatterns) == 0 {
return fmt.Errorf("must provide at least 1 load pattern")
}
for _, p := range req.LoadPatterns {
if err := ValidateLoadPatternWeb(p); err != nil {
return err
}
}
return nil
}
func ValidateTrafficPatternWeb(pattern TrafficPatternWeb) error {
if pattern.Weight <= 0 {
return fmt.Errorf("invalid traffic weight")
}
return nil
}
func ValidateLoadPatternWeb(pattern LoadPatternWeb) error {
duration, err := time.ParseDuration(pattern.Duration)
if err != nil {
return err
}
if duration.Seconds() < 0 {
return fmt.Errorf("load duration must be > 0s")
}
if pattern.Linear != nil {
return ValidateLinearLoadWeb(*pattern.Linear)
}
if pattern.Sine != nil {
return ValidateSineLoadWeb(*pattern.Sine)
}
return fmt.Errorf("must define linear or sine pattern")
}
func ValidateLinearLoadWeb(load LinearLoadWeb) error {
if load.StartRate < 0 {
return fmt.Errorf("start rate cannot be less then 0")
}
if load.EndRate < 0 {
return fmt.Errorf("end rate cannot be less then 0")
}
return nil
}
func ValidateSineLoadWeb(load SineLoadWeb) error {
period, err := time.ParseDuration(load.Period)
if err != nil {
return err
}
if period.Seconds() <= 0 {
return fmt.Errorf("period must be > 0")
}
if load.Mean <= 0 {
return fmt.Errorf("mean must be > 0")
}
if load.Amp < 0 {
return fmt.Errorf("amplitude must be >= 0")
}
if load.Amp >= load.Mean {
return fmt.Errorf("amplitude must be less then mean")
}
return nil
}