Example #1
0
func testAgent(t *testing.T) *agentWrapper {
	l, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	lw := agent.NewLogWriter(512)
	mult := io.MultiWriter(os.Stderr, lw)

	conf := nextConfig()

	dir, err := ioutil.TempDir("", "agent")
	if err != nil {
		t.Fatalf(fmt.Sprintf("err: %v", err))
	}
	conf.DataDir = dir

	a, err := agent.Create(conf, lw)
	if err != nil {
		os.RemoveAll(dir)
		t.Fatalf(fmt.Sprintf("err: %v", err))
	}

	rpc := agent.NewAgentRPC(a, l, mult, lw)
	return &agentWrapper{
		dir:    dir,
		config: conf,
		agent:  a,
		rpc:    rpc,
		addr:   l.Addr().String(),
	}
}
Example #2
0
// NewFixture creates a new testing instance of Consul.
func NewFixture(t *testing.T) Fixture {
	ports := getPorts(t, 6)

	config := agent.DevConfig()
	config.BindAddr = "127.0.0.1"
	config.Bootstrap = true
	config.DisableCoordinates = true
	config.NodeName = "testnode"
	config.Ports = agent.PortConfig{
		DNS:     ports[0],
		HTTP:    ports[1],
		RPC:     ports[2],
		SerfLan: ports[3],
		SerfWan: ports[4],
		Server:  ports[5],
	}
	config.ConsulConfig = consul.DefaultConfig()
	// Set low timeouts so the agent will become the leader quickly.
	config.ConsulConfig.RaftConfig.LeaderLeaseTimeout = 10 * time.Millisecond
	config.ConsulConfig.RaftConfig.HeartbeatTimeout = 20 * time.Millisecond
	config.ConsulConfig.RaftConfig.ElectionTimeout = 20 * time.Millisecond
	// We would rather start as the leader without requiring an election, but Consul
	// contains some atomicity violations that are exacerbated by this option.
	//
	// config.ConsulConfig.RaftConfig.StartAsLeader = true

	a, err := agent.Create(config, os.Stdout)
	if err != nil {
		t.Fatal("creating Consul agent:", err)
	}
	servers, err := agent.NewHTTPServers(a, config, os.Stdout)
	if err != nil {
		t.Fatal("creating Consul HTTP server:", err)
	}
	client, err := api.NewClient(&api.Config{
		Address: fmt.Sprintf("%s:%d", config.BindAddr, config.Ports.HTTP),
	})
	if err != nil {
		t.Fatal("creating Consul client:", err)
	}

	consulClient := ConsulClientFromRaw(client)
	testutil.WaitForLeader(t, a.RPC, config.Datacenter)

	t.Log("starting Consul server with port:", config.Ports.HTTP)
	return Fixture{
		Agent:    a,
		Servers:  servers,
		Client:   consulClient,
		T:        t,
		HTTPPort: config.Ports.HTTP,
	}
}
Example #3
0
func testAgentWithConfig(t *testing.T, cb func(c *agent.Config)) *agentWrapper {
	l, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		t.Fatalf("err: %s", err)
	}

	lw := logger.NewLogWriter(512)
	mult := io.MultiWriter(os.Stderr, lw)

	conf := nextConfig()
	cb(conf)

	dir, err := ioutil.TempDir("", "agent")
	if err != nil {
		t.Fatalf(fmt.Sprintf("err: %v", err))
	}
	conf.DataDir = dir

	a, err := agent.Create(conf, lw, nil, nil)
	if err != nil {
		os.RemoveAll(dir)
		t.Fatalf(fmt.Sprintf("err: %v", err))
	}

	rpc := agent.NewAgentRPC(a, l, mult, lw)

	conf.Addresses.HTTP = "127.0.0.1"
	httpAddr := fmt.Sprintf("127.0.0.1:%d", conf.Ports.HTTP)
	http, err := agent.NewHTTPServers(a, conf, os.Stderr)
	if err != nil {
		os.RemoveAll(dir)
		t.Fatalf(fmt.Sprintf("err: %v", err))
	}

	if http == nil || len(http) == 0 {
		os.RemoveAll(dir)
		t.Fatalf(fmt.Sprintf("Could not create HTTP server to listen on: %s", httpAddr))
	}

	return &agentWrapper{
		dir:      dir,
		config:   conf,
		agent:    a,
		rpc:      rpc,
		http:     http[0],
		addr:     l.Addr().String(),
		httpAddr: httpAddr,
	}
}