@@ -21,60 +21,62 @@ func TestWebhook(t *testing.T) {
2121 hookecho , cleanupHookecho := buildHookecho (t )
2222 defer cleanupHookecho ()
2323
24- config , cleanupConfig := genConfig (t , hookecho )
25- defer cleanupConfig ()
26-
27- webhook , cleanupWebhook := buildWebhook (t )
28- defer cleanupWebhook ()
29-
30- ip , port := serverAddress (t )
31- args := []string {fmt .Sprintf ("-hooks=%s" , config ), fmt .Sprintf ("-ip=%s" , ip ), fmt .Sprintf ("-port=%s" , port ), "-verbose" }
32-
33- cmd := exec .Command (webhook , args ... )
34- //cmd.Stderr = os.Stderr // uncomment to see verbose output
35- cmd .Env = webhookEnv ()
36- cmd .Args [0 ] = "webhook"
37- if err := cmd .Start (); err != nil {
38- t .Fatalf ("failed to start webhook: %s" , err )
39- }
40- defer killAndWait (cmd )
24+ for _ , hookTmpl := range []string {"test/hooks.json.tmpl" , "test/hooks.yaml.tmpl" } {
25+ config , cleanupConfig := genConfig (t , hookecho , hookTmpl )
26+ defer cleanupConfig ()
27+
28+ webhook , cleanupWebhook := buildWebhook (t )
29+ defer cleanupWebhook ()
30+
31+ ip , port := serverAddress (t )
32+ args := []string {fmt .Sprintf ("-hooks=%s" , config ), fmt .Sprintf ("-ip=%s" , ip ), fmt .Sprintf ("-port=%s" , port ), "-verbose" }
33+
34+ cmd := exec .Command (webhook , args ... )
35+ //cmd.Stderr = os.Stderr // uncomment to see verbose output
36+ cmd .Env = webhookEnv ()
37+ cmd .Args [0 ] = "webhook"
38+ if err := cmd .Start (); err != nil {
39+ t .Fatalf ("failed to start webhook: %s" , err )
40+ }
41+ defer killAndWait (cmd )
4142
42- waitForServerReady (t , ip , port )
43+ waitForServerReady (t , ip , port )
4344
44- for _ , tt := range hookHandlerTests {
45- url := fmt .Sprintf ("http://%s:%s/hooks/%s" , ip , port , tt .id )
45+ for _ , tt := range hookHandlerTests {
46+ url := fmt .Sprintf ("http://%s:%s/hooks/%s" , ip , port , tt .id )
4647
47- req , err := http .NewRequest ("POST" , url , ioutil .NopCloser (strings .NewReader (tt .body )))
48- if err != nil {
49- t .Errorf ("New request failed: %s" , err )
50- }
48+ req , err := http .NewRequest ("POST" , url , ioutil .NopCloser (strings .NewReader (tt .body )))
49+ if err != nil {
50+ t .Errorf ("New request failed: %s" , err )
51+ }
5152
52- for k , v := range tt .headers {
53- req .Header .Add (k , v )
54- }
53+ for k , v := range tt .headers {
54+ req .Header .Add (k , v )
55+ }
5556
56- var res * http.Response
57+ var res * http.Response
5758
58- if tt .urlencoded == true {
59- req .Header .Add ("Content-Type" , "application/x-www-form-urlencoded" )
60- } else {
61- req .Header .Add ("Content-Type" , "application/json" )
62- }
59+ if tt .urlencoded == true {
60+ req .Header .Add ("Content-Type" , "application/x-www-form-urlencoded" )
61+ } else {
62+ req .Header .Add ("Content-Type" , "application/json" )
63+ }
6364
64- client := & http.Client {}
65- res , err = client .Do (req )
66- if err != nil {
67- t .Errorf ("client.Do failed: %s" , err )
68- }
65+ client := & http.Client {}
66+ res , err = client .Do (req )
67+ if err != nil {
68+ t .Errorf ("client.Do failed: %s" , err )
69+ }
6970
70- body , err := ioutil .ReadAll (res .Body )
71- res .Body .Close ()
72- if err != nil {
73- t .Errorf ("POST %q: failed to ready body: %s" , tt .desc , err )
74- }
71+ body , err := ioutil .ReadAll (res .Body )
72+ res .Body .Close ()
73+ if err != nil {
74+ t .Errorf ("POST %q: failed to ready body: %s" , tt .desc , err )
75+ }
7576
76- if res .StatusCode != tt .respStatus || string (body ) != tt .respBody {
77- t .Errorf ("failed %q (id: %s):\n expected status: %#v, response: %s\n got status: %#v, response: %s" , tt .desc , tt .id , tt .respStatus , tt .respBody , res .StatusCode , body )
77+ if res .StatusCode != tt .respStatus || string (body ) != tt .respBody {
78+ t .Errorf ("failed %q (id: %s):\n expected status: %#v, response: %s\n got status: %#v, response: %s" , tt .desc , tt .id , tt .respStatus , tt .respBody , res .StatusCode , body )
79+ }
7880 }
7981 }
8082}
@@ -103,8 +105,8 @@ func buildHookecho(t *testing.T) (bin string, cleanup func()) {
103105 return bin , func () { os .RemoveAll (tmp ) }
104106}
105107
106- func genConfig (t * testing.T , bin string ) (config string , cleanup func ()) {
107- tmpl := template .Must (template .ParseFiles ("test/hooks.json.tmpl" ))
108+ func genConfig (t * testing.T , bin string , hookTemplate string ) (config string , cleanup func ()) {
109+ tmpl := template .Must (template .ParseFiles (hookTemplate ))
108110
109111 tmp , err := ioutil .TempDir ("" , "webhook-config-" )
110112 if err != nil {
@@ -116,7 +118,9 @@ func genConfig(t *testing.T, bin string) (config string, cleanup func()) {
116118 }
117119 }()
118120
119- path := filepath .Join (tmp , "hooks.json" )
121+ outputBaseName := filepath .Ext (filepath .Ext (hookTemplate ))
122+
123+ path := filepath .Join (tmp , outputBaseName )
120124 file , err := os .Create (path )
121125 if err != nil {
122126 t .Fatalf ("Creating config template: %v" , err )
0 commit comments