def test_validation(self): ws = web_scraping.WebScraping() self.setup_mock() ws.check({}) self.assert_log_count('info', 0) self.assert_log_count('error', 1) self.assert_log('error', 1, 'skipping instance, no name found.') self.assert_request_not_called() self.assert_gauge_not_called() self.setup_mock() ws.check({'name': 'test'}) self.assert_log_count('info', 0) self.assert_log_count('error', 1) self.assert_log('error', 1, 'skipping instance, no url found.') self.assert_request_not_called() self.assert_gauge_not_called() self.setup_mock() ws.check({'name': 'test', 'url': 'http://example.com'}) self.assert_log_count('info', 0) self.assert_log_count('error', 1) self.assert_log('error', 1, 'skipping instance, no xpath found.') self.assert_request_not_called() self.assert_gauge_not_called()
def test_invalid_url(self): name = 'test' url = 'bad url' self.setup_mock() ws = web_scraping.WebScraping() ws.check({ 'name': name, 'url': url, 'xpath': '', }) self.assert_log_count('info', 0) self.assert_log_count('error', 1) self.assert_log_match('error', 1, r'%s : failed to get website' % name) self.assert_request_url(url) self.assert_gauge_not_called()
def test_success(self): name = 'test' url = 'http://example.com' value = '-100.1' self.setup_mock('<div id="hoge">test=%s</div>' % value) ws = web_scraping.WebScraping() ws.check({ 'name': name, 'url': url, 'xpath': '//*[@id="hoge"]/text()', }) self.assert_log_count('info', 1) self.assert_log('info', 1, '%s = %f' % (name, float(value))) self.assert_log_count('error', 0) self.assert_request_url(url) self.assert_gauge(name, float(value))
def test_invalid_value(self): name = 'test' url = 'http://example.com' self.setup_mock('<div id="hoge">test</div>') ws = web_scraping.WebScraping() ws.check({ 'name': name, 'url': url, 'xpath': '//*[@id="hoge"]/text()', }) self.assert_log_count('info', 0) self.assert_log_count('error', 1) self.assert_log( 'error', 1, '%s : failed to get value (default value used) : could not convert string to float: ' % name) self.assert_request_url(url) self.assert_gauge_not_called()
def test_default_value(self): name = 'test' url = 'http://example.com' default_value = '100.2' self.setup_mock('<div id="hoge">test=-100.1</div>') ws = web_scraping.WebScraping() ws.check({ 'name': name, 'url': url, 'xpath': '//*[@id="fuga"]/text()', 'default': default_value, }) self.assert_log_count('info', 2) self.assert_log('info', 1, '%s : failed to get value (default value used)' % name) self.assert_log('info', 2, '%s = %f' % (name, float(default_value))) self.assert_log_count('error', 0) self.assert_request_url(url) self.assert_gauge(name, float(default_value))