@@ -11,6 +11,8 @@ import (
11
11
"github.com/openshift/origin/pkg/build/api"
12
12
"github.com/openshift/origin/pkg/build/webhook"
13
13
kapi "k8s.io/kubernetes/pkg/api"
14
+ "k8s.io/kubernetes/pkg/api/errors"
15
+ "k8s.io/kubernetes/pkg/api/unversioned"
14
16
)
15
17
16
18
var mockBuildStrategy = api.BuildStrategy {
@@ -52,6 +54,24 @@ func GivenRequestWithRefsPayload(t *testing.T) *http.Request {
52
54
return req
53
55
}
54
56
57
+ func matchWarning (t * testing.T , err error , message string ) {
58
+ status , ok := err .(* errors.StatusError )
59
+ if ! ok {
60
+ t .Errorf ("Expected %v to be a StatusError object" , err )
61
+ return
62
+ }
63
+
64
+ if status .ErrStatus .Status != unversioned .StatusSuccess {
65
+ t .Errorf ("Unexpected response status %v, expected %v" , status .ErrStatus .Status , unversioned .StatusSuccess )
66
+ }
67
+ if status .ErrStatus .Code != http .StatusOK {
68
+ t .Errorf ("Unexpected response code %v, expected %v" , status .ErrStatus .Code , http .StatusOK )
69
+ }
70
+ if status .ErrStatus .Message != message {
71
+ t .Errorf ("Unexpected response message %v, expected %v" , status .ErrStatus .Message , message )
72
+ }
73
+ }
74
+
55
75
func TestVerifyRequestForMethod (t * testing.T ) {
56
76
req := GivenRequest ("GET" )
57
77
buildConfig := & api.BuildConfig {
@@ -327,9 +347,7 @@ func TestExtractWithUnmatchedRefGitPayload(t *testing.T) {
327
347
plugin := New ()
328
348
build , _ , proceed , err := plugin .Extract (buildConfig , "secret100" , "" , req )
329
349
330
- if err != nil {
331
- t .Errorf ("Unexpected error when triggering build: %v" , err )
332
- }
350
+ matchWarning (t , err , `skipping build. Branch reference from "refs/heads/master" does not match configuration` )
333
351
if proceed {
334
352
t .Error ("Expected 'proceed' return value to be 'false' for unmatched refs" )
335
353
}
@@ -471,9 +489,7 @@ func TestExtractWithUnmatchedGitRefsPayload(t *testing.T) {
471
489
plugin := New ()
472
490
revision , _ , proceed , err := plugin .Extract (buildConfig , "secret100" , "" , req )
473
491
474
- if err != nil {
475
- t .Errorf ("Expected to be able to trigger a build without a payload error: %v" , err )
476
- }
492
+ matchWarning (t , err , `skipping build. None of the supplied refs matched "other"` )
477
493
if proceed {
478
494
t .Error ("Expected 'proceed' return value to be 'false'" )
479
495
}
@@ -627,9 +643,7 @@ func TestGitlabPush(t *testing.T) {
627
643
plugin := New ()
628
644
_ , _ , proceed , err := plugin .Extract (buildConfig , "secret100" , "" , req )
629
645
630
- if err != nil {
631
- t .Errorf ("Expected to be able to trigger a build without a payload error: %v" , err )
632
- }
646
+ matchWarning (t , err , "no git information found in payload, ignoring and continuing with build" )
633
647
if ! proceed {
634
648
t .Error ("Expected 'proceed' return value to be 'true'" )
635
649
}
@@ -699,13 +713,115 @@ func TestExtractWithUnmarshalError(t *testing.T) {
699
713
}
700
714
plugin := New ()
701
715
revision , _ , proceed , err := plugin .Extract (buildConfig , "secret100" , "" , req )
702
- if err != nil {
703
- t .Errorf ("Expected to be able to trigger a build without a payload error: %v" , err )
716
+ matchWarning (t , err , `error unmarshalling payload: invalid character '\x00' looking for beginning of value, ignoring payload and continuing with build` )
717
+ if ! proceed {
718
+ t .Error ("Expected 'proceed' return value to be 'true'" )
719
+ }
720
+ if revision != nil {
721
+ t .Error ("Expected the 'revision' return value to be nil" )
722
+ }
723
+ }
724
+
725
+ func TestExtractWithUnmarshalErrorYAML (t * testing.T ) {
726
+ req , _ := http .NewRequest ("POST" , "http://someurl.com" , errJSON {})
727
+ req .Header .Add ("Content-Type" , "application/yaml" )
728
+ buildConfig := & api.BuildConfig {
729
+ Spec : api.BuildConfigSpec {
730
+ Triggers : []api.BuildTriggerPolicy {
731
+ {
732
+ Type : api .GenericWebHookBuildTriggerType ,
733
+ GenericWebHook : & api.WebHookTrigger {
734
+ Secret : "secret100" ,
735
+ },
736
+ },
737
+ },
738
+ CommonSpec : api.CommonSpec {
739
+ Source : api.BuildSource {
740
+ Git : & api.GitBuildSource {
741
+ Ref : "other" ,
742
+ },
743
+ },
744
+ Strategy : mockBuildStrategy ,
745
+ },
746
+ },
747
+ }
748
+ plugin := New ()
749
+ revision , _ , proceed , err := plugin .Extract (buildConfig , "secret100" , "" , req )
750
+ matchWarning (t , err , "error converting payload to json: yaml: control characters are not allowed, ignoring payload and continuing with build" )
751
+ if ! proceed {
752
+ t .Error ("Expected 'proceed' return value to be 'true'" )
753
+ }
754
+ if revision != nil {
755
+ t .Error ("Expected the 'revision' return value to be nil" )
756
+ }
757
+ }
758
+
759
+ func TestExtractWithBadContentType (t * testing.T ) {
760
+ req , _ := http .NewRequest ("POST" , "http://someurl.com" , errJSON {})
761
+ req .Header .Add ("Content-Type" , "bad" )
762
+ buildConfig := & api.BuildConfig {
763
+ Spec : api.BuildConfigSpec {
764
+ Triggers : []api.BuildTriggerPolicy {
765
+ {
766
+ Type : api .GenericWebHookBuildTriggerType ,
767
+ GenericWebHook : & api.WebHookTrigger {
768
+ Secret : "secret100" ,
769
+ },
770
+ },
771
+ },
772
+ CommonSpec : api.CommonSpec {
773
+ Source : api.BuildSource {
774
+ Git : & api.GitBuildSource {
775
+ Ref : "other" ,
776
+ },
777
+ },
778
+ Strategy : mockBuildStrategy ,
779
+ },
780
+ },
704
781
}
782
+ plugin := New ()
783
+ revision , _ , proceed , err := plugin .Extract (buildConfig , "secret100" , "" , req )
784
+ matchWarning (t , err , "invalid Content-Type on payload, ignoring payload and continuing with build" )
705
785
if ! proceed {
706
786
t .Error ("Expected 'proceed' return value to be 'true'" )
707
787
}
708
788
if revision != nil {
709
789
t .Error ("Expected the 'revision' return value to be nil" )
710
790
}
711
791
}
792
+
793
+ func TestExtractWithUnparseableContentType (t * testing.T ) {
794
+ req , _ := http .NewRequest ("POST" , "http://someurl.com" , errJSON {})
795
+ req .Header .Add ("Content-Type" , "bad//bad" )
796
+ buildConfig := & api.BuildConfig {
797
+ Spec : api.BuildConfigSpec {
798
+ Triggers : []api.BuildTriggerPolicy {
799
+ {
800
+ Type : api .GenericWebHookBuildTriggerType ,
801
+ GenericWebHook : & api.WebHookTrigger {
802
+ Secret : "secret100" ,
803
+ },
804
+ },
805
+ },
806
+ CommonSpec : api.CommonSpec {
807
+ Source : api.BuildSource {
808
+ Git : & api.GitBuildSource {
809
+ Ref : "other" ,
810
+ },
811
+ },
812
+ Strategy : mockBuildStrategy ,
813
+ },
814
+ },
815
+ }
816
+ plugin := New ()
817
+ revision , _ , proceed , err := plugin .Extract (buildConfig , "secret100" , "" , req )
818
+ if err == nil || err .Error () != "error parsing Content-Type: mime: expected token after slash" {
819
+ t .Errorf ("Unexpected error %v" , err )
820
+ }
821
+ if proceed {
822
+ t .Error ("Expected 'proceed' return value to be 'false'" )
823
+ }
824
+ if revision != nil {
825
+ t .Error ("Expected the 'revision' return value to be nil" )
826
+ }
827
+ }
0 commit comments