Skip to content

Commit

Permalink
Remove redundant temp folder and unused imports
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
MVrachev committed Mar 4, 2021
1 parent 313511a commit 41f4f74
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 259 deletions.
21 changes: 4 additions & 17 deletions tests/test_arbitrary_package_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
from __future__ import unicode_literals

import os
import tempfile
import shutil
import json
import logging
Expand All @@ -50,7 +49,7 @@
import tuf.keydb
import tuf.log
import tuf.client.updater as updater
import tuf.unittest_toolbox as unittest_toolbox
from tuf import unittest_toolbox

from tests import utils

Expand All @@ -64,11 +63,6 @@ class TestArbitraryPackageAttack(unittest_toolbox.Modified_TestCase):

@classmethod
def setUpClass(cls):
# Create a temporary directory to store the repository, metadata, and target
# files. 'temporary_directory' must be deleted in TearDownModule() so that
# temporary files are always removed, even when exceptions occur.
cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd())

# Launch a SimpleHTTPServer (serves files in the current directory).
# Test cases will request metadata and target files that have been
# pre-generated in 'tuf/tests/repository_data', which will be served by the
Expand All @@ -85,16 +79,11 @@ def tearDownClass(cls):
# Cleans the resources and flush the logged lines (if any).
cls.server_process_handler.clean()

# Remove the temporary repository directory, which should contain all the
# metadata, targets, and key files generated of all the test cases.
shutil.rmtree(cls.temporary_directory)




def setUp(self):
# We are inheriting from custom class.
unittest_toolbox.Modified_TestCase.setUp(self)
super().setUp()

self.repository_name = 'test_repository1'

Expand All @@ -103,7 +92,7 @@ def setUp(self):
# The 'repository_data' directory is expected to exist in 'tuf/tests/'.
original_repository_files = os.path.join(os.getcwd(), 'repository_data')
temporary_repository_root = \
self.make_temp_directory(directory=self.temporary_directory)
self.make_temp_directory(directory=os.getcwd())

# The original repository, keystore, and client directories will be copied
# for each test case.
Expand Down Expand Up @@ -142,9 +131,7 @@ def setUp(self):


def tearDown(self):
# Modified_TestCase.tearDown() automatically deletes temporary files and
# directories that may have been created during each test case.
unittest_toolbox.Modified_TestCase.tearDown(self)
super().tearDown()
# updater.Updater() populates the roledb with the name "test_repository1"
tuf.roledb.clear_roledb(clear_all=True)
tuf.keydb.clear_keydb(clear_all=True)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import tuf.download as download
import tuf.requests_fetcher
import tuf.log
import tuf.unittest_toolbox as unittest_toolbox
import tuf.exceptions
from tuf import unittest_toolbox

from tests import utils

Expand All @@ -64,7 +64,7 @@ def setUp(self):
current working directory.
"""

unittest_toolbox.Modified_TestCase.setUp(self)
super().setUp()

# Making a temporary file.
current_dir = os.getcwd()
Expand Down Expand Up @@ -93,7 +93,7 @@ def setUp(self):

# Stop server process and perform clean up.
def tearDown(self):
unittest_toolbox.Modified_TestCase.tearDown(self)
super().tearDown()

# Cleans the resources and flush the logged lines (if any).
self.server_process_handler.clean()
Expand Down
20 changes: 4 additions & 16 deletions tests/test_endless_data_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
from __future__ import unicode_literals

import os
import tempfile
import shutil
import json
import logging
Expand All @@ -51,8 +50,8 @@
import tuf.formats
import tuf.log
import tuf.client.updater as updater
import tuf.unittest_toolbox as unittest_toolbox
import tuf.roledb
from tuf import unittest_toolbox

from tests import utils

Expand All @@ -66,10 +65,6 @@ class TestEndlessDataAttack(unittest_toolbox.Modified_TestCase):

@classmethod
def setUpClass(cls):
# Create a temporary directory to store the repository, metadata, and target
# files. 'temporary_directory' must be deleted in TearDownModule() so that
# temporary files are always removed, even when exceptions occur.
cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd())

# Launch a SimpleHTTPServer (serves files in the current directory).
# Test cases will request metadata and target files that have been
Expand All @@ -87,16 +82,11 @@ def tearDownClass(cls):
# Cleans the resources and flush the logged lines (if any).
cls.server_process_handler.clean()

# Remove the temporary repository directory, which should contain all the
# metadata, targets, and key files generated of all the test cases.
shutil.rmtree(cls.temporary_directory)




def setUp(self):
# We are inheriting from custom class.
unittest_toolbox.Modified_TestCase.setUp(self)
super().setUp()

self.repository_name = 'test_repository1'

Expand All @@ -105,7 +95,7 @@ def setUp(self):
# The 'repository_data' directory is expected to exist in 'tuf/tests/'.
original_repository_files = os.path.join(os.getcwd(), 'repository_data')
temporary_repository_root = \
self.make_temp_directory(directory=self.temporary_directory)
self.make_temp_directory(directory=os.getcwd())

# The original repository, keystore, and client directories will be copied
# for each test case.
Expand Down Expand Up @@ -143,9 +133,7 @@ def setUp(self):


def tearDown(self):
# Modified_TestCase.tearDown() automatically deletes temporary files and
# directories that may have been created during each test case.
unittest_toolbox.Modified_TestCase.tearDown(self)
super().tearDown()
tuf.roledb.clear_roledb(clear_all=True)
tuf.keydb.clear_keydb(clear_all=True)

Expand Down
20 changes: 4 additions & 16 deletions tests/test_extraneous_dependencies_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
from __future__ import unicode_literals

import os
import tempfile
import shutil
import json
import logging
Expand All @@ -55,7 +54,7 @@
import tuf.client.updater as updater
import tuf.roledb
import tuf.keydb
import tuf.unittest_toolbox as unittest_toolbox
from tuf import unittest_toolbox

from tests import utils

Expand All @@ -70,10 +69,6 @@ class TestExtraneousDependenciesAttack(unittest_toolbox.Modified_TestCase):

@classmethod
def setUpClass(cls):
# Create a temporary directory to store the repository, metadata, and target
# files. 'temporary_directory' must be deleted in TearDownModule() so that
# temporary files are always removed, even when exceptions occur.
cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd())

# Launch a SimpleHTTPServer (serves files in the current directory).
# Test cases will request metadata and target files that have been
Expand All @@ -91,16 +86,11 @@ def tearDownClass(cls):
# Cleans the resources and flush the logged lines (if any).
cls.server_process_handler.clean()

# Remove the temporary repository directory, which should contain all the
# metadata, targets, and key files generated of all the test cases.
shutil.rmtree(cls.temporary_directory)




def setUp(self):
# We are inheriting from custom class.
unittest_toolbox.Modified_TestCase.setUp(self)
super().setUp()

self.repository_name = 'test_repository1'

Expand All @@ -109,7 +99,7 @@ def setUp(self):
# The 'repository_data' directory is expected to exist in 'tuf/tests/'.
original_repository_files = os.path.join(os.getcwd(), 'repository_data')
temporary_repository_root = \
self.make_temp_directory(directory=self.temporary_directory)
self.make_temp_directory(directory=os.getcwd())

# The original repository, keystore, and client directories will be copied
# for each test case.
Expand Down Expand Up @@ -150,9 +140,7 @@ def setUp(self):


def tearDown(self):
# Modified_TestCase.tearDown() automatically deletes temporary files and
# directories that may have been created during each test case.
unittest_toolbox.Modified_TestCase.tearDown(self)
super().tearDown()
tuf.roledb.clear_roledb(clear_all=True)
tuf.keydb.clear_keydb(clear_all=True)

Expand Down
20 changes: 4 additions & 16 deletions tests/test_indefinite_freeze_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import datetime
import os
import time
import tempfile
import shutil
import json
import logging
Expand All @@ -63,10 +62,10 @@
import tuf.log
import tuf.client.updater as updater
import tuf.repository_tool as repo_tool
import tuf.unittest_toolbox as unittest_toolbox
import tuf.roledb
import tuf.keydb
import tuf.exceptions
from tuf import unittest_toolbox

from tests import utils

Expand All @@ -84,10 +83,6 @@ class TestIndefiniteFreezeAttack(unittest_toolbox.Modified_TestCase):

@classmethod
def setUpClass(cls):
# Create a temporary directory to store the repository, metadata, and target
# files. 'temporary_directory' must be deleted in TearDownModule() so that
# temporary files are always removed, even when exceptions occur.
cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd())

# Launch a SimpleHTTPServer (serves files in the current directory).
# Test cases will request metadata and target files that have been
Expand All @@ -105,24 +100,19 @@ def tearDownClass(cls):
# Cleans the resources and flush the logged lines (if any).
cls.server_process_handler.clean()

# Remove the temporary repository directory, which should contain all the
# metadata, targets, and key files generated of all the test cases.
shutil.rmtree(cls.temporary_directory)




def setUp(self):
# We are inheriting from custom class.
unittest_toolbox.Modified_TestCase.setUp(self)
super().setUp()
self.repository_name = 'test_repository1'

# Copy the original repository files provided in the test folder so that
# any modifications made to repository files are restricted to the copies.
# The 'repository_data' directory is expected to exist in 'tuf/tests/'.
original_repository_files = os.path.join(os.getcwd(), 'repository_data')
temporary_repository_root = \
self.make_temp_directory(directory=self.temporary_directory)
self.make_temp_directory(directory=os.getcwd())

# The original repository, keystore, and client directories will be copied
# for each test case.
Expand Down Expand Up @@ -163,9 +153,7 @@ def setUp(self):


def tearDown(self):
# Modified_TestCase.tearDown() automatically deletes temporary files and
# directories that may have been created during each test case.
unittest_toolbox.Modified_TestCase.tearDown(self)
super().tearDown()
tuf.roledb.clear_roledb(clear_all=True)
tuf.keydb.clear_keydb(clear_all=True)

Expand Down
20 changes: 4 additions & 16 deletions tests/test_key_revocation_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@

import os
import shutil
import tempfile
import logging
import unittest
import sys
Expand All @@ -49,8 +48,8 @@
import tuf.roledb
import tuf.keydb
import tuf.repository_tool as repo_tool
import tuf.unittest_toolbox as unittest_toolbox
import tuf.client.updater as updater
from tuf import unittest_toolbox

from tests import utils

Expand All @@ -65,11 +64,6 @@ class TestKeyRevocation(unittest_toolbox.Modified_TestCase):

@classmethod
def setUpClass(cls):
# Create a temporary directory to store the repository, metadata, and target
# files. 'temporary_directory' must be deleted in TearDownModule() so that
# temporary files are always removed, even when exceptions occur.
cls.temporary_directory = tempfile.mkdtemp(dir=os.getcwd())

# Launch a SimpleHTTPServer (serves files in the current directory). Test
# cases will request metadata and target files that have been pre-generated
# in 'tuf/tests/repository_data', which will be served by the
Expand All @@ -86,16 +80,11 @@ def tearDownClass(cls):
# Cleans the resources and flush the logged lines (if any).
cls.server_process_handler.clean()

# Remove the temporary repository directory, which should contain all the
# metadata, targets, and key files generated for the test cases.
shutil.rmtree(cls.temporary_directory)




def setUp(self):
# We are inheriting from custom class.
unittest_toolbox.Modified_TestCase.setUp(self)
super().setUp()

self.repository_name = 'test_repository1'

Expand All @@ -104,7 +93,7 @@ def setUp(self):
# The 'repository_data' directory is expected to exist in 'tuf.tests/'.
original_repository_files = os.path.join(os.getcwd(), 'repository_data')
temporary_repository_root = \
self.make_temp_directory(directory=self.temporary_directory)
self.make_temp_directory(directory=os.getcwd())

# The original repository, keystore, and client directories will be copied
# for each test case.
Expand Down Expand Up @@ -156,8 +145,7 @@ def setUp(self):


def tearDown(self):
# We are inheriting from custom class.
unittest_toolbox.Modified_TestCase.tearDown(self)
super().tearDown()
tuf.roledb.clear_roledb(clear_all=True)
tuf.keydb.clear_keydb(clear_all=True)

Expand Down
5 changes: 2 additions & 3 deletions tests/test_mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import sys

import tuf.mirrors as mirrors
import tuf.unittest_toolbox as unittest_toolbox
from tuf import unittest_toolbox

from tests import utils

Expand All @@ -43,8 +43,7 @@
class TestMirrors(unittest_toolbox.Modified_TestCase):

def setUp(self):

unittest_toolbox.Modified_TestCase.setUp(self)
super().setUp()

self.mirrors = \
{'mirror1': {'url_prefix' : 'http://mirror1.com',
Expand Down
Loading

0 comments on commit 41f4f74

Please sign in to comment.