test_check_surykatka_json.py 27.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
from slapos.grid.promise import PromiseError
from slapos.test.promise.plugin import TestPromisePluginMixin

import os
import shutil
import tempfile


class CheckSurykatkaJSONMixin(TestPromisePluginMixin):
  promise_name = 'check-surykatka-json.py'

  def setUp(self):
    self.working_directory = tempfile.mkdtemp()
    self.json_file = os.path.join(self.working_directory, 'surykatka.json')
    self.addCleanup(shutil.rmtree, self.working_directory)
    TestPromisePluginMixin.setUp(self)

  def writeSurykatkaPromise(self, d=None):
    if d is None:
      d = {}
    content_list = [
      "from slapos.promise.plugin.check_surykatka_json import RunPromise"]
    content_list.append('extra_config_dict = {')
    for k, v in d.items():
      content_list.append("  '%s': '%s'," % (k, v))
    content_list.append('}')
    self.writePromise(self.promise_name, '\n'.join(content_list))

  def writeSurykatkaJson(self, content):
    with open(self.json_file, 'w') as fh:
      fh.write(content)

  def assertFailedMessage(self, result, message):
    self.assertEqual(result['result']['failed'], True)
    self.assertEqual(
      result['result']['message'],
      message)

  def assertPassedMessage(self, result, message):
    self.assertEqual(result['result']['failed'], False)
    self.assertEqual(
      result['result']['message'],
      message)


class TestCheckSurykatkaJSON(CheckSurykatkaJSONMixin):
  def test_no_config(self):
    self.writeSurykatkaPromise()
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
54
      "ERROR File '' does not exists")
55 56 57 58 59 60 61 62

  def test_not_existing_file(self):
    self.writeSurykatkaPromise({'json-file': self.json_file})
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
63
      "ERROR File '%s' does not exists" % (self.json_file,))
64 65 66 67 68 69 70 71 72

  def test_empty_file(self):
    self.writeSurykatkaPromise({'json-file': self.json_file})
    self.writeSurykatkaJson('')
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
73
      "ERROR loading JSON from '%s'" % (self.json_file,))
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91


class TestCheckSurykatkaJSONUnknownReport(CheckSurykatkaJSONMixin):
  def test(self):
    self.writeSurykatkaPromise(
      {
        'report': 'NOT_EXISTING_ENTRY',
        'json-file': self.json_file,
      }
    )
    self.writeSurykatkaJson("""{
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
92
      "ERROR Report 'NOT_EXISTING_ENTRY' is not supported")
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116


class TestCheckSurykatkaJSONBotStatus(CheckSurykatkaJSONMixin):
  def test(self):
    self.writeSurykatkaPromise(
      {
        'report': 'bot_status',
        'json-file': self.json_file,
        'test-utcnow': 'Wed, 13 Dec 2222 09:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "bot_status": [
        {
            "date": "Wed, 13 Dec 2222 09:10:11 -0000",
            "text": "loop"
        }
    ]
}
""")
    self.configureLauncher()
    self.launcher.run()
    self.assertPassedMessage(
      self.getPromiseResult(self.promise_name),
117 118
      "bot_status: OK Last bot status from Fri, 13 Dec 2222 08:10:11 -0000, "
      "UTC now is Wed, 13 Dec 2222 09:11:12 -0000"
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    )

  def test_bot_status_future(self):
    self.writeSurykatkaPromise(
      {
        'report': 'bot_status',
        'json-file': self.json_file,
        'test-utcnow': 'Wed, 13 Dec 2222 09:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "bot_status": [
        {
            "date": "Wed, 13 Dec 2223 09:10:11 -0000",
            "text": "loop"
        }
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
143
      "bot_status: ERROR Last bot datetime Sat, 13 Dec 2223 08:10:11 -0000 is "
144
      "in future, UTC now Wed, 13 Dec 2222 09:11:12 -0000"
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    )

  def test_bot_status_old(self):
    self.writeSurykatkaPromise(
      {
        'report': 'bot_status',
        'json-file': self.json_file,
        'test-utcnow': 'Wed, 13 Dec 2223 09:26:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "bot_status": [
        {
            "date": "Wed, 13 Dec 2223 09:10:11 -0000",
            "text": "loop"
        }
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
169
      "bot_status: ERROR Last bot datetime Sat, 13 Dec 2223 08:10:11 -0000 is "
170
      "more than 15 minutes old, UTC now Wed, 13 Dec 2223 09:26:12 -0000"
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    )

  def test_not_bot_status(self):
    self.writeSurykatkaPromise(
      {
        'report': 'bot_status',
        'json-file': self.json_file,
      }
    )
    self.writeSurykatkaJson("""{
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
188
      "bot_status: ERROR 'bot_status' not in '%s'" % (self.json_file,))
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

  def test_empty_bot_status(self):
    self.writeSurykatkaPromise(
      {
        'report': 'bot_status',
        'json-file': self.json_file,
      }
    )
    self.writeSurykatkaJson("""{
  "bot_status": []
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
206
      "bot_status: ERROR 'bot_status' empty in '%s'" % (self.json_file,))
207 208 209 210 211 212 213 214 215 216


class TestCheckSurykatkaJSONHttpQuery(CheckSurykatkaJSONMixin):
  def test(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '302',
217 218
        'ip-list': '127.0.0.1 127.0.0.2',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/"
        }
241 242 243 244 245 246 247 248 249 250 251 252 253 254
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
255 256 257 258 259 260 261
    ]
}
""")
    self.configureLauncher()
    self.launcher.run()
    self.assertPassedMessage(
      self.getPromiseResult(self.promise_name),
262 263 264 265
      "https://www.erp5.com/ : http_query: OK status code 302 on IPs "
      "127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate will expire on "
      "Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days, UTC now "
      "is Fri, 27 Dec 2019 15:11:12 -0000"
266 267
    )

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
  def test_maximum_elapsed_time(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '302',
        'ip-list': '127.0.0.1 127.0.0.2',
        'maximum-elapsed-time': '5',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/",
            "total_seconds": 4
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 302,
            "url": "https://www.erp5.com/",
            "total_seconds": 4
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/",
            "total_seconds": 4
        }
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
    ]
}
""")
    self.configureLauncher()
    self.launcher.run()
    self.assertPassedMessage(
      self.getPromiseResult(self.promise_name),
324 325 326 327 328 329
      "https://www.erp5.com/ : http_query: OK status code 302 on IPs "
      "127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate will expire on "
      "Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days, UTC now "
      "is Fri, 27 Dec 2019 15:11:12 -0000 elapsed_time: OK IP 127.0.0.1 "
      "replied in 4.00s which is shorter than maximum 5.00s elapsed_time: OK "
      "IP 127.0.0.2 replied in 4.00s which is shorter than maximum 5.00s"
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    )

  def test_maximum_elapsed_time_too_long(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '302',
        'ip-list': '127.0.0.1 127.0.0.2',
        'maximum-elapsed-time': '5',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/",
            "total_seconds": 6
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 302,
            "url": "https://www.erp5.com/",
358
            "total_seconds": 0
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/",
            "total_seconds": 4
        }
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
389
      "https://www.erp5.com/ : elapsed_time: ERROR IP 127.0.0.1 replied in "
390 391 392 393 394
      "6.00s which is longer than maximum 5.00s elapsed_time: ERROR IP "
      "127.0.0.2 failed to reply http_query: OK status code 302 on IPs "
      "127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate will expire on "
      "Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days, UTC now "
      "is Fri, 27 Dec 2019 15:11:12 -0000"
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    )

  def test_maximum_elapsed_time_no_total_seconds(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '302',
        'ip-list': '127.0.0.1 127.0.0.2',
        'maximum-elapsed-time': '5',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/"
        }
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
    ]
}
""")
    self.configureLauncher()
    self.launcher.run()
    self.assertPassedMessage(
      self.getPromiseResult(self.promise_name),
450 451 452 453
      "https://www.erp5.com/ : http_query: OK status code 302 on IPs "
      "127.0.0.1 127.0.0.2 ssl_certificate: OK Certificate will expire on "
      "Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 days, UTC now "
      "is Fri, 27 Dec 2019 15:11:12 -0000"
454 455
    )

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
  def test_http(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'http://www.erp5.com/',
        'status-code': '302',
        'ip-list': '127.0.0.1 127.0.0.2',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "http://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 302,
            "url": "http://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "http://www.erp5.org/"
        }
    ],
    "ssl_certificate": [
    ]
}
""")
    self.configureLauncher()
    self.launcher.run()
    self.assertPassedMessage(
      self.getPromiseResult(self.promise_name),
496 497
      "http://www.erp5.com/ : http_query: OK status code 302 on IPs 127.0.0.1 "
      "127.0.0.2"
498 499
    )

500 501 502 503 504 505 506
  def test_no_ip_list(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '302',
507
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/"
        }
530 531 532 533 534 535 536 537 538 539 540 541 542 543
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
544 545 546 547 548 549 550
    ]
}
""")
    self.configureLauncher()
    self.launcher.run()
    self.assertPassedMessage(
      self.getPromiseResult(self.promise_name),
551 552 553 554
      "https://www.erp5.com/ : http_query: OK with status code 302 "
      "ssl_certificate: OK Certificate will expire on Mon, 13 Jul 2020 "
      "12:00:00 -0000, which is more than 15 days, UTC now is Fri, 27 Dec "
      "2019 15:11:12 -0000"
555 556
    )

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
  def test_no_http_query_data(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '302',
        'ip-list': '127.0.0.1 127.0.0.2',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
592 593 594 595
      "https://www.erp5.com/ : http_query: ERROR No data elapsed_time: ERROR "
      "No data ssl_certificate: OK Certificate will expire on Mon, 13 Jul "
      "2020 12:00:00 -0000, which is more than 15 days, UTC now is Fri, 27 "
      "Dec 2019 15:11:12 -0000"
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
    )

  def test_no_ssl_certificate_data(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '302',
        'ip-list': '127.0.0.1 127.0.0.2',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/"
        }
    ],
    "ssl_certificate": [
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
639 640
      "https://www.erp5.com/ : ssl_certificate: ERROR No data http_query: "
      "OK status code 302 on IPs 127.0.0.1 127.0.0.2"
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    )

  def test_no_ssl_certificate(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '302',
        'ip-list': '127.0.0.1 127.0.0.2',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/"
        }
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
682 683 684
      "https://www.erp5.com/ : ssl_certificate: ERROR No key "
      "'ssl_certificate'. If the error persist, please update surykatka. "
      "http_query: OK status code 302 on IPs 127.0.0.1 127.0.0.2"
685 686
    )

687 688 689 690 691 692 693
  def test_bad_code(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '301',
694
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.2",
            "status_code": 301,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/"
        }
717 718 719 720 721 722 723 724 725 726 727 728 729 730
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
731 732 733 734 735 736 737 738
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
739 740 741 742
      "https://www.erp5.com/ : http_query: ERROR IP 127.0.0.1 got status code "
      "302 instead of 301 ssl_certificate: OK Certificate will expire on Mon, "
      "13 Jul 2020 12:00:00 -0000, which is more than 15 days, UTC now is "
      "Fri, 27 Dec 2019 15:11:12 -0000"
743 744
    )

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
  def _test_bad_code_explanation(self, status_code, explanation):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '301',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": %s,
            "url": "https://www.erp5.com/"
        }
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
    ]
}
""" % status_code)
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
785 786 787 788
      "https://www.erp5.com/ : http_query: ERROR IP 127.0.0.1 got status code "
      "%s instead of 301 ssl_certificate: OK Certificate will expire on Mon, "
      "13 Jul 2020 12:00:00 -0000, which is more than 15 days, UTC now is "
      "Fri, 27 Dec 2019 15:11:12 -0000" % (explanation,)
789 790 791 792 793 794 795 796 797 798 799 800 801 802
    )

  def test_bad_code_explanation_520(self):
    self._test_bad_code_explanation(520, '520 (Too many redirects)')

  def test_bad_code_explanation_523(self):
    self._test_bad_code_explanation(523, '523 (Connection error)')

  def test_bad_code_explanation_524(self):
    self._test_bad_code_explanation(524, '524 (Connection timeout)')

  def test_bad_code_explanation_526(self):
    self._test_bad_code_explanation(526, '526 (SSL Error)')

803 804 805 806 807 808 809
  def test_bad_ip(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '301',
810 811
        'ip-list': '127.0.0.1 127.0.0.2',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 301,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.4",
            "status_code": 301,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/"
        }
834 835 836 837 838 839 840 841 842 843 844 845 846 847
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
848 849 850 851 852 853 854 855
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
856 857 858 859
      "https://www.erp5.com/ : http_query: ERROR expected IPs 127.0.0.1 "
      "127.0.0.2 differes from got 127.0.0.1 127.0.0.4 ssl_certificate: OK "
      "Certificate will expire on Mon, 13 Jul 2020 12:00:00 -0000, which is "
      "more than 15 days, UTC now is Fri, 27 Dec 2019 15:11:12 -0000"
860 861 862 863 864 865 866 867 868
    )

  def test_bad_ip_status_code(self):
    self.writeSurykatkaPromise(
      {
        'report': 'http_query',
        'json-file': self.json_file,
        'url': 'https://www.erp5.com/',
        'status-code': '301',
869 870
        'ip-list': '127.0.0.1 127.0.0.2',
        'test-utcnow': 'Fri, 27 Dec 2019 15:11:12 -0000'
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
      }
    )
    self.writeSurykatkaJson("""{
    "http_query": [
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.1",
            "status_code": 302,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "127.0.0.4",
            "status_code": 301,
            "url": "https://www.erp5.com/"
        },
        {
            "date": "Wed, 11 Dec 2019 09:35:28 -0000",
            "ip": "176.31.129.213",
            "status_code": 200,
            "url": "https://www.erp5.org/"
        }
893 894 895 896 897 898 899 900 901 902 903 904 905 906
    ],
    "ssl_certificate": [
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.1",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        },
        {
            "date": "Fri, 27 Dec 2019 14:43:26 -0000",
            "hostname": "www.erp5.com",
            "ip": "127.0.0.2",
            "not_after": "Mon, 13 Jul 2020 12:00:00 -0000"
        }
907 908 909 910 911 912 913 914
    ]
}
""")
    self.configureLauncher()
    with self.assertRaises(PromiseError):
      self.launcher.run()
    self.assertFailedMessage(
      self.getPromiseResult(self.promise_name),
915 916 917
      "https://www.erp5.com/ : http_query: ERROR IP 127.0.0.1 got status code "
      "302 instead of 301 http_query: ERROR expected IPs 127.0.0.1 127.0.0.2 "
      "differes from got 127.0.0.1 127.0.0.4 ssl_certificate: OK Certificate "
918 919
      "will expire on Mon, 13 Jul 2020 12:00:00 -0000, which is more than 15 "
      "days, UTC now is Fri, 27 Dec 2019 15:11:12 -0000"
920
    )