Skip to content

Commit a7362df

Browse files
authored
Fix “[CI] Tests aren't being mocked correctly on CI reruns” (#60)
* Fix “[CI] Tests aren't being mocked correctly on CI reruns” * add fix for aem test
1 parent d1bc006 commit a7362df

File tree

10 files changed

+83
-27
lines changed

10 files changed

+83
-27
lines changed

scripts/ci/test_source.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env python
2+
3+
# --------------------------------------------------------------------------------------------
4+
# Copyright (c) Microsoft Corporation. All rights reserved.
5+
# Licensed under the MIT License. See License.txt in the project root for license information.
6+
# --------------------------------------------------------------------------------------------
7+
8+
from __future__ import print_function
9+
10+
import os
11+
import sys
12+
import tempfile
13+
import unittest
14+
import shutil
15+
from subprocess import check_call
16+
17+
import mock
18+
from six import with_metaclass
19+
20+
from util import get_repo_root
21+
22+
23+
SOURCES = os.path.join(get_repo_root(), 'src')
24+
25+
ALL_TESTS = []
26+
27+
for src_d in os.listdir(SOURCES):
28+
src_d_full = os.path.join(SOURCES, src_d)
29+
if os.path.isdir(src_d_full):
30+
pkg_name = next((d for d in os.listdir(src_d_full) if d.startswith('azext_')), None)
31+
# Find the package and check it has tests
32+
if pkg_name and os.path.isdir(os.path.join(src_d_full, pkg_name, 'tests')):
33+
ALL_TESTS.append((pkg_name, src_d_full))
34+
35+
36+
class TestExtensionSourceMeta(type):
37+
def __new__(mcs, name, bases, _dict):
38+
39+
def gen_test(ext_path):
40+
def test(self):
41+
pip_args = [sys.executable, '-m', 'pip', 'install', '--upgrade', '--target',
42+
os.path.join(self.ext_dir, 'ext'), ext_path]
43+
check_call(pip_args)
44+
unittest_args = [sys.executable, '-m', 'unittest', 'discover', '-v', ext_path]
45+
check_call(unittest_args)
46+
return test
47+
48+
for tname, ext_path in ALL_TESTS:
49+
test_name = "test_%s" % tname
50+
_dict[test_name] = gen_test(ext_path)
51+
return type.__new__(mcs, name, bases, _dict)
52+
53+
54+
class TestExtensionSource(with_metaclass(TestExtensionSourceMeta, unittest.TestCase)):
55+
56+
def setUp(self):
57+
self.ext_dir = tempfile.mkdtemp()
58+
self.mock_env = mock.patch.dict(os.environ, {'AZURE_EXTENSION_DIR': self.ext_dir})
59+
self.mock_env.start()
60+
61+
def tearDown(self):
62+
self.mock_env.stop()
63+
shutil.rmtree(self.ext_dir)
64+
65+
66+
if __name__ == '__main__':
67+
unittest.main()

scripts/ci/test_source.sh

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,6 @@ pip install "git+https://github.com/Azure/azure-cli@dev#egg=azure-cli-testsdk&su
99
pip install knack==0.3.1 -q
1010
echo "Installed."
1111

12-
13-
_AZURE_EXTENSION_DIR="$AZURE_EXTENSION_DIR"
14-
15-
for d in src/*/azext_*/tests;
16-
do echo "Running tests for $d";
17-
if [ -d $d ]; then
18-
export AZURE_EXTENSION_DIR=$(mktemp -d);
19-
pip install --upgrade --target $AZURE_EXTENSION_DIR/ext $d/../..;
20-
python -m unittest discover -v $d/../..;
21-
rm -rf $AZURE_EXTENSION_DIR;
22-
else
23-
echo "Skipped $d as not a directory."
24-
fi
25-
done;
26-
27-
if ! [ -z "${_AZURE_EXTENSION_DIR+_}" ] ; then
28-
AZURE_EXTENSION_DIR="$_AZURE_EXTENSION_DIR"
29-
export AZURE_EXTENSION_DIR
30-
unset _AZURE_EXTENSION_DIR
31-
fi
12+
python ./scripts/ci/test_source.py -v
3213

3314
echo "OK. Completed tests."
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------

src/aem/azext_aem/tests/test_aem_commands.py renamed to src/aem/azext_aem/tests/latest/test_aem_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_vm_aem_configure(self, resource_group):
1515
self.kwargs.update({
1616
'vm': 'vm1',
1717
})
18-
self.cmd('vm create -g {rg} -n {vm} --image centos')
18+
self.cmd('vm create -g {rg} -n {vm} --image centos --generate-ssh-keys')
1919
self.cmd('vm aem set -g {rg} -n {vm}')
2020
self.cmd('vm aem verify -g {rg} -n {vm}')
2121
self.cmd('vm aem delete -g {rg} -n {vm}')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------

src/subscription/azext_subscription/tests/test_subscription_definition_commands.py renamed to src/subscription/azext_subscription/tests/latest/test_subscription_definition_commands.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,26 @@
1010

1111
class AzureSubscriptionDefinitionScenarioTest(ScenarioTest):
1212
def test_list_subscription_definitions(self):
13-
sub_def_list = self.cmd('account subscriptiondefinition list').get_output_in_json()
13+
sub_def_list = self.cmd('account subscription-definition list').get_output_in_json()
1414
self.assertGreater(len(sub_def_list), 0)
1515
self.assertIsNotNone(sub_def_list[0])
1616
self.assertIsNotNone(sub_def_list[0]['name'])
1717
self.assertIsNotNone(sub_def_list[0]['subscriptionId'])
1818
self.assertIsNotNone(sub_def_list[0]['subscriptionDisplayName'])
1919

2020
def test_show_subscription_definitions(self):
21-
first_sub_def = self.cmd('account subscriptiondefinition list').get_output_in_json()[0]
21+
first_sub_def = self.cmd('account subscription-definition list').get_output_in_json()[0]
2222
self.assertIsNotNone(first_sub_def)
2323
self.assertIsNotNone(first_sub_def['name'])
24-
sub_def = self.cmd('account subscriptiondefinition show -n {}'.format(first_sub_def['name'])).get_output_in_json()
24+
sub_def = self.cmd('account subscription-definition show -n {}'.format(first_sub_def['name'])).get_output_in_json()
2525
self.assertIsNotNone(sub_def)
2626
self.assertEqual(sub_def['name'], first_sub_def['name'])
2727

2828
def test_create_subscription_definitions(self):
29-
sub_def_list_count_before = len(self.cmd('account subscriptiondefinition list').get_output_in_json())
29+
sub_def_list_count_before = len(self.cmd('account subscription-definition list').get_output_in_json())
3030
def_name = self.create_random_name(prefix='cli', length=24)
31-
sub_def = self.cmd('account subscriptiondefinition create -n {} --offer-type MS-AZR-0148P'.format(def_name)).get_output_in_json()
32-
sub_def_list_count_after = len(self.cmd('account subscriptiondefinition list').get_output_in_json())
31+
sub_def = self.cmd('account subscription-definition create -n {} --offer-type MS-AZR-0148P'.format(def_name)).get_output_in_json()
32+
sub_def_list_count_after = len(self.cmd('account subscription-definition list').get_output_in_json())
3333
self.assertIsNotNone(sub_def)
3434
self.assertEqual(def_name, sub_def['name'])
3535
self.assertEqual(def_name, sub_def['subscriptionDisplayName'])

0 commit comments

Comments
 (0)