Skip to content

Commit

Permalink
js: better error message checking
Browse files Browse the repository at this point in the history
Currently, we check exactly matching of error message return by go
standard library. It's working nowaday but not guaranteed to work in
upcoming go version. At least in go1.14, all the certificate expried
message checking test will fail, due to the change of error message
format in net/http.

To fix this, we only check that error message contains the message we
want, e.g certificate expired error should contain word "expired".

While at it, also add some require.NoError to make sure no editor
complains above "can lead to nil pointer dereference".
  • Loading branch information
cuonglm committed Nov 18, 2019
1 parent c5f6881 commit 9306558
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,25 +648,26 @@ func TestVUIntegrationInsecureRequests(t *testing.T) {
}{
"Null": {
lib.Options{},
"GoError: Get https://expired.badssl.com/: x509: certificate has expired or is not yet valid",
"x509: certificate has expired or is not yet valid",
},
"False": {
lib.Options{InsecureSkipTLSVerify: null.BoolFrom(false)},
"GoError: Get https://expired.badssl.com/: x509: certificate has expired or is not yet valid",
"x509: certificate has expired or is not yet valid",
},
"True": {
lib.Options{InsecureSkipTLSVerify: null.BoolFrom(true)},
"",
},
}
for name, data := range testdata {
data := data
t.Run(name, func(t *testing.T) {
r1, err := getSimpleRunner("/script.js", `
import http from "k6/http";
export default function() { http.get("https://expired.badssl.com/"); }
`)
require.NoError(t, err)
r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts))
require.NoError(t, r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts)))

r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
require.NoError(t, err)
Expand All @@ -681,7 +682,8 @@ func TestVUIntegrationInsecureRequests(t *testing.T) {
}
err = vu.RunOnce(context.Background())
if data.errMsg != "" {
assert.EqualError(t, err, data.errMsg)
require.NotNil(t, err)
assert.Contains(t, err.Error(), data.errMsg)
} else {
assert.NoError(t, err)
}
Expand All @@ -703,10 +705,10 @@ func TestVUIntegrationBlacklistOption(t *testing.T) {
if !assert.NoError(t, err) {
return
}
r1.SetOptions(lib.Options{
require.NoError(t, r1.SetOptions(lib.Options{
Throw: null.BoolFrom(true),
BlacklistIPs: []*lib.IPNet{cidr},
})
}))

r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
if !assert.NoError(t, err) {
Expand All @@ -721,7 +723,8 @@ func TestVUIntegrationBlacklistOption(t *testing.T) {
return
}
err = vu.RunOnce(context.Background())
assert.EqualError(t, err, "GoError: Get http://10.1.2.3/: IP (10.1.2.3) is in a blacklisted range (10.0.0.0/8)")
require.NotNil(t, err)
assert.Contains(t, err.Error(), "IP (10.1.2.3) is in a blacklisted range (10.0.0.0/8)")
})
}
}
Expand Down Expand Up @@ -756,7 +759,8 @@ func TestVUIntegrationBlacklistScript(t *testing.T) {
return
}
err = vu.RunOnce(context.Background())
assert.EqualError(t, err, "GoError: Get http://10.1.2.3/: IP (10.1.2.3) is in a blacklisted range (10.0.0.0/8)")
require.NotNil(t, err)
assert.Contains(t, err.Error(), "IP (10.1.2.3) is in a blacklisted range (10.0.0.0/8)")
})
}
}
Expand Down Expand Up @@ -830,7 +834,7 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
},
"UnsupportedCipherSuite": {
lib.Options{TLSCipherSuites: &lib.TLSCipherSuites{tls.TLS_RSA_WITH_RC4_128_SHA}},
"GoError: Get https://sha256.badssl.com/: remote error: tls: handshake failure",
"remote error: tls: handshake failure",
},
"NullVersion": {
lib.Options{},
Expand All @@ -842,10 +846,11 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
},
"UnsupportedVersion": {
lib.Options{TLSVersion: &lib.TLSVersions{Min: tls.VersionSSL30, Max: tls.VersionSSL30}},
"GoError: Get https://sha256.badssl.com/: " + unsupportedVersionErrorMsg,
unsupportedVersionErrorMsg,
},
}
for name, data := range testdata {
data := data
t.Run(name, func(t *testing.T) {
r1, err := getSimpleRunner("/script.js", `
import http from "k6/http";
Expand All @@ -854,7 +859,7 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
if !assert.NoError(t, err) {
return
}
r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts))
require.NoError(t, r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts)))

r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
if !assert.NoError(t, err) {
Expand All @@ -863,6 +868,7 @@ func TestVUIntegrationTLSConfig(t *testing.T) {

runners := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range runners {
r := r
t.Run(name, func(t *testing.T) {
r.Logger, _ = logtest.NewNullLogger()

Expand All @@ -872,7 +878,8 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
}
err = vu.RunOnce(context.Background())
if data.errMsg != "" {
assert.EqualError(t, err, data.errMsg)
require.NotNil(t, err)
assert.Contains(t, err.Error(), data.errMsg)
} else {
assert.NoError(t, err)
}
Expand All @@ -894,10 +901,10 @@ func TestVUIntegrationHTTP2(t *testing.T) {
if !assert.NoError(t, err) {
return
}
r1.SetOptions(lib.Options{
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) {
Expand Down Expand Up @@ -1150,10 +1157,10 @@ func TestVUIntegrationClientCerts(t *testing.T) {
if !assert.NoError(t, err) {
return
}
r1.SetOptions(lib.Options{
require.NoError(t, r1.SetOptions(lib.Options{
Throw: null.BoolFrom(true),
InsecureSkipTLSVerify: null.BoolFrom(true),
})
}))

t.Run("Unauthenticated", func(t *testing.T) {
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
Expand All @@ -1175,7 +1182,7 @@ func TestVUIntegrationClientCerts(t *testing.T) {
}
})

r1.SetOptions(lib.Options{
require.NoError(t, r1.SetOptions(lib.Options{
TLSAuth: []*lib.TLSAuth{
{
TLSAuthFields: lib.TLSAuthFields{
Expand All @@ -1199,7 +1206,7 @@ func TestVUIntegrationClientCerts(t *testing.T) {
},
},
},
})
}))

t.Run("Authenticated", func(t *testing.T) {
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
Expand Down

0 comments on commit 9306558

Please sign in to comment.