コード例 #1
0
 def test_checks(self):
     service = generate_sample_model_tree('Service', self.db)
     round_obj = generate_sample_model_tree('Round', self.db)
     check_1 = Check(round=round_obj, service=service)
     self.db.save(check_1)
     check_2 = Check(round=round_obj, service=service)
     self.db.save(check_2)
     check_3 = Check(round=round_obj, service=service)
     self.db.save(check_3)
     assert service.checks == [check_1, check_2, check_3]
コード例 #2
0
 def test_last_ten_checks_4_checks(self):
     service = generate_sample_model_tree('Service', self.db)
     check_1 = Check(service=service, result=True, output='Good output')
     check_2 = Check(service=service, result=True, output='Good output')
     check_3 = Check(service=service, result=True, output='Good output')
     check_4 = Check(service=service, result=True, output='Good output')
     self.db.save(check_1)
     self.db.save(check_2)
     self.db.save(check_3)
     self.db.save(check_4)
     assert service.last_ten_checks == [check_4, check_3, check_2, check_1]
コード例 #3
0
 def test_checks(self):
     service = generate_sample_model_tree('Service', self.session)
     round_obj = generate_sample_model_tree('Round', self.session)
     check_1 = Check(round=round_obj, service=service)
     self.session.add(check_1)
     check_2 = Check(round=round_obj, service=service)
     self.session.add(check_2)
     check_3 = Check(round=round_obj, service=service)
     self.session.add(check_3)
     self.session.commit()
     assert service.checks == [check_1, check_2, check_3]
コード例 #4
0
 def test_max_score(self):
     service = generate_sample_model_tree('Service', self.db)
     check_1 = Check(service=service, result=True, output='Good output')
     check_2 = Check(service=service, result=True, output='Good output')
     check_3 = Check(service=service, result=True, output='Good output')
     check_4 = Check(service=service, result=True, output='Good output')
     check_5 = Check(service=service, result=False, output='bad output')
     self.db.save(check_1)
     self.db.save(check_2)
     self.db.save(check_3)
     self.db.save(check_4)
     self.db.save(check_5)
     assert service.max_score == 500
コード例 #5
0
 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
コード例 #6
0
 def test_score_earned(self):
     service = generate_sample_model_tree('Service', self.session)
     check_1 = Check(service=service, result=True, output='Good output')
     check_2 = Check(service=service, result=True, output='Good output')
     check_3 = Check(service=service, result=True, output='Good output')
     check_4 = Check(service=service, result=True, output='Good output')
     check_5 = Check(service=service, result=False, output='bad output')
     self.session.add(check_1)
     self.session.add(check_2)
     self.session.add(check_3)
     self.session.add(check_4)
     self.session.add(check_5)
     self.session.commit()
     assert service.score_earned == 400
コード例 #7
0
 def test_percent_earned(self):
     service = generate_sample_model_tree('Service', self.db)
     service = generate_sample_model_tree('Service', self.db)
     check_1 = Check(service=service, result=True, output='Good output')
     check_2 = Check(service=service, result=True, output='Good output')
     check_3 = Check(service=service, result=True, output='Good output')
     check_4 = Check(service=service, result=True, output='Good output')
     check_5 = Check(service=service, result=False, output='bad output')
     self.db.save(check_1)
     self.db.save(check_2)
     self.db.save(check_3)
     self.db.save(check_4)
     self.db.save(check_5)
     assert service.percent_earned == 80
コード例 #8
0
 def test_checks_reversed(self):
     service = generate_sample_model_tree('Service', self.db)
     round_obj_1 = Round(number=1)
     round_obj_2 = Round(number=2)
     round_obj_3 = Round(number=3)
     self.db.save(round_obj_1)
     self.db.save(round_obj_2)
     self.db.save(round_obj_3)
     check_1 = Check(round=round_obj_1, service=service)
     self.db.save(check_1)
     check_2 = Check(round=round_obj_2, service=service)
     self.db.save(check_2)
     check_3 = Check(round=round_obj_3, service=service)
     self.db.save(check_3)
     assert service.checks_reversed == [check_3, check_2, check_1]
コード例 #9
0
ファイル: helpers.py プロジェクト: yankeeprof/scoringengine
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
コード例 #10
0
 def test_finished(self):
     service = generate_sample_model_tree('Service', self.db)
     round_obj = Round(number=1)
     self.db.save(round_obj)
     check = Check(round=round_obj, service=service)
     self.db.save(check)
     assert check.result is None
     assert check.output == ''
     assert check.completed is False
     assert check.reason == ''
     check.finished(True, 'Successful Match', 'good output', 'example command')
     self.db.save(check)
     assert check.result is True
     assert check.output == 'good output'
     assert check.reason == 'Successful Match'
     assert check.command == 'example command'
     assert check.completed is True
     assert type(check.local_completed_timestamp) is str
コード例 #11
0
ファイル: helpers.py プロジェクト: gaudard/scoringengine
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
コード例 #12
0
    def test_check_result_for_round_3_rounds(self):
        service = generate_sample_model_tree('Service', self.db)

        round_1 = Round(number=1)
        self.db.save(round_1)
        check_1 = Check(round=round_1, result=True, service=service)
        self.db.save(check_1)

        round_2 = Round(number=2)
        self.db.save(round_2)
        check_2 = Check(round=round_2, result=True, service=service)
        self.db.save(check_2)

        round_3 = Round(number=3)
        self.db.save(round_3)
        check_3 = Check(round=round_3, result=False, service=service)
        self.db.save(check_3)
        assert service.check_result_for_round(1) is True
        assert service.check_result_for_round(2) is True
        assert service.check_result_for_round(3) is False
コード例 #13
0
    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
コード例 #14
0
    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
コード例 #15
0
 def test_basic_check(self):
     service = generate_sample_model_tree('Service', self.db)
     round_obj = Round(number=1)
     self.db.save(round_obj)
     check = Check(round=round_obj, service=service, result=True, output="example_output")
     self.db.save(check)
     assert check.id is not None
     assert check.round == round_obj
     assert check.round_id == round_obj.id
     assert check.service == service
     assert check.service_id == service.id
     assert check.result is True
     assert check.output == 'example_output'
     assert check.completed is False
コード例 #16
0
 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
コード例 #17
0
ファイル: test_team.py プロジェクト: tanner-g/scoring_engine
    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
コード例 #18
0
 def test_last_ten_checks_15_checks(self):
     service = generate_sample_model_tree('Service', self.session)
     check_1 = Check(service=service, result=True, output='Good output')
     check_2 = Check(service=service, result=True, output='Good output')
     check_3 = Check(service=service, result=True, output='Good output')
     check_4 = Check(service=service, result=True, output='Good output')
     check_5 = Check(service=service, result=True, output='Good output')
     check_6 = Check(service=service, result=True, output='Good output')
     check_7 = Check(service=service, result=True, output='Good output')
     check_8 = Check(service=service, result=True, output='Good output')
     check_9 = Check(service=service, result=True, output='Good output')
     check_10 = Check(service=service, result=True, output='Good output')
     check_11 = Check(service=service, result=True, output='Good output')
     check_12 = Check(service=service, result=True, output='Good output')
     check_13 = Check(service=service, result=True, output='Good output')
     check_14 = Check(service=service, result=True, output='Good output')
     check_15 = Check(service=service, result=True, output='Good output')
     self.session.add(check_1)
     self.session.add(check_2)
     self.session.add(check_3)
     self.session.add(check_4)
     self.session.add(check_5)
     self.session.add(check_6)
     self.session.add(check_7)
     self.session.add(check_8)
     self.session.add(check_9)
     self.session.add(check_10)
     self.session.add(check_11)
     self.session.add(check_12)
     self.session.add(check_13)
     self.session.add(check_14)
     self.session.add(check_15)
     self.session.commit()
     assert service.last_ten_checks == [
         check_15,
         check_14,
         check_13,
         check_12,
         check_11,
         check_10,
         check_9,
         check_8,
         check_7,
         check_6
     ]
コード例 #19
0
    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
コード例 #20
0
 def test_init_check(self):
     check = Check()
     assert check.id is None
     assert check.round is None
     assert check.round_id is None
コード例 #21
0
ファイル: engine.py プロジェクト: tanner-g/scoring_engine
    def run(self):
        while (not self.last_round) and (self.rounds_run < self.total_rounds
                                         or self.total_rounds == 0):
            self.current_round += 1
            logger.info("Running round: " + str(self.current_round))
            self.round_running = True
            self.rounds_run += 1

            services = self.db.session.query(Service).all()[:]
            random.shuffle(services)
            task_ids = {}
            for service in services:
                check_class = self.check_name_to_obj(service.check_name)
                if check_class is None:
                    raise LookupError(
                        "Unable to map service to check code for " +
                        str(service.check_name))
                logger.info("Adding " + service.team.name + ' - ' +
                            service.name + " check to queue")
                environment = random.choice(service.environments)
                check_obj = check_class(environment)
                command_str = check_obj.command()
                job = Job(environment_id=environment.id, command=command_str)
                task = execute_command.delay(job)
                team_name = environment.service.team.name
                if team_name not in task_ids:
                    task_ids[team_name] = []
                task_ids[team_name].append(task.id)

            # We store the list of tasks in the db, so that the web app
            # can consume them and can dynamically update a progress bar
            task_ids_str = json.dumps(task_ids)
            latest_kb = KB(name='task_ids',
                           value=task_ids_str,
                           round_num=self.current_round)
            self.db.save(latest_kb)

            pending_tasks = self.all_pending_tasks(task_ids)
            while pending_tasks:
                waiting_info = "Waiting for all jobs to finish (sleeping " + str(
                    self.worker_wait_time) + " seconds)"
                waiting_info += " " + str(
                    len(pending_tasks)) + " left in queue."
                logger.info(waiting_info)
                self.sleep(self.worker_wait_time)
                pending_tasks = self.all_pending_tasks(task_ids)
            logger.info("All jobs have finished for this round")

            logger.info("Determining check results and saving to db")
            round_obj = Round(number=self.current_round)
            self.db.save(round_obj)

            # We keep track of the number of passed and failed checks per round
            # so we can report a little bit at the end of each round
            teams = {}
            for team_name, task_ids in task_ids.items():
                for task_id in task_ids:
                    task = execute_command.AsyncResult(task_id)
                    environment = self.db.session.query(Environment).get(
                        task.result['environment_id'])
                    if task.result['errored_out']:
                        result = False
                        reason = 'Task Timed Out'
                    else:
                        if re.search(environment.matching_regex,
                                     task.result['output']):
                            result = True
                            reason = "Successful Content Match"
                        else:
                            result = False
                            reason = 'Unsuccessful Content Match'

                    if environment.service.team.name not in teams:
                        teams[environment.service.team.name] = {
                            "Success": [],
                            "Failed": [],
                        }
                    if result:
                        teams[environment.service.team.name]['Success'].append(
                            environment.service.name)
                    else:
                        teams[environment.service.team.name]['Failed'].append(
                            environment.service.name)

                    check = Check(service=environment.service, round=round_obj)
                    check.finished(result=result,
                                   reason=reason,
                                   output=task.result['output'],
                                   command=task.result['command'])
                    self.db.save(check)

            logger.info("Finished Round " + str(self.current_round))
            logger.info("Round Stats:")
            for team_name in sorted(teams):
                stat_string = " " + team_name
                stat_string += " Success: " + str(
                    len(teams[team_name]['Success']))
                stat_string += ", Failed: " + str(
                    len(teams[team_name]['Failed']))
                if len(teams[team_name]['Failed']) > 0:
                    stat_string += ' ' + str(teams[team_name]['Failed'])
                logger.info(stat_string)

            self.round_running = False

            if not self.last_round:
                logger.info("Sleeping in between rounds (" +
                            str(self.round_time_sleep) + " seconds)")
                self.sleep(self.round_time_sleep)
コード例 #22
0
ファイル: engine.py プロジェクト: ritsec/scoringengine
    def run(self):
        if self.total_rounds == 0:
            logger.info("Running engine for unlimited rounds")
        else:
            logger.info("Running engine for {0} round(s)".format(self.total_rounds))

        while not self.is_last_round():
            self.current_round += 1
            logger.info("Running round: " + str(self.current_round))
            self.round_running = True
            self.rounds_run += 1

            services = self.session.query(Service).all()[:]
            random.shuffle(services)
            task_ids = {}
            for service in services:
                check_class = self.check_name_to_obj(service.check_name)
                if check_class is None:
                    raise LookupError("Unable to map service to check code for " + str(service.check_name))
                logger.debug("Adding " + service.team.name + ' - ' + service.name + " check to queue")
                environment = random.choice(service.environments)
                check_obj = check_class(environment)
                command_str = check_obj.command()
                job = Job(environment_id=environment.id, command=command_str)
                task = execute_command.apply_async(args=[job], queue=service.worker_queue)
                team_name = environment.service.team.name
                if team_name not in task_ids:
                    task_ids[team_name] = []
                task_ids[team_name].append(task.id)

            # This array keeps track of all current round objects
            # incase we need to backout any changes to prevent
            # inconsistent check results
            cleanup_items = []

            try:
                # We store the list of tasks in the db, so that the web app
                # can consume them and can dynamically update a progress bar
                task_ids_str = json.dumps(task_ids)
                latest_kb = KB(name='task_ids', value=task_ids_str, round_num=self.current_round)
                cleanup_items.append(latest_kb)
                self.session.add(latest_kb)
                self.session.commit()

                pending_tasks = self.all_pending_tasks(task_ids)
                while pending_tasks:
                    worker_refresh_time = int(Setting.get_setting('worker_refresh_time').value)
                    waiting_info = "Waiting for all jobs to finish (sleeping " + str(worker_refresh_time) + " seconds)"
                    waiting_info += " " + str(len(pending_tasks)) + " left in queue."
                    logger.info(waiting_info)
                    self.sleep(worker_refresh_time)
                    pending_tasks = self.all_pending_tasks(task_ids)
                logger.info("All jobs have finished for this round")

                logger.info("Determining check results and saving to db")
                round_obj = Round(number=self.current_round)
                cleanup_items.append(round_obj)
                self.session.add(round_obj)
                self.session.commit()

                # We keep track of the number of passed and failed checks per round
                # so we can report a little bit at the end of each round
                teams = {}
                # Used so we import the finished checks at the end of the round
                finished_checks = []
                for team_name, task_ids in task_ids.items():
                    for task_id in task_ids:
                        task = execute_command.AsyncResult(task_id)
                        environment = self.session.query(Environment).get(task.result['environment_id'])
                        if task.result['errored_out']:
                            result = False
                            reason = CHECK_TIMED_OUT_TEXT
                        else:
                            if re.search(environment.matching_content, task.result['output']):
                                result = True
                                reason = CHECK_SUCCESS_TEXT
                            else:
                                result = False
                                reason = CHECK_FAILURE_TEXT

                        if environment.service.team.name not in teams:
                            teams[environment.service.team.name] = {
                                "Success": [],
                                "Failed": [],
                            }
                        if result:
                            teams[environment.service.team.name]['Success'].append(environment.service.name)
                        else:
                            teams[environment.service.team.name]['Failed'].append(environment.service.name)

                        check = Check(service=environment.service, round=round_obj)
                        # Grab the first 35,000 characters of output so it'll fit into our TEXT column,
                        # which maxes at 2^32 (65536) characters
                        check.finished(result=result, reason=reason, output=task.result['output'][:35000], command=task.result['command'])
                        finished_checks.append(check)

                for finished_check in finished_checks:
                    cleanup_items.append(finished_check)
                    self.session.add(finished_check)
                self.session.commit()

            except Exception as e:
                # We got an error while writing to db (could be normal docker stop command)
                # but we gotta clean up any trace of the current round so when we startup
                # again, we're at a consistent state
                logger.error('Error received while writing check results to db')
                logger.exception(e)
                logger.error('Ending round and cleaning up the db')
                for cleanup_item in cleanup_items:
                    try:
                        self.session.delete(cleanup_item)
                        self.session.commit()
                    except Exception:
                        pass
                sys.exit(1)

            logger.info("Finished Round " + str(self.current_round))
            logger.info("Round Stats:")
            for team_name in sorted(teams):
                stat_string = " " + team_name
                stat_string += " Success: " + str(len(teams[team_name]['Success']))
                stat_string += ", Failed: " + str(len(teams[team_name]['Failed']))
                if len(teams[team_name]['Failed']) > 0:
                    stat_string += ' (' + ', '.join(teams[team_name]['Failed']) + ')'
                logger.info(stat_string)

            logger.info("Updating Caches")
            update_all_cache()

            self.round_running = False

            if not self.is_last_round():
                round_time_sleep = int(Setting.get_setting('round_time_sleep').value)
                logger.info("Sleeping in between rounds (" + str(round_time_sleep) + " seconds)")
                self.sleep(round_time_sleep)

        logger.info("Engine finished running")
コード例 #23
0
    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())