From 6ee639074b59233b86b9b616c2c99c414ce56c9a Mon Sep 17 00:00:00 2001 From: Derek Bekoe Date: Sun, 11 Feb 2018 20:42:27 -0800 Subject: [PATCH 1/2] =?UTF-8?q?Fix=20=E2=80=9C[CI]=20Tests=20aren't=20bein?= =?UTF-8?q?g=20mocked=20correctly=20on=20CI=20reruns=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/ci/test_source.py | 67 +++++++++++++++++++ scripts/ci/test_source.sh | 21 +----- src/aem/azext_aem/tests/latest/__init__.py | 4 ++ .../recordings}/test_vm_aem_configure.yaml | 0 .../tests/{ => latest}/test_aem_commands.py | 0 .../tests/latest/__init__.py | 4 ++ .../test_create_subscription_definitions.yaml | 0 .../test_list_subscription_definitions.yaml | 0 .../test_show_subscription_definitions.yaml | 0 .../test_subscription_definition_commands.py | 12 ++-- 10 files changed, 82 insertions(+), 26 deletions(-) create mode 100755 scripts/ci/test_source.py create mode 100644 src/aem/azext_aem/tests/latest/__init__.py rename src/aem/azext_aem/tests/{recordings/latest => latest/recordings}/test_vm_aem_configure.yaml (100%) rename src/aem/azext_aem/tests/{ => latest}/test_aem_commands.py (100%) create mode 100644 src/subscription/azext_subscription/tests/latest/__init__.py rename src/subscription/azext_subscription/tests/{ => latest}/recordings/test_create_subscription_definitions.yaml (100%) rename src/subscription/azext_subscription/tests/{ => latest}/recordings/test_list_subscription_definitions.yaml (100%) rename src/subscription/azext_subscription/tests/{ => latest}/recordings/test_show_subscription_definitions.yaml (100%) rename src/subscription/azext_subscription/tests/{ => latest}/test_subscription_definition_commands.py (74%) diff --git a/scripts/ci/test_source.py b/scripts/ci/test_source.py new file mode 100755 index 00000000000..fdda3e6f024 --- /dev/null +++ b/scripts/ci/test_source.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- + +from __future__ import print_function + +import os +import sys +import tempfile +import unittest +import shutil +from subprocess import check_call + +import mock +from six import with_metaclass + +from util import get_repo_root + + +SOURCES = os.path.join(get_repo_root(), 'src') + +ALL_TESTS = [] + +for src_d in os.listdir(SOURCES): + src_d_full = os.path.join(SOURCES, src_d) + if os.path.isdir(src_d_full): + pkg_name = next((d for d in os.listdir(src_d_full) if d.startswith('azext_')), None) + # Find the package and check it has tests + if pkg_name and os.path.isdir(os.path.join(src_d_full, pkg_name, 'tests')): + ALL_TESTS.append((pkg_name, src_d_full)) + + +class TestExtensionSourceMeta(type): + def __new__(mcs, name, bases, _dict): + + def gen_test(ext_path): + def test(self): + pip_args = [sys.executable, '-m', 'pip', 'install', '--upgrade', '--target', + os.path.join(self.ext_dir, 'ext'), ext_path] + check_call(pip_args) + unittest_args = [sys.executable, '-m', 'unittest', 'discover', '-v', ext_path] + check_call(unittest_args) + return test + + for tname, ext_path in ALL_TESTS: + test_name = "test_%s" % tname + _dict[test_name] = gen_test(ext_path) + return type.__new__(mcs, name, bases, _dict) + + +class TestExtensionSource(with_metaclass(TestExtensionSourceMeta, unittest.TestCase)): + + def setUp(self): + self.ext_dir = tempfile.mkdtemp() + self.mock_env = mock.patch.dict(os.environ, {'AZURE_EXTENSION_DIR': self.ext_dir}) + self.mock_env.start() + + def tearDown(self): + self.mock_env.stop() + shutil.rmtree(self.ext_dir) + + +if __name__ == '__main__': + unittest.main() diff --git a/scripts/ci/test_source.sh b/scripts/ci/test_source.sh index 4542897b94d..9984c168096 100755 --- a/scripts/ci/test_source.sh +++ b/scripts/ci/test_source.sh @@ -9,25 +9,6 @@ pip install "git+https://github.com/Azure/azure-cli@dev#egg=azure-cli-testsdk&su pip install knack==0.3.1 -q echo "Installed." - -_AZURE_EXTENSION_DIR="$AZURE_EXTENSION_DIR" - -for d in src/*/azext_*/tests; - do echo "Running tests for $d"; - if [ -d $d ]; then - export AZURE_EXTENSION_DIR=$(mktemp -d); - pip install --upgrade --target $AZURE_EXTENSION_DIR/ext $d/../..; - python -m unittest discover -v $d/../..; - rm -rf $AZURE_EXTENSION_DIR; - else - echo "Skipped $d as not a directory." - fi -done; - -if ! [ -z "${_AZURE_EXTENSION_DIR+_}" ] ; then - AZURE_EXTENSION_DIR="$_AZURE_EXTENSION_DIR" - export AZURE_EXTENSION_DIR - unset _AZURE_EXTENSION_DIR -fi +python ./scripts/ci/test_source.py -v echo "OK. Completed tests." diff --git a/src/aem/azext_aem/tests/latest/__init__.py b/src/aem/azext_aem/tests/latest/__init__.py new file mode 100644 index 00000000000..34913fb394d --- /dev/null +++ b/src/aem/azext_aem/tests/latest/__init__.py @@ -0,0 +1,4 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- diff --git a/src/aem/azext_aem/tests/recordings/latest/test_vm_aem_configure.yaml b/src/aem/azext_aem/tests/latest/recordings/test_vm_aem_configure.yaml similarity index 100% rename from src/aem/azext_aem/tests/recordings/latest/test_vm_aem_configure.yaml rename to src/aem/azext_aem/tests/latest/recordings/test_vm_aem_configure.yaml diff --git a/src/aem/azext_aem/tests/test_aem_commands.py b/src/aem/azext_aem/tests/latest/test_aem_commands.py similarity index 100% rename from src/aem/azext_aem/tests/test_aem_commands.py rename to src/aem/azext_aem/tests/latest/test_aem_commands.py diff --git a/src/subscription/azext_subscription/tests/latest/__init__.py b/src/subscription/azext_subscription/tests/latest/__init__.py new file mode 100644 index 00000000000..34913fb394d --- /dev/null +++ b/src/subscription/azext_subscription/tests/latest/__init__.py @@ -0,0 +1,4 @@ +# -------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for license information. +# -------------------------------------------------------------------------------------------- diff --git a/src/subscription/azext_subscription/tests/recordings/test_create_subscription_definitions.yaml b/src/subscription/azext_subscription/tests/latest/recordings/test_create_subscription_definitions.yaml similarity index 100% rename from src/subscription/azext_subscription/tests/recordings/test_create_subscription_definitions.yaml rename to src/subscription/azext_subscription/tests/latest/recordings/test_create_subscription_definitions.yaml diff --git a/src/subscription/azext_subscription/tests/recordings/test_list_subscription_definitions.yaml b/src/subscription/azext_subscription/tests/latest/recordings/test_list_subscription_definitions.yaml similarity index 100% rename from src/subscription/azext_subscription/tests/recordings/test_list_subscription_definitions.yaml rename to src/subscription/azext_subscription/tests/latest/recordings/test_list_subscription_definitions.yaml diff --git a/src/subscription/azext_subscription/tests/recordings/test_show_subscription_definitions.yaml b/src/subscription/azext_subscription/tests/latest/recordings/test_show_subscription_definitions.yaml similarity index 100% rename from src/subscription/azext_subscription/tests/recordings/test_show_subscription_definitions.yaml rename to src/subscription/azext_subscription/tests/latest/recordings/test_show_subscription_definitions.yaml diff --git a/src/subscription/azext_subscription/tests/test_subscription_definition_commands.py b/src/subscription/azext_subscription/tests/latest/test_subscription_definition_commands.py similarity index 74% rename from src/subscription/azext_subscription/tests/test_subscription_definition_commands.py rename to src/subscription/azext_subscription/tests/latest/test_subscription_definition_commands.py index 7f27617d44b..0c50e59e164 100644 --- a/src/subscription/azext_subscription/tests/test_subscription_definition_commands.py +++ b/src/subscription/azext_subscription/tests/latest/test_subscription_definition_commands.py @@ -10,7 +10,7 @@ class AzureSubscriptionDefinitionScenarioTest(ScenarioTest): def test_list_subscription_definitions(self): - sub_def_list = self.cmd('account subscriptiondefinition list').get_output_in_json() + sub_def_list = self.cmd('account subscription-definition list').get_output_in_json() self.assertGreater(len(sub_def_list), 0) self.assertIsNotNone(sub_def_list[0]) self.assertIsNotNone(sub_def_list[0]['name']) @@ -18,18 +18,18 @@ def test_list_subscription_definitions(self): self.assertIsNotNone(sub_def_list[0]['subscriptionDisplayName']) def test_show_subscription_definitions(self): - first_sub_def = self.cmd('account subscriptiondefinition list').get_output_in_json()[0] + first_sub_def = self.cmd('account subscription-definition list').get_output_in_json()[0] self.assertIsNotNone(first_sub_def) self.assertIsNotNone(first_sub_def['name']) - sub_def = self.cmd('account subscriptiondefinition show -n {}'.format(first_sub_def['name'])).get_output_in_json() + sub_def = self.cmd('account subscription-definition show -n {}'.format(first_sub_def['name'])).get_output_in_json() self.assertIsNotNone(sub_def) self.assertEqual(sub_def['name'], first_sub_def['name']) def test_create_subscription_definitions(self): - sub_def_list_count_before = len(self.cmd('account subscriptiondefinition list').get_output_in_json()) + sub_def_list_count_before = len(self.cmd('account subscription-definition list').get_output_in_json()) def_name = self.create_random_name(prefix='cli', length=24) - sub_def = self.cmd('account subscriptiondefinition create -n {} --offer-type MS-AZR-0148P'.format(def_name)).get_output_in_json() - sub_def_list_count_after = len(self.cmd('account subscriptiondefinition list').get_output_in_json()) + sub_def = self.cmd('account subscription-definition create -n {} --offer-type MS-AZR-0148P'.format(def_name)).get_output_in_json() + sub_def_list_count_after = len(self.cmd('account subscription-definition list').get_output_in_json()) self.assertIsNotNone(sub_def) self.assertEqual(def_name, sub_def['name']) self.assertEqual(def_name, sub_def['subscriptionDisplayName']) From 6ee229418dc1db3d05ea2c0a63b6163f50c64db5 Mon Sep 17 00:00:00 2001 From: Derek Bekoe Date: Sun, 11 Feb 2018 22:30:14 -0800 Subject: [PATCH 2/2] add fix for aem test --- src/aem/azext_aem/tests/latest/test_aem_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aem/azext_aem/tests/latest/test_aem_commands.py b/src/aem/azext_aem/tests/latest/test_aem_commands.py index f2e7fa1f597..aa3a11ddf06 100644 --- a/src/aem/azext_aem/tests/latest/test_aem_commands.py +++ b/src/aem/azext_aem/tests/latest/test_aem_commands.py @@ -15,7 +15,7 @@ def test_vm_aem_configure(self, resource_group): self.kwargs.update({ 'vm': 'vm1', }) - self.cmd('vm create -g {rg} -n {vm} --image centos') + self.cmd('vm create -g {rg} -n {vm} --image centos --generate-ssh-keys') self.cmd('vm aem set -g {rg} -n {vm}') self.cmd('vm aem verify -g {rg} -n {vm}') self.cmd('vm aem delete -g {rg} -n {vm}')