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

Remove redundant temp folder and unused imports #1224

Closed
wants to merge 1 commit into from

Conversation

MVrachev
Copy link
Collaborator

Description of the changes being introduced by the pull request:

In many of the tests classes, we are creating two
temporary directories: one with the name "temp_<random_string>"
and inside it, we generate a new directory for each test with the name
Test<Class_Name>_<random_string>.

I think we don't need the "temp_<random_string>" directory.
The only benefit I can think of is that it could contain multiple
temp folders from failing tests from a particular test run.
But even then, this is not a big bonus because the name
temp_<random_string> is not really descriptive from which test
run this directory was created.

PS: Thanks to Jussi Kukkonen who noticed we are using two temp
folders per class in our tests.

Signed-off-by: Martin Vrachev [email protected]

Please verify and check that the pull request fulfills the following
requirements
:

  • The code follows the Code Style Guidelines
  • Tests have been added for the bug fix or new feature
  • Docs have been added for the bug fix or new feature

Copy link
Member

@joshuagl joshuagl left a comment

Choose a reason for hiding this comment

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

Thank you for taking the time to clean this up. I have a few minor comments, mostly on not adding semi-redundant comments to the code.

tests/test_repository_lib.py Outdated Show resolved Hide resolved
tests/test_arbitrary_package_attack.py Outdated Show resolved Hide resolved
tests/test_repository_lib.py Outdated Show resolved Hide resolved
tests/test_repository_tool.py Outdated Show resolved Hide resolved
tests/test_repository_lib.py Outdated Show resolved Hide resolved
tests/test_root_versioning_integration.py Outdated Show resolved Hide resolved
tests/test_repository_lib.py Outdated Show resolved Hide resolved
@MVrachev
Copy link
Collaborator Author

Addressed all of @joshuagl comments and:

  1. Replaced import tuf.unittest_toolbox as unittest_toolbox with from tuf import unittest_toolbox in all of our test files.
  2. Removed the comments I have added such as We are inheriting from custom class. from all of the tests.
  3. Replaced all unittest_toolbox.Modified_TestCase.function(self) calls with super().function() in all of our tests.

@MVrachev
Copy link
Collaborator Author

MVrachev commented Nov 26, 2020

It seems like because we are supporting Python 2 we have a problem with
super().function() which I used because in Python2 it is super(self).function()
Probably I should replace it with unittest_toolbox.Modified_TestCase.function(self) the way it was?

@joshuagl
Copy link
Member

joshuagl commented Dec 3, 2020

Ah, yes. The syntax changed for super() between Python 2 and Python 3. Let's leave it as is for now and we can fix this later when we've purged Python 2 from our codebase.

@joshuagl
Copy link
Member

joshuagl commented Jan 8, 2021

Would be good to get this cleanup landed. @MVrachev could you revert back to unittest_toolbox.Modified_TestCase.function(self) when you have some cycles? Thank you.

@jku
Copy link
Member

jku commented Jan 15, 2021

I'm a bit worried about

  • using os.getcwd() for the location where temp dirs are created
  • the fact that sometimes temp dirs are not cleaned up (is this only when test run is cancelled? not sure)
  • after this PR probably creating more temp dirs in current dir (one per instance instead of one per class I guess?)

So I wonder if this should only be done if we also start creating the temp dirs somewhere more reasonable?

Does anyone know when the temp dirs are not being removed?

@sechkova
Copy link
Contributor

sechkova commented Jan 18, 2021

Does anyone know when the temp dirs are not being removed?

Besides the expected case when tearDown is not called due to some error, I've noticed temp dirs not being removed on Windows with "being used by another process" error. Unfortunately, I can't say which is the process and I haven't observed this consistently in other environments.

@sechkova
Copy link
Contributor

So I wonder if this should only be done if we also start creating the temp dirs somewhere more reasonable?

I would vote for that if possible. Polluting the tests source code dir on a failure is not particularly harmful but ... unpleasant

@MVrachev
Copy link
Collaborator Author

I'm a bit worried about

  • using os.getcwd() for the location where temp dirs are created

  • the fact that sometimes temp dirs are not cleaned up (is this only when test run is cancelled? not sure)

  • after this PR probably creating more temp dirs in current dir (one per instance instead of one per class I guess?)

Yes, the side effect is that it will create one temp dir per class instance.

So I wonder if this should only be done if we also start creating the temp dirs somewhere more reasonable?

As I understand it, the idea is to have those temp files available for you on test failure for analysis.
Probably it will be better to choose a deterministic path where they can be found, but where should that be?
Maybe ~/tmp?

Does anyone know when the temp dirs are not being removed?

The doc says:
tearDown:

Method called immediately after the test method has been called and the result recorded. 
This is called even if the test method raised an exception, so the implementation 
in subclasses may need to be particularly careful about checking the internal state.
...
This method will only be called if the setUp() succeeds, regardless of the outcome 
of the test method. 

Again from the doc for tearDownClass:

If an exception is raised during a setUpClass then the tests in the class are not run and 
the tearDownClass is not run. 

Also again in the doc

Skipped tests will not have setUp() or tearDown() run around them. 
Skipped classes will not have setUpClass() or tearDownClass() run.

@joshuagl
Copy link
Member

As I understand it, the idea is to have those temp files available for you on test failure for analysis.
Probably it will be better to choose a deterministic path where they can be found, but where should that be?
Maybe ~/tmp?

Python has a function to get an OS appropriate, administrator configurable, temporary directory tempfile.gettempdir(). As well as providing functions to create directories within that host appropriate location: tempfile.mkdtemp(), and tempfile.TemporaryFile().

@sechkova
Copy link
Contributor

Does anyone know when the temp dirs are not being removed?

Besides the expected case when tearDown is not called due to some error, I've noticed temp dirs not being removed on Windows with "being used by another process" error. Unfortunately, I can't say which is the process and I haven't observed this consistently in other environments.

Sorry for giving you a bit random hints and not direct answers but in the Windows case temporary files created by unittest_toolbox.Modified_TestCase.make_temp_data_file cannot be cleaned due to
PermissionError(13, 'This process cannot access the file because it is being used by another process').

But this is a Windows VM so it can be something specific to this case ...

@MVrachev
Copy link
Collaborator Author

MVrachev commented Mar 1, 2021

Blocked, until we remove Python2 from our tests as Joshua has mentioned.
#1224 (comment)

In many of the tests classes, we are creating two
temporary directories: one with the name "temp_<random_string>"
and inside it, we generate a new directory for each test with the name
"Test<Class_Name>_<random_string>".

I think we don't need the "temp_<random_string>" directory.
The only benefit I can think of is that it could contain multiple
temp folders from failing tests from a particular test run.
But even then, this is not a big bonus because the name
"temp_<random_string>" is not really descriptive from which test
run this directory was created.

Also, fixed the way we import "unittest_toolbox" and replace
"unittest_toolbox.Modified_TestCase.function(self)"
with "super().function()".

PS: Thanks to Jussi Kukkonen who noticed we are using two temp
folders per class in our tests.

Signed-off-by: Martin Vrachev <[email protected]>
@MVrachev
Copy link
Collaborator Author

MVrachev commented Mar 4, 2021

After the removal of python2 test cases in 13b0857 I rebased and updated this pr.

@MVrachev
Copy link
Collaborator Author

MVrachev commented Mar 4, 2021

Honestly, I am wondering about this change.
On one hand, on a successful test run, we had generated multiple additional folders for each test class,
on another hand, on an error probably we will end up with many folders for each test class that failed, right?

@joshuagl joshuagl marked this pull request as draft April 13, 2021 13:46
@jku
Copy link
Member

jku commented Apr 13, 2021

I might have broken this PR quite badly with a recent bug fix (moved chained up teardown calls to the correct place in #1346 to fix issues found in another PR). Sorry about that -- I did not remember this one.

I think that PR also makes this one a bit less needed: at least in the tests that I touched I made sure that we only try to remove the "topmost" directory (because that will remove every other directory inside it). In some cases there might still be unneeded temp directories.

Let's discuss when you are back.

@MVrachev
Copy link
Collaborator Author

As I said before, I don't feel so confident about this change anymore.
I will just close it for now and if somebody complains about it in the future, I will reopen or push another one.

@MVrachev MVrachev closed this Apr 19, 2021
@MVrachev MVrachev deleted the rm-tmp-folder branch April 29, 2021 13:02
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.

4 participants