Python airflow.models.DagRun.dag_id() Examples

The following are 30 code examples of airflow.models.DagRun.dag_id(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module airflow.models.DagRun , or try the search function .
Example #1
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_blocked_success_when_selecting_dags(self):
        resp = self.client.post('blocked',
                                data={'dag_ids': ['example_subdag_operator']},
                                follow_redirects=True)
        self.assertEqual(resp.status_code, 200)
        blocked_dags = {blocked['dag_id'] for blocked in json.loads(resp.data.decode('utf-8'))}
        self.assertNotIn('example_bash_operator', blocked_dags)
        self.assertIn('example_subdag_operator', blocked_dags)

        # Multiple
        resp = self.client.post('blocked',
                                data={'dag_ids': ['example_subdag_operator', 'example_bash_operator']},
                                follow_redirects=True)
        self.assertEqual(resp.status_code, 200)
        blocked_dags = {blocked['dag_id'] for blocked in json.loads(resp.data.decode('utf-8'))}
        self.assertIn('example_bash_operator', blocked_dags)
        self.assertIn('example_subdag_operator', blocked_dags)
        self.check_content_not_in_response('example_xcom', resp) 
Example #2
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_delete_dag_button_for_dag_on_scheduler_only(self):
        # Test for JIRA AIRFLOW-3233 (PR 4069):
        # The delete-dag URL should be generated correctly for DAGs
        # that exist on the scheduler (DB) but not the webserver DagBag

        dag_id = 'example_bash_operator'
        test_dag_id = "non_existent_dag"

        DM = models.DagModel
        dag_query = self.session.query(DM).filter(DM.dag_id == dag_id)
        dag_query.first().tags = []  # To avoid "FOREIGN KEY constraint" error
        self.session.commit()

        dag_query.update({'dag_id': test_dag_id})
        self.session.commit()

        resp = self.client.get('/', follow_redirects=True)
        self.check_content_in_response('/delete?dag_id={}'.format(test_dag_id), resp)
        self.check_content_in_response("return confirmDeleteDag(this, '{}')".format(test_dag_id), resp)

        self.session.query(DM).filter(DM.dag_id == test_dag_id).update({'dag_id': dag_id})
        self.session.commit() 
Example #3
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_show_external_log_redirect_link_with_external_log_handler(self, endpoint, mock_log_handler):
        """Show external links if log handler is external."""
        class ExternalHandler(ExternalLoggingMixin):
            LOG_NAME = 'ExternalLog'

            @property
            def log_name(self):
                return self.LOG_NAME

        mock_log_handler.return_value = ExternalHandler()

        url = f'{endpoint}?dag_id=example_bash_operator'
        with self.capture_templates() as templates:
            self.client.get(url, follow_redirects=True)
            ctx = templates[0].local_context
            self.assertTrue(ctx['show_external_log_redirect'])
            self.assertEqual(ctx['external_log_name'], ExternalHandler.LOG_NAME) 
Example #4
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_success_dag_never_run(self):
        endpoint = "success"
        dag_id = "example_bash_operator"
        form = dict(
            task_id="run_this_last",
            dag_id=dag_id,
            execution_date=self.EXAMPLE_DAG_DEFAULT_DATE,
            upstream="false",
            downstream="false",
            future="false",
            past="false",
            origin="/graph?dag_id=example_bash_operator",
        )
        clear_db_runs()
        resp = self.client.post('success', data=form, follow_redirects=True)
        self.check_content_in_response(
            f"Cannot make {endpoint}, seem that dag {dag_id} has never run", resp) 
Example #5
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_trigger_dag_conf(self):

        test_dag_id = "example_bash_operator"
        conf_dict = {'string': 'Hello, World!'}

        DR = models.DagRun
        self.session.query(DR).delete()
        self.session.commit()

        self.client.post('trigger?dag_id={}'.format(test_dag_id), data={'conf': json.dumps(conf_dict)})

        run = self.session.query(DR).filter(DR.dag_id == test_dag_id).first()
        self.assertIsNotNone(run)
        self.assertIn(DagRunType.MANUAL.value, run.run_id)
        self.assertEqual(run.run_type, DagRunType.MANUAL.value)
        self.assertEqual(run.conf, conf_dict) 
Example #6
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_success_fail_for_read_only_role(self):
        # succcess endpoint need can_dag_edit, which read only role can not access
        self.logout()
        self.login(username='dag_read_only',
                   password='dag_read_only')

        form = dict(
            task_id="run_this_last",
            dag_id="example_bash_operator",
            execution_date=self.default_date,
            upstream="false",
            downstream="false",
            future="false",
            past="false",
        )
        resp = self.client.post('success', data=form)
        self.check_content_not_in_response('Wait a minute', resp, resp_code=302) 
Example #7
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_user_defined_filter_and_macros_raise_error(self):
        """
        Test that the Rendered View is able to show rendered values
        even for TIs that have not yet executed
        """
        self.app.dag_bag = mock.MagicMock(
            **{'get_dag.return_value': SerializedDagModel.get(self.dag.dag_id).dag}
        )
        self.assertEqual(self.task2.bash_command,
                         'echo {{ fullname("Apache", "Airflow") | hello }}')

        url = ('rendered?task_id=task2&dag_id=testdag&execution_date={}'
               .format(self.percent_encode(self.default_date)))

        resp = self.client.get(url, follow_redirects=True)
        self.check_content_not_in_response("echo Hello Apache Airflow", resp)
        self.check_content_in_response(
            "Webserver does not have access to User-defined Macros or Filters "
            "when Dag Serialization is enabled. Hence for the task that have not yet "
            "started running, please use 'airflow tasks render' for debugging the "
            "rendering of template_fields.<br/><br/>OriginalError: no filter named &#39;hello&#39",
            resp
        ) 
Example #8
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_get_logs_for_handler_without_read_method(self, mock_log_reader):
        type(mock_log_reader.return_value).supports_read = PropertyMock(return_value=False)

        url_template = "get_logs_with_metadata?dag_id={}&" \
                       "task_id={}&execution_date={}&" \
                       "try_number={}&metadata={}&format=json"
        try_number = 1
        url = url_template.format(self.DAG_ID,
                                  self.TASK_ID,
                                  quote_plus(self.DEFAULT_DATE.isoformat()),
                                  try_number,
                                  json.dumps({}))
        response = self.client.get(url)
        self.assertEqual(200, response.status_code)
        self.assertIn('message', response.json)
        self.assertIn('metadata', response.json)
        self.assertIn(
            'Task log handler does not support read logs.',
            response.json['message']) 
Example #9
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_get_logs_with_metadata_as_download_file(self):
        url_template = "get_logs_with_metadata?dag_id={}&" \
                       "task_id={}&execution_date={}&" \
                       "try_number={}&metadata={}&format=file"
        try_number = 1
        url = url_template.format(self.DAG_ID,
                                  self.TASK_ID,
                                  quote_plus(self.DEFAULT_DATE.isoformat()),
                                  try_number,
                                  json.dumps({}))
        response = self.client.get(url)
        expected_filename = '{}/{}/{}/{}.log'.format(self.DAG_ID,
                                                     self.TASK_ID,
                                                     self.DEFAULT_DATE.isoformat(),
                                                     try_number)

        content_disposition = response.headers.get('Content-Disposition')
        self.assertTrue(content_disposition.startswith('attachment'))
        self.assertTrue(expected_filename in content_disposition)
        self.assertEqual(200, response.status_code)
        self.assertIn('Log for testing.', response.data.decode('utf-8')) 
Example #10
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_get_logs_with_metadata_as_download_large_file(self):
        with mock.patch("airflow.utils.log.file_task_handler.FileTaskHandler.read") as read_mock:
            first_return = (['1st line'], [{}])
            second_return = (['2nd line'], [{'end_of_log': False}])
            third_return = (['3rd line'], [{'end_of_log': True}])
            fourth_return = (['should never be read'], [{'end_of_log': True}])
            read_mock.side_effect = [first_return, second_return, third_return, fourth_return]
            url_template = "get_logs_with_metadata?dag_id={}&" \
                           "task_id={}&execution_date={}&" \
                           "try_number={}&metadata={}&format=file"
            try_number = 1
            url = url_template.format(self.DAG_ID,
                                      self.TASK_ID,
                                      quote_plus(self.DEFAULT_DATE.isoformat()),
                                      try_number,
                                      json.dumps({}))
            response = self.client.get(url)

            self.assertIn('1st line', response.data.decode('utf-8'))
            self.assertIn('2nd line', response.data.decode('utf-8'))
            self.assertIn('3rd line', response.data.decode('utf-8'))
            self.assertNotIn('should never be read', response.data.decode('utf-8')) 
Example #11
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_get_logs_with_metadata_for_removed_dag(self, mock_read):
        mock_read.return_value = (['airflow log line'], [{'end_of_log': True}])
        url_template = "get_logs_with_metadata?dag_id={}&" \
                       "task_id={}&execution_date={}&" \
                       "try_number={}&metadata={}"
        url = url_template.format(self.DAG_ID_REMOVED, self.TASK_ID,
                                  quote_plus(self.DEFAULT_DATE.isoformat()), 1, json.dumps({}))
        response = self.client.get(
            url,
            data=dict(
                username='test',
                password='test'
            ),
            follow_redirects=True
        )

        self.assertIn('"message":', response.data.decode('utf-8'))
        self.assertIn('"metadata":', response.data.decode('utf-8'))
        self.assertIn('airflow log line', response.data.decode('utf-8'))
        self.assertEqual(200, response.status_code) 
Example #12
Source File: test_views.py    From airflow with Apache License 2.0 6 votes vote down vote up
def test_get_logs_with_metadata(self):
        url_template = "get_logs_with_metadata?dag_id={}&" \
                       "task_id={}&execution_date={}&" \
                       "try_number={}&metadata={}"
        response = \
            self.client.get(url_template.format(self.DAG_ID,
                                                self.TASK_ID,
                                                quote_plus(self.DEFAULT_DATE.isoformat()),
                                                1,
                                                json.dumps({})), data=dict(
                                                    username='test',
                                                    password='test'),
                            follow_redirects=True)

        self.assertIn('"message":', response.data.decode('utf-8'))
        self.assertIn('"metadata":', response.data.decode('utf-8'))
        self.assertIn('Log for testing.', response.data.decode('utf-8'))
        self.assertEqual(200, response.status_code) 
Example #13
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_failed_success(self):
        self.logout()
        self.login()
        form = dict(
            task_id="run_this_last",
            dag_id="example_bash_operator",
            execution_date=self.default_date,
            upstream="false",
            downstream="false",
            future="false",
            past="false",
        )
        resp = self.client.post('failed', data=form)
        self.check_content_in_response('example_bash_operator', resp) 
Example #14
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_trigger_dag_form(self):
        test_dag_id = "example_bash_operator"

        resp = self.client.get('trigger?dag_id={}'.format(test_dag_id))

        self.assertEqual(resp.status_code, 200)
        self.check_content_in_response('Trigger DAG: {}'.format(test_dag_id), resp) 
Example #15
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_trigger_dag_button_normal_exist(self):
        resp = self.client.get('/', follow_redirects=True)
        self.assertIn('/trigger?dag_id=example_bash_operator', resp.data.decode('utf-8'))
        self.assertIn("return confirmDeleteDag(this, 'example_bash_operator')", resp.data.decode('utf-8')) 
Example #16
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_log_success_for_user(self):
        self.logout()
        self.login(username='test_user',
                   password='test_user')
        url = ('log?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('Log by attempts', resp)
        url = ('get_logs_with_metadata?task_id=runme_0&dag_id=example_bash_operator&'
               'execution_date={}&try_number=1&metadata=null'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('"message":', resp)
        self.check_content_in_response('"metadata":', resp) 
Example #17
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_refresh_failure_for_viewer(self):
        # viewer role can't refresh
        self.logout()
        self.login(username='test_viewer',
                   password='test_viewer')
        resp = self.client.post('refresh?dag_id=example_bash_operator')
        self.check_content_in_response('Redirecting', resp, resp_code=302) 
Example #18
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_rendered_view(self):
        """
        Test that the Rendered View contains the values from RenderedTaskInstanceFields
        """
        self.assertEqual(self.task1.bash_command, '{{ task_instance_key_str }}')
        ti = TaskInstance(self.task1, self.default_date)

        with create_session() as session:
            session.add(RTIF(ti))

        url = ('rendered?task_id=task1&dag_id=testdag&execution_date={}'
               .format(self.percent_encode(self.default_date)))

        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response("testdag__task1__20200301", resp) 
Example #19
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_rendered_view_for_unexecuted_tis(self):
        """
        Test that the Rendered View is able to show rendered values
        even for TIs that have not yet executed
        """
        self.assertEqual(self.task1.bash_command, '{{ task_instance_key_str }}')

        url = ('rendered?task_id=task1&dag_id=task1&execution_date={}'
               .format(self.percent_encode(self.default_date)))

        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response("testdag__task1__20200301", resp) 
Example #20
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_dag_details_success_for_all_dag_user(self):
        self.logout()
        self.login(username='all_dag_user',
                   password='all_dag_user')
        url = 'dag_details?dag_id=example_bash_operator'
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('example_bash_operator', resp)

        url = 'dag_details?dag_id=example_subdag_operator'
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('example_subdag_operator', resp) 
Example #21
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_run_success(self):
        self.logout()
        self.login()
        form = dict(
            task_id="runme_0",
            dag_id="example_bash_operator",
            ignore_all_deps="false",
            ignore_ti_state="true",
            execution_date=self.default_date,
        )
        resp = self.client.post('run', data=form)
        self.check_content_in_response('', resp, resp_code=302) 
Example #22
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_xcom_success_for_all_dag_user(self):
        self.logout()
        self.login(username='all_dag_user',
                   password='all_dag_user')
        url = ('xcom?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('XCom', resp) 
Example #23
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_xcom_failure(self):
        self.logout()
        self.login(username='dag_faker',
                   password='dag_faker')
        url = ('xcom?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_not_in_response('XCom', resp) 
Example #24
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_xcom_success(self):
        self.logout()
        self.login()
        url = ('xcom?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('XCom', resp) 
Example #25
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_task_success_for_all_dag_user(self):
        self.logout()
        self.login(username='all_dag_user',
                   password='all_dag_user')
        url = ('task?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('Task Instance Details', resp) 
Example #26
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_task_success(self):
        self.logout()
        self.login()
        url = ('task?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('Task Instance Details', resp) 
Example #27
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_rendered_success_for_all_dag_user(self):
        self.logout()
        self.login(username='all_dag_user',
                   password='all_dag_user')
        url = ('rendered?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('Rendered Template', resp) 
Example #28
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_rendered_failure(self):
        self.logout()
        self.login(username='dag_faker',
                   password='dag_faker')
        url = ('rendered?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_not_in_response('Rendered Template', resp) 
Example #29
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_rendered_success(self):
        self.logout()
        self.login()
        url = ('rendered?task_id=runme_0&dag_id=example_bash_operator&execution_date={}'
               .format(self.percent_encode(self.default_date)))
        resp = self.client.get(url, follow_redirects=True)
        self.check_content_in_response('Rendered Template', resp) 
Example #30
Source File: test_views.py    From airflow with Apache License 2.0 5 votes vote down vote up
def teardown(self):
        self.test.session.query(DagRun).filter(
            DagRun.dag_id == self.DAG_ID).delete()
        self.test.session.commit()
        self.test.session.close()