Skip to content

Commit 9ca4510

Browse files
committed
Add sanity limits
1 parent b170ca9 commit 9ca4510

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

cli/cli.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ func main() {
3939

4040
url = flag.Args()[0]
4141

42-
test := goad.NewTest(&goad.TestConfig{
42+
test, testerr := goad.NewTest(&goad.TestConfig{
4343
URL: url,
4444
Concurrency: concurrency,
4545
TotalRequests: requests,
4646
RequestTimeout: time.Duration(timeout) * time.Second,
4747
Region: region,
4848
})
49+
if testerr != nil {
50+
fmt.Println(testerr)
51+
os.Exit(1)
52+
}
4953

5054
var finalResult queue.RegionsAggData
5155
defer printSummary(&finalResult)

goad.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goad
22

33
import (
4+
"errors"
45
"fmt"
56
"log"
67
"strings"
@@ -13,6 +14,7 @@ import (
1314
"github.com/gophergala2016/goad/queue"
1415
)
1516

17+
// TestConfig type
1618
type TestConfig struct {
1719
URL string
1820
Concurrency uint
@@ -21,18 +23,27 @@ type TestConfig struct {
2123
Region string
2224
}
2325

26+
const nano = 1000000000
27+
2428
func (c *TestConfig) cmd(sqsURL string) string {
2529
return fmt.Sprintf("./goad-lambda %s %d %d %s %s", c.URL, c.Concurrency, c.TotalRequests, sqsURL, c.Region)
2630
}
2731

32+
// Test type
2833
type Test struct {
2934
config *TestConfig
3035
}
3136

32-
func NewTest(config *TestConfig) *Test {
33-
return &Test{config}
37+
// NewTest returns a configured Test
38+
func NewTest(config *TestConfig) (*Test, error) {
39+
err := config.check()
40+
if err != nil {
41+
return nil, err
42+
}
43+
return &Test{config}, nil
3444
}
3545

46+
// Start a test
3647
func (t *Test) Start() <-chan queue.RegionsAggData {
3748
awsConfig := aws.NewConfig().WithRegion(t.config.Region)
3849
infra, err := infrastructure.New(awsConfig)
@@ -64,3 +75,16 @@ func (t *Test) invokeLambda(awsConfig *aws.Config, sqsURL string) {
6475
})
6576
fmt.Println(resp, err)
6677
}
78+
79+
func (c TestConfig) check() error {
80+
if c.Concurrency < 1 || c.Concurrency > 100000 {
81+
return errors.New("Invalid concurrency (use 1 - 100000)")
82+
}
83+
if c.TotalRequests < 1 || c.TotalRequests > 1000000 {
84+
return errors.New("Invalid total requests (use 1 - 1000000)")
85+
}
86+
if c.RequestTimeout.Nanoseconds() < nano || c.RequestTimeout.Nanoseconds() > nano*100 {
87+
return errors.New("Invalid timeout (1s - 100s)")
88+
}
89+
return nil
90+
}

webapi/webapi.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/json"
55
"flag"
6+
"fmt"
67
"log"
78
"net/http"
89
"strconv"
@@ -85,7 +86,11 @@ func serveResults(w http.ResponseWriter, r *http.Request) {
8586
"eu-west-1",
8687
}
8788

88-
test := goad.NewTest(&config)
89+
test, testerr := goad.NewTest(&config)
90+
if testerr != nil {
91+
fmt.Println(testerr)
92+
return
93+
}
8994
resultChan := test.Start()
9095

9196
for result := range resultChan {

0 commit comments

Comments
 (0)