def test_output_result(self): web_ping = WebPing(timeout=3) url_field = URLField( "test_ping", "title", "this is a test" ) result = WebPing.ping( url_field.to_python("https://www.google.com/"), timeout=3 ) out = StringIO() web_ping.output_result(result, "stanza", "title", unbroken=True, close=True, out=out) self.assertTrue(out.getvalue().find("response_code=200") >= 0)
def test_output_result_unavailable(self): web_ping = WebPing(timeout=3) url_field = URLField( "test_ping", "title", "this is a test" ) result = WebPing.ping( url_field.to_python("http://192.168.30.23/"), timeout=3 ) out = StringIO() web_ping.output_result(result, "stanza", "title", unbroken=True, close=True, out=out) self.assertTrue(out.getvalue().find("timed_out=True") >= 0)
def test_bad_checkpoint(self): web_ping = WebPing() # Make sure the call does return the expected error (is attempting to load the data data) with self.assertRaises(ValueError): web_ping.get_checkpoint_data( os.path.join( self.get_test_dir(), "configs"), throw_errors=True ) # Make sure the test returns None data = web_ping.get_checkpoint_data( os.path.join( self.get_test_dir(), "configs", "web_ping://TextCritical.net") ) self.assertEqual(data, None)
def test_needs_another_run(self): # Test case where file does not exist self.assertTrue( WebPing.needs_another_run( "/Users/lmurphey/Applications/splunk/var/lib/splunk/modinputs/web_ping", "web_ping://DoesNotExist", 60 ) ) # Test an interval right at the earlier edge self.assertFalse( WebPing.needs_another_run( os.path.join( self.get_test_dir(), "configs" ), "web_ping://TextCritical.com", 60, 1365486765 ) ) # Test an interval at the later edge self.assertFalse( WebPing.needs_another_run( os.path.join( self.get_test_dir(), "configs" ), "web_ping://TextCritical.com", 10, 1365486775 ) ) # Test interval beyond later edge self.assertTrue( WebPing.needs_another_run( os.path.join( self.get_test_dir(), "configs" ), "web_ping://TextCritical.com", 10, 1365486776 ) )
def test_ping_with_basic_authentication(self): # Try with valid authentication url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port)), timeout=3, username="******", password="******") self.assertEqual(result.response_code, 200) self.assertEqual(result.response_md5, '1f6c14189070f50c4c06ada640c14850') # This is 1f6c14189070f50c4c06ada640c14850 on disk self.assertEqual(result.response_sha224, 'deaf4c0062539c98b4e957712efcee6d42832fed2d803c2bbf984b23') # Verify that bad authentication fails result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port)), timeout=3, username="******", password="******") self.assertEqual(result.response_code, 401) self.assertGreater(result.request_time, 0)
def test_determine_auth_method_ntlm_comma_header(self): # Try with digest auth url_field = URLField( "test_ping", "title", "this is a test") auth_type = WebPing.determine_auth_type(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/ntlm_auth_negotiate")) self.assertEqual(auth_type, WebPing.HTTP_AUTH_NTLM)
def test_determine_auth_method_digest(self): # Try with digest auth url_field = URLField( "test_ping", "title", "this is a test") auth_type = WebPing.determine_auth_type(url_field.to_python("http://httpbin.org/digest-auth/auth/user/passwd")) self.assertEqual(auth_type, WebPing.HTTP_AUTH_DIGEST)
def test_determine_auth_method_basic(self): # Try with basic auth url_field = URLField( "test_ping", "title", "this is a test") auth_type = WebPing.determine_auth_type(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port))) self.assertEqual(auth_type, WebPing.HTTP_AUTH_BASIC)
def test_ping_with_ntlm_negotiate_authentication(self): # Try with valid authentication url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/ntlm_auth_negotiate"), timeout=3, username="******", password="******") self.assertEqual(result.response_code, 200)
def test_ping_with_digest_authentication(self): # Try with valid authentication url_field = URLField( "test_ping", "title", "this is a test") result = WebPing.ping( url_field.to_python("http://httpbin.org/digest-auth/auth/user/passwd"), timeout=3, username="******", password="******") self.assertEqual(result.response_code, 200)
def test_determine_auth_method_none(self): # Try with digest auth url_field = URLField( "test_ping", "title", "this is a test" ) auth_type = WebPing.determine_auth_type( url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/test_page")) self.assertEquals(auth_type, WebPing.HTTP_AUTH_NONE)
def test_ping(self): url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/test_page"), timeout=3) self.assertEqual(result.response_code, 200) self.assertGreater(result.request_time, 0)
def test_ping(self): url_field = URLField( "test_ping", "title", "this is a test" ) result = WebPing.ping( url_field.to_python("https://www.google.com/"), timeout=3 ) self.assertEquals(result.response_code, 200) self.assertGreater(result.request_time, 0)
def test_output_result_unavailable(self): web_ping = WebPing(timeout=3) url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping(url_field.to_python("http://192.168.30.23/"), timeout=3) out = StringIO() web_ping.output_result(result, "stanza", "title", unbroken=True, close=True, out=out) self.assertTrue(out.getvalue().find("timed_out=True") >= 0)
def test_ping_non_existent_domain(self): # https://answers.splunk.com/answers/337070/website-monitoring-app-setup.html#answer-338487 url_field = URLField( "test_ping_non_existent_domain", "title", "this is a test" ) result = WebPing.ping( url_field.to_python("http://xyz"), timeout=3 ) self.assertEquals(result.response_code, 0) self.assertEquals(result.request_time, 0)
def test_hash(self): url_field = URLField( "test_ping", "title", "this is a test" ) result = WebPing.ping( url_field.to_python("http://127.0.0.1:8888/test_page"), timeout=3 ) self.assertEquals(result.response_code, 200) self.assertEquals(result.response_md5, '1f6c14189070f50c4c06ada640c14850') # This is 1f6c14189070f50c4c06ada640c14850 on disk self.assertEquals(result.response_sha224, 'deaf4c0062539c98b4e957712efcee6d42832fed2d803c2bbf984b23')
def test_ping_over_proxy(self): url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping(url_field.to_python("http://textcritical.com"), timeout=3, proxy_type=self.proxy_type, proxy_server=self.proxy_address, proxy_port=self.proxy_port) self.assertEquals(result.response_code, 200)
def test_ping_super_long_url(self): # https://answers.splunk.com/answers/488784/why-my-website-monitoring-only-check-1-time.html url_field = URLField("test_ping", "title", "this is a test") #result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/test_page?s=superloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong"), timeout=3) result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/test_page_superlooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong"), timeout=3) self.assertEqual(result.response_code, 200) self.assertGreater(result.request_time, 0)
def test_output_result(self): web_ping = WebPing(timeout=3) url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping( url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/test_page"), timeout=3) out = StringIO() web_ping.output_result(result, "stanza", "title", unbroken=True, close=True, out=out) self.assertTrue(out.getvalue().find("response_code=200") >= 0)
def test_ping_with_headers(self): url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/test_page"), timeout=3, return_headers=True) self.assertEqual(result.response_code, 200) self.assertGreater(result.request_time, 0) self.assertGreater(len(result.headers), 0) self.assertEqual(result.headers['Content-type'], 'text/html')
class TestOnCloud(unittest.TestCase): def setUp(self): super(TestOnCloud, self).setUp() # Configure an instance of the class to test self.web_ping = WebPing() # Force the class to act like it is on cloud self.web_ping.is_on_cloud = self.fake_is_on_cloud def fake_is_on_cloud(self, session_key): return True def test_get_proxy_config(self): # See https://lukemurphey.net/issues/2445 self.web_ping.is_on_cloud = self.fake_is_on_cloud self.web_ping.get_proxy_config('a session key') self.assertEqual(self.web_ping.get_proxy_config('a session key'), ("http", None, None, None, None, None))
def handle_results(self, results, session_key, in_preview): # FYI: we ignore results since this is a generating command # Make sure that the url field was provided if self.url is None: self.logger.warn("No url was provided") return # Parse the URL url_field = URLField('name', 'title', 'description') url_parsed = url_field.to_python(self.url) # Do the web-ping result = WebPing.ping(url_parsed, logger=self.logger, should_contain_string=self.expected_string, return_headers=self.return_headers) # Prep the result dictionary data = { 'response_code': result.response_code if result.response_code > 0 else '', 'total_time': round(result.request_time, 2) if result.request_time > 0 else '', 'timed_out': result.timed_out, 'url': result.url } # Add the MD5 of the response if available if result.response_md5 is not None: data['content_md5'] = result.response_md5 # Add the SHA-224 of the response if available if result.response_sha224 is not None: data['content_sha224'] = result.response_sha224 # Add the size of the response if available if result.response_size is not None: data['content_size'] = result.response_size # Add the variable noting if the expected string was present if result.has_expected_string is not None: data['has_expected_string'] = str( result.has_expected_string).lower() # Add the the headers if present if result.headers is not None: for header in result.headers: data['header_' + header] = result.headers[header] # Output the results self.output_results([data])
def test_ping_with_ntlm_authentication_missing_domain(self): # Try with missing domain url_field = URLField("test_ping", "title", "this is a test") self.assertRaises( NTLMAuthenticationValueException, lambda: WebPing.ping(url_field.to_python("http://127.0.0.1:" + str( self.web_server_port) + "/ntlm_auth"), timeout=3, username="******", password="******"))
def test_should_contain_string_no_match(self): url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/test_page"), timeout=3, should_contain_string="<h1>Should not Match!</h1>") self.assertEqual(result.response_code, 200) self.assertEqual(result.response_md5, '1f6c14189070f50c4c06ada640c14850') # This is 1f6c14189070f50c4c06ada640c14850 on disk self.assertEqual(result.response_sha224, 'deaf4c0062539c98b4e957712efcee6d42832fed2d803c2bbf984b23') self.assertEqual(result.has_expected_string, False)
def test_ping_with_basic_authentication_optional(self): # Try with valid authentication url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping( url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/optional_auth"), timeout=3, username="******", password="******") self.assertEquals(result.response_code, 203) # Verify that no authentication still works result = WebPing.ping( url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/optional_auth"), timeout=3) self.assertEquals(result.response_code, 202) self.assertGreater(result.request_time, 0)
def test_missing_servername(self): """ Some web-servers require that the "Host" be included on SSL connections when the server is hosting multiple domains on the same IP. Without the host header, the server is unable to determine which certificate to provide and thus closes the connection. http://lukemurphey.net/issues/1035 """ url_field = URLField("test_ping", "title", "this is a test") result = WebPing.ping(url_field.to_python("https://lukemurphey.net"), timeout=3) self.assertEqual(result.response_code, 200)
def test_cleanup_threads(self): threads_running = [1, 2] max_runs = 10 def thread_function(n): total_time = 0 while n in threads_running and total_time < max_runs: time.sleep(1) total_time += 1 thread_1 = threading.Thread(target=thread_function, args=(1,)) thread_2 = threading.Thread(target=thread_function, args=(2,)) threads = { '1': thread_1, '2': thread_2 } thread_1.start() thread_2.start() web_ping = WebPing() self.assertEqual(len(threads), 2) self.assertEqual(web_ping.cleanup_threads(threads), 0) self.assertEqual(len(threads), 2) # Stop the first thread and wait for it to complete threads_running = [2] thread_1.join() self.assertEqual(web_ping.cleanup_threads(threads), 1) self.assertEqual(len(threads), 1) # Stop the second thread and wait for it to complete threads_running = [] thread_2.join() self.assertEqual(web_ping.cleanup_threads(threads), 1) self.assertEqual(len(threads), 0)
def test_custom_user_agent(self): """ http://lukemurphey.net/issues/1341 """ url_field = URLField("test_ping", "title", "this is a test") # Make sure that the server is validating the user-agent by returning 200 when the user-agent doesn't match # This just validates that the test case works result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/user_agent_check"), user_agent="USER_AGENT_CHECK_DOESNT_MATCH", timeout=3) self.assertEquals(result.response_code, 200) # Make sure that the server is validating the user-agent which returns 201 when the user-agent matches "USER_AGENT_CHECK" result = WebPing.ping(url_field.to_python("http://127.0.0.1:" + str(self.web_server_port) + "/user_agent_check"), user_agent="USER_AGENT_CHECK", timeout=3) self.assertEquals(result.response_code, 201)
def test_missing_servername(self): """ Some web-servers require that the "Host" be included on SSL connections when the server is hosting multiple domains on the same IP. Without the host header, the server is unable to determine which certificate to provide and thus closes the connection. http://lukemurphey.net/issues/1035 """ web_ping = WebPing(timeout=3) url_field = URLField( "test_ping", "title", "this is a test" ) result = WebPing.ping( url_field.to_python("https://www.penfolds.com"), timeout=3 ) self.assertEquals(result.response_code, 200)
def test_ping_over_proxy(self): if self.proxy_address is None: print "No proxy address defined, proxy based test will not run" return if self.proxy_port is None: print "No proxy port defined, proxy based test will not run" return if self.proxy_type is None: print "No proxy type defined, proxy based test will not run" return url_field = URLField( "test_ping", "title", "this is a test" ) result = WebPing.ping( url_field.to_python("http://textcritical.com"), timeout=3, proxy_type=self.proxy_type, proxy_server=self.proxy_address, proxy_port=self.proxy_port) self.assertEquals(result.response_code, 200)
def handle_results(self, results, session_key, in_preview): # FYI: we ignore results since this is a generating command # Make sure that the url field was provided if self.url is None: self.logger.warn("No url was provided") return # Parse the URL url_field = URLField('name','title','description') url_parsed = url_field.to_python(self.url) # Do the web-ping result = WebPing.ping(url_parsed, logger=self.logger, should_contain_string=self.expected_string) # Prep the result dictionary data = { 'response_code': result.response_code if result.response_code > 0 else '', 'total_time': round(result.request_time, 2) if result.request_time > 0 else '', 'timed_out': result.timed_out, 'url': result.url } # Add the MD5 of the response of available if result.response_md5 is not None: data['content_md5'] = result.response_md5 # Add the SHA-224 of the response of available if result.response_sha224 is not None: data['content_sha224'] = result.response_sha224 # Add the MD5 of the response of available if result.response_size is not None: data['content_size'] = result.response_size # Add the variable noting if the expected string was present if result.has_expected_string is not None: data['has_expected_string'] = str(result.has_expected_string).lower() # Output the results self.output_results([data])
def test_is_expired(self): self.assertFalse( WebPing.is_expired(time.time(), 30) ) self.assertTrue( WebPing.is_expired(time.time() - 31, 30) )
def test_save_checkpoint(self): web_ping = WebPing() web_ping.save_checkpoint(self.tmp_dir, "web_ping://TextCritical.com", 100) self.assertEquals( web_ping.last_ran(self.tmp_dir, "web_ping://TextCritical.com"), 100)
def test_ping_timeout(self): url_field = URLField( "test_ping_timeout", "title", "this is a test" ) result = WebPing.ping( url_field.to_python("https://192.168.30.23/"), timeout=3 ) self.assertEquals(result.timed_out, True)
def test_get_file_path(self): self.assertEquals( WebPing.get_file_path( "/Users/lmurphey/Applications/splunk/var/lib/splunk/modinputs/web_ping", "web_ping://TextCritical.com"), "/Users/lmurphey/Applications/splunk/var/lib/splunk/modinputs/web_ping/35163af7282b92013f810b2b4822d7df.json")