def test_basic_service(self):
     service = generate_sample_model_tree('Service', self.db)
     environment = Environment(service=service, matching_regex='*')
     self.db.save(environment)
     property_1 = Property(name='example_property1', value='somevalue', environment=environment)
     property_2 = Property(name='example_property2', value='anotheralue', environment=environment)
     self.db.save(property_1)
     self.db.save(property_2)
     assert environment.id is not None
     assert environment.service_id == service.id
     assert environment.service == service
     assert environment.properties == [property_1, property_2]
 def test_init_property(self):
     property_obj = Property(name="testname", value="testvalue")
     assert property_obj.id is None
     assert property_obj.name == "testname"
     assert property_obj.value == "testvalue"
     assert property_obj.environment is None
     assert property_obj.environment_id is None
示例#3
0
 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)
示例#4
0
    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
示例#5
0
 def test_nonhidden_property(self):
     environment = generate_sample_model_tree('Environment', self.db)
     property_obj = Property(name="ip",
                             value="127.0.0.1",
                             environment=environment,
                             visible=True)
     self.db.save(property_obj)
     assert property_obj.visible is True
示例#6
0
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
示例#7
0
 def test_basic_property(self):
     environment = generate_sample_model_tree('Environment', self.db)
     property_obj = Property(name="ip",
                             value="127.0.0.1",
                             environment=environment)
     self.db.save(property_obj)
     assert property_obj.id is not None
     assert property_obj.environment == environment
     assert property_obj.environment_id == environment.id
     assert property_obj.visible is False
示例#8
0
 def test_bad_properties(self):
     environment = Environment(service=self.service, matching_regex='*')
     self.db.save(environment)
     self.service.environments = [environment]
     temp_properties = dict(self.properties)
     if len(temp_properties) > 0:
         temp_properties.popitem()
     else:
         temp_properties['exampleproperty'] = 'propertyvalueexample'
     for property_name, property_value in temp_properties.items():
         property_obj = Property(environment=environment,
                                 name=property_name,
                                 value=property_value)
         self.db.save(property_obj)
     with pytest.raises(LookupError):
         self.engine.check_name_to_obj(self.check_name)(environment)
示例#9
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()