Fixes bug 992088- Testcases for Console Output and one test case to test_authorization.py

Change-Id: I38535d09d7cc9186a9db1ba48f884441af0fe87f
diff --git a/tempest/openstack.py b/tempest/openstack.py
index 18af647..e23373f 100644
--- a/tempest/openstack.py
+++ b/tempest/openstack.py
@@ -31,6 +31,8 @@
 from tempest.services.nova.json.floating_ips_client import FloatingIPsClient
 from tempest.services.nova.json.keypairs_client import KeyPairsClient
 from tempest.services.nova.json.volumes_client import VolumesClient
+from tempest.services.nova.json.console_output_client \
+import ConsoleOutputsClient
 from tempest.services.identity.json.admin_client import AdminClient
 from tempest.services.identity.json.admin_client import TokenClient
 
@@ -82,6 +84,7 @@
         self.security_groups_client = SecurityGroupsClient(*client_args)
         self.floating_ips_client = FloatingIPsClient(*client_args)
         self.volumes_client = VolumesClient(*client_args)
+        self.console_outputs_client = ConsoleOutputsClient(*client_args)
         self.admin_client = AdminClient(*client_args)
         self.token_client = TokenClient(self.config)
         self.network_client = NetworkClient(*client_args)
diff --git a/tempest/services/nova/json/console_output_client.py b/tempest/services/nova/json/console_output_client.py
new file mode 100644
index 0000000..4a8795b
--- /dev/null
+++ b/tempest/services/nova/json/console_output_client.py
@@ -0,0 +1,18 @@
+from tempest.common.rest_client import RestClient
+import json
+
+
+class ConsoleOutputsClient(RestClient):
+
+    def __init__(self, config, username, password, auth_url, tenant_name=None):
+        super(ConsoleOutputsClient, self).__init__(config, username, password,
+                                             auth_url, tenant_name)
+        self.service = self.config.compute.catalog_type
+
+    def get_console_output(self, server_id, length):
+        post_body = {'os-getConsoleOutput': {'length': length}}
+        url = "/servers/%s/action" % server_id
+        post_body = json.dumps(post_body)
+        resp, body = self.post(url, post_body, self.headers)
+        body = json.loads(body)
+        return resp, body['output']
diff --git a/tempest/tests/base_compute_test.py b/tempest/tests/base_compute_test.py
index 2b16d8c..d7dac10 100644
--- a/tempest/tests/base_compute_test.py
+++ b/tempest/tests/base_compute_test.py
@@ -15,6 +15,7 @@
     keypairs_client = os.keypairs_client
     floating_ips_client = os.floating_ips_client
     security_groups_client = os.security_groups_client
+    console_outputs_client = os.console_outputs_client
     limits_client = os.limits_client
     config = os.config
     build_interval = config.compute.build_interval
diff --git a/tempest/tests/test_authorization.py b/tempest/tests/test_authorization.py
index b21e0d3..8a0cf1b 100644
--- a/tempest/tests/test_authorization.py
+++ b/tempest/tests/test_authorization.py
@@ -18,6 +18,7 @@
         cls.images_client = cls.os.images_client
         cls.keypairs_client = cls.os.keypairs_client
         cls.security_client = cls.os.security_groups_client
+        cls.console_outputs_client = cls.os.console_outputs_client
         cls.config = cls.os.config
         cls.image_ref = cls.config.compute.image_ref
         cls.flavor_ref = cls.config.compute.flavor_ref
@@ -42,6 +43,8 @@
                 cls.other_keypairs_client = cls.other_manager.keypairs_client
                 cls.other_security_client = \
                 cls.other_manager.security_groups_client
+                cls.other_console_outputs_client = \
+                cls.other_manager.console_outputs_client
             except exceptions.AuthenticationFailure:
                 # multi_user is already set to false, just fall through
                 pass
@@ -420,3 +423,13 @@
             resp, body = \
             self.images_client.delete_image_metadata_item(self.image['id'],
                                                             'meta1')
+
+    @raises(exceptions.NotFound)
+    @attr(type='negative')
+    @utils.skip_unless_attr('multi_user', 'Second user not configured')
+    def test_get_console_output_of_other_account_server_fails(self):
+        """
+        A Get Console Output for another user's server should fail
+        """
+        self.other_console_outputs_client.get_console_output(self.server['id'],
+                                                                    10)
diff --git a/tempest/tests/test_console_output.py b/tempest/tests/test_console_output.py
new file mode 100644
index 0000000..1aa4a1c
--- /dev/null
+++ b/tempest/tests/test_console_output.py
@@ -0,0 +1,59 @@
+from nose.plugins.attrib import attr
+from tempest import openstack
+from tempest import exceptions
+from base_compute_test import BaseComputeTest
+from tempest.common.utils.data_utils import rand_name
+
+
+class ConsoleOutputTest(BaseComputeTest):
+
+    @classmethod
+    def setUpClass(cls):
+        cls.client = cls.console_outputs_client
+        cls.servers_client = cls.servers_client
+        cls.name = rand_name('server')
+        resp, server = cls.servers_client.create_server(cls.name,
+                                                 cls.image_ref,
+                                                 cls.flavor_ref)
+        cls.server_id = server['id']
+
+        cls.servers_client.wait_for_server_status(cls.server_id, 'ACTIVE')
+
+    @classmethod
+    def tearDownClass(cls):
+        cls.servers_client.delete_server(cls.server_id)
+
+    @attr(type='positive')
+    def test_get_console_output(self):
+            resp, output = self.client.get_console_output(self.server_id, 10)
+            self.assertEqual(200, resp.status)
+            self.assertNotEqual(output, None)
+            lines = len(output.split('\n'))
+            self.assertEqual(lines, 10)
+
+    @attr(type='negative')
+    def test_get_console_output_invalid_server_id(self):
+        try:
+            resp, output = self.client.get_console_output('!@#$%^&*()', 10)
+        except exceptions.NotFound:
+            pass
+
+    @attr(type='positive')
+    def test_get_console_output_server_id_in_reboot_status(self):
+        try:
+            resp, output = self.servers_client.reboot(self.server_id, 'SOFT')
+            self.servers_client.wait_for_server_status(self.server_id,
+                                                        'REBOOT')
+            resp, server = self.servers_client.get_server(self.server_id)
+            if (server['status'] == 'REBOOT'):
+                resp, output = self.client.get_console_output(self.server_id,
+                                                                10)
+                self.assertEqual(200, resp.status)
+                self.assertNotEqual(output, None)
+                lines = len(output.split('\n'))
+                self.assertEqual(lines, 10)
+            else:
+                self.fail("Could not capture instance in Reboot status")
+        finally:
+            self.servers_client.wait_for_server_status(self.server_id,
+                                                        'ACTIVE')