Skip to content

Commit f1d32db

Browse files
kiwikDean Troyer
authored andcommitted
Clean up the changes of os.environ in functional tests
Use fixtures to restore the API version changes of os.environ in each functional tests, aims to avoid the following test cases failing in unexpected context. And make sure setUpClass/tearDownClass call super class's corresponding methods first. Change-Id: Ie248fe9d3a9e25f1b076c9f2c363200f29a83817 Closes-Bug: #1696080
1 parent ac8cac4 commit f1d32db

37 files changed

Lines changed: 336 additions & 228 deletions

openstackclient/tests/functional/common/test_extension.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ExtensionTests(base.TestCase):
2525

2626
@classmethod
2727
def setUpClass(cls):
28+
super(ExtensionTests, cls).setUpClass()
2829
cls.haz_network = base.is_service_enabled('network')
2930

3031
def test_extension_list_compute(self):

openstackclient/tests/functional/common/test_help.py

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

1313
import os
1414

15+
import fixtures
16+
1517
from openstackclient.tests.functional import base
1618

1719

@@ -76,10 +78,11 @@ def test_networking_commands_help(self):
7678

7779
def test_commands_help_no_auth(self):
7880
"""Check help commands without auth info."""
79-
# Pop all auth info
80-
auth_info = {key: os.environ.pop(key)
81-
for key in os.environ.keys()
82-
if key.startswith('OS_')}
81+
# Pop all auth info. os.environ will be changed in loop, so do not
82+
# replace os.environ.keys() to os.environ
83+
for key in os.environ.keys():
84+
if key.startswith('OS_'):
85+
self.useFixture(fixtures.EnvironmentVariable(key, None))
8386

8487
raw_output = self.openstack('help')
8588
self.assertIn('usage: openstack', raw_output)
@@ -115,6 +118,3 @@ def test_commands_help_no_auth(self):
115118
self.assertIn('List containers', raw_output)
116119
raw_output = self.openstack('container list --help')
117120
self.assertIn('List containers', raw_output)
118-
119-
# Restore auth info
120-
os.environ.update(auth_info)

openstackclient/tests/functional/common/test_quota.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class QuotaTests(base.TestCase):
2626

2727
@classmethod
2828
def setUpClass(cls):
29+
super(QuotaTests, cls).setUpClass()
2930
cls.haz_network = base.is_service_enabled('network')
3031
cls.PROJECT_NAME =\
3132
cls.get_openstack_configuration_value('auth.project_name')

openstackclient/tests/functional/compute/v2/test_flavor.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class FlavorTests(base.TestCase):
2323

2424
@classmethod
2525
def setUpClass(cls):
26+
super(FlavorTests, cls).setUpClass()
2627
# Make a project
2728
cmd_output = json.loads(cls.openstack(
2829
"project create -f json --enable " + cls.PROJECT_NAME
@@ -31,8 +32,11 @@ def setUpClass(cls):
3132

3233
@classmethod
3334
def tearDownClass(cls):
34-
raw_output = cls.openstack("project delete " + cls.PROJECT_NAME)
35-
cls.assertOutput('', raw_output)
35+
try:
36+
raw_output = cls.openstack("project delete " + cls.PROJECT_NAME)
37+
cls.assertOutput('', raw_output)
38+
finally:
39+
super(FlavorTests, cls).tearDownClass()
3640

3741
def test_flavor_delete(self):
3842
"""Test create w/project, delete multiple"""

openstackclient/tests/functional/compute/v2/test_server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ServerTests(common.ComputeTestCase):
2525

2626
@classmethod
2727
def setUpClass(cls):
28+
super(ServerTests, cls).setUpClass()
2829
cls.haz_network = base.is_service_enabled('network')
2930

3031
def test_server_list(self):

openstackclient/tests/functional/identity/v2/common.py

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

1313
import os
1414

15+
import fixtures
1516
from tempest.lib.common.utils import data_utils
1617
from tempest.lib import exceptions as tempest_exceptions
1718

@@ -41,17 +42,13 @@ class IdentityTests(base.TestCase):
4142

4243
@classmethod
4344
def setUpClass(cls):
44-
# prepare v2 env
45-
os.environ['OS_IDENTITY_API_VERSION'] = '2.0'
46-
auth_url = os.environ.get('OS_AUTH_URL')
47-
if auth_url:
48-
os.environ['OS_AUTH_URL'] = auth_url.replace('v3', 'v2.0')
49-
45+
super(IdentityTests, cls).setUpClass()
5046
# create dummy project
5147
cls.project_name = data_utils.rand_name('TestProject')
5248
cls.project_description = data_utils.rand_name('description')
5349
try:
5450
cls.openstack(
51+
'--os-identity-api-version 2 '
5552
'project create '
5653
'--description %(description)s '
5754
'--enable '
@@ -69,7 +66,25 @@ def setUpClass(cls):
6966

7067
@classmethod
7168
def tearDownClass(cls):
72-
cls.openstack('project delete %s' % cls.project_name)
69+
try:
70+
cls.openstack(
71+
'--os-identity-api-version 2 '
72+
'project delete %s' % cls.project_name)
73+
finally:
74+
super(IdentityTests, cls).tearDownClass()
75+
76+
def setUp(self):
77+
super(IdentityTests, self).setUp()
78+
# prepare v2 env
79+
ver_fixture = fixtures.EnvironmentVariable(
80+
'OS_IDENTITY_API_VERSION', '2.0')
81+
self.useFixture(ver_fixture)
82+
auth_url = os.environ.get('OS_AUTH_URL')
83+
if auth_url:
84+
auth_url_fixture = fixtures.EnvironmentVariable(
85+
'OS_AUTH_URL', auth_url.replace('v3', 'v2.0')
86+
)
87+
self.useFixture(auth_url_fixture)
7388

7489
def _create_dummy_project(self, add_clean_up=True):
7590
project_name = data_utils.rand_name('TestProject')

openstackclient/tests/functional/identity/v3/common.py

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

1313
import os
1414

15+
import fixtures
1516
from tempest.lib.common.utils import data_utils
1617

1718
from openstackclient.tests.functional import base
@@ -53,16 +54,12 @@ class IdentityTests(base.TestCase):
5354

5455
@classmethod
5556
def setUpClass(cls):
56-
# prepare v3 env
57-
os.environ['OS_IDENTITY_API_VERSION'] = '3'
58-
auth_url = os.environ.get('OS_AUTH_URL')
59-
if auth_url:
60-
os.environ['OS_AUTH_URL'] = auth_url.replace('v2.0', 'v3')
61-
57+
super(IdentityTests, cls).setUpClass()
6258
# create dummy domain
6359
cls.domain_name = data_utils.rand_name('TestDomain')
6460
cls.domain_description = data_utils.rand_name('description')
6561
cls.openstack(
62+
'--os-identity-api-version 3 '
6663
'domain create '
6764
'--description %(description)s '
6865
'--enable '
@@ -73,6 +70,7 @@ def setUpClass(cls):
7370
cls.project_name = data_utils.rand_name('TestProject')
7471
cls.project_description = data_utils.rand_name('description')
7572
cls.openstack(
73+
'--os-identity-api-version 3 '
7674
'project create '
7775
'--domain %(domain)s '
7876
'--description %(description)s '
@@ -83,11 +81,30 @@ def setUpClass(cls):
8381

8482
@classmethod
8583
def tearDownClass(cls):
86-
# delete dummy project
87-
cls.openstack('project delete %s' % cls.project_name)
88-
# disable and delete dummy domain
89-
cls.openstack('domain set --disable %s' % cls.domain_name)
90-
cls.openstack('domain delete %s' % cls.domain_name)
84+
try:
85+
# delete dummy project
86+
cls.openstack('--os-identity-api-version 3 '
87+
'project delete %s' % cls.project_name)
88+
# disable and delete dummy domain
89+
cls.openstack('--os-identity-api-version 3 '
90+
'domain set --disable %s' % cls.domain_name)
91+
cls.openstack('--os-identity-api-version 3 '
92+
'domain delete %s' % cls.domain_name)
93+
finally:
94+
super(IdentityTests, cls).tearDownClass()
95+
96+
def setUp(self):
97+
super(IdentityTests, self).setUp()
98+
# prepare v3 env
99+
ver_fixture = fixtures.EnvironmentVariable(
100+
'OS_IDENTITY_API_VERSION', '3')
101+
self.useFixture(ver_fixture)
102+
auth_url = os.environ.get('OS_AUTH_URL')
103+
if auth_url:
104+
auth_url_fixture = fixtures.EnvironmentVariable(
105+
'OS_AUTH_URL', auth_url.replace('v2.0', 'v3')
106+
)
107+
self.useFixture(auth_url_fixture)
91108

92109
def _create_dummy_user(self, add_clean_up=True):
93110
username = data_utils.rand_name('TestUser')

openstackclient/tests/functional/image/v1/test_image.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
# under the License.
1212

1313
import json
14-
import os
1514
import uuid
1615

16+
import fixtures
17+
1718
from openstackclient.tests.functional import base
1819

1920

@@ -25,8 +26,9 @@ class ImageTests(base.TestCase):
2526

2627
@classmethod
2728
def setUpClass(cls):
28-
os.environ['OS_IMAGE_API_VERSION'] = '1'
29+
super(ImageTests, cls).setUpClass()
2930
json_output = json.loads(cls.openstack(
31+
'--os-image-api-version 1 '
3032
'image create -f json ' +
3133
cls.NAME
3234
))
@@ -35,10 +37,21 @@ def setUpClass(cls):
3537

3638
@classmethod
3739
def tearDownClass(cls):
38-
cls.openstack(
39-
'image delete ' +
40-
cls.image_id
40+
try:
41+
cls.openstack(
42+
'--os-image-api-version 1 '
43+
'image delete ' +
44+
cls.image_id
45+
)
46+
finally:
47+
super(ImageTests, cls).tearDownClass()
48+
49+
def setUp(self):
50+
super(ImageTests, self).setUp()
51+
ver_fixture = fixtures.EnvironmentVariable(
52+
'OS_IMAGE_API_VERSION', '1'
4153
)
54+
self.useFixture(ver_fixture)
4255

4356
def test_image_list(self):
4457
json_output = json.loads(self.openstack(

openstackclient/tests/functional/image/v2/test_image.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
# under the License.
1212

1313
import json
14-
import os
1514
import uuid
1615

16+
import fixtures
1717
# from glanceclient import exc as image_exceptions
1818

1919
from openstackclient.tests.functional import base
@@ -27,8 +27,9 @@ class ImageTests(base.TestCase):
2727

2828
@classmethod
2929
def setUpClass(cls):
30-
os.environ['OS_IMAGE_API_VERSION'] = '2'
30+
super(ImageTests, cls).setUpClass()
3131
json_output = json.loads(cls.openstack(
32+
'--os-image-api-version 2 '
3233
'image create -f json ' +
3334
cls.NAME
3435
))
@@ -37,10 +38,21 @@ def setUpClass(cls):
3738

3839
@classmethod
3940
def tearDownClass(cls):
40-
cls.openstack(
41-
'image delete ' +
42-
cls.image_id
41+
try:
42+
cls.openstack(
43+
'--os-image-api-version 2 '
44+
'image delete ' +
45+
cls.image_id
46+
)
47+
finally:
48+
super(ImageTests, cls).tearDownClass()
49+
50+
def setUp(self):
51+
super(ImageTests, self).setUp()
52+
ver_fixture = fixtures.EnvironmentVariable(
53+
'OS_IMAGE_API_VERSION', '2'
4354
)
55+
self.useFixture(ver_fixture)
4456

4557
def test_image_list(self):
4658
json_output = json.loads(self.openstack(

openstackclient/tests/functional/network/v2/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ class NetworkTests(base.TestCase):
1818

1919
@classmethod
2020
def setUpClass(cls):
21-
# super(NetworkTests, cls).setUp()
21+
super(NetworkTests, cls).setUpClass()
2222
cls.haz_network = base.is_service_enabled('network')

0 commit comments

Comments
 (0)