Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renovations #294

Merged
merged 3 commits into from
Aug 11, 2021
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
109 changes: 62 additions & 47 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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",
Expand All @@ -93,13 +95,15 @@ func main() {
Usage: "create a new proxy\n\tusage: 'toxiproxy-cli create <proxyName> --listen <addr> --upstream <addr>'\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),
Expand All @@ -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: "<proxyName>",
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),
Expand All @@ -161,17 +171,20 @@ func main() {
Usage: "update an enabled toxic",
ArgsUsage: "<proxyName>",
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),
Expand All @@ -182,23 +195,25 @@ func main() {
Usage: "remove an enabled toxic",
ArgsUsage: "<proxyName>",
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),
},
},
},
}
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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 5 additions & 3 deletions cmd/toxiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
45 changes: 21 additions & 24 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
3 changes: 1 addition & 2 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions proxy_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stream/io_chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading