|
| 1 | +import re |
| 2 | +import time |
| 3 | + |
| 4 | +from tests.integration.conftest import get_regions_with_capabilities |
| 5 | +from tests.integration.helpers import ( |
| 6 | + exec_failing_test_command, |
| 7 | + exec_test_command, |
| 8 | +) |
| 9 | + |
| 10 | +BASE_CMD = ["linode-cli", "vpcs"] |
| 11 | + |
| 12 | + |
| 13 | +def test_list_vpcs(test_vpc_wo_subnet): |
| 14 | + vpc_id = test_vpc_wo_subnet |
| 15 | + res = ( |
| 16 | + exec_test_command(BASE_CMD + ["ls", "--text"]).stdout.decode().rstrip() |
| 17 | + ) |
| 18 | + headers = ["id", "label", "description", "region"] |
| 19 | + |
| 20 | + for header in headers: |
| 21 | + assert header in res |
| 22 | + assert vpc_id in res |
| 23 | + |
| 24 | + |
| 25 | +def test_view_vpc(test_vpc_wo_subnet): |
| 26 | + vpc_id = test_vpc_wo_subnet |
| 27 | + |
| 28 | + res = ( |
| 29 | + exec_test_command(BASE_CMD + ["view", vpc_id, "--text", "--no-headers"]) |
| 30 | + .stdout.decode() |
| 31 | + .rstrip() |
| 32 | + ) |
| 33 | + |
| 34 | + assert vpc_id in res |
| 35 | + |
| 36 | + |
| 37 | +def test_update_vpc(test_vpc_wo_subnet): |
| 38 | + vpc_id = test_vpc_wo_subnet |
| 39 | + |
| 40 | + new_label = str(time.time_ns()) + "label" |
| 41 | + |
| 42 | + updated_label = ( |
| 43 | + exec_test_command( |
| 44 | + BASE_CMD |
| 45 | + + [ |
| 46 | + "update", |
| 47 | + vpc_id, |
| 48 | + "--label", |
| 49 | + new_label, |
| 50 | + "--description", |
| 51 | + "new description", |
| 52 | + "--text", |
| 53 | + "--no-headers", |
| 54 | + "--format=label", |
| 55 | + ] |
| 56 | + ) |
| 57 | + .stdout.decode() |
| 58 | + .rstrip() |
| 59 | + ) |
| 60 | + |
| 61 | + description = ( |
| 62 | + exec_test_command( |
| 63 | + BASE_CMD |
| 64 | + + ["view", vpc_id, "--text", "--no-headers", "--format=description"] |
| 65 | + ) |
| 66 | + .stdout.decode() |
| 67 | + .rstrip() |
| 68 | + ) |
| 69 | + |
| 70 | + assert new_label == updated_label |
| 71 | + assert "new description" in description |
| 72 | + |
| 73 | + |
| 74 | +def test_list_subnets(test_vpc_w_subnet): |
| 75 | + vpc_id = test_vpc_w_subnet |
| 76 | + |
| 77 | + res = ( |
| 78 | + exec_test_command( |
| 79 | + BASE_CMD + ["subnets-list", vpc_id, "--text", "--delimiter=,"] |
| 80 | + ) |
| 81 | + .stdout.decode() |
| 82 | + .rstrip() |
| 83 | + ) |
| 84 | + |
| 85 | + lines = res.splitlines() |
| 86 | + |
| 87 | + headers = ["id", "label", "ipv4", "linodes"] |
| 88 | + |
| 89 | + for header in headers: |
| 90 | + assert header in lines[0] |
| 91 | + |
| 92 | + for line in lines[1:]: |
| 93 | + assert re.match( |
| 94 | + r"^(\d+),(\w+),(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/\d+),", line |
| 95 | + ), "String format does not match" |
| 96 | + |
| 97 | + |
| 98 | +def test_view_subnet(test_vpc_wo_subnet, test_subnet): |
| 99 | + # note calling test_subnet fixture will add subnet to test_vpc_wo_subnet |
| 100 | + res, label = test_subnet |
| 101 | + |
| 102 | + res = res.split(",") |
| 103 | + |
| 104 | + vpc_subnet_id = res[0] |
| 105 | + |
| 106 | + vpc_id = test_vpc_wo_subnet |
| 107 | + |
| 108 | + output = ( |
| 109 | + exec_test_command( |
| 110 | + BASE_CMD + ["subnet-view", vpc_id, vpc_subnet_id, "--text"] |
| 111 | + ) |
| 112 | + .stdout.decode() |
| 113 | + .rstrip() |
| 114 | + ) |
| 115 | + |
| 116 | + headers = ["id", "label", "ipv4", "linodes"] |
| 117 | + |
| 118 | + for header in headers: |
| 119 | + assert header in output |
| 120 | + assert vpc_subnet_id in output |
| 121 | + |
| 122 | + |
| 123 | +def test_update_subnet(test_vpc_w_subnet): |
| 124 | + vpc_id = test_vpc_w_subnet |
| 125 | + |
| 126 | + new_label = str(time.time_ns()) + "label" |
| 127 | + |
| 128 | + subnet_id = ( |
| 129 | + exec_test_command( |
| 130 | + BASE_CMD |
| 131 | + + ["subnets-list", vpc_id, "--text", "--format=id", "--no-headers"] |
| 132 | + ) |
| 133 | + .stdout.decode() |
| 134 | + .rstrip() |
| 135 | + ) |
| 136 | + |
| 137 | + updated_label = ( |
| 138 | + exec_test_command( |
| 139 | + BASE_CMD |
| 140 | + + [ |
| 141 | + "subnet-update", |
| 142 | + vpc_id, |
| 143 | + subnet_id, |
| 144 | + "--label", |
| 145 | + new_label, |
| 146 | + "--text", |
| 147 | + "--format=label", |
| 148 | + "--no-headers", |
| 149 | + ] |
| 150 | + ) |
| 151 | + .stdout.decode() |
| 152 | + .rstrip() |
| 153 | + ) |
| 154 | + |
| 155 | + assert new_label == updated_label |
| 156 | + |
| 157 | + |
| 158 | +def test_fails_to_create_vpc_invalid_label(): |
| 159 | + invalid_label = "invalid_label" |
| 160 | + region = get_regions_with_capabilities(["VPCs"])[0] |
| 161 | + |
| 162 | + res = ( |
| 163 | + exec_failing_test_command( |
| 164 | + BASE_CMD + ["create", "--label", invalid_label, "--region", region] |
| 165 | + ) |
| 166 | + .stderr.decode() |
| 167 | + .rstrip() |
| 168 | + ) |
| 169 | + |
| 170 | + assert "Request failed: 400" in res |
| 171 | + assert "Label must include only ASCII" in res |
| 172 | + |
| 173 | + |
| 174 | +def test_fails_to_create_vpc_duplicate_label(test_vpc_wo_subnet): |
| 175 | + vpc_id = test_vpc_wo_subnet |
| 176 | + label = ( |
| 177 | + exec_test_command( |
| 178 | + BASE_CMD |
| 179 | + + ["view", vpc_id, "--text", "--no-headers", "--format=label"] |
| 180 | + ) |
| 181 | + .stdout.decode() |
| 182 | + .rstrip() |
| 183 | + ) |
| 184 | + region = get_regions_with_capabilities(["VPCs"])[0] |
| 185 | + |
| 186 | + res = ( |
| 187 | + exec_failing_test_command( |
| 188 | + BASE_CMD + ["create", "--label", label, "--region", region] |
| 189 | + ) |
| 190 | + .stderr.decode() |
| 191 | + .rstrip() |
| 192 | + ) |
| 193 | + |
| 194 | + assert "Label must be unique among your VPCs" in res |
| 195 | + |
| 196 | + |
| 197 | +def test_fails_to_update_vpc_invalid_label(test_vpc_wo_subnet): |
| 198 | + vpc_id = test_vpc_wo_subnet |
| 199 | + invalid_label = "invalid_label" |
| 200 | + |
| 201 | + res = ( |
| 202 | + exec_failing_test_command( |
| 203 | + BASE_CMD + ["update", vpc_id, "--label", invalid_label] |
| 204 | + ) |
| 205 | + .stderr.decode() |
| 206 | + .rstrip() |
| 207 | + ) |
| 208 | + |
| 209 | + assert "Request failed: 400" in res |
| 210 | + assert "Label must include only ASCII" in res |
| 211 | + |
| 212 | + |
| 213 | +def test_fails_to_create_vpc_subnet_w_invalid_label(test_vpc_wo_subnet): |
| 214 | + vpc_id = test_vpc_wo_subnet |
| 215 | + invalid_label = "invalid_label" |
| 216 | + region = get_regions_with_capabilities(["VPCs"])[0] |
| 217 | + |
| 218 | + res = exec_failing_test_command( |
| 219 | + BASE_CMD |
| 220 | + + [ |
| 221 | + "subnet-create", |
| 222 | + "--label", |
| 223 | + invalid_label, |
| 224 | + "--ipv4", |
| 225 | + "10.1.0.0/24", |
| 226 | + vpc_id, |
| 227 | + ] |
| 228 | + ).stderr.decode() |
| 229 | + |
| 230 | + assert "Request failed: 400" in res |
| 231 | + assert "Label must include only ASCII" in res |
| 232 | + |
| 233 | + |
| 234 | +def test_fails_to_update_vpc_subenet_w_invalid_label(test_vpc_w_subnet): |
| 235 | + vpc_id = test_vpc_w_subnet |
| 236 | + |
| 237 | + invalid_label = "invalid_label" |
| 238 | + |
| 239 | + subnet_id = ( |
| 240 | + exec_test_command( |
| 241 | + BASE_CMD |
| 242 | + + ["subnets-list", vpc_id, "--text", "--format=id", "--no-headers"] |
| 243 | + ) |
| 244 | + .stdout.decode() |
| 245 | + .rstrip() |
| 246 | + ) |
| 247 | + |
| 248 | + res = ( |
| 249 | + exec_failing_test_command( |
| 250 | + BASE_CMD |
| 251 | + + [ |
| 252 | + "subnet-update", |
| 253 | + vpc_id, |
| 254 | + subnet_id, |
| 255 | + "--label", |
| 256 | + invalid_label, |
| 257 | + "--text", |
| 258 | + "--format=label", |
| 259 | + "--no-headers", |
| 260 | + ] |
| 261 | + ) |
| 262 | + .stderr.decode() |
| 263 | + .rstrip() |
| 264 | + ) |
| 265 | + |
| 266 | + assert "Request failed: 400" in res |
| 267 | + assert "Label must include only ASCII" in res |
0 commit comments