def test_permissions_and_mtime_affect_checksum(self): """ Check that changing permissions or mtime affect checksum """ config = NginxConfig(simple_config) config.full_parse() old_checksum = config.checksum() os.system('touch %s' % (os.getcwd() + '/test/fixtures/nginx/simple/conf.d/')) config.full_parse() new_checksum = config.checksum() assert_that(new_checksum, not_(equal_to(old_checksum)))
def test_parse_complex(self): config = NginxConfig(complex_config) config.full_parse() context.log.info(config.index) context.log.info(config.tree) context.log.info(config.files) context.log.info(config.checksum()) assert_that(config.error_logs, empty()) assert_that(config.access_logs, empty()) assert_that(config.log_formats, empty()) assert_that(config.stub_status_urls, empty())
def test_parse_complex(self): config = NginxConfig(complex_config) config.full_parse() context.log.info(config.index) context.log.info(config.tree) context.log.info(config.files) context.log.info(config.checksum()) # error logs assert_that(config.error_logs, has_length(0)) # access logs assert_that(config.access_logs, has_length(0)) # log formats assert_that(config.log_formats, has_length(0)) # stub status url assert_that(config.stub_status_urls, has_length(0))
def collect(self): try: config = NginxConfig( self.object.conf_path, binary=self.object.bin_path, prefix=self.object.prefix ) # check if config is changed (changes are: new files/certs, new mtimes) config_files, config_dirs = config.collect_structure(include_ssl_certs=self.object.upload_ssl) if config_files == self.previous_files and config_dirs == self.previous_directories: return # parse config tree config.full_parse() # Send event for parsing nginx config. # Use config.parser.filename to account for default value defined in NginxConfigParser. self.object.eventd.event( level=INFO, message='nginx config parsed, read from %s' % config.filename, ) for error in config.parser_errors: self.object.eventd.event(level=WARNING, message=error) # run ssl checks if self.object.upload_ssl: config.run_ssl_analysis() else: context.log.info('ssl analysis skipped due to users settings') # run upload checksum = config.checksum() if self.object.upload_config: self.upload(config, checksum) if self.previous_checksum: # config changed, so we need to restart the object self.object.need_restart = True else: # otherwise run test if self.object.run_config_test and config.total_size() < 20*1024*1024: # 20 MB run_time = config.run_test() # send event for testing nginx config if config.test_errors: self.object.eventd.event(level=WARNING, message='nginx config test failed') else: self.object.eventd.event(level=INFO, message='nginx config tested ok') for error in config.test_errors: self.object.eventd.event(level=CRITICAL, message=error) # stop -t if it took too long if run_time > context.app_config['containers']['nginx']['max_test_duration']: context.app_config['containers']['nginx']['run_test'] = False context.app_config.mark_unchangeable('run_test') self.object.eventd.event( level=WARNING, message='%s -t -c %s took %s seconds, disabled until agent restart' % ( config.binary, config.filename, run_time ) ) self.object.run_config_test = False self.previous_checksum = checksum self.previous_files = copy.copy(config_files) self.previous_directories = copy.copy(config_dirs) except Exception as e: exception_name = e.__class__.__name__ context.log.error('failed to collect due to %s' % exception_name) context.log.debug('additional info:', exc_info=True) self.object.eventd.event( level=INFO, message='nginx config parser failed, path %s' % self.object.conf_path, onetime=True )
def collect(self): try: config = NginxConfig(self.object.conf_path, binary=self.object.bin_path, prefix=self.object.prefix) # check if config is changed (changes are: new files/certs, new mtimes) all_config_files = config.get_all_files( include_ssl_certs=self.object.upload_ssl) if all_config_files == self.previous_files: return # parse config tree config.full_parse() # Send event for parsing nginx config. # Use config.parser.filename to account for default value defined in NginxConfigParser. self.object.eventd.event( level=INFO, message='nginx config parsed, read from %s' % config.filename, ) for error in config.parser_errors: self.object.eventd.event(level=WARNING, message=error) # run ssl checks if self.object.upload_ssl: config.run_ssl_analysis() else: context.log.info('ssl analysis skipped due to users settings') # run upload checksum = config.checksum() if self.object.upload_config: self.upload(config, checksum) # config changed, so we need to restart the object if self.previous_checksum: self.object.need_restart = True # otherwise run test else: # run test if self.object.run_config_test and config.total_size( ) < 20 * 1024 * 1024: # 20 MB run_time = config.run_test() # send event for testing nginx config if config.test_errors: self.object.eventd.event( level=WARNING, message='nginx config test failed') else: self.object.eventd.event( level=INFO, message='nginx config tested ok') for error in config.test_errors: self.object.eventd.event(level=CRITICAL, message=error) # stop -t if it took too long if run_time > context.app_config['containers']['nginx'][ 'max_test_duration']: context.app_config['containers']['nginx'][ 'run_test'] = False context.app_config.mark_unchangeable('run_test') self.object.eventd.event( level=WARNING, message= '%s -t -c %s took %s seconds, disabled until agent restart' % (config.binary, config.filename, run_time)) self.object.run_config_test = False self.previous_checksum = checksum self.previous_files = copy.copy(all_config_files) except Exception as e: exception_name = e.__class__.__name__ context.log.error('failed to collect due to %s' % exception_name) context.log.debug('additional info:', exc_info=True) self.object.eventd.event( level=INFO, message='nginx config parser failed, path %s' % self.object.conf_path, onetime=True)