Ejemplo n.º 1
0
func init() {
	if os.Getenv("DEBUG") == "true" {
		Logger = log.New(os.Stdout, "[sarama] ", log.LstdFlags)
	}

	seed := time.Now().UTC().UnixNano()
	if tmp := os.Getenv("TEST_SEED"); tmp != "" {
		seed, _ = strconv.ParseInt(tmp, 0, 64)
	}
	Logger.Println("Using random seed:", seed)
	rand.Seed(seed)

	proxyAddr := os.Getenv("TOXIPROXY_ADDR")
	if proxyAddr == "" {
		proxyAddr = VagrantToxiproxy
	}
	proxyClient = toxiproxy.NewClient(proxyAddr)

	kafkaPeers := os.Getenv("KAFKA_PEERS")
	if kafkaPeers == "" {
		kafkaPeers = VagrantKafkaPeers
	}
	kafkaBrokers = strings.Split(kafkaPeers, ",")

	if c, err := net.DialTimeout("tcp", kafkaBrokers[0], 5*time.Second); err == nil {
		if err = c.Close(); err == nil {
			kafkaAvailable = true
		}
	}

	kafkaRequired = os.Getenv("CI") != ""
}
Ejemplo n.º 2
0
func TestIndexWithNoProxies(t *testing.T) {
	WithServer(t, func(addr string) {
		client := tclient.NewClient(addr)
		proxies, err := client.Proxies()
		if err != nil {
			t.Fatal("Failed getting proxies: ", err)
		}

		if len(proxies) > 0 {
			t.Fatal("Expected no proxies, got: ", proxies)
		}
	})
}
Ejemplo n.º 3
0
func initToxiproxy() error {
	toxiproxyClient = toxiproxy.NewClient("http://" + proxyHost + ":" + proxyPort)
	proxies, err := toxiproxyClient.Proxies()
	if err != nil {
		return err
	}
	for name, proxy := range proxies {
		// toxiproxy thinks its listening on [::] since its inside
		// vagrant - adjust the host to point to the vagrant cluster
		// instead
		port := strings.TrimPrefix(proxy.Listen, "[::]:")
		hostport := fmt.Sprintf("%s:%s", proxyHost, port)
		if strings.HasPrefix(name, "zk") {
			zookeeperAddrs = append(zookeeperAddrs, hostport)
			zookeeperProxies = append(zookeeperProxies, proxy)
		}
	}
	return nil
}
Ejemplo n.º 4
0
package main

import (
	"io/ioutil"
	"net/http"
	"testing"
	"time"

	tclient "github.com/Shopify/toxiproxy/client"
)

var testServer *server

var client = tclient.NewClient("http://127.0.0.1:8475")
var testProxy = client.NewProxy(&tclient.Proxy{
	Name:     "mysql_master",
	Listen:   "localhost:3310",
	Upstream: "localhost:20001",
	Enabled:  true,
})

func WithServer(t *testing.T, f func(string)) {
	// Make sure only one server is running at a time. Apparently there's no clean
	// way to shut it down between each test run.
	if testServer == nil {
		testServer = NewServer(NewProxyCollection())
		go testServer.Listen("localhost", "8475")

		// Allow server to start. There's no clean way to know when it listens.
		time.Sleep(50 * time.Millisecond)
	}
Ejemplo n.º 5
0
func main() {
	dc, err := dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		log.Fatalf("Failed to init dockerclient: %v", err)
	}
	tpIP := getTpHost(dc)
	tp := toxiproxy.NewClient("http://" + tpIP + ":8474")

	proxies, err := tp.Proxies()
	if err != nil {
		log.Fatalf("Failed to list toxiproxy proxies: %v", err)
	}
	proxyInfos := map[string]ProxyInfo{}

	firstAvailablePort := uint16(9000)
	for proxyName, proxy := range proxies {
		tpPort, err := strconv.ParseUint(strings.Split(proxy.Listen, ":")[1], 10, 16)
		if err != nil {
			log.Printf("unable to parse port from proxy.Listen=%s\n", proxy.Listen)
			continue
		} else if uint16(tpPort) >= firstAvailablePort {
			log.Printf("port %d is taken, increasing firstAvailablePort=%d->%d\n", tpPort, firstAvailablePort, tpPort+1)
			firstAvailablePort = uint16(tpPort) + 1
		} else {
			log.Printf("port %d is taken, nobody cares\n", tpPort)
		}

		// hacky extraction of data from the container name
		dstPort, err := strconv.ParseUint(strings.Split(strings.Split(proxyName, ";")[1], ":")[1], 10, 16)
		if err == nil {
			proxyInfos[proxyName] = ProxyInfo{
				strings.Split(proxyName, ";")[2],
				strings.Split(strings.Split(proxyName, ";")[1], ":")[0],
				uint16(dstPort),
				tpIP,
				uint16(tpPort),
				proxy}
		} else {
			log.Println("failed to parse toxyproxy proxy name")
		}
	}

	s := Server{
		nextTpPort:      firstAvailablePort,
		queryConns:      make(chan bool),
		queryConnsReply: make(chan []Conn),
		tp:              tp,
		tpIP:            tpIP,
		dc:              dc,
		tpProxies:       proxyInfos,
	}

	r := mux.NewRouter()
	r.HandleFunc("/api/proxies", s.addProxyHandler).Methods("POST")
	r.HandleFunc("/api/proxies", s.getProxiesHandler).Methods("GET")
	r.HandleFunc("/api/proxies", s.deleteProxyHandler).Methods("DELETE")
	r.HandleFunc("/api/proxies/{proxyName}/toxics", s.createToxicHandler).Methods("POST")
	r.HandleFunc("/api/conns", s.getConnsHandler).Methods("GET")
	fs := FileServer(http.Dir("assets"))
	r.PathPrefix("/").Handler(fs)

	// set up the channels for the gorouties
	recordConn := make(chan Conn)

	// start the poller and the state tracker
	go connPoller(recordConn)
	go connStateTracker(recordConn, s.queryConns, s.queryConnsReply)

	log.Println("Listening on 3000...")
	http.ListenAndServe(":3000", r)
}