Skip to content

Commit ffebbbf

Browse files
committed
Add main_test
1 parent 9b1b4fd commit ffebbbf

File tree

2 files changed

+262
-25
lines changed

2 files changed

+262
-25
lines changed

main.go

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/lucasgomide/snitch/types"
99
"log"
1010
"os"
11-
"regexp"
11+
"strings"
1212
)
1313

1414
var (
@@ -21,40 +21,50 @@ func init() {
2121
flag.Parse()
2222
}
2323

24+
func execute(h snitch.Hook, t snitch.Tsuru, deploy []snitch.Deploy) error {
25+
var err error
26+
if *appNameContains != "" && !strings.Contains(os.Getenv("TSURU_APP_NAME"), *appNameContains) {
27+
return errors.New("Tsuru App Name does not match with " + *appNameContains)
28+
} else {
29+
h.SetWebHookURL(*webHookURL)
30+
31+
err = findLastDeploy(t, &deploy)
32+
if err != nil {
33+
return err
34+
}
35+
err = callHook(h, deploy)
36+
if err != nil {
37+
return err
38+
}
39+
return nil
40+
}
41+
}
42+
2443
func main() {
2544
var (
26-
h snitch.Hook
27-
t snitch.Tsuru
28-
err error
29-
deploy []snitch.Deploy
45+
h snitch.Hook
46+
t snitch.Tsuru
47+
err error
48+
deploy []snitch.Deploy
49+
hookDefined = false
3050
)
31-
tsuruAppName := os.Getenv("TSURU_APP_NAME")
3251

33-
if match, _ := regexp.MatchString(*appNameContains, tsuruAppName); !match && *appNameContains != "" {
52+
if err = validateParams(); err != nil {
53+
printError(err.Error())
3454
} else {
35-
36-
if err = validateParams(); err != nil {
37-
log.Fatal(err)
38-
}
39-
4055
switch *hookName {
4156
case "slack":
4257
h = &hook.Slack{}
58+
hookDefined = true
4359
default:
44-
log.Fatal("The service " + *hookName + "wasn't implemented yet")
60+
printError("The service " + *hookName + " wasn't implemented yet")
4561
}
62+
if hookDefined {
63+
t = tsuru.TsuruAPI{AppToken: os.Getenv("TSURU_APP_TOKEN"), ApiHost: os.Getenv("TSURU_HOST"), AppName: os.Getenv("TSURU_APP_NAME")}
4664

47-
h.SetWebHookURL(*webHookURL)
48-
49-
t = tsuru.TsuruAPI{AppToken: os.Getenv("TSURU_APP_TOKEN"), ApiHost: os.Getenv("TSURU_HOST"), AppName: tsuruAppName}
50-
51-
err = findLastDeploy(t, &deploy)
52-
if err != nil {
53-
log.Fatal(err)
54-
}
55-
err = callHook(h, deploy)
56-
if err != nil {
57-
log.Fatal(err)
65+
if err := execute(h, t, deploy); err != nil {
66+
printError(err.Error())
67+
}
5868
}
5969
}
6070
}
@@ -69,9 +79,13 @@ func callHook(hook snitch.Hook, deploy []snitch.Deploy) error {
6979

7080
func validateParams() error {
7181
if *hookName == "" {
72-
return errors.New("The option -hook is required ")
82+
return errors.New("The option -hook is required")
7383
} else if *webHookURL == "" {
7484
return errors.New("The option -hook-url is required")
7585
}
7686
return nil
7787
}
88+
89+
func printError(text string) {
90+
log.Printf(">> Snitch: %s", text)
91+
}

main_test.go

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"flag"
7+
"github.com/lucasgomide/snitch/types"
8+
"gopkg.in/jarcoal/httpmock.v1"
9+
"log"
10+
"os"
11+
"strings"
12+
"testing"
13+
)
14+
15+
type HookFake struct {
16+
Err error
17+
}
18+
19+
type TsuruFake struct {
20+
Err error
21+
}
22+
23+
func (h HookFake) CallHook(deploy []snitch.Deploy) error {
24+
if h.Err != nil {
25+
return h.Err
26+
}
27+
return nil
28+
}
29+
30+
func (h HookFake) SetWebHookURL(url string) {
31+
}
32+
33+
func (t TsuruFake) FindLastDeploy(deploy *[]snitch.Deploy) error {
34+
if t.Err != nil {
35+
return t.Err
36+
}
37+
return nil
38+
}
39+
40+
func setUpSuite() {
41+
os.Setenv("TSURU_APP_TOKEN", "abc123")
42+
os.Setenv("TSURU_HOST", "http://0.0.0.0")
43+
os.Setenv("TSURU_APP_NAME", "someapp-name")
44+
}
45+
46+
func tearDownSuite() {
47+
os.Unsetenv("TSURU_TARGET")
48+
os.Unsetenv("TSURU_TOKEN")
49+
flag.Set("hook", "")
50+
flag.Set("hook-url", "")
51+
flag.Set("app-name-contains", "")
52+
}
53+
54+
func TestExecutedSuccessfully(t *testing.T) {
55+
setUpSuite()
56+
defer tearDownSuite()
57+
flag.Set("hook", "slack")
58+
flag.Set("hook-url", "http://123")
59+
60+
var (
61+
h HookFake
62+
tsuru TsuruFake
63+
err error
64+
deploy []snitch.Deploy
65+
)
66+
67+
if err = execute(h, tsuru, deploy); err != nil {
68+
t.Error(err)
69+
}
70+
}
71+
72+
func TestReturnsErrorWhenAppNameDoesNotContainsSomething(t *testing.T) {
73+
setUpSuite()
74+
defer tearDownSuite()
75+
appContains := "$#@"
76+
flag.Set("hook", "slack")
77+
flag.Set("hook-url", "http://123")
78+
flag.Set("app-name-contains", appContains)
79+
80+
var (
81+
h HookFake
82+
tsuru TsuruFake
83+
err error
84+
deploy []snitch.Deploy
85+
)
86+
87+
wanted := "Tsuru App Name does not match with " + appContains
88+
err = execute(h, tsuru, deploy)
89+
90+
if err == nil {
91+
t.Error("Expected error, got nil")
92+
} else if err.Error() != wanted {
93+
t.Error("Expected error: " + wanted + ", wanted " + err.Error())
94+
}
95+
}
96+
97+
func TestReturnsErrorWhenFindLastDeployFail(t *testing.T) {
98+
setUpSuite()
99+
defer tearDownSuite()
100+
101+
flag.Set("hook-url", "http://123")
102+
flag.Set("hook", "somehook")
103+
104+
var (
105+
h HookFake
106+
tsuru TsuruFake
107+
err error
108+
deploy []snitch.Deploy
109+
)
110+
111+
expected := "FindLastDeploy has failed"
112+
tsuru.Err = errors.New(expected)
113+
err = execute(h, tsuru, deploy)
114+
115+
if err == nil {
116+
t.Error("Expected error, got nil")
117+
} else if err.Error() != expected {
118+
t.Error("Expected error: " + expected + ", got " + err.Error())
119+
}
120+
}
121+
122+
func TestReturnsErrorWhenCallHookFail(t *testing.T) {
123+
setUpSuite()
124+
defer tearDownSuite()
125+
126+
flag.Set("hook-url", "http://123")
127+
flag.Set("hook", "somehook")
128+
129+
var (
130+
h HookFake
131+
tsuru TsuruFake
132+
err error
133+
deploy []snitch.Deploy
134+
)
135+
136+
expected := "CallHook has failed"
137+
h.Err = errors.New(expected)
138+
err = execute(h, tsuru, deploy)
139+
140+
if err == nil {
141+
t.Error("Expected error, got nil")
142+
} else if err.Error() != expected {
143+
t.Error("Expected error: " + expected + ", got " + err.Error())
144+
}
145+
}
146+
147+
func TestPrintErrorWhenHookIsntSupported(t *testing.T) {
148+
setUpSuite()
149+
defer tearDownSuite()
150+
151+
flag.Set("hook", "somehook")
152+
flag.Set("hook-url", "http://foo.bar")
153+
154+
var buf bytes.Buffer
155+
log.SetOutput(&buf)
156+
157+
main()
158+
159+
expected := "The service somehook wasn't implemented yet"
160+
msg := buf.String()
161+
if !strings.Contains(msg, expected) {
162+
t.Errorf("%#v, wanted %#v", msg, expected)
163+
}
164+
}
165+
166+
func TestPrintErrorWhenHookURLFlagIsEmpty(t *testing.T) {
167+
setUpSuite()
168+
defer tearDownSuite()
169+
170+
flag.Set("hook", "somehook")
171+
172+
var buf bytes.Buffer
173+
log.SetOutput(&buf)
174+
175+
main()
176+
177+
expected := "The option -hook-url is required\n"
178+
msg := buf.String()
179+
if !strings.Contains(msg, expected) {
180+
t.Errorf("%#v, wanted %#v", msg, expected)
181+
}
182+
}
183+
184+
func TestPrintErrorWhenHookFlagIsEmpty(t *testing.T) {
185+
setUpSuite()
186+
defer tearDownSuite()
187+
188+
flag.Set("hook-url", "http://foo.bar")
189+
190+
var buf bytes.Buffer
191+
log.SetOutput(&buf)
192+
193+
main()
194+
195+
expected := "The option -hook is required\n"
196+
msg := buf.String()
197+
if !strings.Contains(msg, expected) {
198+
t.Errorf("%#v, wanted %#v", msg, expected)
199+
}
200+
}
201+
202+
func TestPrintErrorWhenHookRequestFail(t *testing.T) {
203+
setUpSuite()
204+
defer tearDownSuite()
205+
flag.Set("hook-url", "http://foo.bar")
206+
flag.Set("hook", "slack")
207+
208+
httpmock.Activate()
209+
defer httpmock.DeactivateAndReset()
210+
211+
httpmock.RegisterNoResponder(nil)
212+
213+
var buf bytes.Buffer
214+
log.SetOutput(&buf)
215+
216+
main()
217+
218+
expected := "no responder found"
219+
msg := buf.String()
220+
if !strings.Contains(msg, expected) {
221+
t.Errorf("%#v, wanted %#v", msg, expected)
222+
}
223+
}

0 commit comments

Comments
 (0)