forked from linode/linode-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plugin_ssh.py
More file actions
132 lines (101 loc) · 3.35 KB
/
Copy pathtest_plugin_ssh.py
File metadata and controls
132 lines (101 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import json
from sys import platform
from typing import Any, Dict
import pytest
from tests.integration.helpers import (
BASE_CMDS,
COMMAND_JSON_OUTPUT,
delete_target_id,
exec_failing_test_command,
exec_test_command,
get_random_region_with_caps,
get_random_text,
wait_for_condition,
)
TEST_REGION = get_random_region_with_caps(required_capabilities=["Linodes"])
TEST_IMAGE = "linode/ubuntu24.10"
TEST_TYPE = "g6-nanode-1"
TEST_ROOT_PASS = "r00tp@ss!long-long-and-longer"
BASE_CMD = ["linode-cli", "ssh"]
INSTANCE_WAIT_TIMEOUT_SECONDS = 120
SSH_WAIT_TIMEOUT_SECONDS = 120
POLL_INTERVAL = 5
@pytest.fixture
def target_instance(ssh_key_pair_generator, linode_cloud_firewall):
instance_label = f"cli-test-{get_random_text(length=6)}"
pubkey_file, privkey_file = ssh_key_pair_generator
with open(pubkey_file, "r") as f:
pubkey = f.read().rstrip()
output = exec_test_command(
BASE_CMDS["linodes"]
+ [
"create",
"--image",
TEST_IMAGE,
"--region",
TEST_REGION,
"--type",
TEST_TYPE,
"--label",
instance_label,
"--root_pass",
TEST_ROOT_PASS,
"--authorized_keys",
pubkey,
"--firewall_id",
linode_cloud_firewall,
]
+ COMMAND_JSON_OUTPUT
)
instance_json = json.loads(output)[0]
yield instance_json
delete_target_id(target="linodes", id=str(instance_json["id"]))
@pytest.mark.skipif(platform == "win32", reason="Test N/A on Windows")
def test_help():
output = exec_test_command(BASE_CMDS["ssh"] + ["--help"])
assert "[USERNAME@]LABEL" in output
assert "uses the Linode's SLAAC address for SSH" in output
@pytest.mark.skipif(platform == "win32", reason="Test N/A on Windows")
def test_ssh_instance_provisioning(target_instance: Dict[str, Any]):
output = exec_failing_test_command(
BASE_CMDS["ssh"] + ["root@" + target_instance["label"]], expected_code=2
)
assert "is not running" in output
@pytest.mark.smoke
@pytest.mark.skipif(platform == "win32", reason="Test N/A on Windows")
def test_ssh_instance_ready(
ssh_key_pair_generator, target_instance: Dict[str, Any]
):
pubkey, privkey = ssh_key_pair_generator
instance_data = {}
def instance_poll_func():
nonlocal instance_data
output = exec_test_command(
BASE_CMDS["linodes"]
+ ["view", str(target_instance["id"])]
+ COMMAND_JSON_OUTPUT
)
instance_data = json.loads(output)[0]
return instance_data["status"] == "running"
def ssh_poll_func():
output = exec_test_command(
BASE_CMDS["ssh"]
+ [
"root@" + instance_data["label"],
"-o",
"StrictHostKeyChecking=no",
"-o",
"IdentitiesOnly=yes",
"-i",
privkey,
"echo 'hello world!'",
]
)
return "hello world!" in output
# Wait for the instance to be ready
wait_for_condition(
POLL_INTERVAL, INSTANCE_WAIT_TIMEOUT_SECONDS, instance_poll_func
)
assert instance_data["status"] == "running"
# Wait for SSH to be available
wait_for_condition(POLL_INTERVAL, SSH_WAIT_TIMEOUT_SECONDS, ssh_poll_func)