Skip to content

Commit

Permalink
chore: Add a test http2 server and replace akamai's in tests
Browse files Browse the repository at this point in the history
part of #537
  • Loading branch information
mstoykov committed Mar 30, 2020
1 parent 8507e1d commit fde11bf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 51 deletions.
8 changes: 4 additions & 4 deletions js/modules/k6/http/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@ func TestRequestAndBatch(t *testing.T) {
})
t.Run("HTTP/2", func(t *testing.T) {
stats.GetBufferedSamples(samples) // Clean up buffered samples from previous tests
_, err := common.RunString(rt, `
let res = http.request("GET", "https://http2.akamai.com/demo");
_, err := common.RunString(rt, sr(`
let res = http.request("GET", "HTTP2BIN_URL/get");
if (res.status != 200) { throw new Error("wrong status: " + res.status) }
if (res.proto != "HTTP/2.0") { throw new Error("wrong proto: " + res.proto) }
`)
`))
assert.NoError(t, err)

bufSamples := stats.GetBufferedSamples(samples)
assertRequestMetricsEmitted(t, bufSamples, "GET", "https://http2.akamai.com/demo", "", 200, "")
assertRequestMetricsEmitted(t, bufSamples, "GET", sr("HTTP2BIN_URL/get"), "", 200, "")
for _, sampleC := range bufSamples {
for _, sample := range sampleC.GetSamples() {
proto, ok := sample.Tags.Get("proto")
Expand Down
47 changes: 0 additions & 47 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,53 +941,6 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
}
}

func TestVUIntegrationHTTP2(t *testing.T) {
r1, err := getSimpleRunner("/script.js", `
import http from "k6/http";
export default function() {
let res = http.request("GET", "https://http2.akamai.com/demo");
if (res.status != 200) { throw new Error("wrong status: " + res.status) }
if (res.proto != "HTTP/2.0") { throw new Error("wrong proto: " + res.proto) }
}
`)
if !assert.NoError(t, err) {
return
}
require.NoError(t, r1.SetOptions(lib.Options{
Throw: null.BoolFrom(true),
SystemTags: stats.NewSystemTagSet(stats.TagProto),
}))

r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
if !assert.NoError(t, err) {
return
}

runners := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range runners {
t.Run(name, func(t *testing.T) {
samples := make(chan stats.SampleContainer, 100)
vu, err := r.NewVU(samples)
if !assert.NoError(t, err) {
return
}
err = vu.RunOnce(context.Background())
assert.NoError(t, err)

protoFound := false
for _, sampleC := range stats.GetBufferedSamples(samples) {
for _, sample := range sampleC.GetSamples() {
if proto, ok := sample.Tags.Get("proto"); ok {
protoFound = true
assert.Equal(t, "HTTP/2.0", proto)
}
}
}
assert.True(t, protoFound)
})
}
}

func TestVUIntegrationOpenFunctionError(t *testing.T) {
r, err := getSimpleRunner("/script.js", `
export default function() { open("/tmp/foo") }
Expand Down
23 changes: 23 additions & 0 deletions lib/testutils/httpmultibin/httpmultibin.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type HTTPMultiBin struct {
Mux *http.ServeMux
ServerHTTP *httptest.Server
ServerHTTPS *httptest.Server
ServerHTTP2 *httptest.Server
Replacer *strings.Replacer
TLSClientConfig *tls.Config
Dialer *netext.Dialer
Expand Down Expand Up @@ -226,6 +227,20 @@ func NewHTTPMultiBin(t testing.TB) *HTTPMultiBin {
require.NotNil(t, httpsIP)
tlsConfig := GetTLSClientConfig(t, httpsSrv)

// Initialize the HTTP2 server, with a copy of the https tls config
http2Srv := httptest.NewUnstartedServer(mux)
err = http2.ConfigureServer(http2Srv.Config, &http2.Server{
IdleTimeout: 30,
})
http2Srv.TLS = &(*tlsConfig) // copy it
http2Srv.TLS.NextProtos = []string{http2.NextProtoTLS}
require.NoError(t, err)
http2Srv.StartTLS()
http2URL, err := url.Parse(http2Srv.URL)
require.NoError(t, err)
http2IP := net.ParseIP(http2URL.Hostname())
require.NotNil(t, http2IP)

// Set up the dialer with shorter timeouts and the custom domains
dialer := netext.NewDialer(net.Dialer{
Timeout: 2 * time.Second,
Expand All @@ -249,6 +264,7 @@ func NewHTTPMultiBin(t testing.TB) *HTTPMultiBin {
Mux: mux,
ServerHTTP: httpSrv,
ServerHTTPS: httpsSrv,
ServerHTTP2: http2Srv,
Replacer: strings.NewReplacer(
"HTTPBIN_IP_URL", httpSrv.URL,
"HTTPBIN_DOMAIN", httpDomain,
Expand All @@ -262,12 +278,19 @@ func NewHTTPMultiBin(t testing.TB) *HTTPMultiBin {
"WSSBIN_URL", fmt.Sprintf("wss://%s:%s", httpsDomain, httpsURL.Port()),
"HTTPSBIN_IP", httpsIP.String(),
"HTTPSBIN_PORT", httpsURL.Port(),

"HTTP2BIN_IP_URL", http2Srv.URL,
"HTTP2BIN_DOMAIN", httpsDomain,
"HTTP2BIN_URL", fmt.Sprintf("https://%s:%s", httpsDomain, http2URL.Port()),
"HTTP2BIN_IP", http2IP.String(),
"HTTP2BIN_PORT", http2URL.Port(),
),
TLSClientConfig: tlsConfig,
Dialer: dialer,
HTTPTransport: transport,
Context: ctx,
Cleanup: func() {
http2Srv.Close()
httpsSrv.Close()
httpSrv.Close()
ctxCancel()
Expand Down

0 comments on commit fde11bf

Please sign in to comment.