Skip to content

Commit 4b91cd4

Browse files
Brian Haleybrianphaley
authored andcommitted
Stop leaving temp files after unit test runs
test_shell.CLOUD_2 is using an absolute path for a temp file, so leaves /tmp/test_log_file around after the unit tests are run. Use a fixture instead so it's cleaned automatically, which also removes the possibility of two tests using the same file and interfering with each other. Change-Id: If722b860be4010b91635c6d46f634da980e17152
1 parent 1bc44fc commit 4b91cd4

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

openstackclient/tests/unit/integ/cli/test_shell.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import copy
1414

15+
import fixtures
1516
import mock
1617
from osc_lib.tests import utils as osc_lib_utils
1718

@@ -399,6 +400,16 @@ def setUp(self):
399400
test_shell.PUBLIC_1['public-clouds']['megadodo']['auth']['auth_url'] \
400401
= test_base.V3_AUTH_URL
401402

403+
def get_temp_file_path(self, filename):
404+
"""Returns an absolute path for a temporary file.
405+
406+
:param filename: filename
407+
:type filename: string
408+
:returns: absolute file path string
409+
"""
410+
temp_dir = self.useFixture(fixtures.TempDir())
411+
return temp_dir.join(filename)
412+
402413
@mock.patch(CONFIG_MOCK_BASE + ".OpenStackConfig._load_vendor_file")
403414
@mock.patch(CONFIG_MOCK_BASE + ".OpenStackConfig._load_config_file")
404415
def test_shell_args_precedence_1(self, config_mock, vendor_mock):
@@ -408,7 +419,9 @@ def test_shell_args_precedence_1(self, config_mock, vendor_mock):
408419
"""
409420

410421
def config_mock_return():
411-
return ('file.yaml', copy.deepcopy(test_shell.CLOUD_2))
422+
log_file = self.get_temp_file_path('test_log_file')
423+
cloud2 = test_shell.get_cloud(log_file)
424+
return ('file.yaml', cloud2)
412425
config_mock.side_effect = config_mock_return
413426

414427
def vendor_mock_return():
@@ -478,7 +491,9 @@ def test_shell_args_precedence_2(self, config_mock, vendor_mock):
478491
"""
479492

480493
def config_mock_return():
481-
return ('file.yaml', copy.deepcopy(test_shell.CLOUD_2))
494+
log_file = self.get_temp_file_path('test_log_file')
495+
cloud2 = test_shell.get_cloud(log_file)
496+
return ('file.yaml', cloud2)
482497
config_mock.side_effect = config_mock_return
483498

484499
def vendor_mock_return():

openstackclient/tests/unit/test_shell.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,23 +70,6 @@
7070
}
7171
}
7272

73-
CLOUD_2 = {
74-
'clouds': {
75-
'megacloud': {
76-
'cloud': 'megadodo',
77-
'auth': {
78-
'project_name': 'heart-o-gold',
79-
'username': 'zaphod',
80-
},
81-
'region_name': 'occ-cloud,krikkit,occ-env',
82-
'log_file': '/tmp/test_log_file',
83-
'log_level': 'debug',
84-
'cert': 'mycert',
85-
'key': 'mickey',
86-
}
87-
}
88-
}
89-
9073
PUBLIC_1 = {
9174
'public-clouds': {
9275
'megadodo': {
@@ -118,6 +101,26 @@
118101
}
119102

120103

104+
def get_cloud(log_file):
105+
CLOUD = {
106+
'clouds': {
107+
'megacloud': {
108+
'cloud': 'megadodo',
109+
'auth': {
110+
'project_name': 'heart-o-gold',
111+
'username': 'zaphod',
112+
},
113+
'region_name': 'occ-cloud,krikkit,occ-env',
114+
'log_file': log_file,
115+
'log_level': 'debug',
116+
'cert': 'mycert',
117+
'key': 'mickey',
118+
}
119+
}
120+
}
121+
return CLOUD
122+
123+
121124
# Wrap the osc_lib make_shell() function to set the shell class since
122125
# osc-lib's TestShell class doesn't allow us to specify it yet.
123126
# TODO(dtroyer): remove this once the shell_class_patch patch is released

0 commit comments

Comments
 (0)