Skip to content

Commit

Permalink
Make test output honor output.logstash.proxy_url
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurai-youhei committed Sep 29, 2023
1 parent b4b9e05 commit a7d3de1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
30 changes: 25 additions & 5 deletions transport/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"errors"
"fmt"
"net"
"net/url"
"sync"
"time"

Expand Down Expand Up @@ -215,19 +216,38 @@ func (c *Client) handleError(err error) error {

func (c *Client) Test(d testing.Driver) {
d.Run("logstash: "+c.host, func(d testing.Driver) {
if c.config.Proxy.URL != "" {
d.Run("proxy", func(d testing.Driver) {
url, err1 := url.Parse(c.config.Proxy.URL)
d.Fatal("parse url", err1)
dialer := TestNetDialer(d, c.config.Timeout)
_, err2 := dialer.Dial("tcp", url.Host)
d.Fatal("dial up", err2)
})
}

d.Run("connection", func(d testing.Driver) {
netDialer := TestNetDialer(d, c.config.Timeout)
_, err := netDialer.Dial("tcp", c.host)
var dialer Dialer
if c.config.Proxy.URL == "" {
dialer = TestNetDialer(d, c.config.Timeout)
} else {
dialer = NetDialer(c.config.Timeout)
dialer = TestProxyDialer(d, dialer, c.config.Proxy, c.config.Timeout)
}
_, err := dialer.Dial("tcp", c.host)
d.Fatal("dial up", err)
})

if c.config.TLS == nil {
d.Warn("TLS", "secure connection disabled")
} else {
d.Run("TLS", func(d testing.Driver) {
netDialer := NetDialer(c.config.Timeout)
tlsDialer := TestTLSDialer(d, netDialer, c.config.TLS, c.config.Timeout)
_, err := tlsDialer.Dial("tcp", c.host)
dialer := NetDialer(c.config.Timeout)
if c.config.Proxy.URL != "" {
dialer = TestProxyDialer(d, dialer, c.config.Proxy, c.config.Timeout)
}
dialer = TestTLSDialer(d, dialer, c.config.TLS, c.config.Timeout)
_, err := dialer.Dial("tcp", c.host)
d.Fatal("dial up", err)
})
}
Expand Down
13 changes: 13 additions & 0 deletions transport/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ package transport
import (
"net"
"net/url"
"time"

"golang.org/x/net/proxy"

"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/testing"
)

// ProxyConfig holds the configuration information required to proxy
Expand Down Expand Up @@ -97,3 +99,14 @@ func ProxyDialer(log *logp.Logger, config *ProxyConfig, forward Dialer) (Dialer,
return DialWith(dialer, network, host, addresses, port)
}), nil
}

func TestProxyDialer(
d testing.Driver,
forward Dialer,
config *ProxyConfig,
timeout time.Duration,
) Dialer {
dialer, err := ProxyDialer(logp.L(), config, forward)
d.Fatal("proxy", err)
return dialer
}

0 comments on commit a7d3de1

Please sign in to comment.