Skip to content

Commit 8911dc2

Browse files
cuonglmna--
authored andcommitted
js: better error message checking
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".
1 parent c5f6881 commit 8911dc2

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

js/runner_test.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -648,25 +648,26 @@ func TestVUIntegrationInsecureRequests(t *testing.T) {
648648
}{
649649
"Null": {
650650
lib.Options{},
651-
"GoError: Get https://expired.badssl.com/: x509: certificate has expired or is not yet valid",
651+
"x509: certificate has expired or is not yet valid",
652652
},
653653
"False": {
654654
lib.Options{InsecureSkipTLSVerify: null.BoolFrom(false)},
655-
"GoError: Get https://expired.badssl.com/: x509: certificate has expired or is not yet valid",
655+
"x509: certificate has expired or is not yet valid",
656656
},
657657
"True": {
658658
lib.Options{InsecureSkipTLSVerify: null.BoolFrom(true)},
659659
"",
660660
},
661661
}
662662
for name, data := range testdata {
663+
data := data
663664
t.Run(name, func(t *testing.T) {
664665
r1, err := getSimpleRunner("/script.js", `
665666
import http from "k6/http";
666667
export default function() { http.get("https://expired.badssl.com/"); }
667668
`)
668669
require.NoError(t, err)
669-
r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts))
670+
require.NoError(t, r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts)))
670671

671672
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
672673
require.NoError(t, err)
@@ -681,7 +682,8 @@ func TestVUIntegrationInsecureRequests(t *testing.T) {
681682
}
682683
err = vu.RunOnce(context.Background())
683684
if data.errMsg != "" {
684-
assert.EqualError(t, err, data.errMsg)
685+
require.NotNil(t, err)
686+
assert.Contains(t, err.Error(), data.errMsg)
685687
} else {
686688
assert.NoError(t, err)
687689
}
@@ -703,10 +705,10 @@ func TestVUIntegrationBlacklistOption(t *testing.T) {
703705
if !assert.NoError(t, err) {
704706
return
705707
}
706-
r1.SetOptions(lib.Options{
708+
require.NoError(t, r1.SetOptions(lib.Options{
707709
Throw: null.BoolFrom(true),
708710
BlacklistIPs: []*lib.IPNet{cidr},
709-
})
711+
}))
710712

711713
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
712714
if !assert.NoError(t, err) {
@@ -721,7 +723,8 @@ func TestVUIntegrationBlacklistOption(t *testing.T) {
721723
return
722724
}
723725
err = vu.RunOnce(context.Background())
724-
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)")
726+
require.NotNil(t, err)
727+
assert.Contains(t, err.Error(), "IP (10.1.2.3) is in a blacklisted range (10.0.0.0/8)")
725728
})
726729
}
727730
}
@@ -756,7 +759,8 @@ func TestVUIntegrationBlacklistScript(t *testing.T) {
756759
return
757760
}
758761
err = vu.RunOnce(context.Background())
759-
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)")
762+
require.NotNil(t, err)
763+
assert.Contains(t, err.Error(), "IP (10.1.2.3) is in a blacklisted range (10.0.0.0/8)")
760764
})
761765
}
762766
}
@@ -830,7 +834,7 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
830834
},
831835
"UnsupportedCipherSuite": {
832836
lib.Options{TLSCipherSuites: &lib.TLSCipherSuites{tls.TLS_RSA_WITH_RC4_128_SHA}},
833-
"GoError: Get https://sha256.badssl.com/: remote error: tls: handshake failure",
837+
"remote error: tls: handshake failure",
834838
},
835839
"NullVersion": {
836840
lib.Options{},
@@ -842,10 +846,11 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
842846
},
843847
"UnsupportedVersion": {
844848
lib.Options{TLSVersion: &lib.TLSVersions{Min: tls.VersionSSL30, Max: tls.VersionSSL30}},
845-
"GoError: Get https://sha256.badssl.com/: " + unsupportedVersionErrorMsg,
849+
unsupportedVersionErrorMsg,
846850
},
847851
}
848852
for name, data := range testdata {
853+
data := data
849854
t.Run(name, func(t *testing.T) {
850855
r1, err := getSimpleRunner("/script.js", `
851856
import http from "k6/http";
@@ -854,7 +859,7 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
854859
if !assert.NoError(t, err) {
855860
return
856861
}
857-
r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts))
862+
require.NoError(t, r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}.Apply(data.opts)))
858863

859864
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
860865
if !assert.NoError(t, err) {
@@ -872,7 +877,8 @@ func TestVUIntegrationTLSConfig(t *testing.T) {
872877
}
873878
err = vu.RunOnce(context.Background())
874879
if data.errMsg != "" {
875-
assert.EqualError(t, err, data.errMsg)
880+
require.NotNil(t, err)
881+
assert.Contains(t, err.Error(), data.errMsg)
876882
} else {
877883
assert.NoError(t, err)
878884
}
@@ -894,10 +900,10 @@ func TestVUIntegrationHTTP2(t *testing.T) {
894900
if !assert.NoError(t, err) {
895901
return
896902
}
897-
r1.SetOptions(lib.Options{
903+
require.NoError(t, r1.SetOptions(lib.Options{
898904
Throw: null.BoolFrom(true),
899905
SystemTags: stats.NewSystemTagSet(stats.TagProto),
900-
})
906+
}))
901907

902908
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
903909
if !assert.NoError(t, err) {
@@ -1150,10 +1156,10 @@ func TestVUIntegrationClientCerts(t *testing.T) {
11501156
if !assert.NoError(t, err) {
11511157
return
11521158
}
1153-
r1.SetOptions(lib.Options{
1159+
require.NoError(t, r1.SetOptions(lib.Options{
11541160
Throw: null.BoolFrom(true),
11551161
InsecureSkipTLSVerify: null.BoolFrom(true),
1156-
})
1162+
}))
11571163

11581164
t.Run("Unauthenticated", func(t *testing.T) {
11591165
r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
@@ -1175,7 +1181,7 @@ func TestVUIntegrationClientCerts(t *testing.T) {
11751181
}
11761182
})
11771183

1178-
r1.SetOptions(lib.Options{
1184+
require.NoError(t, r1.SetOptions(lib.Options{
11791185
TLSAuth: []*lib.TLSAuth{
11801186
{
11811187
TLSAuthFields: lib.TLSAuthFields{
@@ -1199,7 +1205,7 @@ func TestVUIntegrationClientCerts(t *testing.T) {
11991205
},
12001206
},
12011207
},
1202-
})
1208+
}))
12031209

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

0 commit comments

Comments
 (0)