|
| 1 | +import os |
| 2 | +import time |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from linode_api4.linode_client import LinodeClient, LongviewSubscription |
| 7 | + |
| 8 | +ENV_TOKEN_NAME = "LINODE_TOKEN" |
| 9 | +RUN_LONG_TESTS = "RUN_LONG_TESTS" |
| 10 | + |
| 11 | + |
| 12 | +def get_token(): |
| 13 | + return os.environ.get(ENV_TOKEN_NAME, None) |
| 14 | + |
| 15 | + |
| 16 | +def run_long_tests(): |
| 17 | + return os.environ.get(RUN_LONG_TESTS, None) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="session") |
| 21 | +def create_linode(get_client): |
| 22 | + client = get_client |
| 23 | + available_regions = client.regions() |
| 24 | + chosen_region = available_regions[0] |
| 25 | + timestamp = str(int(time.time())) |
| 26 | + label = "TestSDK-" + timestamp |
| 27 | + |
| 28 | + linode_instance, password = client.linode.instance_create( |
| 29 | + "g5-standard-4", chosen_region, image="linode/debian9", label=label |
| 30 | + ) |
| 31 | + |
| 32 | + yield linode_instance |
| 33 | + |
| 34 | + linode_instance.delete() |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def create_linode_for_pass_reset(get_client): |
| 39 | + client = get_client |
| 40 | + available_regions = client.regions() |
| 41 | + chosen_region = available_regions[0] |
| 42 | + timestamp = str(int(time.time())) |
| 43 | + label = "TestSDK-" + timestamp |
| 44 | + |
| 45 | + linode_instance, password = client.linode.instance_create( |
| 46 | + "g5-standard-4", chosen_region, image="linode/debian9", label=label |
| 47 | + ) |
| 48 | + |
| 49 | + yield linode_instance, password |
| 50 | + |
| 51 | + linode_instance.delete() |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture(scope="session") |
| 55 | +def ssh_key_gen(): |
| 56 | + output = os.popen("ssh-keygen -q -t rsa -f ./sdk-sshkey -q -N ''") |
| 57 | + |
| 58 | + time.sleep(1) |
| 59 | + |
| 60 | + pub_file = open("./sdk-sshkey.pub", "r") |
| 61 | + pub_key = pub_file.read().rstrip() |
| 62 | + |
| 63 | + priv_file = open("./sdk-sshkey", "r") |
| 64 | + priv_key = priv_file.read().rstrip() |
| 65 | + |
| 66 | + yield pub_key, priv_key |
| 67 | + |
| 68 | + os.popen("rm ./sdk-sshkey*") |
| 69 | + |
| 70 | + |
| 71 | +@pytest.fixture(scope="session") |
| 72 | +def get_client(): |
| 73 | + token = get_token() |
| 74 | + client = LinodeClient(token) |
| 75 | + return client |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture |
| 79 | +def set_account_settings(get_client): |
| 80 | + client = get_client |
| 81 | + account_settings = client.account.settings() |
| 82 | + account_settings._populated = True |
| 83 | + account_settings.network_helper = True |
| 84 | + |
| 85 | + account_settings.save() |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture(scope="session") |
| 89 | +def create_domain(get_client): |
| 90 | + client = get_client |
| 91 | + |
| 92 | + timestamp = str(int(time.time())) |
| 93 | + domain_addr = timestamp + "-example.com" |
| 94 | + soa_email = "pathiel-test123@linode.com" |
| 95 | + |
| 96 | + domain = client.domain_create( |
| 97 | + domain=domain_addr, soa_email=soa_email, tags=["test-tag"] |
| 98 | + ) |
| 99 | + |
| 100 | + # Create a SRV record |
| 101 | + domain.record_create( |
| 102 | + "SRV", |
| 103 | + target="rc_test", |
| 104 | + priority=10, |
| 105 | + weight=5, |
| 106 | + port=80, |
| 107 | + service="service_test", |
| 108 | + ) |
| 109 | + |
| 110 | + yield domain |
| 111 | + |
| 112 | + domain.delete() |
| 113 | + |
| 114 | + |
| 115 | +@pytest.fixture(scope="session") |
| 116 | +def create_volume(get_client): |
| 117 | + client = get_client |
| 118 | + timestamp = str(int(time.time())) |
| 119 | + label = "TestSDK-" + timestamp |
| 120 | + |
| 121 | + volume = client.volume_create(label=label, region="ap-west") |
| 122 | + |
| 123 | + yield volume |
| 124 | + |
| 125 | + volume.delete() |
| 126 | + |
| 127 | + |
| 128 | +@pytest.fixture |
| 129 | +def create_tag(get_client): |
| 130 | + client = get_client |
| 131 | + |
| 132 | + timestamp = str(int(time.time())) |
| 133 | + label = "TestSDK-" + timestamp |
| 134 | + |
| 135 | + tag = client.tag_create(label=label) |
| 136 | + |
| 137 | + yield tag |
| 138 | + |
| 139 | + tag.delete() |
| 140 | + |
| 141 | + |
| 142 | +@pytest.fixture |
| 143 | +def create_nodebalancer(get_client): |
| 144 | + client = get_client |
| 145 | + |
| 146 | + timestamp = str(int(time.time())) |
| 147 | + label = "TestSDK-" + timestamp |
| 148 | + |
| 149 | + nodebalancer = client.nodebalancer_create(region="us-east", label=label) |
| 150 | + |
| 151 | + yield nodebalancer |
| 152 | + |
| 153 | + nodebalancer.delete() |
| 154 | + |
| 155 | + |
| 156 | +@pytest.fixture |
| 157 | +def create_longview_client(get_client): |
| 158 | + client = get_client |
| 159 | + timestamp = str(int(time.time())) |
| 160 | + label = "TestSDK-" + timestamp |
| 161 | + longview_client = client.longview.client_create(label=label) |
| 162 | + |
| 163 | + yield longview_client |
| 164 | + |
| 165 | + longview_client.delete() |
| 166 | + |
| 167 | + |
| 168 | +@pytest.fixture |
| 169 | +def upload_sshkey(get_client, ssh_key_gen): |
| 170 | + pub_key = ssh_key_gen[0] |
| 171 | + client = get_client |
| 172 | + key = client.profile.ssh_key_upload(pub_key, "IntTestSDK-sshkey") |
| 173 | + |
| 174 | + yield key |
| 175 | + |
| 176 | + key.delete() |
| 177 | + |
| 178 | + |
| 179 | +@pytest.fixture |
| 180 | +def create_ssh_keys_object_storage(get_client): |
| 181 | + client = get_client |
| 182 | + label = "TestSDK-obj-storage-key" |
| 183 | + key = client.object_storage.keys_create(label) |
| 184 | + |
| 185 | + yield key |
| 186 | + |
| 187 | + key.delete() |
| 188 | + |
| 189 | + |
| 190 | +@pytest.fixture(scope="session") |
| 191 | +def create_firewall(get_client): |
| 192 | + client = get_client |
| 193 | + rules = { |
| 194 | + "outbound": [], |
| 195 | + "outbound_policy": "DROP", |
| 196 | + "inbound": [], |
| 197 | + "inbound_policy": "ACCEPT", |
| 198 | + } |
| 199 | + |
| 200 | + firewall = client.networking.firewall_create( |
| 201 | + "test-firewall", rules=rules, status="enabled" |
| 202 | + ) |
| 203 | + |
| 204 | + yield firewall |
| 205 | + |
| 206 | + firewall.delete() |
| 207 | + |
| 208 | + |
| 209 | +@pytest.fixture |
| 210 | +def create_oauth_client(get_client): |
| 211 | + client = get_client |
| 212 | + oauth_client = client.account.oauth_client_create( |
| 213 | + "test-oauth-client", "https://localhost/oauth/callback" |
| 214 | + ) |
| 215 | + |
| 216 | + yield oauth_client |
| 217 | + |
| 218 | + oauth_client.delete() |
0 commit comments