Merge "Updates SFC to use nitrogen"
authorFeng Pan <fpan@redhat.com>
Tue, 12 Sep 2017 15:45:50 +0000 (15:45 +0000)
committerGerrit Code Review <gerrit@opnfv.org>
Tue, 12 Sep 2017 15:45:50 +0000 (15:45 +0000)
apex/common/utils.py
apex/tests/playbooks/test_failed_playbook.yaml [new file with mode: 0644]
apex/tests/test_apex_common_utils.py
lib/ansible/playbooks/configure_undercloud.yml

index e21ab83..b1837b9 100644 (file)
@@ -95,13 +95,36 @@ def run_ansible(ansible_vars, playbook, host='localhost', user='root',
             with open(ansible_tmp, 'w') as fh:
                 fh.write("ANSIBLE_HOST_KEY_CHECKING=FALSE {}".format(
                     ' '.join(ansible_command)))
-    try:
-        my_env = os.environ.copy()
-        my_env['ANSIBLE_HOST_KEY_CHECKING'] = 'False'
-        logging.info("Executing playbook...this may take some time")
-        logging.info(subprocess.check_output(ansible_command, env=my_env,
-                     stderr=subprocess.STDOUT).decode('utf-8'))
-    except subprocess.CalledProcessError as e:
-        logging.error("Error executing ansible: {}".format(
-            pprint.pformat(e.output.decode('utf-8'))))
-        raise
+
+    my_env = os.environ.copy()
+    my_env['ANSIBLE_HOST_KEY_CHECKING'] = 'False'
+    logging.info("Executing playbook...this may take some time")
+    p = subprocess.Popen(ansible_command,
+                         stdin=subprocess.PIPE,
+                         stdout=subprocess.PIPE,
+                         bufsize=1,
+                         env=my_env,
+                         universal_newlines=True)
+    # read first line
+    x = p.stdout.readline()
+    # initialize task
+    task = ''
+    while x:
+        # append lines to task
+        task += x
+        # log the line and read another
+        x = p.stdout.readline()
+        # deliver the task to info when we get a blank line
+        if not x.strip():
+            task += x
+            logging.info(task.replace('\\n', '\n'))
+            task = ''
+            x = p.stdout.readline()
+    # clean up and get return code
+    p.stdout.close()
+    rc = p.wait()
+    if rc:
+        # raise errors
+        e = "Ansible playbook failed. See Ansible logs for details."
+        logging.error(e)
+        raise Exception(e)
diff --git a/apex/tests/playbooks/test_failed_playbook.yaml b/apex/tests/playbooks/test_failed_playbook.yaml
new file mode 100644 (file)
index 0000000..d12cefb
--- /dev/null
@@ -0,0 +1,5 @@
+---
+- hosts: localhost
+  tasks:
+      - fail:
+            msg: "Failure to test with"
index 12aeaf2..aee39a7 100644 (file)
@@ -19,7 +19,8 @@ from apex.tests.constants import (
 from nose.tools import (
     assert_equal,
     assert_is_instance,
-    assert_not_is_instance)
+    assert_not_is_instance,
+    assert_raises)
 
 NET_SETS = os.path.join(TEST_CONFIG_DIR, 'network', 'network_settings.yaml')
 
@@ -60,3 +61,8 @@ class TestCommonUtils:
         playbook = 'apex/tests/playbooks/test_playbook.yaml'
         assert_equal(utils.run_ansible(None, os.path.join(playbook),
                                        dry_run=True), None)
+
+    def test_failed_run_ansible(self):
+        playbook = 'apex/tests/playbooks/test_failed_playbook.yaml'
+        assert_raises(Exception, utils.run_ansible, None,
+                      os.path.join(playbook), dry_run=True)
index 91a8f9a..c0e1cd3 100644 (file)
@@ -16,7 +16,7 @@
         src: /root/.ssh/id_rsa.pub
         dest: /home/stack/jumphost_id_rsa.pub
         owner: stack
-        owner: stack
+        group: stack
         mode: 0644
     - copy:
         src: "{{ apex_temp_dir }}/{{ item }}.yaml"