diff --git a/cli/cli.go b/cli/cli.go index 91ccdf61..e3115c01 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -9,8 +9,8 @@ import ( toxiproxyServer "github.com/Shopify/toxiproxy" "github.com/Shopify/toxiproxy/client" - "github.com/urfave/cli" - "golang.org/x/crypto/ssh/terminal" + "github.com/urfave/cli/v2" + terminal "golang.org/x/term" ) const ( @@ -67,15 +67,17 @@ var toxicDescription = ` example: toxiproxy-cli toxic delete myProxy -n myToxic ` -var hostname string -var isTTY bool +var ( + hostname string + isTTY bool +) func main() { app := cli.NewApp() app.Name = "toxiproxy-cli" app.Version = toxiproxyServer.Version app.Usage = "Simulate network and system conditions" - app.Commands = []cli.Command{ + app.Commands = []*cli.Command{ { Name: "list", Usage: "list all proxies\n\tusage: 'toxiproxy-cli list'\n", @@ -93,13 +95,15 @@ func main() { Usage: "create a new proxy\n\tusage: 'toxiproxy-cli create --listen --upstream '\n", Aliases: []string{"c", "new"}, Flags: []cli.Flag{ - cli.StringFlag{ - Name: "listen, l", - Usage: "proxy will listen on this address", + &cli.StringFlag{ + Name: "listen", + Aliases: []string{"l"}, + Usage: "proxy will listen on this address", }, - cli.StringFlag{ - Name: "upstream, u", - Usage: "proxy will forward to this address", + &cli.StringFlag{ + Name: "upstream", + Aliases: []string{"u"}, + Usage: "proxy will forward to this address", }, }, Action: withToxi(createProxy), @@ -121,36 +125,42 @@ func main() { Aliases: []string{"t"}, Usage: "\tadd, remove or update a toxic\n\t\tusage: see 'toxiproxy-cli toxic'\n", Description: toxicDescription, - Subcommands: []cli.Command{ + Subcommands: []*cli.Command{ { Name: "add", Aliases: []string{"a"}, Usage: "add a new toxic", ArgsUsage: "", Flags: []cli.Flag{ - cli.StringFlag{ - Name: "toxicName, n", - Usage: "name of the toxic", + &cli.StringFlag{ + Name: "toxicName", + Aliases: []string{"n"}, + Usage: "name of the toxic", }, - cli.StringFlag{ - Name: "type, t", - Usage: "type of toxic", + &cli.StringFlag{ + Name: "type", + Aliases: []string{"t"}, + Usage: "type of toxic", }, - cli.StringFlag{ - Name: "toxicity, tox", - Usage: "toxicity of toxic", + &cli.StringFlag{ + Name: "toxicity", + Aliases: []string{"tox"}, + Usage: "toxicity of toxic", }, - cli.StringSliceFlag{ - Name: "attribute, a", - Usage: "toxic attribute in key=value format", + &cli.StringSliceFlag{ + Name: "attribute", + Aliases: []string{"a"}, + Usage: "toxic attribute in key=value format", }, - cli.BoolFlag{ - Name: "upstream, u", - Usage: "add toxic to upstream", + &cli.BoolFlag{ + Name: "upstream", + Aliases: []string{"u"}, + Usage: "add toxic to upstream", }, - cli.BoolFlag{ - Name: "downstream, d", - Usage: "add toxic to downstream", + &cli.BoolFlag{ + Name: "downstream", + Aliases: []string{"d"}, + Usage: "add toxic to downstream", }, }, Action: withToxi(addToxic), @@ -161,17 +171,20 @@ func main() { Usage: "update an enabled toxic", ArgsUsage: "", Flags: []cli.Flag{ - cli.StringFlag{ - Name: "toxicName, n", - Usage: "name of the toxic", + &cli.StringFlag{ + Name: "toxicName", + Aliases: []string{"n"}, + Usage: "name of the toxic", }, - cli.StringFlag{ - Name: "toxicity, tox", - Usage: "toxicity of toxic", + &cli.StringFlag{ + Name: "toxicity", + Aliases: []string{"tox"}, + Usage: "toxicity of toxic", }, - cli.StringSliceFlag{ - Name: "attribute, a", - Usage: "toxic attribute in key=value format", + &cli.StringSliceFlag{ + Name: "attribute", + Aliases: []string{"a"}, + Usage: "toxic attribute in key=value format", }, }, Action: withToxi(updateToxic), @@ -182,9 +195,10 @@ func main() { Usage: "remove an enabled toxic", ArgsUsage: "", Flags: []cli.Flag{ - cli.StringFlag{ - Name: "toxicName, n", - Usage: "name of the toxic", + &cli.StringFlag{ + Name: "toxicName", + Aliases: []string{"n"}, + Usage: "name of the toxic", }, }, Action: withToxi(removeToxic), @@ -192,13 +206,14 @@ func main() { }, }, } - cli.HelpFlag = cli.BoolFlag{ + cli.HelpFlag = &cli.BoolFlag{ Name: "help", Usage: "show help", } app.Flags = []cli.Flag{ - cli.StringFlag{ - Name: "host, h", + &cli.StringFlag{ + Name: "host", + Aliases: []string{"h"}, Value: "http://localhost:8474", Usage: "toxiproxy host to connect to", Destination: &hostname, @@ -371,7 +386,7 @@ func deleteProxy(c *cli.Context, t *toxiproxy.Client) error { } func parseToxicity(c *cli.Context, defaultToxicity float32) (float32, error) { - var toxicity = defaultToxicity + toxicity := defaultToxicity toxicityString := c.String("toxicity") if toxicityString != "" { tox, err := strconv.ParseFloat(toxicityString, 32) @@ -587,7 +602,7 @@ func hint(m string) { } func errorf(m string, args ...interface{}) error { - return cli.NewExitError(fmt.Sprintf(m, args...), 1) + return cli.Exit(fmt.Sprintf(m, args...), 1) } func printWidth(col string, m string, numTabs int) { diff --git a/client/client.go b/client/client.go index f061ce97..ceb61bfc 100644 --- a/client/client.go +++ b/client/client.go @@ -46,7 +46,7 @@ type Proxy struct { // NewClient creates a new client which provides the base of all communication // with Toxiproxy. Endpoint is the address to the proxy (e.g. localhost:8474 if -// not overriden) +// not overridden) func NewClient(endpoint string) *Client { if strings.HasPrefix(endpoint, "https://") { log.Fatal("the toxiproxy client does not support https") @@ -215,7 +215,6 @@ func (proxy *Proxy) Disable() error { func (proxy *Proxy) Delete() error { httpClient := &http.Client{} req, err := http.NewRequest("DELETE", proxy.client.endpoint+"/proxies/"+proxy.Name, nil) - if err != nil { return err } diff --git a/cmd/toxiproxy.go b/cmd/toxiproxy.go index 5fb5b8e0..a1b5de75 100644 --- a/cmd/toxiproxy.go +++ b/cmd/toxiproxy.go @@ -11,9 +11,11 @@ import ( "github.com/Shopify/toxiproxy" ) -var host string -var port string -var config string +var ( + host string + port string + config string +) func init() { flag.StringVar(&host, "host", "localhost", "Host for toxiproxy's API to listen on") diff --git a/go.mod b/go.mod index 4322b1ce..82f91629 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,11 @@ module github.com/Shopify/toxiproxy -go 1.12 +go 1.15 require ( - github.com/gorilla/mux v1.7.2 - github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect - github.com/sirupsen/logrus v1.4.2 - github.com/stretchr/testify v1.3.0 // indirect - github.com/urfave/cli v1.20.0 - golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 - golang.org/x/sys v0.0.0-20190606165138-5da285871e9c // indirect + github.com/gorilla/mux v1.8.0 + github.com/sirupsen/logrus v1.8.1 + github.com/urfave/cli/v2 v2.3.0 + golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 ) diff --git a/go.sum b/go.sum index 11fd227e..b436cbd0 100644 --- a/go.sum +++ b/go.sum @@ -1,31 +1,28 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gorilla/mux v1.7.2 h1:zoNxOV7WjqXptQOVngLmcSQgXmgk4NMz1HibBchjl/I= -github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= -github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= -github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 h1:58fnuSXlxZmFdJyvtTFVmVhcMLU6v5fEb/ok4wyqtNU= -golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190606165138-5da285871e9c h1:+EXw7AwNOKzPFXMZ1yNjO40aWCh3PIquJB2fYlv9wcs= -golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M= +github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/proxy.go b/proxy.go index 1d165053..6c358f85 100644 --- a/proxy.go +++ b/proxy.go @@ -2,13 +2,12 @@ package toxiproxy import ( "errors" + "net" "sync" "github.com/Shopify/toxiproxy/stream" "github.com/sirupsen/logrus" tomb "gopkg.in/tomb.v1" - - "net" ) // Proxy represents the proxy in its entirity with all its links. The main diff --git a/proxy_collection.go b/proxy_collection.go index 3070f5e7..8f9ab001 100644 --- a/proxy_collection.go +++ b/proxy_collection.go @@ -8,7 +8,7 @@ import ( ) // ProxyCollection is a collection of proxies. It's the interface for anything -// to add and remove proxies from the toxiproxy instance. It's responsibilty is +// to add and remove proxies from the toxiproxy instance. It's responsibility is // to maintain the integrity of the proxy set, by guarding for things such as // duplicate names. type ProxyCollection struct { @@ -79,27 +79,27 @@ func (collection *ProxyCollection) PopulateJson(data io.Reader) ([]*Proxy, error // Check for valid input before creating any proxies t := true - for i, p := range input { - if len(p.Name) < 1 { + for i := range input { + if len(input[i].Name) < 1 { return nil, joinError(fmt.Errorf("name at proxy %d", i+1), ErrMissingField) } - if len(p.Upstream) < 1 { + if len(input[i].Upstream) < 1 { return nil, joinError(fmt.Errorf("upstream at proxy %d", i+1), ErrMissingField) } - if p.Enabled == nil { + if input[i].Enabled == nil { input[i].Enabled = &t } } proxies := make([]*Proxy, 0, len(input)) - for _, p := range input { + for i := range input { proxy := NewProxy() - proxy.Name = p.Name - proxy.Listen = p.Listen - proxy.Upstream = p.Upstream + proxy.Name = input[i].Name + proxy.Listen = input[i].Listen + proxy.Upstream = input[i].Upstream - err = collection.AddOrReplace(proxy, *p.Enabled) + err = collection.AddOrReplace(proxy, *input[i].Enabled) if err != nil { break } diff --git a/proxy_test.go b/proxy_test.go index 78a7d9b5..212b542f 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -46,7 +46,8 @@ func WithTCPServer(t *testing.T, f func(string, chan []byte)) { select { case <-tomb.Dying(): default: - t.Fatal("Failed to accept client") + t.Error("Failed to accept client") + return } return } @@ -55,7 +56,8 @@ func WithTCPServer(t *testing.T, f func(string, chan []byte)) { val, err := ioutil.ReadAll(src) if err != nil { - t.Fatal("Failed to read from client") + t.Error("Failed to read from client") + return } response <- val diff --git a/stream/io_chan.go b/stream/io_chan.go index 4038c32e..7229035e 100644 --- a/stream/io_chan.go +++ b/stream/io_chan.go @@ -14,7 +14,7 @@ const ( NumDirections ) -// Stores a slice of bytes with its receive timestmap +// Stores a slice of bytes with its receive timestamp type StreamChunk struct { Data []byte Timestamp time.Time diff --git a/testhelper/testhelper.go b/testhelper/testhelper.go index 0801bf94..85963ea9 100644 --- a/testhelper/testhelper.go +++ b/testhelper/testhelper.go @@ -1,8 +1,8 @@ package testhelper import ( + "fmt" "time" - "fmt" ) func TimeoutAfter(after time.Duration, f func()) error { diff --git a/testhelper/testhelper_test.go b/testhelper/testhelper_test.go index b6f1762e..850981cf 100644 --- a/testhelper/testhelper_test.go +++ b/testhelper/testhelper_test.go @@ -1,20 +1,20 @@ package testhelper import ( - "testing" - "time" + "testing" + "time" ) func TestTimeoutAfter(t *testing.T) { - err := TimeoutAfter(5*time.Millisecond, func() {}) - if err != nil { - t.Fatal("Non blocking function should not timeout.") - } + err := TimeoutAfter(5*time.Millisecond, func() {}) + if err != nil { + t.Fatal("Non blocking function should not timeout.") + } - err = TimeoutAfter(5*time.Millisecond, func() { - time.Sleep(time.Second) - }) - if err == nil { - t.Fatal("Blocking function should timeout.") - } + err = TimeoutAfter(5*time.Millisecond, func() { + time.Sleep(time.Second) + }) + if err == nil { + t.Fatal("Blocking function should timeout.") + } } diff --git a/testing/endpoint.go b/testing/endpoint.go index a96506e2..8696bef1 100644 --- a/testing/endpoint.go +++ b/testing/endpoint.go @@ -6,9 +6,11 @@ import ( "net/http" ) -var stuff []byte -var out []byte -var out2 []byte +var ( + stuff []byte + out []byte + out2 []byte +) func handler1(w http.ResponseWriter, r *http.Request) { n, err := w.Write(out) diff --git a/toxics/bandwidth.go b/toxics/bandwidth.go index 6c4624a9..f0dac05d 100644 --- a/toxics/bandwidth.go +++ b/toxics/bandwidth.go @@ -32,7 +32,10 @@ func (t *BandwidthToxic) Pipe(stub *ToxicStub) { for int64(len(p.Data)) > t.Rate*100 { select { case <-time.After(100 * time.Millisecond): - stub.Output <- &stream.StreamChunk{p.Data[:t.Rate*100], p.Timestamp} + stub.Output <- &stream.StreamChunk{ + Data: p.Data[:t.Rate*100], + Timestamp: p.Timestamp, + } p.Data = p.Data[t.Rate*100:] sleep -= 100 * time.Millisecond case <-stub.Interrupt: diff --git a/toxics/bandwidth_test.go b/toxics/bandwidth_test.go index 6a3cf8d5..95631c71 100644 --- a/toxics/bandwidth_test.go +++ b/toxics/bandwidth_test.go @@ -57,7 +57,7 @@ func TestBandwidthToxic(t *testing.T) { _, err = io.ReadAtLeast(serverConn, buf2, len(buf2)) if err != nil { t.Errorf("Proxy read failed: %v", err) - } else if bytes.Compare(buf, buf2) != 0 { + } else if !bytes.Equal(buf, buf2) { t.Errorf("Server did not read correct buffer from client!") } diff --git a/toxics/latency.go b/toxics/latency.go index 2baf549b..a56370af 100644 --- a/toxics/latency.go +++ b/toxics/latency.go @@ -19,8 +19,9 @@ func (t *LatencyToxic) GetBufferSize() int { func (t *LatencyToxic) delay() time.Duration { // Delay = t.Latency +/- t.Jitter delay := t.Latency - jitter := int64(t.Jitter) + jitter := t.Jitter if jitter > 0 { + //#nosec delay += rand.Int63n(jitter*2) - jitter } return time.Duration(delay) * time.Millisecond diff --git a/toxics/latency_test.go b/toxics/latency_test.go index b5c41b87..5da5384a 100644 --- a/toxics/latency_test.go +++ b/toxics/latency_test.go @@ -156,7 +156,7 @@ func TestLatencyToxicCloseRace(t *testing.T) { func TestTwoLatencyToxics(t *testing.T) { WithEchoProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) { - toxics := []*toxics.LatencyToxic{&toxics.LatencyToxic{Latency: 500}, &toxics.LatencyToxic{Latency: 500}} + toxics := []*toxics.LatencyToxic{{Latency: 500}, {Latency: 500}} for i, toxic := range toxics { _, err := proxy.Toxics.AddToxicJson(ToxicToJson(t, "latency_"+strconv.Itoa(i), "latency", "upstream", toxic)) if err != nil { diff --git a/toxics/limit_data.go b/toxics/limit_data.go index 5384fac7..217dcddf 100644 --- a/toxics/limit_data.go +++ b/toxics/limit_data.go @@ -13,7 +13,7 @@ type LimitDataToxicState struct { func (t *LimitDataToxic) Pipe(stub *ToxicStub) { state := stub.State.(*LimitDataToxicState) - var bytesRemaining = t.Bytes - state.bytesTransmitted + bytesRemaining := t.Bytes - state.bytesTransmitted for { select { diff --git a/toxics/slicer.go b/toxics/slicer.go index 7bb83a64..dd4e05a6 100644 --- a/toxics/slicer.go +++ b/toxics/slicer.go @@ -8,7 +8,7 @@ import ( ) // The SlicerToxic slices data into multiple smaller packets -// to simulate real-world TCP behaviour. +// to simulate real-world TCP behavior. type SlicerToxic struct { // Average number of bytes to slice at AverageSize int `json:"average_size"` @@ -39,6 +39,7 @@ func (t *SlicerToxic) chunk(start int, end int) []int { // +1 in the size variation to offset favoring of smaller // numbers by integer division + //#nosec mid := start + (end-start)/2 + (rand.Intn(t.SizeVariation*2) - t.SizeVariation) + rand.Intn(1) left := t.chunk(start, mid) right := t.chunk(mid, end) diff --git a/toxics/slicer_test.go b/toxics/slicer_test.go index 6be1da19..b457be77 100644 --- a/toxics/slicer_test.go +++ b/toxics/slicer_test.go @@ -52,7 +52,7 @@ L: if reads < 480/2 || reads > 480/2+480 { t.Errorf("Expected to read about 480 times, but read %d times.", reads) } - if bytes.Compare(buf, data) != 0 { + if !bytes.Equal(buf, data) { t.Errorf("Server did not read correct buffer from client!") } } diff --git a/toxics/toxic.go b/toxics/toxic.go index bccd5f88..5d66ac29 100644 --- a/toxics/toxic.go +++ b/toxics/toxic.go @@ -78,6 +78,7 @@ func NewToxicStub(input <-chan *stream.StreamChunk, output chan<- *stream.Stream func (s *ToxicStub) Run(toxic *ToxicWrapper) { s.running = make(chan struct{}) defer close(s.running) + //#nosec if rand.Float32() < toxic.Toxicity { toxic.Pipe(s) } else { @@ -113,8 +114,10 @@ func (s *ToxicStub) Close() { } } -var ToxicRegistry map[string]Toxic -var registryMutex sync.RWMutex +var ( + ToxicRegistry map[string]Toxic + registryMutex sync.RWMutex +) func Register(typeName string, toxic Toxic) { registryMutex.Lock() diff --git a/toxics/toxic_test.go b/toxics/toxic_test.go index 2f5d87d6..93b25295 100644 --- a/toxics/toxic_test.go +++ b/toxics/toxic_test.go @@ -48,7 +48,8 @@ func WithEchoServer(t *testing.T, f func(string, chan []byte)) { select { case <-tomb.Dying(): default: - t.Fatal("Failed to accept client") + t.Error("Failed to accept client") + return } return }