[Django] #32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure

1 view
Skip to first unread message

Django

unread,
Feb 11, 2021, 3:48:18 PM2/11/21
to django-...@googlegroups.com
#32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure
-------------------------------------+-------------------------------------
Reporter: Chris | Owner: nobody
Jerdonek |
Type: Bug | Status: new
Component: Testing | Version: 2.2
framework | Keywords:
Severity: Normal | LiveServerTestCase,setUpClass
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
While working on #32417, I noticed in
[https://code.djangoproject.com/ticket/32417#comment:6 this comment] that
`LiveServerTestCase.setUpClass()` doesn't clean up all the way if
`setUpClass()` errors out.

Specifically, `LiveServerTestCase.setUpClass()`
[https://github.com/django/django/blob/0963f184abd96800b76b19a6a181e1b544c7fafe/django/test/testcases.py#L1543-L1547
looks like] this:

{{{#!python
@classmethod
def setUpClass(cls):
super().setUpClass()
...
cls._live_server_modified_settings = modify_settings(
ALLOWED_HOSTS={'append': cls.allowed_host},
)
...
if cls.server_thread.error:
# Clean up behind ourselves, since tearDownClass won't get called
in
# case of errors.
cls._tearDownClassInternal()
raise cls.server_thread.error
}}}

But `_tearDownClassInternal()`
[https://github.com/django/django/blob/0963f184abd96800b76b19a6a181e1b544c7fafe/django/test/testcases.py#L1559-L1568
doesn't undo] the call to `modify_settings()`.

I will post a PR shortly.

--
Ticket URL: <https://code.djangoproject.com/ticket/32437>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 11, 2021, 4:27:22 PM2/11/21
to django-...@googlegroups.com
#32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
| Jerdonek
Type: Bug | Status: assigned
Component: Testing framework | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage:
LiveServerTestCase,setUpClass | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Chris Jerdonek):

* owner: nobody => Chris Jerdonek
* status: new => assigned
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/32437#comment:1>

Django

unread,
Feb 11, 2021, 5:20:53 PM2/11/21
to django-...@googlegroups.com
#32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
| Jerdonek
Type: Bug | Status: assigned
Component: Testing framework | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage:
LiveServerTestCase,setUpClass | Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Chris Jerdonek:

Old description:

New description:

While working on #32417, I noticed in
[https://code.djangoproject.com/ticket/32417#comment:6 this comment] that
`LiveServerTestCase.setUpClass()` doesn't clean up all the way if
`setUpClass()` errors out.

{{{#!python
@classmethod
def setUpClass(cls):
super().setUpClass()
...
cls._live_server_modified_settings = modify_settings(
ALLOWED_HOSTS={'append': cls.allowed_host},
)

cls._live_server_modified_settings.enable()


...
if cls.server_thread.error:
# Clean up behind ourselves, since tearDownClass won't get called
in
# case of errors.
cls._tearDownClassInternal()
raise cls.server_thread.error
}}}

But `_tearDownClassInternal()`
[https://github.com/django/django/blob/0963f184abd96800b76b19a6a181e1b544c7fafe/django/test/testcases.py#L1559-L1568
doesn't undo] the call to `cls._live_server_modified_settings.enable()`.

I will post a PR shortly.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/32437#comment:2>

Django

unread,
Feb 12, 2021, 12:41:42 AM2/12/21
to django-...@googlegroups.com
#32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
| Jerdonek
Type: Bug | Status: assigned
Component: Testing framework | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
LiveServerTestCase,setUpClass |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Florian Apolloner, Tobias McNulty (added)
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/32437#comment:3>

Django

unread,
Feb 12, 2021, 3:22:54 AM2/12/21
to django-...@googlegroups.com
#32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
| Jerdonek
Type: Bug | Status: assigned
Component: Testing framework | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
LiveServerTestCase,setUpClass |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Florian Apolloner):

The cleanup precedes the commit that modified the settings by three years
(https://github.com/django/django/commit/17e661641ddaf8266e7430d83cfb2039abc55df7
&
https://github.com/django/django/commit/73a610d2a81bc3bf2d3834786b2458bc85953ed0)
so it is more than likely that this was just an oversight at the time.
I'll give the PR a review in a second.

--
Ticket URL: <https://code.djangoproject.com/ticket/32437#comment:4>

Django

unread,
Feb 12, 2021, 3:59:24 AM2/12/21
to django-...@googlegroups.com
#32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
| Jerdonek
Type: Bug | Status: assigned
Component: Testing framework | Version: 2.2
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
LiveServerTestCase,setUpClass | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32437#comment:5>

Django

unread,
Feb 12, 2021, 5:53:55 AM2/12/21
to django-...@googlegroups.com
#32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
| Jerdonek
Type: Bug | Status: closed

Component: Testing framework | Version: 2.2
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
LiveServerTestCase,setUpClass | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"694deff82f86e025561dfa724425f67e2ff7cbb7" 694deff8]:
{{{
#!CommitTicketReference repository=""
revision="694deff82f86e025561dfa724425f67e2ff7cbb7"
Fixed #32437 -- Fixed cleaning up ALLOWED_HOSTS in LiveServerTestCase on
setUpClass() failure.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32437#comment:6>

Django

unread,
Feb 12, 2021, 5:54:01 AM2/12/21
to django-...@googlegroups.com
#32437: LiveServerTestCase.setUpClass() doesn't clean up fully on failure
-------------------------------------+-------------------------------------
Reporter: Chris Jerdonek | Owner: Chris
| Jerdonek
Type: Bug | Status: closed
Component: Testing framework | Version: 2.2
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
LiveServerTestCase,setUpClass | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"65a620948c38c6ac6c7d93e79f70590331bcb2d0" 65a62094]:
{{{
#!CommitTicketReference repository=""
revision="65a620948c38c6ac6c7d93e79f70590331bcb2d0"
[3.2.x] Fixed #32437 -- Fixed cleaning up ALLOWED_HOSTS in
LiveServerTestCase on setUpClass() failure.

Backport of 694deff82f86e025561dfa724425f67e2ff7cbb7 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32437#comment:7>

Reply all
Reply to author
Forward
0 new messages