Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
include:
- os: linux
sudo: required
dist: xenial
dist: bionic

before_deploy:
- make packages
Expand Down
8 changes: 8 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,12 @@ pipeline {
}
}
}
post {
success {
slackSend (color: '#2eb886', message: "SUCCESS: `${env.JOB_NAME}` <${env.BUILD_URL}|build #${env.BUILD_NUMBER}>")
}
failure {
slackSend (color: '#b82e60', message: "FAILED: `${env.JOB_NAME}` <${env.BUILD_URL}|build #${env.BUILD_NUMBER}>")
}
}
}
6 changes: 4 additions & 2 deletions discovery/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,11 @@ func testProxyMockUp(t *testing.T, code int, errContains string) {
require.NoError(t, err)

require.NoError(t, proxy.Start())
defer func() { proxy.Stop() }()
defer func() {
proxy.Stop()
}()

require.NoError(t, testutils.SetTransportProxy())
require.NoError(t, proxy.SetTransportProxy())

queue := make(chan *github.Repository, 50)
advertiseRepos := func(
Expand Down
2 changes: 1 addition & 1 deletion downloader/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func testPeriodicallyBrokenGithubAPI(t *testing.T, h *testhelper.Helper) {
require.NoError(t, proxy.Start())
defer func() { proxy.Stop() }()

require.NoError(t, testutils.SetTransportProxy())
require.NoError(t, proxy.SetTransportProxy())

errs := concurrentDownloads(h, httpsProtocol)
var failedCounter int
Expand Down
29 changes: 17 additions & 12 deletions testutils/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ import (
"crypto/tls"
"fmt"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"strconv"
"sync"
"time"

"github.com/cenkalti/backoff"

"gopkg.in/src-d/go-log.v1"
)

const (
ip = "localhost"
port = "8888"
addr = ip + ":" + port
URL = "https://" + addr

pingInterval = 500 * time.Millisecond
pingTimeout = 15 * time.Second
)

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

/*
Current file provides functionality of https and http proxy with ability to mock up responses
for testing purposes. Usually it's needed when client is not accessible from testing environment.
Expand All @@ -47,7 +47,7 @@ Snippet:
proxy.Start()
defer func() { proxy.Stop() }()

require.NoError(t, testutils.SetTransportProxy())
require.NoError(t, proxy.SetTransportProxy())
...
*/

Expand All @@ -58,6 +58,7 @@ type Proxy struct {
mu sync.Mutex
server *http.Server
transport http.RoundTripper
port string
// options for mock ups
options *Options
// FailEachNthRequestCounter is a counter required for failing condition on each nth request
Expand Down Expand Up @@ -92,9 +93,12 @@ type Options struct {
// NewProxy is a proxy constructor
func NewProxy(transport http.RoundTripper, options *Options) (*Proxy, error) {
processOptions(options)
port := strconv.Itoa(rand.Intn(10000) + 10000)

proxy := &Proxy{
transport: transport,
options: options,
port: port,
server: &http.Server{
Addr: ":" + port,
// Disable HTTP/2.
Expand Down Expand Up @@ -130,7 +134,7 @@ func (p *Proxy) Start() error {
bo.MaxInterval = pingInterval
bo.MaxElapsedTime = pingTimeout
return backoff.Retry(func() error {
conn, err := net.DialTimeout("tcp", addr, time.Second/2)
conn, err := net.DialTimeout("tcp", p.server.Addr, time.Second/2)
if err == nil {
conn.Close()
return nil
Expand All @@ -147,17 +151,18 @@ func (p *Proxy) Stop() {
}

// SetTransportProxy changes http.DefaultTransport to the one that uses current server as a proxy
func SetTransportProxy() error {
u, err := url.Parse(URL)
func (p *Proxy) SetTransportProxy() error {
u, err := url.Parse("https://localhost:" + p.port)
if err != nil {
return err
}

http.DefaultTransport = &http.Transport{
Proxy: http.ProxyURL(u),
// Disable HTTP/2.
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: 2 * time.Second,
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

return nil
Expand Down