Skip to content

Commit

Permalink
Support a list of well knonwn linters
Browse files Browse the repository at this point in the history
Base on some examples picked few more linters.
  • Loading branch information
miry committed Sep 17, 2021
1 parent b27b226 commit 53f4f36
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 50 deletions.
30 changes: 28 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,41 @@ linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- exhaustive
- funlen
- gocritic
- gocyclo
- godot
- gofmt
- goimports
- gosec
- gosimple
- govet
- ineffassign
- lll
- misspell

- staticcheck
- structcheck
- typecheck
- unused
- varcheck
- whitespace
fast: false
linters-settings:
gosec:
excludes:
- G107

funlen:
lines: 80
statements: 30

lll:
line-length: 100
tab-width: 1
tab-width: 2

misspell:
locale: US
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Introduce a new way to manage toxics with `ToxicOptions` sturcture (#321, @miry)
* Split `Proxy.server` to multiple small (#322, @miry)
* Extract initializetion of fake upstream server to test helper (#323, @miry)
* Support a list of well knonwn linters (#326, @miry)
* `--host` flag uses `TOXIPROXY_URL` if it is set (#319, @maaslalani)
* Run benchmarks in CI/CD (#320, @miry)
* Use scratch docker base image instead of alpine (#325, @miry)
Expand Down
59 changes: 43 additions & 16 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ func TestPopulateProxy(t *testing.T) {

if err != nil {
t.Fatal("Unable to populate:", err)
} else if len(testProxies) != 2 {
}

if len(testProxies) != 2 {
t.Fatalf("Wrong number of proxies returned: %d != 2", len(testProxies))
} else if testProxies[0].Name != "one" || testProxies[1].Name != "two" {
}

if testProxies[0].Name != "one" || testProxies[1].Name != "two" {
t.Fatalf("Wrong proxy names returned: %s, %s", testProxies[0].Name, testProxies[1].Name)
}

Expand Down Expand Up @@ -196,9 +200,13 @@ func TestPopulateDisabledProxy(t *testing.T) {

if err != nil {
t.Fatal("Unable to populate:", err)
} else if len(testProxies) != 2 {
}

if len(testProxies) != 2 {
t.Fatalf("Wrong number of proxies returned: %d != 2", len(testProxies))
} else if testProxies[0].Name != "one" || testProxies[1].Name != "two" {
}

if testProxies[0].Name != "one" || testProxies[1].Name != "two" {
t.Fatalf("Wrong proxy names returned: %s, %s", testProxies[0].Name, testProxies[1].Name)
}

Expand Down Expand Up @@ -227,7 +235,7 @@ func TestPopulateExistingProxy(t *testing.T) {
testProxies, err := client.Populate([]tclient.Proxy{
{
Name: "one",
Listen: "127.0.0.1:7070", // TODO(xthexder): Will replace existing proxy if not resolved ip...
Listen: "127.0.0.1:7070",
Upstream: "localhost:7171",
Enabled: true,
},
Expand All @@ -241,11 +249,18 @@ func TestPopulateExistingProxy(t *testing.T) {

if err != nil {
t.Fatal("Unable to populate:", err)
} else if len(testProxies) != 2 {
}

if len(testProxies) != 2 {
t.Fatalf("Wrong number of proxies returned: %d != 2", len(testProxies))
} else if testProxies[0].Name != "one" || testProxies[1].Name != "two" {
}

if testProxies[0].Name != "one" || testProxies[1].Name != "two" {
t.Fatalf("Wrong proxy names returned: %s, %s", testProxies[0].Name, testProxies[1].Name)
} else if testProxies[0].Listen != "127.0.0.1:7070" || testProxies[1].Listen != "127.0.0.1:7575" {
}

if testProxies[0].Listen != "127.0.0.1:7070" ||
testProxies[1].Listen != "127.0.0.1:7575" {
t.Fatalf("Wrong proxy listen addresses returned: %s, %s",
testProxies[0].Listen, testProxies[1].Listen,
)
Expand Down Expand Up @@ -283,9 +298,13 @@ func TestPopulateWithBadName(t *testing.T) {

if err == nil {
t.Fatal("Expected Populate to fail.")
} else if err.Error() != "Populate: HTTP 400: missing required field: name at proxy 2" {
}

if err.Error() != "Populate: HTTP 400: missing required field: name at proxy 2" {
t.Fatal("Expected different error during populate:", err)
} else if len(testProxies) != 0 {
}

if len(testProxies) != 0 {
t.Fatalf("Wrong number of proxies returned: %d != 0", len(testProxies))
}

Expand Down Expand Up @@ -323,9 +342,13 @@ func TestPopulateProxyWithBadDataShouldReturnError(t *testing.T) {

if err == nil {
t.Fatal("Expected Populate to fail.")
} else if len(testProxies) != 1 {
}

if len(testProxies) != 1 {
t.Fatalf("Wrong number of proxies returned: %d != %d", len(testProxies), 1)
} else if testProxies[0].Name != "one" {
}

if testProxies[0].Name != "one" {
t.Fatalf("Wrong proxy name returned: %s != one", testProxies[0].Name)
}

Expand Down Expand Up @@ -1037,19 +1060,23 @@ func AssertToxicExists(
var toxic *tclient.Toxic
var actualType, actualStream string

for _, tox := range toxics {
for i, tox := range toxics {
if name == tox.Name {
toxic = &tox
toxic = &toxics[i]
actualType = tox.Type
actualStream = tox.Stream
}
}
if exists {
if toxic == nil {
t.Fatalf("Expected to see %s toxic in list", name)
} else if actualType != typeName {
}

if actualType != typeName {
t.Fatalf("Expected %s to be of type %s, found %s", name, typeName, actualType)
} else if actualStream != stream {
}

if actualStream != stream {
t.Fatalf("Expected %s to be in stream %s, found %s", name, stream, actualStream)
}
} else if toxic != nil && actualStream == stream {
Expand Down
7 changes: 3 additions & 4 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ const (
GREEN = "\x1b[32m"
YELLOW = "\x1b[33m"
BLUE = "\x1b[34m"
CYAN = "\x1b[36m"
PURPLE = "\x1b[35m"
GRAY = "\x1b[37m"
NONE = "\x1b[0m"
)

Expand Down Expand Up @@ -134,8 +132,9 @@ func cliCommands() []*cli.Command {
Action: withToxi(createProxy),
},
{
Name: "toggle",
Usage: "\ttoggle enabled status on a proxy\n\t\tusage: 'toxiproxy-cli toggle <proxyName>'\n",
Name: "toggle",
Usage: "\ttoggle enabled status on a proxy\n" +
"\t\tusage: 'toxiproxy-cli toggle <proxyName>'\n",
Aliases: []string{"tog"},
Action: withToxi(toggleProxy),
},
Expand Down
10 changes: 5 additions & 5 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 overridden)
// 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 @@ -90,7 +90,7 @@ func (client *Client) NewProxy() *Proxy {
}

// CreateProxy instantiates a new proxy and starts listening on the specified address.
// This is an alias for `NewProxy()` + `proxy.Save()`
// This is an alias for `NewProxy()` + `proxy.Save()`.
func (client *Client) CreateProxy(name, listen, upstream string) (*Proxy, error) {
proxy := &Proxy{
Name: name,
Expand Down Expand Up @@ -167,7 +167,7 @@ func (client *Client) Populate(config []Proxy) ([]*Proxy, error) {
return proxies.Proxies, err
}

// AddToxic creates a toxic to proxy
// AddToxic creates a toxic to proxy.
func (client *Client) AddToxic(options *ToxicOptions) (*Toxic, error) {
proxy, err := client.Proxy(options.ProxyName)
if err != nil {
Expand All @@ -189,7 +189,7 @@ func (client *Client) AddToxic(options *ToxicOptions) (*Toxic, error) {
return toxic, nil
}

// UpdateToxic update a toxic in proxy
// UpdateToxic update a toxic in proxy.
func (client *Client) UpdateToxic(options *ToxicOptions) (*Toxic, error) {
proxy, err := client.Proxy(options.ProxyName)
if err != nil {
Expand All @@ -213,7 +213,7 @@ func (client *Client) UpdateToxic(options *ToxicOptions) (*Toxic, error) {
return toxic, nil
}

// RemoveToxic removes toxic from proxy
// RemoveToxic removes toxic from proxy.
func (client *Client) RemoveToxic(options *ToxicOptions) error {
proxy, err := client.Proxy(options.ProxyName)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/toxiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
}

// Handle SIGTERM to exit cleanly
signals := make(chan os.Signal)
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGTERM)
go func() {
<-signals
Expand Down
4 changes: 2 additions & 2 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
//
// NoopToxic LatencyToxic
// v v
// Input > ToxicStub > ToxicStub > Output
// Input > ToxicStub > ToxicStub > Output.
//
type ToxicLink struct {
stubs []*toxics.ToxicStub
Expand Down Expand Up @@ -60,7 +60,7 @@ func NewToxicLink(
return link
}

// Start the link with the specified toxics
// Start the link with the specified toxics.
func (link *ToxicLink) Start(name string, source io.Reader, dest io.WriteCloser) {
go func() {
bytes, err := io.Copy(link.input, source)
Expand Down
22 changes: 17 additions & 5 deletions link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ func TestStubInitializaation(t *testing.T) {
link := NewToxicLink(nil, collection, stream.Downstream)
if len(link.stubs) != 1 {
t.Fatalf("Link created with wrong number of stubs: %d != 1", len(link.stubs))
} else if cap(link.stubs) != toxics.Count()+1 {
}

if cap(link.stubs) != toxics.Count()+1 {
t.Fatalf("Link created with wrong capacity: %d != %d", cap(link.stubs), toxics.Count()+1)
} else if cap(link.stubs[0].Input) != 0 {
}

if cap(link.stubs[0].Input) != 0 {
t.Fatalf("Noop buffer was not initialized as 0: %d", cap(link.stubs[0].Input))
} else if cap(link.stubs[0].Output) != 0 {
}

if cap(link.stubs[0].Output) != 0 {
t.Fatalf("Link output buffer was not initialized as 0: %d", cap(link.stubs[0].Output))
}
}
Expand All @@ -47,13 +53,19 @@ func TestStubInitializaationWithToxics(t *testing.T) {
Toxicity: 1,
})
link := NewToxicLink(nil, collection, stream.Downstream)

if len(link.stubs) != 3 {
t.Fatalf("Link created with wrong number of stubs: %d != 3", len(link.stubs))
} else if cap(link.stubs) != toxics.Count()+1 {
}

if cap(link.stubs) != toxics.Count()+1 {
t.Fatalf("Link created with wrong capacity: %d != %d", cap(link.stubs), toxics.Count()+1)
} else if cap(link.stubs[len(link.stubs)-1].Output) != 0 {
}

if cap(link.stubs[len(link.stubs)-1].Output) != 0 {
t.Fatalf("Link output buffer was not initialized as 0: %d", cap(link.stubs[0].Output))
}

for i, toxic := range collection.chain[stream.Downstream] {
if cap(link.stubs[i].Input) != toxic.BufferSize {
t.Fatalf(
Expand Down
6 changes: 3 additions & 3 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// responsibility of Proxy is to accept new client and create Links between the
// client and upstream.
//
// Client <-> toxiproxy <-> Upstream
// Client <-> toxiproxy <-> Upstream.
//
type Proxy struct {
sync.Mutex
Expand Down Expand Up @@ -205,7 +205,7 @@ func (proxy *Proxy) RemoveConnection(name string) {
delete(proxy.connections.list, name)
}

// Starts a proxy, assumes the lock has already been taken
// Starts a proxy, assumes the lock has already been taken.
func start(proxy *Proxy) error {
if proxy.Enabled {
return ErrProxyAlreadyStarted
Expand All @@ -219,7 +219,7 @@ func start(proxy *Proxy) error {
return err
}

// Stops a proxy, assumes the lock has already been taken
// Stops a proxy, assumes the lock has already been taken.
func stop(proxy *Proxy) {
if !proxy.Enabled {
return
Expand Down
8 changes: 4 additions & 4 deletions stream/io_chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ const (
NumDirections
)

// Stores a slice of bytes with its receive timestamp
// Stores a slice of bytes with its receive timestamp.
type StreamChunk struct {
Data []byte
Timestamp time.Time
}

// Implements the io.WriteCloser interface for a chan []byte
// Implements the io.WriteCloser interface for a chan []byte.
type ChanWriter struct {
output chan<- *StreamChunk
}
Expand All @@ -38,13 +38,13 @@ func (c *ChanWriter) Write(buf []byte) (int, error) {
return len(buf), nil
}

// Close the output channel
// Close the output channel.
func (c *ChanWriter) Close() error {
close(c.output)
return nil
}

// Implements the io.Reader interface for a chan []byte
// Implements the io.Reader interface for a chan []byte.
type ChanReader struct {
input <-chan *StreamChunk
interrupt <-chan struct{}
Expand Down
Loading

0 comments on commit 53f4f36

Please sign in to comment.