github.com/stakater/IngressMonitorController@v1.0.103/pkg/monitors/uptimerobot/uptime-monitor_test.go (about) 1 package uptimerobot 2 3 import ( 4 "strings" 5 "testing" 6 7 log "github.com/sirupsen/logrus" 8 "github.com/stakater/IngressMonitorController/pkg/util" 9 10 "github.com/stakater/IngressMonitorController/pkg/config" 11 "github.com/stakater/IngressMonitorController/pkg/models" 12 ) 13 14 // Not a test case. Cleanup to remove added dummy Monitors 15 func TestRemoveDanglingMonitors(t *testing.T) { 16 config := config.GetControllerConfig() 17 18 service := UpTimeMonitorService{} 19 provider := util.GetProviderWithName(config, "UptimeRobot") 20 service.Setup(*provider) 21 22 mons, err := service.GetAllByName("google-test") 23 24 log.Println(mons) 25 if err == nil && mons == nil { 26 log.Println("No Dangling Monitors") 27 } 28 if err == nil && mons != nil { 29 for _, mon := range mons { 30 service.Remove(mon) 31 } 32 } 33 } 34 35 func TestAddMonitorWithCorrectValues(t *testing.T) { 36 config := config.GetControllerConfig() 37 38 service := UpTimeMonitorService{} 39 // service.Setup(config.Providers[0]) 40 provider := util.GetProviderWithName(config, "UptimeRobot") 41 service.Setup(*provider) 42 43 m := models.Monitor{Name: "google-test", URL: "https://google.com"} 44 service.Add(m) 45 46 mRes, err := service.GetByName("google-test") 47 48 if err != nil { 49 t.Error("Error: " + err.Error()) 50 } 51 if mRes.Name != m.Name { 52 t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 53 } 54 if mRes.URL != m.URL { 55 t.Error("The URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL) 56 } 57 service.Remove(*mRes) 58 } 59 60 func TestUpdateMonitorWithCorrectValues(t *testing.T) { 61 config := config.GetControllerConfig() 62 63 service := UpTimeMonitorService{} 64 provider := util.GetProviderWithName(config, "UptimeRobot") 65 service.Setup(*provider) 66 67 m := models.Monitor{Name: "google-test", URL: "https://google.com"} 68 service.Add(m) 69 70 mRes, err := service.GetByName("google-test") 71 72 if err != nil { 73 t.Error("Error: " + err.Error()) 74 } 75 if mRes.Name != m.Name { 76 t.Error("The initial name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 77 } 78 if mRes.URL != m.URL { 79 t.Error("The initial URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL) 80 } 81 82 mRes.URL = "https://facebook.com" 83 84 service.Update(*mRes) 85 86 mRes, err = service.GetByName("google-test") 87 88 if err != nil { 89 t.Error("Error: " + err.Error()) 90 } 91 if mRes.URL != "https://facebook.com" { 92 t.Error("The URL should have been updated, expected: https://facebook.com, but was: " + mRes.URL) 93 } 94 95 service.Remove(*mRes) 96 } 97 98 func TestAddMonitorWithIntervalAnnotations(t *testing.T) { 99 config := config.GetControllerConfig() 100 101 service := UpTimeMonitorService{} 102 provider := util.GetProviderWithName(config, "UptimeRobot") 103 service.Setup(*provider) 104 105 var annotations = map[string]string{ 106 "uptimerobot.monitor.stakater.com/interval": "600", 107 } 108 109 m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations} 110 service.Add(m) 111 112 mRes, err := service.GetByName("google-test") 113 114 if err != nil { 115 t.Error("Error: " + err.Error()) 116 } 117 if mRes.Name != m.Name { 118 t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 119 } 120 if mRes.URL != m.URL { 121 t.Error("The URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL) 122 } 123 if "600" != mRes.Annotations["uptimerobot.monitor.stakater.com/interval"] { 124 t.Error("The interval is incorrect, expected: 600, but was: " + mRes.Annotations["uptimerobot.monitor.stakater.com/interval"]) 125 } 126 service.Remove(*mRes) 127 } 128 129 func TestUpdateMonitorIntervalAnnotations(t *testing.T) { 130 config := config.GetControllerConfig() 131 132 service := UpTimeMonitorService{} 133 provider := util.GetProviderWithName(config, "UptimeRobot") 134 service.Setup(*provider) 135 136 var annotations = map[string]string{ 137 "uptimerobot.monitor.stakater.com/interval": "600", 138 } 139 140 m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations} 141 service.Add(m) 142 143 mRes, err := service.GetByName("google-test") 144 145 if err != nil { 146 t.Error("Error: " + err.Error()) 147 } 148 if mRes.Name != m.Name { 149 t.Error("The initial name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 150 } 151 if mRes.URL != m.URL { 152 t.Error("The initial URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL) 153 } 154 if "600" != mRes.Annotations["uptimerobot.monitor.stakater.com/interval"] { 155 t.Error("The initial interval is incorrect: 600, but was: " + mRes.Annotations["uptimerobot.monitor.stakater.com/interval"]) 156 } 157 158 mRes.URL = "https://facebook.com" 159 annotations["uptimerobot.monitor.stakater.com/interval"] = "900" 160 mRes.Annotations = annotations 161 162 service.Update(*mRes) 163 164 mRes, err = service.GetByName("google-test") 165 166 if err != nil { 167 t.Error("Error: " + err.Error()) 168 } 169 if mRes.URL != "https://facebook.com" { 170 t.Error("The updated URL is incorrect, expected: https://facebook.com, but was: " + mRes.URL) 171 } 172 if "900" != mRes.Annotations["uptimerobot.monitor.stakater.com/interval"] { 173 t.Error("The updated interval is incorrect, expected: 900, but was: " + mRes.Annotations["uptimerobot.monitor.stakater.com/interval"]) 174 } 175 176 service.Remove(*mRes) 177 } 178 179 func TestAddMonitorWithStatusPageAnnotations(t *testing.T) { 180 config := config.GetControllerConfig() 181 182 service := UpTimeMonitorService{} 183 provider := util.GetProviderWithName(config, "UptimeRobot") 184 service.Setup(*provider) 185 186 statusPageService := UpTimeStatusPageService{} 187 statusPageService.Setup(config.Providers[0]) 188 189 statusPage := UpTimeStatusPage{Name: "status-page-test"} 190 ID, err := statusPageService.Add(statusPage) 191 if err != nil { 192 t.Error("Error: " + err.Error()) 193 } 194 statusPage.ID = ID 195 196 var annotations = map[string]string{ 197 "uptimerobot.monitor.stakater.com/status-pages": statusPage.ID, 198 } 199 200 m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations} 201 service.Add(m) 202 203 mRes, err := service.GetByName("google-test") 204 205 if err != nil { 206 t.Error("Error: " + err.Error()) 207 } 208 if mRes.Name != m.Name { 209 t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 210 } 211 if mRes.URL != m.URL { 212 t.Error("The URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL) 213 } 214 statusPageRes, err := statusPageService.Get(statusPage.ID) 215 if err != nil { 216 t.Error("Error: " + err.Error()) 217 } 218 if !util.ContainsString(statusPageRes.Monitors, mRes.ID) { 219 t.Error("The status page does not contain the monitor, expected: " + mRes.ID + ", but was: " + strings.Join(statusPageRes.Monitors, "-")) 220 } 221 service.Remove(*mRes) 222 statusPageService.Remove(statusPage) 223 } 224 225 func TestUpdateMonitorIntervalStatusPageAnnotations(t *testing.T) { 226 config := config.GetControllerConfig() 227 228 service := UpTimeMonitorService{} 229 provider := util.GetProviderWithName(config, "UptimeRobot") 230 service.Setup(*provider) 231 232 statusPageService := UpTimeStatusPageService{} 233 statusPageService.Setup(*provider) 234 235 m := models.Monitor{Name: "google-test", URL: "https://google.com"} 236 service.Add(m) 237 238 mRes, err := service.GetByName("google-test") 239 240 if err != nil { 241 t.Error("Error: " + err.Error()) 242 } 243 if mRes.Name != m.Name { 244 t.Error("The initial name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 245 } 246 if mRes.URL != m.URL { 247 t.Error("The initial URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL) 248 } 249 250 statusPage := UpTimeStatusPage{Name: "status-page-test"} 251 ID, err := statusPageService.Add(statusPage) 252 if err != nil { 253 t.Error("Error: " + err.Error()) 254 } 255 statusPage.ID = ID 256 257 var annotations = map[string]string{ 258 "uptimerobot.monitor.stakater.com/status-pages": statusPage.ID, 259 } 260 261 mRes.URL = "https://facebook.com" 262 mRes.Annotations = annotations 263 264 service.Update(*mRes) 265 266 mRes, err = service.GetByName("google-test") 267 268 if err != nil { 269 t.Error("Error: " + err.Error()) 270 } 271 if mRes.URL != "https://facebook.com" { 272 t.Error("The updated URL is incorrect, expected: https://facebook.com, but was: " + mRes.URL) 273 } 274 statusPageRes, err := statusPageService.Get(statusPage.ID) 275 if err != nil { 276 t.Error("Error: " + err.Error()) 277 } 278 if !util.ContainsString(statusPageRes.Monitors, mRes.ID) { 279 t.Error("The status page does not contain the monitor, expected: " + mRes.ID + ", but was: " + strings.Join(statusPageRes.Monitors, "-")) 280 } 281 282 service.Remove(*mRes) 283 statusPageService.Remove(statusPage) 284 } 285 286 func TestAddMonitorWithMonitorTypeAnnotations(t *testing.T) { 287 config := config.GetControllerConfig() 288 289 service := UpTimeMonitorService{} 290 provider := util.GetProviderWithName(config, "UptimeRobot") 291 service.Setup(*provider) 292 293 // Check for monitor type 'keyword' 294 var annotations = map[string]string{ 295 "uptimerobot.monitor.stakater.com/monitor-type": "keyword", 296 "uptimerobot.monitor.stakater.com/keyword-exists": "yes", 297 "uptimerobot.monitor.stakater.com/keyword-value": "google", 298 } 299 300 m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations} 301 service.Add(m) 302 303 mRes, err := service.GetByName("google-test") 304 305 if err != nil { 306 t.Error("Error: " + err.Error()) 307 } 308 if mRes.Name != m.Name { 309 t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 310 } 311 312 service.Remove(*mRes) 313 314 // Check for monitor type 'http' 315 annotations = map[string]string{ 316 "uptimerobot.monitor.stakater.com/monitor-type": "http", 317 } 318 319 m = models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations} 320 service.Add(m) 321 322 mRes, err = service.GetByName("google-test") 323 324 if err != nil { 325 t.Error("Error: " + err.Error()) 326 } 327 if mRes.Name != m.Name { 328 t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 329 } 330 331 service.Remove(*mRes) 332 } 333 334 func TestAddMonitorWithIncorrectValues(t *testing.T) { 335 config := config.GetControllerConfig() 336 337 service := UpTimeMonitorService{} 338 provider := util.GetProviderWithName(config, "UptimeRobot") 339 provider.ApiKey = "dummy-api-key" 340 service.Setup(*provider) 341 342 m := models.Monitor{Name: "google-test", URL: "https://google.com"} 343 service.Add(m) 344 345 mRes, err := service.GetByName("google-test") 346 347 if err != nil { 348 t.Error("Error: " + err.Error()) 349 } 350 351 if mRes != nil { 352 t.Error("Monitor should not be added") 353 } 354 } 355 356 func TestAddMonitorWithAlertContactsAnnotations(t *testing.T) { 357 config := config.GetControllerConfig() 358 359 service := UpTimeMonitorService{} 360 provider := util.GetProviderWithName(config, "UptimeRobot") 361 service.Setup(*provider) 362 363 var annotations = map[string]string{ 364 "uptimerobot.monitor.stakater.com/alert-contacts": "2628365_0_0", 365 } 366 367 m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations} 368 service.Add(m) 369 370 mRes, err := service.GetByName("google-test") 371 372 if err != nil { 373 t.Error("Error: " + err.Error()) 374 } 375 if mRes.Name != m.Name { 376 t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name) 377 } 378 if mRes.URL != m.URL { 379 t.Error("The URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL) 380 } 381 if "2628365_0_0" != mRes.Annotations["uptimerobot.monitor.stakater.com/alert-contacts"] { 382 t.Error("The alert-contacts is incorrect, expected: 2628365_0_0, but was: " + mRes.Annotations["uptimerobot.monitor.stakater.com/alert-contacts"]) 383 } 384 service.Remove(*mRes) 385 }