|
| 1 | +import base64 |
| 2 | +import random |
| 3 | +import string |
| 4 | +import time |
| 5 | + |
| 6 | +def random_string(length=10): |
| 7 | + alphabet = string.ascii_lowercase + string.digits |
| 8 | + return "".join(random.choice(alphabet) for _ in range(length)) |
| 9 | + |
| 10 | + |
| 11 | +def tagged_name(prefix="osc-sdk-python-test", length=10): |
| 12 | + return "{}-{}".format(prefix, random_string(length)) |
| 13 | + |
| 14 | + |
| 15 | +def log_step(message): |
| 16 | + print("[tests] {}".format(message), flush=True) |
| 17 | + |
| 18 | + |
| 19 | +def wait_until(fetch, ready, timeout=300, interval=10, failure=None, describe=None): |
| 20 | + deadline = time.time() + timeout |
| 21 | + last_value = None |
| 22 | + while time.time() < deadline: |
| 23 | + last_value = fetch() |
| 24 | + if describe is not None: |
| 25 | + log_step(describe(last_value)) |
| 26 | + if ready(last_value): |
| 27 | + return last_value |
| 28 | + if failure is not None and failure(last_value): |
| 29 | + raise AssertionError("Resource entered an unexpected state: {}".format(last_value)) |
| 30 | + time.sleep(interval) |
| 31 | + raise AssertionError("Timed out waiting for resource state: {}".format(last_value)) |
| 32 | + |
| 33 | + |
| 34 | +def first(items, message): |
| 35 | + if not items: |
| 36 | + raise AssertionError(message) |
| 37 | + return items[0] |
| 38 | + |
| 39 | + |
| 40 | +def first_subregion_name(gw): |
| 41 | + subregions = gw.ReadSubregions() |
| 42 | + subregion = first(subregions.get("Subregions"), "No subregions returned") |
| 43 | + name = subregion.get("SubregionName") |
| 44 | + if not name: |
| 45 | + raise AssertionError("SubregionName is missing") |
| 46 | + return name |
| 47 | + |
| 48 | + |
| 49 | +def latest_public_ubuntu_image_id(gw): |
| 50 | + images = gw.ReadImages( |
| 51 | + Filters={ |
| 52 | + "AccountAliases": ["Outscale"], |
| 53 | + "ImageNames": ["Ubuntu*"], |
| 54 | + "PermissionsToLaunchGlobalPermission": True, |
| 55 | + "States": ["available"], |
| 56 | + }, |
| 57 | + ResultsPerPage=10, |
| 58 | + ).get("Images", []) |
| 59 | + image = first( |
| 60 | + sorted(images, key=lambda item: item.get("CreationDate", ""), reverse=True), |
| 61 | + "No public Ubuntu image returned", |
| 62 | + ) |
| 63 | + image_id = image.get("ImageId") |
| 64 | + if not image_id: |
| 65 | + raise AssertionError("ImageId is missing") |
| 66 | + return image_id |
| 67 | + |
| 68 | + |
| 69 | +def read_single(gw, action, key, resource_id_key, resource_id): |
| 70 | + response = getattr(gw, action)( |
| 71 | + Filters={ |
| 72 | + resource_id_key: [resource_id], |
| 73 | + } |
| 74 | + ) |
| 75 | + items = response.get(key) |
| 76 | + if not items or len(items) != 1: |
| 77 | + raise AssertionError( |
| 78 | + "{} did not return exactly one resource for {}".format(action, resource_id) |
| 79 | + ) |
| 80 | + return items[0] |
| 81 | + |
| 82 | + |
| 83 | +def create_name_tag(resource_id): |
| 84 | + return { |
| 85 | + "ResourceIds": [resource_id], |
| 86 | + "Tags": [{"Key": "Name", "Value": tagged_name("osc-sdk-python-tag")}], |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | +def linux_http_user_data(): |
| 91 | + script = """#!/bin/sh |
| 92 | +set -eu |
| 93 | +mkdir -p /tmp/python-sdk-lb |
| 94 | +echo ok > /tmp/python-sdk-lb/index.html |
| 95 | +nohup python3 -m http.server 80 --directory /tmp/python-sdk-lb >/tmp/python-sdk-lb/http.log 2>&1 & |
| 96 | +""" |
| 97 | + return base64.b64encode(script.encode("utf-8")).decode("ascii") |
0 commit comments