importrequestsimporttimefromdatadog_checks.baseimportAgentCheck,ConfigurationErrorclassAwesomeCheck(AgentCheck):"""AwesomeCheck derives from AgentCheck, and provides the required check method."""defcheck(self,instance):url=instance.get('url')search_string=instance.get('search_string')# It's a very good idea to do some basic sanity checking.# Try to be as specific as possible with the exceptions.ifnoturlornotsearch_string:raiseConfigurationError('Configuration error, please fix awesome.yaml')try:response=requests.get(url)response.raise_for_status()# Something went horribly wrongexceptExceptionase:# Ideally we'd use a more specific message...self.service_check('awesome.search',self.CRITICAL,message=str(e))# Submit an error logself.send_log({'message':f'Failed to access {url}: {str(e)}','timestamp':time.time(),'status':'error','service':'awesome','url':url})# Page is accessibleelse:# search_string is presentifsearch_stringinresponse.text:self.service_check('awesome.search',self.OK)# Submit an info logself.send_log({'message':f'Successfully found "{search_string}" at {url}','timestamp':time.time(),'status':'info','service':'awesome','url':url,'search_string':search_string})# search_string was not foundelse:self.service_check('awesome.search',self.WARNING)# Submit a warning logself.send_log({'message':f'String "{search_string}" not found at {url}','timestamp':time.time(),'status':'warning','service':'awesome','url':url,'search_string':search_string})
importpytest# Don't forget to import your integrationfromdatadog_checks.awesomeimportAwesomeCheckfromdatadog_checks.baseimportConfigurationError@pytest.mark.unitdeftest_config():instance={}c=AwesomeCheck('awesome',{},[instance])# empty instancewithpytest.raises(ConfigurationError):c.check(instance)# only the urlwithpytest.raises(ConfigurationError):c.check({'url':'http://foobar'})# only the search stringwithpytest.raises(ConfigurationError):c.check({'search_string':'foo'})# this should not failc.check({'url':'http://foobar','search_string':'foo'})
pytest はマーカーをサポートし、これを使用してテストをカテゴリにグループ化できます。test_config が unit テストとしてマークされていることに注目してください。
importosimportpytestfromdatadog_checks.devimportdocker_run,get_docker_hostname,get_hereURL='http://{}:8000'.format(get_docker_hostname())SEARCH_STRING='Thank you for using nginx.'INSTANCE={'url':URL,'search_string':SEARCH_STRING}@pytest.fixture(scope='session')defdd_environment():compose_file=os.path.join(get_here(),'docker-compose.yml')# This does 3 things:## 1. Spins up the services defined in the compose file# 2. Waits for the url to be available before running the tests# 3. Tears down the services when the tests are finishedwithdocker_run(compose_file,endpoints=[URL]):yieldINSTANCE@pytest.fixturedefinstance():returnINSTANCE.copy()
@pytest.mark.integration@pytest.mark.usefixtures('dd_environment')deftest_service_check(aggregator,instance):c=AwesomeCheck('awesome',{},[instance])# the check should send OKc.check(instance)aggregator.assert_service_check('awesome.search',AwesomeCheck.OK)# the check should send WARNINGinstance['search_string']='Apache'c.check(instance)aggregator.assert_service_check('awesome.search',AwesomeCheck.WARNING)
## Version Number / Date (YYYY-MM-DD)
***Added***:
* Description of new feature
* Description of new feature
***Fixed***:
* Description of fix
* Description of fix
***Changed***:
* Description of update or improvement
* Description of update or improvement
***Removed***:
* Description of removed feature
* Description of removed feature