Example #1
0
def _create_default_tenant():
    default_tenant = Tenant(
        id=constants.DEFAULT_TENANT_ID,
        name=constants.DEFAULT_TENANT_NAME
    )
    db.session.add(default_tenant)
    return default_tenant
    def test_different_tenant(self):
        """A new tenant sees none of the events of other tenants"""
        tenant = Tenant(name='other_tenant')
        db.session.add(tenant)
        db.session.commit()

        query, event_count = EventsV1._build_select_query(
            self.DEFAULT_FILTERS, self.DEFAULT_SORT,
            self.DEFAULT_RANGE_FILTERS, tenant.id)
        events = query.params(**self.DEFAULT_PAGINATION).all()

        self.assertEqual(events, [])
        self.assertEqual(event_count, 0)
    def _populate_db(self):
        """Populate database with events and logs."""
        fake = Faker()
        session = db.session

        tenant = Tenant(name='test_tenant')
        session.add(tenant)
        session.commit()

        blueprints = [
            Blueprint(
                id='blueprint_{}'.format(fake.uuid4()),
                created_at=fake.date_time(),
                main_file_name=fake.file_name(),
                plan='<plan>',
                _tenant_id=tenant.id,
                _creator_id=fake.uuid4(),
            ) for _ in xrange(self.BLUEPRINT_COUNT)
        ]
        session.add_all(blueprints)
        session.commit()

        deployments = []
        for _ in xrange(self.DEPLOYMENT_COUNT):
            blueprint = choice(blueprints)
            deployments.append(
                Deployment(id='deployment_{}'.format(fake.uuid4()),
                           created_at=fake.date_time(),
                           _blueprint_fk=blueprint._storage_id,
                           _creator_id=fake.uuid4(),
                           _tenant_id=blueprint._tenant_id))
        session.add_all(deployments)
        session.commit()

        executions = []
        for _ in xrange(self.EXECUTION_COUNT):
            deployment = choice(deployments)
            executions.append(
                Execution(
                    id='execution_{}'.format(fake.uuid4()),
                    created_at=fake.date_time(),
                    is_system_workflow=False,
                    workflow_id=fake.uuid4(),
                    _tenant_id=deployment._tenant_id,
                    _creator_id=fake.uuid4(),
                    _deployment_fk=deployment._storage_id,
                ))
        session.add_all(executions)
        session.commit()

        nodes = []
        for _ in xrange(self.NODE_COUNT):
            deployment = choice(deployments)
            nodes.append(
                Node(
                    id='node_{}'.format(fake.uuid4()),
                    deploy_number_of_instances=1,
                    max_number_of_instances=1,
                    min_number_of_instances=1,
                    number_of_instances=1,
                    planned_number_of_instances=1,
                    type='<type>',
                    _deployment_fk=deployment._storage_id,
                    _tenant_id=deployment._tenant_id,
                    _creator_id=deployment._creator_id,
                ))
        session.add_all(nodes)
        session.commit()

        node_instances = []
        for _ in xrange(self.NODE_INSTANCE_COUNT):
            node = choice(nodes)
            node_instances.append(
                NodeInstance(
                    id='node_instance_{}'.format(fake.uuid4()),
                    state='<state>',
                    _node_fk=node._storage_id,
                    _tenant_id=node._tenant_id,
                    _creator_id=node._creator_id,
                ))
        session.add_all(node_instances)
        session.commit()

        def create_event():
            """Create new event using the execution created above."""
            execution = choice(executions)
            return Event(
                id='event_{}'.format(fake.uuid4()),
                timestamp=fake.date_time(),
                reported_timestamp=fake.date_time(),
                _execution_fk=execution._storage_id,
                _tenant_id=execution._tenant_id,
                _creator_id=execution._creator_id,
                node_id=choice(node_instances).id,
                operation='<operation>',
                event_type=choice(self.EVENT_TYPES),
                message=fake.sentence(),
                message_code='<message_code>',
            )

        def create_log():
            """Create new log using the execution created above."""
            execution = choice(executions)
            return Log(
                id='log_{}'.format(fake.uuid4()),
                timestamp=fake.date_time(),
                reported_timestamp=fake.date_time(),
                _execution_fk=execution._storage_id,
                _tenant_id=execution._tenant_id,
                _creator_id=execution._creator_id,
                node_id=choice(node_instances).id,
                operation='<operation>',
                logger='<logger>',
                level=choice(self.LOG_LEVELS),
                message=fake.sentence(),
                message_code='<message_code>',
            )

        events = [
            choice([create_event, create_log])()
            for _ in xrange(self.EVENT_COUNT)
        ]
        sorted_events = sorted(events, key=lambda event: event.timestamp)
        session.add_all(sorted_events)
        session.commit()

        self.tenant = tenant
        self.fake = fake
        self.blueprints = blueprints
        self.deployments = deployments
        self.executions = executions
        self.nodes = nodes
        self.node_instances = node_instances
        self.events = sorted_events