Add tempest test for heat resource OS::Nova::KeyPair

The template will create two OS::Nova::KeyPair and will test that:
   [*] The key with save_private_key flag set to true outputs both
       public and private keys.
   [*] The key with save_private_key flag set to false outputs
       only the public key.

Testing other cases will require to recreate the stack and
include a negative test case.

Change-Id: I751a85e0efd997a1238c53d8802ebdd8b551e7aa
Partial-Bug: #1271995
diff --git a/tempest/api/orchestration/stacks/test_nova_keypair_resources.py b/tempest/api/orchestration/stacks/test_nova_keypair_resources.py
new file mode 100644
index 0000000..9d3bf13
--- /dev/null
+++ b/tempest/api/orchestration/stacks/test_nova_keypair_resources.py
@@ -0,0 +1,181 @@
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+
+import logging
+
+from tempest.api.orchestration import base
+from tempest.common.utils import data_utils
+from tempest.test import attr
+
+
+LOG = logging.getLogger(__name__)
+
+
+class NovaKeyPairResourcesYAMLTest(base.BaseOrchestrationTest):
+    _interface = 'json'
+    template = """
+heat_template_version: 2013-05-23
+
+description: >
+  Template which creates two key pairs.
+
+parameters:
+  KeyPairName1:
+    type: string
+    default: testkey
+
+  KeyPairName2:
+    type: string
+    default: testkey2
+
+resources:
+  KeyPairSavePrivate:
+    type: OS::Nova::KeyPair
+    properties:
+      name: { get_param: KeyPairName1 }
+      save_private_key: true
+
+  KeyPairDontSavePrivate:
+    type: OS::Nova::KeyPair
+    properties:
+      name: { get_param: KeyPairName2 }
+      save_private_key: false
+
+outputs:
+  KeyPair_PublicKey:
+    description: Public Key of generated keypair
+    value: { get_attr: [KeyPairSavePrivate, public_key] }
+
+  KeyPair_PrivateKey:
+    description: Private Key of generated keypair
+    value: { get_attr: [KeyPairSavePrivate, private_key] }
+
+  KeyPairDontSavePrivate_PublicKey:
+    description: Public Key of generated keypair
+    value: { get_attr: [KeyPairDontSavePrivate, public_key] }
+
+  KeyPairDontSavePrivate_PrivateKey:
+    description: Private Key of generated keypair
+    value: { get_attr: [KeyPairDontSavePrivate, private_key] }
+"""
+
+    @classmethod
+    def setUpClass(cls):
+        super(NovaKeyPairResourcesYAMLTest, cls).setUpClass()
+        cls.client = cls.orchestration_client
+        cls.stack_name = data_utils.rand_name('heat')
+
+        # create the stack, avoid any duplicated key.
+        cls.stack_identifier = cls.create_stack(
+            cls.stack_name,
+            cls.template,
+            parameters={
+                'KeyPairName1': cls.stack_name + '_1',
+                'KeyPairName2': cls.stack_name + '_2'
+            })
+
+        cls.stack_id = cls.stack_identifier.split('/')[1]
+        cls.client.wait_for_stack_status(cls.stack_id, 'CREATE_COMPLETE')
+        _, resources = cls.client.list_resources(cls.stack_identifier)
+        cls.test_resources = {}
+        for resource in resources:
+            cls.test_resources[resource['logical_resource_id']] = resource
+
+    @attr(type='slow')
+    def test_created_resources(self):
+        """Verifies created keypair resource."""
+        resources = [('KeyPairSavePrivate', 'OS::Nova::KeyPair'),
+                     ('KeyPairDontSavePrivate', 'OS::Nova::KeyPair')]
+
+        for resource_name, resource_type in resources:
+            resource = self.test_resources.get(resource_name, None)
+            self.assertIsInstance(resource, dict)
+            self.assertEqual(resource_name, resource['logical_resource_id'])
+            self.assertEqual(resource_type, resource['resource_type'])
+            self.assertEqual('CREATE_COMPLETE', resource['resource_status'])
+
+    @attr(type='slow')
+    def test_stack_keypairs_output(self):
+        resp, stack = self.client.get_stack(self.stack_name)
+        self.assertEqual('200', resp['status'])
+        self.assertIsInstance(stack, dict)
+
+        output_map = {}
+        for outputs in stack['outputs']:
+            output_map[outputs['output_key']] = outputs['output_value']
+        #Test that first key generated public and private keys
+        self.assertTrue('KeyPair_PublicKey' in output_map)
+        self.assertTrue("Generated by" in output_map['KeyPair_PublicKey'])
+        self.assertTrue('KeyPair_PrivateKey' in output_map)
+        self.assertTrue('-----BEGIN' in output_map['KeyPair_PrivateKey'])
+        #Test that second key generated public key, and private key is not
+        #in the output due to save_private_key = false
+        self.assertTrue('KeyPairDontSavePrivate_PublicKey' in output_map)
+        self.assertTrue('Generated by' in
+                        output_map['KeyPairDontSavePrivate_PublicKey'])
+        self.assertTrue(u'KeyPairDontSavePrivate_PrivateKey' in output_map)
+        private_key = output_map['KeyPairDontSavePrivate_PrivateKey']
+        self.assertTrue(len(private_key) == 0)
+
+
+class NovaKeyPairResourcesAWSTest(NovaKeyPairResourcesYAMLTest):
+    template = """
+{
+  "AWSTemplateFormatVersion" : "2010-09-09",
+  "Description" : "Template which create two key pairs.",
+  "Parameters" : {
+    "KeyPairName1": {
+      "Type": "String",
+      "Default": "testkey1"
+      },
+    "KeyPairName2": {
+      "Type": "String",
+      "Default": "testkey2"
+      }
+   },
+   "Resources" : {
+     "KeyPairSavePrivate": {
+       "Type": "OS::Nova::KeyPair",
+       "Properties": {
+         "name" : { "Ref" : "KeyPairName1" },
+         "save_private_key": true
+       }
+     },
+     "KeyPairDontSavePrivate": {
+       "Type": "OS::Nova::KeyPair",
+       "Properties": {
+         "name" : { "Ref" : "KeyPairName2" },
+         "save_private_key": false
+      }
+     }
+  },
+ "Outputs": {
+   "KeyPair_PublicKey": {
+     "Description": "Public Key of generated keypair.",
+     "Value": { "Fn::GetAtt" : ["KeyPairSavePrivate", "public_key"] }
+    },
+   "KeyPair_PrivateKey": {
+     "Description": "Private Key of generated keypair.",
+     "Value": { "Fn::GetAtt" : ["KeyPairSavePrivate", "private_key"] }
+   },
+   "KeyPairDontSavePrivate_PublicKey": {
+     "Description": "Public Key of generated keypair.",
+     "Value": { "Fn::GetAtt" : ["KeyPairDontSavePrivate", "public_key"] }
+   },
+  "KeyPairDontSavePrivate_PrivateKey": {
+     "Description": "Private Key of generated keypair.",
+     "Value": { "Fn::GetAtt" : ["KeyPairDontSavePrivate", "private_key"] }
+   }
+  }
+}
+"""