Debian Codegraph @

test/verify/check-examples

raw
1 #!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv)
2
3 import os
4
5 import testlib
6
7 EXAMPLES_DIR = os.path.join(os.path.dirname(testlib.TEST_DIR), "examples")
8
9
10 @testlib.skipBeiboot("no local overrides/config in beiboot mode")
11 @testlib.nondestructive
12 class TestPinger(testlib.MachineCase):
13
14 def setUp(self):
15 super().setUp()
16 self.restore_dir("/home/admin")
17 self.machine.execute("mkdir -p ~admin/.local/share/cockpit")
18 self.machine.upload([os.path.join(EXAMPLES_DIR, "pinger")], "/home/admin/.local/share/cockpit/")
19
20 def testBasic(self):
21 b = self.browser
22
23 self.login_and_go("/pinger/ping")
24 b.set_val("#address", "127.0.0.1")
25 b.click("button#ping")
26 b.wait_in_text("#result", "success")
27 b.wait_in_text("#output", "--- 127.0.0.1 ping statistics")
28
29 def testExtend(self):
30 """
31 This is example test should set summary
32
33 and it also setup description text
34 """
35 self.testBasic()
36
37
38 @testlib.skipBeiboot("no local overrides/config in beiboot mode")
39 @testlib.nondestructive
40 class TestXHRProxy(testlib.MachineCase):
41 def setUp(self):
42 super().setUp()
43 self.restore_dir("/home/admin")
44 self.machine.execute("mkdir -p ~admin/.local/share/cockpit")
45 self.machine.upload([os.path.join(EXAMPLES_DIR, "xhr-proxy")], "/home/admin/.local/share/cockpit/")
46
47 @testlib.skipOstree("No Python installed")
48 def testBasic(self):
49 m = self.machine
50 b = self.browser
51
52 # set up served directory
53 httpdir = self.vm_tmpdir + "/root"
54 m.write(f"{httpdir}/hello.txt", "world\n")
55 # start webserver
56 pid = m.spawn(f"cd {httpdir}; exec python3 -m http.server 12345", "httpserver")
57 self.addCleanup(m.execute, "kill %i" % pid)
58 self.machine.wait_for_cockpit_running(port=12345) # wait for changelog HTTP server to start up
59
60 self.login_and_go("/xhr-proxy/xhrproxy")
61
62 # directory index
63 b.set_val("#address", "http://localhost:12345/")
64 b.click("#get")
65 b.wait_text("#result", "200")
66 b.wait_in_text("#output", "Directory listing")
67 b.wait_in_text("#output", "hello.txt")
68
69 # specific file
70 b.set_val("#address", "http://localhost:12345/hello.txt")
71 b.click("#get")
72 b.wait_text("#result", "200")
73 b.wait_text("#output", "world\n")
74
75 # nonexisting path
76 b.set_val("#address", "http://localhost:12345/nosuchfile")
77 b.click("#get")
78 b.wait_text("#result", "404")
79 b.wait_in_text("#output", "File not found")
80
81
82 @testlib.skipBeiboot("no local overrides/config in beiboot mode")
83 @testlib.nondestructive
84 class TestLongRunning(testlib.MachineCase):
85
86 def setUp(self):
87 super().setUp()
88 m = self.machine
89 self.restore_dir("/home/admin")
90 m.execute("mkdir -p ~admin/.local/share/cockpit")
91 m.upload([os.path.join(EXAMPLES_DIR, "long-running-process")], "/home/admin/.local/share/cockpit/")
92 # clean up after test failures
93 self.addCleanup(m.execute, "systemctl stop cockpit-longrunning.service 2>/dev/null && systemctl reset-failed cockpit-longrunning.service || true")
94
95 # sometimes fails with: unexpected failure of GetUnit(cockpit-longrunning.service): Not permitted to perform this action
96 def testBasic(self):
97 b = self.browser
98 m = self.machine
99
100 self.login_and_go("/long-running-process")
101
102 self.assertEqual(m.execute("systemctl is-active cockpit-longrunning.service || true").strip(), "inactive")
103 b.wait_text("#state", "cockpit-longrunning.service stopped")
104 b.wait_text("#output", "")
105
106 # run a command that the test can control synchronously
107 ack_file = self.vm_tmpdir + "/ack_a"
108 b.set_val("#command", f"date; echo STEP_A; until [ -e {ack_file} ]; do sleep 1; done; echo STEP_B; sleep 1; echo DONE")
109 b.wait_text("button#run", "Start")
110 b.click("button#run")
111
112 b.wait_text("#state", "cockpit-longrunning.service running")
113 b.wait_in_text("#output", "\nSTEP_A\n")
114 self.assertNotIn("\nSTEP_B", b.text("#output"))
115 self.assertEqual(m.execute("systemctl is-active cockpit-longrunning.service || true").strip(), "activating")
116
117 # reattaches in new session
118 b.logout()
119 b.login_and_go("/long-running-process")
120 b.wait_text("#state", "cockpit-longrunning.service running")
121 b.wait_text("button#run", "Terminate")
122 b.wait_in_text("#output", "\nSTEP_A\n")
123 self.assertEqual(m.execute("systemctl is-active cockpit-longrunning.service || true").strip(), "activating")
124
125 # resume process
126 m.execute(f"mkdir -p {self.vm_tmpdir}; touch {ack_file}")
127 b.wait_in_text("#output", "\nSTEP_B\n")
128 # wait for completion
129 b.wait_in_text("#output", "\nDONE\n")
130 b.wait_text("#state", "cockpit-longrunning.service stopped")
131 self.assertEqual(m.execute("systemctl is-active cockpit-longrunning.service || true").strip(), "inactive")
132
133 # in next session it is back at "not running"
134 b.logout()
135 b.login_and_go("/long-running-process")
136 b.wait_text("#state", "cockpit-longrunning.service stopped")
137 b.wait_text("#output", "")
138 b.wait_text("button#run", "Start")
139
140 # failing process
141 m.execute("rm -f " + ack_file)
142 b.set_val("#command", f"date; echo BREAK_A; until [ -e {ack_file} ]; do sleep 1; done; false; echo NOTME")
143 b.click("button#run")
144 b.wait_text("#state", "cockpit-longrunning.service running")
145 b.wait_in_text("#output", "\nBREAK_A\n")
146 m.execute("touch " + ack_file)
147 b.wait_text("#state", "cockpit-longrunning.service failed")
148 b.wait_in_text("#output", "cockpit-longrunning.service: Main process exited, code=exited, status=1/FAILURE")
149 out = b.text("#output")
150 self.assertNotIn("\nNOTME", out)
151 # does not contain previous logs
152 self.assertNotIn("STEP_B", out)
153 b.wait_text("button#run", "Reset")
154
155 # failing state gets picked up on page reconnect
156 b.logout()
157 b.login_and_go("/long-running-process")
158 b.wait_text("#state", "cockpit-longrunning.service failed")
159 b.wait_in_text("#output", "cockpit-longrunning.service: Main process exited, code=exited, status=1/FAILURE")
160 out = b.text("#output")
161 self.assertIn("\nBREAK_A\n", out)
162 self.assertNotIn("\nNOTME", out)
163 b.wait_text("button#run", "Reset")
164
165 # reset
166 b.click("button#run")
167 b.wait_text("#state", "cockpit-longrunning.service stopped")
168
169 # cancel long-running command
170 b.set_val("#command", "for i in $(seq 100); do echo $FOOBAR$i; sleep 1; done")
171 # ensure we can set systemd-run arguments
172 b.set_val("#sysrun-args", '--setenv=FOOBAR=LONG')
173 b.wait_text("button#run", "Start")
174 b.click("button#run")
175 b.wait_text("#state", "cockpit-longrunning.service running")
176 b.wait_text("button#run", "Terminate")
177 b.wait_in_text("#output", "\nLONG2\n")
178 b.click("button#run")
179 # terminates cleanly
180 b.wait_text("#state", "cockpit-longrunning.service stopped")
181 self.assertEqual(m.execute("systemctl is-active cockpit-longrunning.service || true").strip(), "inactive")
182 b.wait_in_text("#output", "\nLONG2\n")
183 self.assertNotIn("\nLONG30\n", b.text("#output"))
184
185
186 if __name__ == '__main__':
187 testlib.test_main()