Beispiel #1
0
func (*tcpProxySuite) TestCloseConns(c *gc.C) {
	var wg sync.WaitGroup

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	c.Assert(err, gc.IsNil)
	defer listener.Close()
	wg.Add(1)
	go tcpEcho(&wg, listener)

	p := testing.NewTCPProxy(c, listener.Addr().String())
	c.Assert(p.Addr(), gc.Not(gc.Equals), listener.Addr().String())

	// Make a couple of connections through the proxy
	// and test that they work.
	conn1, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn1.Close()
	assertEcho(c, conn1)

	conn2, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn1.Close()
	assertEcho(c, conn1)

	p.CloseConns()

	// Assert that both the connections have been broken.
	assertEOF(c, conn1)
	assertEOF(c, conn2)

	// Check that we can still make a connection.
	conn3, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn3.Close()
	assertEcho(c, conn3)

	// Close the proxy and check that the last connection goes.
	p.Close()
	assertEOF(c, conn3)

	listener.Close()
	// Make sure that all our connections have gone away too.
	wg.Wait()
}
Beispiel #2
0
func (*tcpProxySuite) TestTCPProxy(c *gc.C) {
	var wg sync.WaitGroup

	listener, err := net.Listen("tcp", "127.0.0.1:0")
	c.Assert(err, gc.IsNil)
	defer listener.Close()
	wg.Add(1)
	go tcpEcho(&wg, listener)

	p := testing.NewTCPProxy(c, listener.Addr().String())
	c.Assert(p.Addr(), gc.Not(gc.Equals), listener.Addr().String())

	// Dial the proxy and check that we see the text echoed correctly.
	conn, err := net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn.Close()

	assertEcho(c, conn)

	// Close the connection and check that we see
	// the connection closed for read.
	conn.(*net.TCPConn).CloseWrite()
	assertEOF(c, conn)

	// Make another connection and close the proxy,
	// which should close down the proxy and cause us
	// to get an error.
	conn, err = net.Dial("tcp", p.Addr())
	c.Assert(err, gc.IsNil)
	defer conn.Close()

	p.Close()
	assertEOF(c, conn)

	// Make sure that we cannot dial the proxy address either.
	conn, err = net.Dial("tcp", p.Addr())
	c.Assert(err, gc.ErrorMatches, ".*connection refused")

	listener.Close()
	// Make sure that all our connections have gone away too.
	wg.Wait()
}
Beispiel #3
0
func (s *serverSuite) TestNewServerDoesNotAccessState(c *gc.C) {
	mongoInfo := s.MongoInfo(c)

	proxy := testing.NewTCPProxy(c, mongoInfo.Addrs[0])
	mongoInfo.Addrs = []string{proxy.Addr()}

	st, err := state.Open(s.State.EnvironTag(), mongoInfo, mongo.DefaultDialOpts(), nil)
	c.Assert(err, gc.IsNil)
	defer st.Close()

	// Now close the proxy so that any attempts to use the
	// state server will fail.
	proxy.Close()

	// Creating the server should succeed because it doesn't
	// access the state (note that newServer does not log in,
	// which *would* access the state).
	srv := newServer(c, st)
	srv.Stop()
}
Beispiel #4
0
func (s *apiclientSuite) TestConnectWebsocketMultiple(c *gc.C) {
	// Create a socket that proxies to the API server.
	info := s.APIInfo(c)
	serverAddr := info.Addrs[0]
	proxy := testing.NewTCPProxy(c, serverAddr)
	defer proxy.Close()

	// Check that we can use the proxy to connect.
	info.Addrs = []string{proxy.Addr()}
	conn, _, err := api.ConnectWebsocket(info, api.DialOpts{})
	c.Assert(err, jc.ErrorIsNil)
	conn.Close()
	assertConnAddrForEnv(c, conn, proxy.Addr(), s.State.EnvironUUID(), "/api")

	// Now break Addrs[0], and ensure that Addrs[1]
	// is successfully connected to.
	proxy.Close()
	info.Addrs = []string{proxy.Addr(), serverAddr}
	conn, _, err = api.ConnectWebsocket(info, api.DialOpts{})
	c.Assert(err, jc.ErrorIsNil)
	conn.Close()
	assertConnAddrForEnv(c, conn, serverAddr, s.State.EnvironUUID(), "/api")
}
Beispiel #5
0
func (s *serverSuite) TestNewServerDoesNotAccessState(c *gc.C) {
	mongoInfo := s.MongoInfo(c)

	proxy := testing.NewTCPProxy(c, mongoInfo.Addrs[0])
	mongoInfo.Addrs = []string{proxy.Addr()}

	dialOpts := mongo.DialOpts{
		Timeout:       5 * time.Second,
		SocketTimeout: 5 * time.Second,
	}
	st, err := state.Open(s.State.ModelTag(), s.State.ControllerTag(), mongoInfo, dialOpts, nil)
	c.Assert(err, gc.IsNil)
	defer st.Close()

	// Now close the proxy so that any attempts to use the
	// controller will fail.
	proxy.Close()

	// Creating the server should succeed because it doesn't
	// access the state (note that newServer does not log in,
	// which *would* access the state).
	_, srv := newServer(c, st)
	srv.Stop()
}