Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AIRFLOW-1867] Fix sendgrid py3k bug; add sandbox mode #2824

Merged
merged 1 commit into from
Oct 20, 2018

Conversation

thesquelched
Copy link
Contributor

@thesquelched thesquelched commented Nov 29, 2017

Dear Airflow maintainers,

Please accept this PR. I understand that it will not be reviewed until I have checked off all the steps below!

JIRA

Description

  • Here are some details about my PR, including screenshots of any UI changes:
    • Fix sendgrid attachments bug in py3k
    • Add sandbox mode option
    • Minor style fixes
    • Add test

Tests

  • My PR adds the following unit tests OR does not need testing for this extremely good reason:
    • tests/contrib/utils/test_sendgrid.py: add attachment to existing test

Commits

  • My commits all reference JIRA issues in their subject lines, and I have squashed multiple commits if they address the same issue. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

@codecov-io
Copy link

codecov-io commented Nov 29, 2017

Codecov Report

Merging #2824 into master will decrease coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2824      +/-   ##
==========================================
- Coverage   75.91%   75.91%   -0.01%     
==========================================
  Files         199      199              
  Lines       15959    15957       -2     
==========================================
- Hits        12116    12113       -3     
- Misses       3843     3844       +1
Impacted Files Coverage Δ
airflow/models.py 91.94% <0%> (-0.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 74c613b...123cf66. Read the comment docs.

Copy link
Contributor

@gwax gwax left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM


# Test the right email is constructed.
@mock.patch('os.environ.get')
@mock.patch('airflow.contrib.utils.sendgrid._post_sendgrid_mail')
def test_send_email_sendgrid_correct_email(self, mock_post, mock_get):
mock_get.return_value = '[email protected]'
send_email(self.to, self.subject, self.html_content, cc=self.cc, bcc=self.bcc)

tempdir = tempfile.mkdtemp()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we only need one file, can we use tempfile.NamedTemporaryFile in a context instead of dealing with a temporary directory (and removing it)?

with tempfile.NamedTemporaryFile(mode='wt') as f:
    f.write('this is some test data')
    f.flush()
    send_email(..., files=[f.name])

attachment.filename = basename
attachment.disposition = "attachment"
attachment.content_id = '<%s>' % basename
attachment.content = base64.b64encode(f.read()).decode('utf8')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure that the sendgrid api expects a str and not a bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The sendgrid code turns this data into a python dict and then json.dumps it. In python3, bytes can not be JSON-serialized:

Python 3.4.2 (default, Oct  8 2014, 10:45:20)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.dumps(b'welkrjeklwj')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.4/json/__init__.py", line 230, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'welkrjeklwj' is not JSON serializable

attachment = Attachment()
attachment.type = mimetypes.guess_type(basename)[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pulling out everything that doesn't need to be in the context.

@Fokko
Copy link
Contributor

Fokko commented Jan 22, 2018

Please fix the CI

@Fokko
Copy link
Contributor

Fokko commented Feb 27, 2018

@thesquelched Please fix the CI

- Fix sendgrid attachments bug in py3k
- Add sandbox mode option
- Minor style fixes
- Add test
@thesquelched
Copy link
Contributor Author

@Fokko fixed CI failures, rebased, and squashed

Copy link
Contributor

@TrevorEdwards TrevorEdwards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@Fokko Fokko merged commit a5b0a3d into apache:master Oct 20, 2018
@Fokko
Copy link
Contributor

Fokko commented Oct 20, 2018

Thanks @thesquelched

tekn0ir pushed a commit to tekn0ir/incubator-airflow that referenced this pull request Oct 26, 2018
* master:
  [AIRFLOW-520] Fix Version Info in Flask UI (apache#4072)
  [AIRFLOW-XXX] Add Neoway to companies list (apache#4081)
  [AIRFLOW-XXX] Add Surfline to companies list (apache#4079)
  Revert "[AIRFLOW-461] Restore parameter position for BQ run_load method (apache#4077)"
  [AIRFLOW-461] Restore parameter position for BQ run_load method (apache#4077)
  [AIRFLOW-461]  Support autodetected schemas in BigQuery run_load (apache#3880)
  [AIRFLOW-3238] Fix models.DAG to deactivate unknown DAGs on initdb (apache#4073)
  [AIRFLOW-3239] Fix test recovery further (apache#4074)
  [AIRFLOW-3203] Fix DockerOperator & some operator test (apache#4049)
  [AIRFLOW-1867] Add sandbox mode and py3k bug  (apache#2824)
  [AIRFLOW-2993] s3_to_sftp and sftp_to_s3 operators (apache#3828)
  [AIRFLOW-XXX] BigQuery Hook - Minor Refactoring (apache#4066)
  [AIRFLOW-3232] More readable GCF operator documentation (apache#4067)
aliceabe pushed a commit to aliceabe/incubator-airflow that referenced this pull request Jan 3, 2019
- Fix sendgrid attachments bug in py3k
- Add sandbox mode option
- Minor style fixes
- Add test
wmorris75 pushed a commit to modmed/incubator-airflow that referenced this pull request Jul 29, 2019
- Fix sendgrid attachments bug in py3k
- Add sandbox mode option
- Minor style fixes
- Add test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants