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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
{ pkgs, lib, ... }:
let
user = "alice"; # from ./common/user-account.nix
password = "foobar"; # from ./common/user-account.nix
# A minimal Cockpit plugin that runs `hello` and displays the output
# This tests that the cockpitPath mechanism works correctly
helloPlugin =
pkgs.runCommand "cockpit-hello-test" { } ''
mkdir -p $out/share/cockpit/hello-test
cat > $out/share/cockpit/hello-test/manifest.json << 'EOF'
{
"name": "hello-test",
"menu": {
"index": {
"label": "Hello Test",
"order": 100
}
}
}
EOF
cat > $out/share/cockpit/hello-test/index.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Hello Test</title>
<meta charset="utf-8">
<script src="../base1/cockpit.js"></script>
</head>
<body>
<h1>Hello Test Plugin</h1>
<div id="output">Loading...</div>
<script src="test.js"></script>
</body>
</html>
EOF
cat > $out/share/cockpit/hello-test/test.js << 'EOF'
(function() {
var output = document.getElementById("output");
if (typeof cockpit === "undefined") {
output.innerText = "FAILED: cockpit.js not loaded";
return;
}
cockpit.spawn(["hello"], { err: "message" })
.then(function(data) {
output.innerText = "SUCCESS: " + data;
})
.catch(function(error) {
output.innerText = "FAILED: " + (error ? error.message : "unknown error");
});
})();
EOF
''
// {
passthru.cockpitPath = [ pkgs.hello ];
};
in
{
name = "cockpit";
meta = {
maintainers = with lib.maintainers; [ lucasew ];
};
nodes = {
server =
{ config, ... }:
{
imports = [ ./common/user-account.nix ];
security.polkit.enable = true;
users.users.${user} = {
extraGroups = [ "wheel" ];
};
services.cockpit = {
enable = true;
port = 7890;
openFirewall = true;
plugins = [ helloPlugin ];
allowed-origins = [
"https://server:${toString config.services.cockpit.port}"
];
};
};
client =
{ config, ... }:
{
imports = [ ./common/user-account.nix ];
environment.systemPackages =
let
seleniumScript =
pkgs.writers.writePython3Bin "selenium-script"
{
libraries = with pkgs.python3Packages; [ selenium ];
}
''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def log(msg):
from sys import stderr
print(f"[*] {msg}", file=stderr)
log("Initializing")
options = Options()
options.add_argument("--headless")
service = webdriver.FirefoxService(executable_path="${lib.getExe pkgs.geckodriver}") # noqa: E501
driver = webdriver.Firefox(options=options, service=service)
driver.implicitly_wait(10)
log("Opening homepage")
driver.get("https://server:7890")
def wait_elem(by, query, timeout=10):
wait = WebDriverWait(driver, timeout)
wait.until(EC.presence_of_element_located((by, query)))
def wait_title_contains(title, timeout=30):
wait = WebDriverWait(driver, timeout)
wait.until(EC.title_contains(title))
def find_element(by, query):
return driver.find_element(by, query)
log("Waiting for the homepage to load")
# cockpit sets initial title as hostname
wait_title_contains("server")
wait_elem(By.CSS_SELECTOR, 'input#login-user-input')
log("Homepage loaded!")
# Prefer send_keys over setting .value via JS: Cockpit's login
# form (and PatternFly widgets) do not always react to the latter.
log("Filling out username")
login_input = find_element(By.CSS_SELECTOR, 'input#login-user-input')
login_input.clear()
login_input.send_keys("${user}")
log("Filling out password")
password_input = find_element(
By.CSS_SELECTOR, 'input#login-password-input'
)
password_input.clear()
password_input.send_keys("${password}")
log("Submitting credentials for login")
driver.find_element(By.CSS_SELECTOR, 'button#login-button').click()
log("Waiting dashboard to load")
wait_title_contains("${user}@server")
log("Looking for that banner that tells about limited access")
wait_elem(By.CSS_SELECTOR, 'iframe.container-frame', timeout=30)
container_iframe = find_element(
By.CSS_SELECTOR, 'iframe.container-frame'
)
driver.switch_to.frame(container_iframe)
# Wait for the overview iframe to render the limited-access alert.
# Fixed sleeps were flaky after Cockpit 364 (slower SPA init).
phrase = "Web console is running in limited access mode"
WebDriverWait(driver, 30).until(
lambda d: phrase in d.page_source
)
log("Clicking the sudo button")
# Button label is "Turn on administrative access"
for button in driver.find_elements(By.TAG_NAME, "button"):
if 'admin' in button.text.casefold():
button.click()
break
driver.switch_to.default_content()
log("Checking that /nonexistent is not a thing")
assert '/nonexistent' not in driver.page_source
assert len(driver.find_elements(By.CSS_SELECTOR, '#machine-reconnect')) == 0
log("Checking plugin path")
driver.get("https://server:7890/hello-test")
wait_elem(By.CSS_SELECTOR, 'iframe.container-frame', timeout=30)
for iframe in driver.find_elements(By.TAG_NAME, "iframe"):
if "hello-test" in (iframe.get_attribute("src") or ""):
driver.switch_to.frame(iframe)
break
output = find_element(By.ID, "output")
WebDriverWait(driver, 30).until(
lambda d: "SUCCESS: Hello, world!" in output.text
or output.text.startswith("FAILED:")
)
text = output.text
assert "SUCCESS: Hello, world!" in text, f"Plugin failed: {text}"
driver.close()
'';
in
with pkgs;
[
firefox-unwrapped
geckodriver
seleniumScript
];
};
};
testScript = ''
start_all()
server.wait_for_unit("sockets.target")
server.wait_for_open_port(7890)
# Port open is not enough: cockpit-ws may still be finishing startup.
server.wait_until_succeeds("curl -k https://127.0.0.1:7890 -o /dev/null")
client.wait_until_succeeds("curl -k https://server:7890 -o /dev/stderr")
print(client.succeed("whoami"))
client.succeed('PYTHONUNBUFFERED=1 selenium-script')
'';
}
|