1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.arvados.org/arvados.git/sdk/go/arvados"
13 check "gopkg.in/check.v1"
20 // newPgProxy sets up a TCP proxy, listening on all interfaces, that
21 // forwards all connections to the cluster's PostgreSQL server. This
22 // allows the caller to run a docker container that can connect to a
23 // postgresql instance that listens on the test host's loopback
26 // Caller is responsible for calling Close() on the returned pgproxy.
27 func newPgProxy(c *check.C, cluster *arvados.Cluster) *pgproxy {
28 host := cluster.PostgreSQL.Connection["host"]
32 port := cluster.PostgreSQL.Connection["port"]
36 target := net.JoinHostPort(host, port)
38 ln, err := net.Listen("tcp", ":")
39 c.Assert(err, check.IsNil)
42 downstream, err := ln.Accept()
43 if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
46 c.Assert(err, check.IsNil)
48 c.Logf("pgproxy accepted connection from %s", downstream.RemoteAddr().String())
49 defer downstream.Close()
50 upstream, err := net.Dial("tcp", target)
52 c.Logf("net.Dial(%q): %s", target, err)
55 defer upstream.Close()
56 go io.Copy(downstream, upstream)
57 io.Copy(upstream, downstream)
61 c.Logf("pgproxy listening at %s", ln.Addr().String())
62 return &pgproxy{Listener: ln}
65 func (proxy *pgproxy) Port() string {
66 _, port, _ := net.SplitHostPort(proxy.Addr().String())