Skip to content

Commit

Permalink
Pass http.ProxyFromEnvironment configuration to http.Transport (#987)
Browse files Browse the repository at this point in the history
* Pass http.ProxyFromEnvironment configuration to http.Transport

* add test

* move logs

* extend wait time

* log tinyproxy container

* skip test

---------

Co-authored-by: Kuba Kaflik <[email protected]>
  • Loading branch information
slvrtrn and jkaflik authored May 8, 2023
1 parent eec85ec commit 05f17d9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions conn_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func dialHttp(ctx context.Context, addr string, num int, opt *Options) (*httpCon
u.RawQuery = query.Encode()

t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: opt.DialTimeout,
KeepAlive: opt.ConnMaxLifetime,
Expand Down
54 changes: 54 additions & 0 deletions tests/std/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package std

import (
"bufio"
"context"
"crypto/tls"
"database/sql"
"fmt"
"os"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -364,3 +367,54 @@ func TestEmptyDatabaseConfig(t *testing.T) {
_, err = setupConn.ExecContext(context.Background(), `CREATE DATABASE "default"`)
require.NoError(t, err)
}

func TestHTTPProxy(t *testing.T) {
t.Skip("test is flaky, tinyproxy container can't be started in CI")

// check if CLICKHOUSE_HOST env is postfixed with "clickhouse.cloud", skip if not
clickHouseHost := clickhouse_tests.GetEnv("CLICKHOUSE_HOST", "")
if !strings.HasSuffix(clickHouseHost, "clickhouse.cloud") {
t.Skip("Skip test in non cloud environment.")
}

proxyEnv, err := clickhouse_tests.CreateTinyProxyTestEnvironment(t)
defer func() {
if proxyEnv.Container != nil {
proxyEnv.Container.Terminate(context.Background())
}
}()
require.NoError(t, err)

proxyURL := proxyEnv.ProxyUrl(t)

os.Setenv("HTTP_PROXY", proxyURL)
os.Setenv("HTTPS_PROXY", proxyURL)
defer func() {
os.Unsetenv("HTTP_PROXY")
os.Unsetenv("HTTPS_PROXY")
}()

logs, err := proxyEnv.Container.Logs(context.Background())
require.NoError(t, err)
defer logs.Close()
scanner := bufio.NewScanner(logs)

useSSL, err := strconv.ParseBool(clickhouse_tests.GetEnv("CLICKHOUSE_USE_SSL", "false"))
require.NoError(t, err)
conn, err := GetStdDSNConnection(clickhouse.HTTP, useSSL, nil)

require.NoError(t, err)
defer conn.Close()

require.NoError(t, conn.Ping())

assert.Eventually(t, func() bool {
if !scanner.Scan() {
return false
}

text := scanner.Text()
t.Log(text)
return strings.Contains(text, fmt.Sprintf("Established connection to host \"%s\"", clickHouseHost))
}, 60*time.Second, time.Millisecond, "proxy logs should contain clickhouse.cloud instance host")
}
40 changes: 40 additions & 0 deletions tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/docker/go-connections/nat"
"github.com/docker/go-units"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"math/rand"
Expand All @@ -40,6 +41,7 @@ import (
"runtime"
"strconv"
"strings"
"testing"
"time"
)

Expand Down Expand Up @@ -573,3 +575,41 @@ func CreateNginxReverseProxyTestEnvironment(clickhouseEnv ClickHouseTestEnvironm
NginxContainer: nginxContainer,
}, nil
}

type TinyProxyTestEnvironment struct {
HttpPort int
Container testcontainers.Container `json:"-"`
}

func (e TinyProxyTestEnvironment) ProxyUrl(t *testing.T) string {
require.NotNil(t, e.Container)

host, err := e.Container.Host(context.Background())
require.NoError(t, err)

return fmt.Sprintf("http://%s:%d", host, e.HttpPort)
}

func CreateTinyProxyTestEnvironment(t *testing.T) (TinyProxyTestEnvironment, error) {
ctx := context.Background()

req := testcontainers.ContainerRequest{
Image: "monokal/tinyproxy",
Name: fmt.Sprintf("tinyproxy-clickhouse-go-%d", time.Now().UnixNano()),
ExposedPorts: []string{"8888/tcp"},
WaitingFor: wait.ForListeningPort("8888/tcp").WithStartupTimeout(time.Second * time.Duration(120)),
Cmd: []string{"--enable-debug", "ANY"},
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})
require.NoError(t, err)

p, _ := container.MappedPort(ctx, "8888")
return TinyProxyTestEnvironment{
HttpPort: p.Int(),
Container: container,
}, nil
}

0 comments on commit 05f17d9

Please sign in to comment.