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

Reopening PR-211 - Populate toxics at startup from a json string #211 #501

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,12 @@ Toxiproxy.

For large application we recommend storing the Toxiproxy configurations in a
separate configuration file. We use `config/toxiproxy.json`. This file can be
passed to the server using the `-config` option, or loaded by the application
passed to the server using the `-configFile` option, or loaded by the application
to use with the `populate` function.
Alternatively you can pass a json string with the `-configJson` option to populate
these configurations. This is specially useful when running Toxiproxy as a docker container, e.g.:

```docker run -it shopify/toxiproxy -configJson '[{"name": "redis","listen": "0.0.0.0:9092","upstream": "redis:6379"}]'```

Use ports outside the ephemeral port range to avoid random port conflicts.
It's `32,768` to `61,000` on Linux by default, see
Expand Down
36 changes: 23 additions & 13 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package toxiproxy

import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net"
"net/http"
Expand All @@ -24,26 +26,34 @@ func NewServer() *ApiServer {
}
}

func (server *ApiServer) PopulateConfig(filename string) {
func (server *ApiServer) PopulateConfigFromFile(filename string) {
file, err := os.Open(filename)
if err != nil {
logrus.WithFields(logrus.Fields{
"config": filename,
"error": err,
}).Error("Error reading config file")
} else {
proxies, err := server.Collection.PopulateJson(file)
if err != nil {
logrus.WithFields(logrus.Fields{
"config": filename,
"error": err,
}).Error("Failed to populate proxies from file")
} else {
logrus.WithFields(logrus.Fields{
"config": filename,
"proxies": len(proxies),
}).Info("Populated proxies from file")
}
server.populateConfig(file)
}
}

func (server *ApiServer) PopulateConfigFromJsonString(json string) {
var data = bytes.NewReader([]byte(json))

server.populateConfig(data)
}

func (server *ApiServer) populateConfig(data io.Reader) {
proxies, err := server.Collection.PopulateJson(data)
if err != nil {
logrus.WithFields(logrus.Fields{
"error": err,
}).Error("Failed to populate proxies from file")
} else {
logrus.WithFields(logrus.Fields{
"proxies": len(proxies),
}).Info("Populated proxies from file")
}
}

Expand Down
2 changes: 1 addition & 1 deletion api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func TestPopulateProxyWithBadDataShouldReturnError(t *testing.T) {

for _, p := range proxies {
if p.Name == "two" || p.Name == "three" {
t.Fatalf("Proxy %s exists, populate did not fail correctly.")
t.Fatalf("Proxy %s exists, populate did not fail correctly.", p.Name)
}
}
})
Expand Down
22 changes: 18 additions & 4 deletions cmd/toxiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,39 @@ import (
"time"

"github.com/Shopify/toxiproxy"
"github.com/sirupsen/logrus"
)

var host string
var port string
var config string
var configJson string
var configFile string

func init() {
flag.StringVar(&host, "host", "localhost", "Host for toxiproxy's API to listen on")
flag.StringVar(&port, "port", "8474", "Port for toxiproxy's API to listen on")
flag.StringVar(&config, "config", "", "JSON file containing proxies to create on startup")
flag.StringVar(&configFile, "configFile", "", "JSON file containing proxies to create on startup")
flag.StringVar(&configJson, "configJson", "", "JSON literal containing proxies to create on startup")
seed := flag.Int64("seed", time.Now().UTC().UnixNano(), "Seed for randomizing toxics with")
flag.Parse()
rand.Seed(*seed)
}

func main() {
server := toxiproxy.NewServer()
if len(config) > 0 {
server.PopulateConfig(config)

if len(configFile) > 0 && len(configJson) > 0 {
logrus.WithFields(logrus.Fields{
"configFile": configFile,
"configJson": configJson,
}).Error("configFile and configJson are mutually exclusive")
} else {
if len(configFile) > 0 {
server.PopulateConfigFromFile(configFile)
}
if len(configJson) > 0 {
server.PopulateConfigFromJsonString(configJson)
}
}

// Handle SIGTERM to exit cleanly
Expand Down