From 91b18eadfb2ca83d9291281210342dfefa7694b2 Mon Sep 17 00:00:00 2001
From: zhongjun <zhong.jun@zte.com.cn>
Date: Wed, 30 Aug 2017 14:30:26 +0800
Subject: Add some test case in test_post_execute.py

1.add some test case in test_post_execute.py.
2.rename test_execute.py to test_prepare_execute.py and
add clearup handle in test function.

Change-Id: I1ed27ce6f0a5ba8bbd56544ae8d53e14f7daa9ea
Signed-off-by: zhongjun <zhong.jun@zte.com.cn>
---
 tests/data/openrc_conf/admin-openrc.sh     |   9 ++
 tests/data/openrc_conf/globals.yml         |  12 +++
 tests/data/openrc_conf/globals_odl.yml     |  13 +++
 tests/post/test_keystoneauth.py            |  20 -----
 tests/post/test_post_execute.py            |  97 ---------------------
 tests/prepare/test_execute.py              |  55 ------------
 tests/unit/post/test_keystoneauth.py       |  82 ++++++++++++++++++
 tests/unit/post/test_post_execute.py       | 131 +++++++++++++++++++++++++++++
 tests/unit/prepare/test_prepare_execure.py |  67 +++++++++++++++
 9 files changed, 314 insertions(+), 172 deletions(-)
 create mode 100644 tests/data/openrc_conf/admin-openrc.sh
 create mode 100644 tests/data/openrc_conf/globals.yml
 create mode 100644 tests/data/openrc_conf/globals_odl.yml
 delete mode 100644 tests/post/test_keystoneauth.py
 delete mode 100644 tests/post/test_post_execute.py
 delete mode 100644 tests/prepare/test_execute.py
 create mode 100644 tests/unit/post/test_keystoneauth.py
 create mode 100644 tests/unit/post/test_post_execute.py
 create mode 100644 tests/unit/prepare/test_prepare_execure.py

(limited to 'tests')

diff --git a/tests/data/openrc_conf/admin-openrc.sh b/tests/data/openrc_conf/admin-openrc.sh
new file mode 100644
index 00000000..3db0bcf3
--- /dev/null
+++ b/tests/data/openrc_conf/admin-openrc.sh
@@ -0,0 +1,9 @@
+export OS_PROJECT_DOMAIN_NAME=Default
+export OS_USER_DOMAIN_NAME=Default
+export OS_PROJECT_NAME=admin
+export OS_TENANT_NAME=admin
+export OS_USERNAME=admin
+export OS_PASSWORD=keystone
+export OS_AUTH_URL=http://10.20.11.11:35357/v3
+export OS_INTERFACE=internal
+export OS_IDENTITY_API_VERSION=3
diff --git a/tests/data/openrc_conf/globals.yml b/tests/data/openrc_conf/globals.yml
new file mode 100644
index 00000000..6d663125
--- /dev/null
+++ b/tests/data/openrc_conf/globals.yml
@@ -0,0 +1,12 @@
+docker_namespace: kolla
+docker_registry: 10.20.11.2:4000
+enable_ceph: 'yes'
+enable_cinder: 'yes'
+keepalived_interface: ens9
+kolla_external_vip_interface: ens3
+kolla_internal_vip_address: 10.20.11.11
+network_interface: ens3
+neutron_external_interface: ens8
+openstack_release: 4.0.2
+storage_interface: ens3
+tunnel_interface: ens3
diff --git a/tests/data/openrc_conf/globals_odl.yml b/tests/data/openrc_conf/globals_odl.yml
new file mode 100644
index 00000000..50df4084
--- /dev/null
+++ b/tests/data/openrc_conf/globals_odl.yml
@@ -0,0 +1,13 @@
+docker_namespace: kolla
+docker_registry: 10.20.11.2:4000
+enable_ceph: 'yes'
+enable_cinder: 'yes'
+keepalived_interface: ens9
+kolla_external_vip_interface: ens3
+kolla_internal_vip_address: 10.20.11.11
+network_interface: ens3
+neutron_external_interface: ens8
+openstack_release: 4.0.2
+storage_interface: ens3
+tunnel_interface: ens3
+opendaylight_leader_ip_address: 10.20.7.3
\ No newline at end of file
diff --git a/tests/post/test_keystoneauth.py b/tests/post/test_keystoneauth.py
deleted file mode 100644
index 1b208952..00000000
--- a/tests/post/test_keystoneauth.py
+++ /dev/null
@@ -1,20 +0,0 @@
-##############################################################################
-# Copyright (c) 2017 ZTE Corp and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-import pytest
-
-from deploy.post.keystoneauth import Keystoneauth
-
-
-@pytest.mark.parametrize('openrc, expected', [
-    ('/etc/kolla/admin-openrc.sh', '/etc/kolla/admin-openrc.sh'),
-    (None, '/etc/kolla/admin-openrc.sh')])
-def test_create_Keystoneauth_instance_(openrc, expected):
-    KeystoneClient = Keystoneauth(openrc)
-    assert KeystoneClient.openrc == expected
diff --git a/tests/post/test_post_execute.py b/tests/post/test_post_execute.py
deleted file mode 100644
index 38a67c0e..00000000
--- a/tests/post/test_post_execute.py
+++ /dev/null
@@ -1,97 +0,0 @@
-##############################################################################
-# Copyright (c) 2017 ZTE Corp and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-import pytest
-
-from deploy.post.execute import (
-    _config_external_network,
-    _config_icmp_security_group_rule,
-    _config_ssh_security_group_rule
-)
-
-
-@pytest.mark.parametrize('ext_name, physnet, expected', [
-    ('EXTERNAL', 'ens8',
-     {
-         'network': {
-             'name': 'EXTERNAL',
-             'admin_state_up': True,
-             'shared': False,
-             'provider:network_type': 'flat',
-             'provider:physical_network': 'ens8',
-             'router:external': True
-         }
-     }),
-    ('EXTERNAL', 'ens3',
-     {
-         'network': {
-             'name': 'EXTERNAL',
-             'admin_state_up': True,
-             'shared': False,
-             'provider:network_type': 'flat',
-             'provider:physical_network': 'ens3',
-             'router:external': True
-         }
-     })])
-def test__config_external_network(ext_name, physnet, expected):
-    assert _config_external_network(ext_name, physnet) == expected
-
-
-@pytest.mark.parametrize('security_group_id, expected', [
-    ('0x1111',
-     {
-         'security_group_rule': {
-             'direction': 'ingress',
-             'ethertype': 'IPv4',
-             'protocol': 'icmp',
-             'remote_ip_prefix': '0.0.0.0/0',
-             'security_group_id': '0x1111'
-         }
-     }),
-    ('0xaaaa',
-     {
-         'security_group_rule': {
-             'direction': 'ingress',
-             'ethertype': 'IPv4',
-             'protocol': 'icmp',
-             'remote_ip_prefix': '0.0.0.0/0',
-             'security_group_id': '0xaaaa'
-         }
-     })])
-def test__config_icmp_security_group_rule(security_group_id, expected):
-    assert _config_icmp_security_group_rule(security_group_id) == expected
-
-
-@pytest.mark.parametrize('security_group_id, expected', [
-    ('0x1111',
-     {
-         'security_group_rule': {
-             'direction': 'ingress',
-             'ethertype': 'IPv4',
-             'protocol': 'tcp',
-             'port_range_min': 22,
-             'port_range_max': 22,
-             'remote_ip_prefix': '0.0.0.0/0',
-             'security_group_id': '0x1111'
-         }
-     }),
-    ('0xaaaa',
-     {
-         'security_group_rule': {
-             'direction': 'ingress',
-             'ethertype': 'IPv4',
-             'protocol': 'tcp',
-             'port_range_min': 22,
-             'port_range_max': 22,
-             'remote_ip_prefix': '0.0.0.0/0',
-             'security_group_id': '0xaaaa'
-         }
-     })])
-def test__config_ssh_security_group_rule(security_group_id, expected):
-    assert _config_ssh_security_group_rule(security_group_id) == expected
diff --git a/tests/prepare/test_execute.py b/tests/prepare/test_execute.py
deleted file mode 100644
index 6eb27025..00000000
--- a/tests/prepare/test_execute.py
+++ /dev/null
@@ -1,55 +0,0 @@
-##############################################################################
-# Copyright (c) 2017 ZTE Corp and others.
-#
-# All rights reserved. This program and the accompanying materials
-# are made available under the terms of the Apache License, Version 2.0
-# which accompanies this distribution, and is available at
-# http://www.apache.org/licenses/LICENSE-2.0
-##############################################################################
-
-import os
-import pytest
-
-import deploy.prepare.execute
-from deploy.prepare.execute import (
-    _set_qemu_compute,
-    _set_default_floating_pool,
-    _set_trusts_auth
-)
-
-deploy.prepare.execute.KOLLA_CONF_PATH = '/tmp'
-
-
-@pytest.fixture(scope="module")
-def kolla_conf_file_nov_path():
-    return os.path.join(deploy.prepare.execute.KOLLA_CONF_PATH, 'nova')
-
-
-@pytest.fixture(scope="module")
-def kolla_conf_file_heat_dir():
-    return os.path.join(deploy.prepare.execute.KOLLA_CONF_PATH, 'heat')
-
-
-@pytest.fixture(scope="module")
-def conf_file_dir(data_root):
-    return os.path.join(data_root, 'lab_conf')
-
-
-def test__set_qemu_compute(kolla_conf_file_nov_path):
-    _set_qemu_compute()
-    exp_conf_file = os.path.join(kolla_conf_file_nov_path, 'nova-compute.conf')
-    assert os.path.isfile(exp_conf_file)
-
-
-def test__set_default_floating_pool(kolla_conf_file_nov_path, conf_file_dir):
-    network_conf_file = os.path.join(conf_file_dir, 'network_virtual1.yml')
-    _set_default_floating_pool(network_conf_file)
-    exp_conf_file = os.path.join(kolla_conf_file_nov_path, 'nova-api.conf')
-    assert os.path.isfile(exp_conf_file)
-
-
-def test__set_trusts_auth(kolla_conf_file_heat_dir):
-    _set_trusts_auth()
-    exp_conf_file_1 = os.path.join(kolla_conf_file_heat_dir, 'heat-api.conf')
-    exp_conf_file_2 = os.path.join(kolla_conf_file_heat_dir, 'heat-engine.conf')
-    assert (os.path.isfile(exp_conf_file_1) and os.path.isfile(exp_conf_file_2))
diff --git a/tests/unit/post/test_keystoneauth.py b/tests/unit/post/test_keystoneauth.py
new file mode 100644
index 00000000..afd9062a
--- /dev/null
+++ b/tests/unit/post/test_keystoneauth.py
@@ -0,0 +1,82 @@
+##############################################################################
+# Copyright (c) 2017 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+import os
+
+import pytest
+
+from deploy.post.keystoneauth import Keystoneauth
+
+
+@pytest.mark.parametrize('openrc, expected', [
+    ('/etc/kolla/admin-openrc.sh', '/etc/kolla/admin-openrc.sh'),
+    (None, '/etc/kolla/admin-openrc.sh')])
+def test_create_Keystoneauth_instance(openrc, expected):
+    KeystoneClient = Keystoneauth(openrc)
+    assert KeystoneClient.openrc == expected
+
+
+@pytest.mark.parametrize('raws, expected', [
+    (
+        {
+            'OS_USERNAME': 'admin',
+            'OS_PASSWORD': 'keystone',
+            'OS_AUTH_URL': 'http://10.20.11.11:35357/v3',
+            'OS_TENANT_NAME': 'admin',
+            'OS_USER_DOMAIN_NAME': 'Default',
+            'OS_PROJECT_DOMAIN_NAME': 'Default',
+            'OS_PROJECT_NAME': 'admin',
+            'OS_INTERFACE': 'internal',
+            'OS_IDENTITY_API_VERSION': 'region_name'
+        },
+        {
+            'username': 'admin',
+            'password': 'keystone',
+            'auth_url': 'http://10.20.11.11:35357/v3',
+            'tenant_name': 'admin',
+            'user_domain_name': 'Default',
+            'project_domain_name': 'Default',
+            'project_name': 'admin'
+        }),
+    (
+        {
+            'OS_USERNAME': 'admin',
+            'OS_PASSWORD': 'keystone',
+            'OS_AUTH_URL': 'http://10.20.11.11:35357/v3',
+            'OS_TENANT_NAME': 'admin',
+            'OS_USER_DOMAIN_NAME': 'Default',
+            'OS_PROJECT_DOMAIN_NAME': 'Default',
+            'OS_PROJECT_NAME': 'admin',
+            'OS_ENDPOINT_TYPE': 'Default',
+            'OS_REGION_NAME': 'Default'
+        },
+        {
+            'username': 'admin',
+            'password': 'keystone',
+            'auth_url': 'http://10.20.11.11:35357/v3',
+            'tenant_name': 'admin',
+            'user_domain_name': 'Default',
+            'project_domain_name': 'Default',
+            'project_name': 'admin',
+            'endpoint_type': 'Default',
+            'region_name': 'Default'
+        }
+    )])
+def test__parse_credentials_in_Keystoneauth(raws, expected):
+    assert Keystoneauth._parse_credentials(raws) == expected
+
+
+@pytest.fixture(scope="session")
+def openrc_conf_file_dir(data_root):
+    return os.path.join(data_root, 'openrc_conf')
+
+
+def test_session(openrc_conf_file_dir):
+    openrc = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh')
+    KeystoneClient = Keystoneauth(openrc)
+    assert KeystoneClient.session
diff --git a/tests/unit/post/test_post_execute.py b/tests/unit/post/test_post_execute.py
new file mode 100644
index 00000000..d614360c
--- /dev/null
+++ b/tests/unit/post/test_post_execute.py
@@ -0,0 +1,131 @@
+##############################################################################
+# Copyright (c) 2017 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+import shutil
+import os
+
+from deepdiff import DeepDiff
+import pytest
+
+from deploy.post.execute import (
+    _config_external_network,
+    _config_icmp_security_group_rule,
+    _config_ssh_security_group_rule,
+    _config_kolla_admin_openrc
+)
+
+
+@pytest.mark.parametrize('ext_name, physnet, expected', [
+    ('EXTERNAL', 'ens8',
+     {
+         'network': {
+             'name': 'EXTERNAL',
+             'admin_state_up': True,
+             'shared': False,
+             'provider:network_type': 'flat',
+             'provider:physical_network': 'ens8',
+             'router:external': True
+         }
+     }),
+    ('EXTERNAL', 'ens3',
+     {
+         'network': {
+             'name': 'EXTERNAL',
+             'admin_state_up': True,
+             'shared': False,
+             'provider:network_type': 'flat',
+             'provider:physical_network': 'ens3',
+             'router:external': True
+         }
+     })])
+def test__config_external_network(ext_name, physnet, expected):
+    assert _config_external_network(ext_name, physnet) == expected
+
+
+@pytest.mark.parametrize('security_group_id, expected', [
+    ('0x1111',
+     {
+         'security_group_rule': {
+             'direction': 'ingress',
+             'ethertype': 'IPv4',
+             'protocol': 'icmp',
+             'remote_ip_prefix': '0.0.0.0/0',
+             'security_group_id': '0x1111'
+         }
+     }),
+    ('0xaaaa',
+     {
+         'security_group_rule': {
+             'direction': 'ingress',
+             'ethertype': 'IPv4',
+             'protocol': 'icmp',
+             'remote_ip_prefix': '0.0.0.0/0',
+             'security_group_id': '0xaaaa'
+         }
+     })])
+def test__config_icmp_security_group_rule(security_group_id, expected):
+    assert _config_icmp_security_group_rule(security_group_id) == expected
+
+
+@pytest.mark.parametrize('security_group_id, expected', [
+    ('0x1111',
+     {
+         'security_group_rule': {
+             'direction': 'ingress',
+             'ethertype': 'IPv4',
+             'protocol': 'tcp',
+             'port_range_min': 22,
+             'port_range_max': 22,
+             'remote_ip_prefix': '0.0.0.0/0',
+             'security_group_id': '0x1111'
+         }
+     }),
+    ('0xaaaa',
+     {
+         'security_group_rule': {
+             'direction': 'ingress',
+             'ethertype': 'IPv4',
+             'protocol': 'tcp',
+             'port_range_min': 22,
+             'port_range_max': 22,
+             'remote_ip_prefix': '0.0.0.0/0',
+             'security_group_id': '0xaaaa'
+         }
+     })])
+def test__config_ssh_security_group_rule(security_group_id, expected):
+    assert _config_ssh_security_group_rule(security_group_id) == expected
+
+
+@pytest.fixture(scope="module")
+def openrc_conf_file_dir(data_root):
+    return os.path.join(data_root, 'openrc_conf')
+
+
+@pytest.mark.parametrize('globals_file_name', [
+    ('globals.yml'), ('globals_odl.yml')])
+def test__config_kolla_admin_openrc(globals_file_name, openrc_conf_file_dir, tmpdir):
+    src_globals_file_path = os.path.join(openrc_conf_file_dir, globals_file_name)
+    dst_globals_file_path = os.path.join(tmpdir.dirname, 'globals.yml')
+    shutil.copyfile(src_globals_file_path, dst_globals_file_path)
+
+    src_openrc_file_path = os.path.join(openrc_conf_file_dir, 'admin-openrc.sh')
+    dst_openrc_file_path = os.path.join(tmpdir.dirname, 'admin-openrc.sh')
+    shutil.copyfile(src_openrc_file_path, dst_openrc_file_path)
+
+    _config_kolla_admin_openrc(tmpdir.dirname)
+    src_openrc_lines = open(src_openrc_file_path, 'r').readlines()
+    dst_openrc_lines = open(dst_openrc_file_path, 'r').readlines()
+    if globals_file_name == 'globals.yml':
+        assert DeepDiff(src_openrc_lines, dst_openrc_lines, ignore_order=True) == {}
+    elif globals_file_name == 'globals_odl.yml':
+        diff = DeepDiff(src_openrc_lines, dst_openrc_lines, ignore_order=True)
+        assert len(diff) == 1 and diff.get('iterable_item_added') is not None
+        assert len(diff['iterable_item_added']) == 1
+        for val in diff['iterable_item_added'].values():
+            assert 'export SDN_CONTROLLER_IP' in val
+    tmpdir.remove()
diff --git a/tests/unit/prepare/test_prepare_execure.py b/tests/unit/prepare/test_prepare_execure.py
new file mode 100644
index 00000000..9e72722d
--- /dev/null
+++ b/tests/unit/prepare/test_prepare_execure.py
@@ -0,0 +1,67 @@
+##############################################################################
+# Copyright (c) 2017 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import os
+import pytest
+
+import deploy.prepare.execute
+from deploy.prepare.execute import (
+    _set_qemu_compute,
+    _set_default_floating_pool,
+    _set_trusts_auth
+)
+
+deploy.prepare.execute.KOLLA_CONF_PATH = '/tmp'
+
+
+@pytest.fixture(scope="module")
+def kolla_conf_file_nov_path():
+    return os.path.join(deploy.prepare.execute.KOLLA_CONF_PATH, 'nova')
+
+
+@pytest.fixture(scope="module")
+def kolla_conf_file_heat_dir():
+    return os.path.join(deploy.prepare.execute.KOLLA_CONF_PATH, 'heat')
+
+
+@pytest.fixture(scope="module")
+def conf_file_dir(data_root):
+    return os.path.join(data_root, 'lab_conf')
+
+
+def clear_tmp_dir(path):
+    filelist = os.listdir(path)
+    for file in filelist:
+        file_path = os.path.join(path, file)
+        if os.path.isfile(file_path):
+            os.remove(file_path)
+    os.rmdir(path)
+
+
+def test__set_qemu_compute(kolla_conf_file_nov_path):
+    _set_qemu_compute()
+    exp_conf_file = os.path.join(kolla_conf_file_nov_path, 'nova-compute.conf')
+    assert os.path.isfile(exp_conf_file)
+    clear_tmp_dir(kolla_conf_file_nov_path)
+
+
+def test__set_default_floating_pool(kolla_conf_file_nov_path, conf_file_dir):
+    network_conf_file = os.path.join(conf_file_dir, 'network_virtual1.yml')
+    _set_default_floating_pool(network_conf_file)
+    exp_conf_file = os.path.join(kolla_conf_file_nov_path, 'nova-api.conf')
+    assert os.path.isfile(exp_conf_file)
+    clear_tmp_dir(kolla_conf_file_nov_path)
+
+
+def test__set_trusts_auth(kolla_conf_file_heat_dir):
+    _set_trusts_auth()
+    exp_conf_file_1 = os.path.join(kolla_conf_file_heat_dir, 'heat-api.conf')
+    exp_conf_file_2 = os.path.join(kolla_conf_file_heat_dir, 'heat-engine.conf')
+    assert (os.path.isfile(exp_conf_file_1) and os.path.isfile(exp_conf_file_2))
+    clear_tmp_dir(kolla_conf_file_heat_dir)
-- 
cgit