|
| 1 | +import json |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | + |
| 5 | +testsdir = os.path.dirname(__file__) |
| 6 | +outputdir = os.path.abspath(os.path.join(testsdir, "output")) |
| 7 | +os.makedirs(outputdir, exist_ok=True) |
| 8 | + |
| 9 | +os.makedirs("/etc/salt/cloud.providers.d", exist_ok=True) |
| 10 | +if os.path.exists("/etc/salt/cloud.providers.d/cloudcli-server-test-kamatera.conf"): |
| 11 | + os.unlink("/etc/salt/cloud.providers.d/cloudcli-server-test-kamatera.conf") |
| 12 | +with open("/etc/salt/cloud.providers.d/cloudcli-server-test-kamatera.conf", "w") as f: |
| 13 | + f.write(""" |
| 14 | +cloudcli-server-test-kamatera: |
| 15 | + driver: kamatera |
| 16 | + api_client_id: {KAMATERA_API_CLIENT_ID} |
| 17 | + api_secret: {KAMATERA_API_SECRET} |
| 18 | + api_url: {KAMATERA_API_URL} |
| 19 | +""".format(**os.environ)) |
| 20 | + |
| 21 | +print("listing locations...") |
| 22 | +with open(os.path.join(outputdir, "salt-locations.json"), "wb") as f: |
| 23 | + f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "--list-locations", "cloudcli-server-test-kamatera"])) |
| 24 | + |
| 25 | +with open(os.path.join(outputdir, "salt-locations.json")) as f: |
| 26 | + locations = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"] |
| 27 | +assert locations["IL"] == "Rosh Haayin, Israel (Middle East)" |
| 28 | +assert len(locations) > 10 |
| 29 | +print("OK") |
| 30 | + |
| 31 | +print("listing sizes...") |
| 32 | +with open(os.path.join(outputdir, "salt-sizes.json"), "wb") as f: |
| 33 | + f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "--location=IL", "--list-sizes", "cloudcli-server-test-kamatera"])) |
| 34 | + |
| 35 | +with open(os.path.join(outputdir, "salt-sizes.json")) as f: |
| 36 | + sizes = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"] |
| 37 | +assert len(sizes) > 3 |
| 38 | +assert sizes["B"]["name"] == "Type B - General Purpose" |
| 39 | +print("OK") |
| 40 | + |
| 41 | +print("listing available server options...") |
| 42 | +with open(os.path.join(outputdir, "salt-server-options.json"), "wb") as f: |
| 43 | + f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "--location=IL", "-f", "avail_server_options", "cloudcli-server-test-kamatera"])) |
| 44 | + |
| 45 | +with open(os.path.join(outputdir, "salt-server-options.json")) as f: |
| 46 | + server_options = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"] |
| 47 | +assert len(server_options) > 1 |
| 48 | +assert server_options["monthlyTrafficPackage"]["t5000"] == "5000GB/month on 10Gbit/sec port" |
| 49 | +print("OK") |
| 50 | + |
| 51 | +print("listing images...") |
| 52 | +with open(os.path.join(outputdir, "salt-images.json"), "wb") as f: |
| 53 | + f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "--location=IL", "--list-images", "cloudcli-server-test-kamatera"])) |
| 54 | + |
| 55 | +with open(os.path.join(outputdir, "salt-images.json")) as f: |
| 56 | + images = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"] |
| 57 | +assert len(images) > 10 |
| 58 | +assert images["IL:6000C29a5a7220dcf84716e7bba74215"] == "Ubuntu Server version 18.04 LTS (bionic) 64-bit" |
| 59 | +print("OK") |
| 60 | + |
| 61 | +os.makedirs("/etc/salt/cloud.profiles.d", exist_ok=True) |
| 62 | +if os.path.exists("/etc/salt/cloud.profiles.d/cloudcli-server-test.conf"): |
| 63 | + os.unlink("/etc/salt/cloud.profiles.d/cloudcli-server-test.conf") |
| 64 | +with open("/etc/salt/cloud.profiles.d/cloudcli-server-test.conf", "w") as f: |
| 65 | + f.write(""" |
| 66 | +cloudcli-server-test: |
| 67 | + provider: cloudcli-server-test-kamatera |
| 68 | + location: IL |
| 69 | + cpu_type: B |
| 70 | + cpu_cores: 2 |
| 71 | + ram_mb: 2048 |
| 72 | + disk_size_gb: 20 |
| 73 | + extra_disk_sizes_gb: |
| 74 | + - 15 |
| 75 | + billing_cycle: hourly |
| 76 | + monthly_traffic_package: t5000 |
| 77 | + image: IL:6000C29a5a7220dcf84716e7bba74215 |
| 78 | + networks: |
| 79 | + - name: wan |
| 80 | + ip: auto |
| 81 | + daily_backup: false |
| 82 | + managed: false |
| 83 | +""") |
| 84 | + |
| 85 | +print("creating server...") |
| 86 | +with open(os.path.join(outputdir, "salt-create-server.json"), "wb") as f: |
| 87 | + f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "-p", "cloudcli-server-test", "salttest"])) |
| 88 | + |
| 89 | +with open(os.path.join(outputdir, "salt-create-server.json")) as f: |
| 90 | + jsonlines = None |
| 91 | + for line in f.readlines(): |
| 92 | + if line.strip() == "{": |
| 93 | + jsonlines = ["{"] |
| 94 | + elif jsonlines is not None: |
| 95 | + jsonlines.append(line.strip()) |
| 96 | + created_server = json.loads("\n".join(jsonlines))["salttest"] |
| 97 | +assert created_server["deployed"] == True |
| 98 | +created_server_name = created_server["name"] |
| 99 | +print("created_server_name = " + created_server_name) |
| 100 | +print("OK") |
| 101 | + |
| 102 | +print("listing servers..") |
| 103 | +with open(os.path.join(outputdir, "salt-query.json"), "wb") as f: |
| 104 | + f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "-Q"])) |
| 105 | + |
| 106 | +with open(os.path.join(outputdir, "salt-query.json")) as f: |
| 107 | + servers = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"] |
| 108 | +assert len(servers) > 0 |
| 109 | +assert servers[created_server_name]["state"] == "running" |
| 110 | +print("OK") |
| 111 | + |
| 112 | +print("deleting server...") |
| 113 | +with open(os.path.join(outputdir, "salt-delete.json"), "wb") as f: |
| 114 | + f.write(subprocess.check_output(["%s/salt-cloud" % os.environ["SALT_BIN_DIR"], "--out=json", "-y", "-d", created_server_name])) |
| 115 | + |
| 116 | +with open(os.path.join(outputdir, "salt-delete.json")) as f: |
| 117 | + servers = json.load(f)["cloudcli-server-test-kamatera"]["kamatera"] |
| 118 | +assert len(servers) > 0 |
| 119 | +assert servers[created_server_name]["state"] == "destroyed" |
| 120 | +assert servers[created_server_name]["success"] == True |
| 121 | +print("OK") |
0 commit comments