summaryrefslogtreecommitdiff
path: root/tests/cloud_tests/platforms/platforms.py
diff options
context:
space:
mode:
authorChad Smith <chad.smith@canonical.com>2018-02-02 11:11:36 -0700
committerChad Smith <chad.smith@canonical.com>2018-02-02 11:11:36 -0700
commit78013bc65030421699b5feb66bc8b7a205abfbc0 (patch)
tree2ebf7111129f4aaf8a833ba6d226d4513ed59388 /tests/cloud_tests/platforms/platforms.py
parent192261fe38a32edbd1f605ba25bbb6f4822a0720 (diff)
parentf7deaf15acf382d62554e2b1d70daa9a9109d542 (diff)
downloadvyos-cloud-init-78013bc65030421699b5feb66bc8b7a205abfbc0.tar.gz
vyos-cloud-init-78013bc65030421699b5feb66bc8b7a205abfbc0.zip
merge from master at 17.2-30-gf7deaf15
Diffstat (limited to 'tests/cloud_tests/platforms/platforms.py')
-rw-r--r--tests/cloud_tests/platforms/platforms.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/tests/cloud_tests/platforms/platforms.py b/tests/cloud_tests/platforms/platforms.py
new file mode 100644
index 00000000..1542b3be
--- /dev/null
+++ b/tests/cloud_tests/platforms/platforms.py
@@ -0,0 +1,96 @@
+# This file is part of cloud-init. See LICENSE file for license information.
+
+"""Base platform class."""
+import os
+
+from simplestreams import filters, mirrors
+from simplestreams import util as s_util
+
+from cloudinit import util as c_util
+
+
+class Platform(object):
+ """Base class for platforms."""
+
+ platform_name = None
+
+ def __init__(self, config):
+ """Set up platform."""
+ self.config = config
+ self._generate_ssh_keys(config['data_dir'])
+
+ def get_image(self, img_conf):
+ """Get image using specified image configuration.
+
+ @param img_conf: configuration for image
+ @return_value: cloud_tests.images instance
+ """
+ raise NotImplementedError
+
+ def destroy(self):
+ """Clean up platform data."""
+ pass
+
+ def _generate_ssh_keys(self, data_dir):
+ """Generate SSH keys to be used with image."""
+ filename = os.path.join(data_dir, self.config['private_key'])
+
+ if os.path.exists(filename):
+ c_util.del_file(filename)
+
+ c_util.subp(['ssh-keygen', '-t', 'rsa', '-b', '4096',
+ '-f', filename, '-P', '',
+ '-C', 'ubuntu@cloud_test'],
+ capture=True)
+
+ @staticmethod
+ def _query_streams(img_conf, img_filter):
+ """Query streams for latest image given a specific filter.
+
+ @param img_conf: configuration for image
+ @param filters: array of filters as strings format 'key=value'
+ @return: dictionary with latest image information or empty
+ """
+ def policy(content, path):
+ return s_util.read_signed(content, keyring=img_conf['keyring'])
+
+ (url, path) = s_util.path_from_mirror_url(img_conf['mirror_url'], None)
+ smirror = mirrors.UrlMirrorReader(url, policy=policy)
+
+ config = {'max_items': 1, 'filters': filters.get_filters(img_filter)}
+ tmirror = FilterMirror(config)
+ tmirror.sync(smirror, path)
+
+ try:
+ return tmirror.json_entries[0]
+ except IndexError:
+ raise RuntimeError('no images found with filter: %s' % img_filter)
+
+
+class FilterMirror(mirrors.BasicMirrorWriter):
+ """Taken from sstream-query to return query result as json array."""
+
+ def __init__(self, config=None):
+ super(FilterMirror, self).__init__(config=config)
+ if config is None:
+ config = {}
+ self.config = config
+ self.filters = config.get('filters', [])
+ self.json_entries = []
+
+ def load_products(self, path=None, content_id=None):
+ return {'content_id': content_id, 'products': {}}
+
+ def filter_item(self, data, src, target, pedigree):
+ return filters.filter_item(self.filters, data, src, pedigree)
+
+ def insert_item(self, data, src, target, pedigree, contentsource):
+ # src and target are top level products:1.0
+ # data is src['products'][ped[0]]['versions'][ped[1]]['items'][ped[2]]
+ # contentsource is a ContentSource if 'path' exists in data or None
+ data = s_util.products_exdata(src, pedigree)
+ if 'path' in data:
+ data.update({'item_url': contentsource.url})
+ self.json_entries.append(data)
+
+# vi: ts=4 expandtab