|
| 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