Beispiel #1
0
    def test_context(self, JobClass, sqlite_copy_expert, project, inventory_source):
        """The Jinja context defines all of the fields that can be used by a template. Ensure that the context generated
        for each job type has the expected structure."""
        def check_structure(expected_structure, obj):
            if isinstance(expected_structure, dict):
                assert isinstance(obj, dict)
                for key in obj:
                    assert key in expected_structure
                    if obj[key] is None:
                        continue
                    if isinstance(expected_structure[key], dict):
                        assert isinstance(obj[key], dict)
                        check_structure(expected_structure[key], obj[key])
                    else:
                        if key == 'job_explanation':
                            assert isinstance(str(obj[key]), expected_structure[key])
                        else:
                            assert isinstance(obj[key], expected_structure[key])
        kwargs = {}
        if JobClass is InventoryUpdate:
            kwargs['inventory_source'] = inventory_source
            kwargs['source'] = inventory_source.source
        elif JobClass is ProjectUpdate:
            kwargs['project'] = project

        job = JobClass.objects.create(name='foo', **kwargs)
        job_serialization = UnifiedJobSerializer(job).to_representation(job)

        context = job.context(job_serialization)
        check_structure(TestJobNotificationMixin.CONTEXT_STRUCTURE, context)
Beispiel #2
0
    def build_notification_message(self, nt, status):
        env = sandbox.ImmutableSandboxedEnvironment()

        from awx.api.serializers import UnifiedJobSerializer
        job_serialization = UnifiedJobSerializer(self).to_representation(self)
        context = self.context(job_serialization)

        msg_template = body_template = None
        msg = body = ''

        # Use custom template if available
        if nt.messages:
            template = nt.messages.get(self.STATUS_TO_TEMPLATE_TYPE[status], {}) or {}
            msg_template = template.get('message', None)
            body_template = template.get('body', None)
        # If custom template not provided, look up default template
        default_template = nt.notification_class.default_messages[self.STATUS_TO_TEMPLATE_TYPE[status]]
        if not msg_template:
            msg_template = default_template.get('message', None)
        if not body_template:
            body_template = default_template.get('body', None)

        if msg_template:
            try:
                msg = env.from_string(msg_template).render(**context)
            except (TemplateSyntaxError, UndefinedError, SecurityError):
                msg = ''

        if body_template:
            try:
                body = env.from_string(body_template).render(**context)
            except (TemplateSyntaxError, UndefinedError, SecurityError):
                body = ''

        return (msg, body)
Beispiel #3
0
    def build_notification_message(self, nt, status):
        env = sandbox.ImmutableSandboxedEnvironment()

        from awx.api.serializers import UnifiedJobSerializer
        job_serialization = UnifiedJobSerializer(self).to_representation(self)
        context = self.context(job_serialization)

        msg_template = body_template = None

        if nt.messages:
            templates = nt.messages.get(self.STATUS_TO_TEMPLATE_TYPE[status], {}) or {}
            msg_template = templates.get('message', {})
            body_template = templates.get('body', {})

        if msg_template:
            try:
                notification_subject = env.from_string(msg_template).render(**context)
            except (TemplateSyntaxError, UndefinedError, SecurityError):
                notification_subject = ''
        else:
            notification_subject = u"{} #{} '{}' {}: {}".format(self.get_notification_friendly_name(),
                                                                self.id,
                                                                self.name,
                                                                status,
                                                                self.get_ui_url())
        notification_body = self.notification_data()
        notification_body['friendly_name'] = self.get_notification_friendly_name()
        if body_template:
            try:
                notification_body['body'] = env.from_string(body_template).render(**context)
            except (TemplateSyntaxError, UndefinedError, SecurityError):
                notification_body['body'] = ''

        return (notification_subject, notification_body)
Beispiel #4
0
    def test_schedule_context(self, job_template, admin_user):
        schedule = Schedule.objects.create(
            name='job-schedule',
            rrule='DTSTART:20171129T155939z\nFREQ=MONTHLY',
            unified_job_template=job_template)
        job = Job.objects.create(name='fake-job',
                                 launch_type='workflow',
                                 schedule=schedule,
                                 job_template=job_template)

        job_serialization = UnifiedJobSerializer(job).to_representation(job)

        context = job.context(job_serialization)
        self.check_structure(TestJobNotificationMixin.CONTEXT_STRUCTURE,
                             context)
Beispiel #5
0
    def test_context(self, JobClass, sqlite_copy_expert, project,
                     inventory_source):
        """The Jinja context defines all of the fields that can be used by a template. Ensure that the context generated
        for each job type has the expected structure."""
        kwargs = {}
        if JobClass is InventoryUpdate:
            kwargs['inventory_source'] = inventory_source
            kwargs['source'] = inventory_source.source
        elif JobClass is ProjectUpdate:
            kwargs['project'] = project

        job = JobClass.objects.create(name='foo', **kwargs)
        job_serialization = UnifiedJobSerializer(job).to_representation(job)

        context = job.context(job_serialization)
        self.check_structure(TestJobNotificationMixin.CONTEXT_STRUCTURE,
                             context)
Beispiel #6
0
 def test_context_job_metadata_with_unicode(self):
     job = Job.objects.create(name='批量安装项目')
     job_serialization = UnifiedJobSerializer(job).to_representation(job)
     context = job.context(job_serialization)
     assert '批量安装项目' in context['job_metadata']