def test_rank(self): team_1 = Team(name="Blue Team 1", color="Blue") self.session.add(team_1) service_1 = Service(name="Example Service 1", team=team_1, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=True, output='Good output') self.session.add(check_1) self.session.add(check_2) self.session.commit() team_2 = Team(name="Blue Team 2", color="Blue") self.session.add(team_2) service_1 = Service(name="Example Service 1", team=team_2, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=True, output='Good output') self.session.add(check_1) self.session.add(check_2) self.session.commit() team_3 = Team(name="Blue Team 3", color="Blue") self.session.add(team_3) service_1 = Service(name="Example Service 1", team=team_3, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=False, output='Good output') self.session.add(check_1) self.session.add(check_2) self.session.commit() assert team_1.services[0].rank == 1 assert team_2.services[0].rank == 1 assert team_3.services[0].rank == 3
def test_current_score(self): team = generate_sample_model_tree('Team', self.session) service_1 = Service(name="Example Service 1", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output') self.session.add(check_1) service_2 = Service(name="Example Service 2", team=team, check_name="SSH IPv4 Check", host='127.0.0.2') self.session.add(service_2) check_2 = Check(service=service_2, result=True, output='Good output') self.session.add(check_2) check_3 = Check(service=service_2, result=True, output='Good output') self.session.add(check_3) service_3 = Service(name="Example Service 3", team=team, check_name="SSH IPv4 Check", host='127.0.0.3') self.session.add(service_3) check_3 = Check(service=service_3, result=False, output='bad output') self.session.add(check_3) self.session.commit() assert team.current_score == 300
def test_services(self): team = generate_sample_model_tree('Team', self.db) service_1 = Service(name="Example Service 1", team=team, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') service_2 = Service(name="Example Service 2", team=team, check_name="SSH IPv4 Check", ip_address='2.3.4.5') self.db.save(service_1) self.db.save(service_2) assert team.services == [service_1, service_2]
def test_services(self): team = generate_sample_model_tree('Team', self.session) service_1 = Service(name="Example Service 1", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') service_2 = Service(name="Example Service 2", team=team, check_name="SSH IPv4 Check", host='2.3.4.5') self.session.add(service_1) self.session.add(service_2) self.session.commit() assert team.services == [service_1, service_2]
def populate_sample_data(session): team = Team(name="Blue Team 1", color="Blue") session.add(team) service = Service(name="Example Service 1", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') session.add(service) round_1 = Round(number=1) session.add(round_1) check_1 = Check(service=service, result=True, output='Good output', round=round_1) session.add(check_1) round_2 = Round(number=2) session.add(round_2) check_2 = Check(service=service, result=False, output='Bad output', round=round_2) session.add(check_2) session.commit() return team
def test_cmd(self): engine = Engine() service = Service(name='Example Service', check_name=self.check_name, host='127.0.0.1', port=1234) environment = Environment(service=service, matching_content='*') if not hasattr(self, 'properties'): self.properties = {} if not hasattr(self, 'accounts'): self.accounts = {} for property_name, property_value in self.properties.items(): self.session.add( Property(environment=environment, name=property_name, value=property_value)) for account_name, account_pass in self.accounts.items(): self.session.add( Account(username=account_name, password=account_pass, service=service)) self.session.add(service) self.session.add(environment) self.session.commit() check_obj = engine.check_name_to_obj(self.check_name)(environment) assert check_obj.command() == self.cmd
def setup(self): super(TestBasicCheck, self).setup() self.service = Service(name="Example Service", check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.environment = Environment(matching_regex='*', service=self.service)
def setup(self): super(CheckTest, self).setup() self.engine = Engine() self.service = Service(name='Example Service', check_name=self.check_name, ip_address='127.0.0.1', port=100) self.environment = Environment(service=self.service, matching_regex='*') if not hasattr(self, 'properties'): self.properties = {} if not hasattr(self, 'accounts'): self.accounts = {} for property_name, property_value in self.properties.items(): self.db.save( Property(environment=self.environment, name=property_name, value=property_value)) for account_name, account_pass in self.accounts.items(): self.db.save( Account(username=account_name, password=account_pass, service=self.service)) self.db.save(self.service) self.db.save(self.environment)
def test_last_check_result_true(self): team = generate_sample_model_tree('Team', self.session) service = Service(name="Example Service", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service) round_obj = generate_sample_model_tree('Round', self.session) check_1 = Check(round=round_obj, service=service, result=False, output='Check exceeded time') self.session.add(check_1) check_2 = Check(round=round_obj, service=service, result=False, output='Check exceeded time') self.session.add(check_2) check_3 = Check(round=round_obj, service=service, result=True, output='Good output') self.session.add(check_3) self.session.commit() assert service.last_check_result() is True
def test_init_service(self): service = Service(name="Example Service", check_name="ICMP IPv4 Check", host='127.0.0.1') assert service.id is None assert service.name == "Example Service" assert service.team is None assert service.team is None assert service.check_name == "ICMP IPv4 Check" assert service.points is None
def test_last_check_result_not_found(self): team = generate_sample_model_tree('Team', self.db) service = Service(name="Example Service", team=team, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service) assert service.last_check_result() is None
def setup(self): super(TestAdmin, self).setup() user = self.create_default_user() service = Service(name="Example Service", check_name="ICMP IPv4 Check", host='127.0.0.1', team=user.team) self.session.add(service) self.session.commit()
def test_last_check_result_not_found(self): team = generate_sample_model_tree('Team', self.session) service = Service(name="Example Service", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service) self.session.commit() assert service.last_check_result() is None
def generate_sample_model_tree(model, session): # Team team = Team(name="Team 1", color="Blue") session.add(team) session.commit() if model == 'Team': return team # Users user = User(username="******" + str(random.randrange(10000)), password="******", team=team) session.add(user) session.commit() if model == 'User': return user # Services service = Service(name="ICMP IPv4", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') session.add(service) session.commit() if model == 'Service': return service # Environments environment = Environment(service=service, matching_regex='*') session.add(environment) session.commit() if model == 'Environment': return environment # Properties property_obj = Property(name="testproperty", value="testvalue", environment=environment) session.add(property_obj) session.commit() if model == 'Property': return property_obj # Rounds round_obj = Round(number=1) session.add(round_obj) session.commit() if model == 'Round': return round_obj # Checks check = Check(round=round_obj, service=service) session.add(check) session.commit() if model == 'Check': return check
def test_basic_service_with_worker_queue(self): team = generate_sample_model_tree('Team', self.session) service = Service(name="Example Service", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1', worker_queue='somequeue') self.session.add(service) self.session.commit() assert service.id is not None assert service.name == "Example Service" assert service.team == team assert service.team_id == team.id assert service.check_name == "ICMP IPv4 Check" assert service.port == 0 assert service.points == 100 assert service.worker_queue == 'somequeue'
def test_place(self): team_1 = Team(name="Blue Team 1", color="Blue") self.db.save(team_1) service_1 = Service(name="Example Service 1", team=team_1, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=True, output='Good output') self.db.save(check_1) self.db.save(check_2) team_2 = Team(name="Blue Team 2", color="Blue") self.db.save(team_2) service_1 = Service(name="Example Service 1", team=team_2, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service_1) check_1 = Check(service=service_1, result=False, output='Good output') check_2 = Check(service=service_1, result=False, output='Good output') self.db.save(check_1) self.db.save(check_2) team_3 = Team(name="Blue Team 3", color="Blue") self.db.save(team_3) service_1 = Service(name="Example Service 1", team=team_3, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service_1) check_1 = Check(service=service_1, result=True, output='Good output') check_2 = Check(service=service_1, result=False, output='Good output') self.db.save(check_1) self.db.save(check_2) assert team_1.place == 1 assert team_2.place == 3 assert team_3.place == 2
def test_current_score(self): team = generate_sample_model_tree('Team', self.session) round_obj = Round(number=1) self.session.add(round_obj) self.session.commit() service_1 = Service(name="Example Service 1", team=team, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output', round=round_obj) self.session.add(check_1) service_2 = Service(name="Example Service 2", team=team, check_name="SSH IPv4 Check", host='127.0.0.2') self.session.add(service_2) check_2 = Check(service=service_2, result=True, output='Good output', round=round_obj) self.session.add(check_2) service_3 = Service(name="Example Service 3", team=team, check_name="SSH IPv4 Check", host='127.0.0.3') self.session.add(service_3) check_3 = Check(service=service_3, result=False, output='bad output', round=round_obj) self.session.add(check_3) self.session.commit() util.update_team_score(team.id, 1, 1, add=True) assert team.current_score == 200
def test_basic_service(self): team = generate_sample_model_tree('Team', self.db) service = Service(name="Example Service", team=team, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service) assert service.id is not None assert service.name == "Example Service" assert service.team == team assert service.team_id == team.id assert service.check_name == "ICMP IPv4 Check" assert service.port == 0 assert service.points == 100
def test_basic_service_with_points(self): team = generate_sample_model_tree('Team', self.session) service = Service(name="Example Service", team=team, check_name="ICMP IPv4 Check", points=500, host='127.0.0.1', port=100) self.session.add(service) self.session.commit() assert service.id is not None assert service.name == "Example Service" assert service.team == team assert service.team_id == team.id assert service.check_name == "ICMP IPv4 Check" assert service.port == 100 assert service.points == 500 assert service.score_earned == 0 assert service.max_score == 0 assert service.percent_earned == 0
def save(self, db_session): for team_dict in self['teams']: logger.info("Creating {0} Team: {1}".format( team_dict['color'], team_dict['name'])) team_obj = Team(name=team_dict['name'], color=team_dict['color']) db_session.add(team_obj) for user_dict in team_dict['users']: logger.info("\tCreating User {0}:{1}".format( user_dict['username'], user_dict['password'])) db_session.add( User(username=user_dict['username'], password=user_dict['password'], team=team_obj)) if 'services' in team_dict: for service_dict in team_dict['services']: logger.info("\tCreating {0} Service".format( service_dict['name'])) service_obj = Service( name=service_dict['name'], team=team_obj, check_name=service_dict['check_name'], host=service_dict['host'], port=service_dict['port'], points=service_dict['points']) if 'worker_queue' in service_dict: service_obj.worker_queue = service_dict['worker_queue'] db_session.add(service_obj) if 'accounts' in service_dict: for account_dict in service_dict['accounts']: db_session.add( Account(username=account_dict['username'], password=account_dict['password'], service=service_obj)) for environment_dict in service_dict['environments']: environment_obj = Environment( service=service_obj, matching_content=environment_dict[ 'matching_content']) db_session.add(environment_obj) if 'properties' in environment_dict: for property_dict in environment_dict[ 'properties']: db_session.add( Property(environment=environment_obj, name=property_dict['name'], value=property_dict['value'])) db_session.commit()
def test_last_check_result_false(self): team = generate_sample_model_tree('Team', self.db) service = Service(name="Example Service", team=team, check_name="ICMP IPv4 Check", ip_address='127.0.0.1') self.db.save(service) round_obj = generate_sample_model_tree('Round', self.db) check_1 = Check(round=round_obj, service=service, result=True, output='Good output') self.db.save(check_1) check_2 = Check(round=round_obj, service=service, result=True, output='Good output') self.db.save(check_2) check_3 = Check(round=round_obj, service=service, result=False, output='Check exceeded time') self.db.save(check_3) assert service.last_check_result() is False
def test_check_result_for_round_no_rounds(self): service = Service(name="Example Service", check_name="ICMP IPv4 Check", host='127.0.0.1') assert service.check_result_for_round(1) is False
def test_place(self): round_1 = Round(number=1) round_2 = Round(number=2) self.session.add(round_1) self.session.add(round_2) self.session.commit() team_1 = Team(name="Blue Team 1", color="Blue") self.session.add(team_1) service_1 = Service(name="Example Service 1", team=team_1, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output', round=round_1) check_2 = Check(service=service_1, result=True, output='Good output', round=round_2) self.session.add(check_1) self.session.add(check_2) self.session.commit() team_2 = Team(name="Blue Team 2", color="Blue") self.session.add(team_2) service_1 = Service(name="Example Service 1", team=team_2, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output', round=round_1) check_2 = Check(service=service_1, result=True, output='Good output', round=round_2) self.session.add(check_1) self.session.add(check_2) self.session.commit() team_3 = Team(name="Blue Team 3", color="Blue") self.session.add(team_3) service_1 = Service(name="Example Service 1", team=team_3, check_name="ICMP IPv4 Check", host='127.0.0.1') self.session.add(service_1) check_1 = Check(service=service_1, result=True, output='Good output', round=round_1) check_2 = Check(service=service_1, result=False, output='Good output', round=round_2) self.session.add(check_1) self.session.add(check_2) self.session.commit() util.update_team_score(team_1.id, 1, 2, add=True) util.update_team_score(team_2.id, 1, 2, add=True) util.update_team_score(team_3.id, 1, 2, add=True) assert team_1.place == 1 assert team_2.place == 1 assert team_3.place == 3
def test_overview_data(self): # create 1 white team, 1 red team, 5 blue teams, 3 services per team, 5 checks per service # White Team self.db.save(Team(name="whiteteam", color="White")) # Red Team self.db.save(Team(name="redteam", color="Red")) teams = [] last_check_results = { "team1": { "FTPUpload": True, "DNS": True, "ICMP": True, }, "team2": { "FTPUpload": False, "DNS": True, "ICMP": True, }, "team3": { "FTPUpload": False, "DNS": True, "ICMP": False, }, "team4": { "FTPUpload": True, "DNS": False, "ICMP": False, }, "team5": { "FTPUpload": False, "DNS": False, "ICMP": False, }, } for team_num in range(1, 6): team = Team(name="team" + str(team_num), color="Blue") icmp_service = Service(name="ICMP", team=team, check_name="ICMP IPv4 Check", ip_address="127.0.0.1") dns_service = Service(name="DNS", team=team, check_name="DNSCheck", ip_address="8.8.8.8", port=53) ftp_upload_service = Service(name='FTPUpload', team=team, check_name='FTPUploadCheck', ip_address='1.2.3.4', port=21) self.db.save(icmp_service) self.db.save(dns_service) self.db.save(ftp_upload_service) # 5 rounds of checks round_1 = Round(number=1) icmp_check_1 = Check(round=round_1, service=icmp_service, result=True, output="example_output") dns_check_1 = Check(round=round_1, service=dns_service, result=False, output="example_output") ftp_upload_check_1 = Check(round=round_1, service=ftp_upload_service, result=True, output="example_output") self.db.save(round_1) self.db.save(icmp_check_1) self.db.save(dns_check_1) self.db.save(ftp_upload_check_1) round_2 = Round(number=2) icmp_check_2 = Check(round=round_2, service=icmp_service, result=True, output="example_output") dns_check_2 = Check(round=round_2, service=dns_service, result=True, output="example_output") ftp_upload_check_2 = Check(round=round_2, service=ftp_upload_service, result=True, output="example_output") self.db.save(round_2) self.db.save(icmp_check_2) self.db.save(dns_check_2) self.db.save(ftp_upload_check_2) round_3 = Round(number=3) icmp_check_3 = Check(round=round_3, service=icmp_service, result=True, output="example_output") dns_check_3 = Check(round=round_3, service=dns_service, result=False, output="example_output") ftp_upload_check_3 = Check(round=round_3, service=ftp_upload_service, result=True, output="example_output") self.db.save(round_3) self.db.save(icmp_check_3) self.db.save(dns_check_3) self.db.save(ftp_upload_check_3) round_4 = Round(number=4) icmp_check_4 = Check(round=round_4, service=icmp_service, result=False, output="example_output") dns_check_4 = Check(round=round_4, service=dns_service, result=False, output="example_output") ftp_upload_check_4 = Check(round=round_4, service=ftp_upload_service, result=False, output="example_output") self.db.save(round_4) self.db.save(icmp_check_4) self.db.save(dns_check_4) self.db.save(ftp_upload_check_4) round_5 = Round(number=5) icmp_check_5 = Check(round=round_5, service=icmp_service, result=last_check_results[team.name]['ICMP'], output="example_output") dns_check_5 = Check(round=round_5, service=dns_service, result=last_check_results[team.name]['DNS'], output="example_output") ftp_upload_check_5 = Check(round=round_5, service=ftp_upload_service, result=last_check_results[team.name]['FTPUpload'], output="example_output") self.db.save(round_5) self.db.save(icmp_check_5) self.db.save(dns_check_5) self.db.save(ftp_upload_check_5) self.db.save(team) teams.append(team) overview_data = self.client.get('/api/overview/data') overview_dict = json.loads(overview_data.data.decode('utf8')) expected_dict = { 'team1': { 'FTPUpload': {'passing': True, 'ip_address': '1.2.3.4', 'port': 21}, 'DNS': {'passing': True, 'ip_address': '8.8.8.8', 'port': 53}, 'ICMP': {'passing': True, 'ip_address': '127.0.0.1', 'port': 0}}, 'team2': { 'FTPUpload': {'passing': False, 'ip_address': '1.2.3.4', 'port': 21}, 'DNS': {'passing': True, 'ip_address': '8.8.8.8', 'port': 53}, 'ICMP': {'passing': True, 'ip_address': '127.0.0.1', 'port': 0}}, 'team3': { 'FTPUpload': {'passing': False, 'ip_address': '1.2.3.4', 'port': 21}, 'DNS': {'passing': True, 'ip_address': '8.8.8.8', 'port': 53}, 'ICMP': {'passing': False, 'ip_address': '127.0.0.1', 'port': 0}}, 'team4': { 'FTPUpload': {'passing': True, 'ip_address': '1.2.3.4', 'port': 21}, 'DNS': {'passing': False, 'ip_address': '8.8.8.8', 'port': 53}, 'ICMP': {'passing': False, 'ip_address': '127.0.0.1', 'port': 0}}, 'team5': { 'FTPUpload': {'passing': False, 'ip_address': '1.2.3.4', 'port': 21}, 'DNS': {'passing': False, 'ip_address': '8.8.8.8', 'port': 53}, 'ICMP': {'passing': False, 'ip_address': '127.0.0.1', 'port': 0} } } assert sorted(overview_dict.items()) == sorted(expected_dict.items())