summaryrefslogtreecommitdiff
path: root/tests/cloud_tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/cloud_tests')
-rw-r--r--tests/cloud_tests/platforms/nocloudkvm/instance.py39
-rw-r--r--tests/cloud_tests/platforms/nocloudkvm/snapshot.py20
2 files changed, 35 insertions, 24 deletions
diff --git a/tests/cloud_tests/platforms/nocloudkvm/instance.py b/tests/cloud_tests/platforms/nocloudkvm/instance.py
index a87d76a6..9bb24256 100644
--- a/tests/cloud_tests/platforms/nocloudkvm/instance.py
+++ b/tests/cloud_tests/platforms/nocloudkvm/instance.py
@@ -2,13 +2,16 @@
"""Base NoCloud KVM instance."""
+import copy
import os
import paramiko
import socket
import subprocess
import time
+import uuid
from ..instances import Instance
+from cloudinit.atomic_helper import write_json
from cloudinit import util as c_util
from tests.cloud_tests import util
@@ -37,14 +40,38 @@ class NoCloudKVMInstance(Instance):
@param features: dictionary of supported feature flags
"""
self.user_data = user_data
- self.meta_data = meta_data
- self.ssh_key_file = os.path.join(platform.config['data_dir'],
- platform.config['private_key'])
+ if meta_data:
+ meta_data = copy.deepcopy(meta_data)
+ else:
+ meta_data = {}
+
+ if 'instance-id' in meta_data:
+ iid = meta_data['instance-id']
+ else:
+ iid = str(uuid.uuid1())
+ meta_data['instance-id'] = iid
+
+ self.instance_id = iid
+ self.ssh_key_file = os.path.join(
+ platform.config['data_dir'], platform.config['private_key'])
+ self.ssh_pubkey_file = os.path.join(
+ platform.config['data_dir'], platform.config['public_key'])
+
+ self.ssh_pubkey = None
+ if self.ssh_pubkey_file:
+ with open(self.ssh_pubkey_file, "r") as fp:
+ self.ssh_pubkey = fp.read().rstrip('\n')
+
+ if not meta_data.get('public-keys'):
+ meta_data['public-keys'] = []
+ meta_data['public-keys'].append(self.ssh_pubkey)
+
self.ssh_port = None
self.pid = None
self.pid_file = None
self.console_file = None
self.disk = image_path
+ self.meta_data = meta_data
super(NoCloudKVMInstance, self).__init__(
platform, name, properties, config, features)
@@ -78,11 +105,15 @@ class NoCloudKVMInstance(Instance):
"""Generate nocloud seed from user-data"""
seed_file = os.path.join(tmpdir, '%s_seed.img' % self.name)
user_data_file = os.path.join(tmpdir, '%s_user_data' % self.name)
+ meta_data_file = os.path.join(tmpdir, '%s_meta_data' % self.name)
with open(user_data_file, "w") as ud_file:
ud_file.write(self.user_data)
- c_util.subp(['cloud-localds', seed_file, user_data_file])
+ # meta-data can be yaml, but more easily pretty printed with json
+ write_json(meta_data_file, self.meta_data)
+ c_util.subp(['cloud-localds', seed_file, user_data_file,
+ meta_data_file])
return seed_file
diff --git a/tests/cloud_tests/platforms/nocloudkvm/snapshot.py b/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
index 0005e1f2..2dae3590 100644
--- a/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
+++ b/tests/cloud_tests/platforms/nocloudkvm/snapshot.py
@@ -41,10 +41,6 @@ class NoCloudKVMSnapshot(Snapshot):
@param use_desc: description of snapshot instance use
@return_value: an Instance
"""
- key_file = os.path.join(self.platform.config['data_dir'],
- self.platform.config['public_key'])
- user_data = self.inject_ssh_key(user_data, key_file)
-
instance = self.platform.create_instance(
self.properties, self.config, self.features,
self._image_path, image_desc=str(self), use_desc=use_desc,
@@ -55,22 +51,6 @@ class NoCloudKVMSnapshot(Snapshot):
return instance
- def inject_ssh_key(self, user_data, key_file):
- """Inject the authorized key into the user_data."""
- with open(key_file) as f:
- value = f.read()
-
- key = 'ssh_authorized_keys:'
- value = ' - %s' % value.strip()
- user_data = user_data.split('\n')
- if key in user_data:
- user_data.insert(user_data.index(key) + 1, '%s' % value)
- else:
- user_data.insert(-1, '%s' % key)
- user_data.insert(-1, '%s' % value)
-
- return '\n'.join(user_data)
-
def destroy(self):
"""Clean up snapshot data."""
shutil.rmtree(self._workd)