| 1 |
#!/usr/bin/python3 -cimport os, sys; os.execv(os.path.dirname(sys.argv[1]) + "/../common/pywrap", sys.argv) |
| 2 |
|
| 3 |
# |
| 4 |
# Copyright (C) 2020 Red Hat, Inc. |
| 5 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
|
| 7 |
|
| 8 |
import os |
| 9 |
import re |
| 10 |
import subprocess |
| 11 |
import unittest |
| 12 |
|
| 13 |
import testlib |
| 14 |
|
| 15 |
dirname = os.path.dirname(__file__) |
| 16 |
run_tests = os.path.join(testlib.TEST_DIR, "common", "run-tests") |
| 17 |
EXAMPLE_DIR = os.path.join(testlib.TEST_DIR, "example") |
| 18 |
VERIFY_DIR = os.path.join(testlib.TEST_DIR, "verify") |
| 19 |
ROOT_DIR = os.path.dirname(testlib.TEST_DIR) |
| 20 |
|
| 21 |
|
| 22 |
class TestRunTestListing(unittest.TestCase): |
| 23 |
|
| 24 |
def testBasic(self): |
| 25 |
# Listing on check-* file |
| 26 |
self.assertEqual(subprocess.check_output([os.path.join(EXAMPLE_DIR, "check-example"), "-l", "TestNondestructiveExample"]).strip().decode(), |
| 27 |
"TestNondestructiveExample.testOne\nTestNondestructiveExample.testTwo") |
| 28 |
# Filter on class |
| 29 |
p = subprocess.run([run_tests, "--test-dir", EXAMPLE_DIR, "-l", "TestNondestructiveExample"], capture_output=True, check=True) |
| 30 |
self.assertIn(b"TestNondestructiveExample.testOne\nTestNondestructiveExample.testTwo", p.stdout.strip()) |
| 31 |
# Filter a specific test |
| 32 |
self.assertIn("TestNondestructiveExample.testOne", |
| 33 |
subprocess.check_output([run_tests, "--test-dir", EXAMPLE_DIR, "-l", "TestNondestructiveExample.testOne"]).strip().decode()) |
| 34 |
# Exclude test patterns |
| 35 |
out = subprocess.check_output([run_tests, "--test-dir", EXAMPLE_DIR, "-l", |
| 36 |
"--exclude", "bogus", "--exclude", "TestNondestructiveExample.testTwo", |
| 37 |
"TestNondestructiveExample"]).strip().decode() |
| 38 |
self.assertIn("TestNondestructiveExample.testOne", out) |
| 39 |
self.assertNotIn("testTwo", out) |
| 40 |
|
| 41 |
ndtests = subprocess.run([run_tests, "--test-dir", EXAMPLE_DIR, "-n", "-l"], check=True, capture_output=True) |
| 42 |
self.assertIn(b"TestExample.testNondestructive\n", ndtests.stdout) |
| 43 |
self.assertIn(b"TestNondestructiveExample.testOne\nTestNondestructiveExample.testTwo", ndtests.stdout) |
| 44 |
|
| 45 |
# nondestructive tests are sorted alphabetically |
| 46 |
verify_ndtests = subprocess.run([run_tests, "--test-dir", VERIFY_DIR, "-n", "-l"], |
| 47 |
check=True, capture_output=True) |
| 48 |
self.assertRegex( |
| 49 |
verify_ndtests.stdout, |
| 50 |
re.compile(rb".*TestAccounts.*TestFirewall.*TestLogin.*TestServices.*TestTerminal.*", re.S)) |
| 51 |
|
| 52 |
def testNonDestructive(self): |
| 53 |
self.assertEqual(subprocess.check_output([run_tests, "--test-dir", EXAMPLE_DIR, "--nondestructive", "-l", "TestExample"]).strip().decode(), |
| 54 |
"TestExample.testNondestructive") |
| 55 |
|
| 56 |
# with short option and substring |
| 57 |
self.assertEqual(subprocess.check_output([run_tests, "--test-dir", EXAMPLE_DIR, "-nl", "TestExamp"]).strip().decode(), |
| 58 |
"TestExample.testNondestructive") |
| 59 |
|
| 60 |
|
| 61 |
# This can't be @nondestructive, as we run our own @nondestructive test nested inside this test. This will already call the |
| 62 |
# cleanup handlers, and the outside cleanup would then fail to do that again. |
| 63 |
@testlib.onlyImage("Only run on the required image", "fedora-coreos") |
| 64 |
class TestRunTest(testlib.MachineCase): |
| 65 |
def testExistingMachine(self): |
| 66 |
env = os.environ.copy() |
| 67 |
try: |
| 68 |
del env["TEST_JOBS"] |
| 69 |
except KeyError: |
| 70 |
pass |
| 71 |
out = subprocess.check_output([run_tests, "--test-dir", EXAMPLE_DIR, "-vt", |
| 72 |
"--machine", self.machine.ssh_address + ":" + self.machine.ssh_port, |
| 73 |
"--browser", self.machine.web_address + ":" + self.machine.web_port, |
| 74 |
"TestNondestructiveExample"], env=env) |
| 75 |
self.assertRegex(out, b"\nok .* TestNondestructiveExample.testOne") |
| 76 |
self.assertRegex(out, b"\nok .* TestNondestructiveExample.testTwo") |
| 77 |
self.assertIn(b"TESTS PASSED", out) |
| 78 |
|
| 79 |
# implies --nondestructive |
| 80 |
out = subprocess.check_output([run_tests, "--test-dir", EXAMPLE_DIR, "--machine", "1.2.3.4:56", "--browser", "1.2.3.4:67", |
| 81 |
"TestExample.testBasic"], env=env) |
| 82 |
self.assertIn(b"1..0", out) |
| 83 |
self.assertNotIn(b"TestExample", out) |
| 84 |
|
| 85 |
def testRetry(self): |
| 86 |
# Don't test this if not in git repo as we use `git diff` for this logic |
| 87 |
if not os.path.exists(os.path.join(ROOT_DIR, ".git")): |
| 88 |
return |
| 89 |
|
| 90 |
env = os.environ.copy() |
| 91 |
env["TEST_FAILURES"] = "1" |
| 92 |
# pretend this was a PR against main (set by CI normally, or by the user with --base) |
| 93 |
env["BASE_BRANCH"] = "main" |
| 94 |
try: |
| 95 |
del env["TEST_JOBS"] |
| 96 |
except KeyError: |
| 97 |
pass |
| 98 |
|
| 99 |
# Check that we retry 3 times failing tests |
| 100 |
process = subprocess.run([run_tests, "--test-dir", EXAMPLE_DIR, "TestExample.testFail", "TestExample.testSkip"], |
| 101 |
env=env, capture_output=True) |
| 102 |
stdout = process.stdout |
| 103 |
self.assertRegex(stdout, rb"\nnot ok 1 .*test\/example\/check-example TestExample.testFail \(1\.7GB\) # RETRY 1 \(be robust against unstable tests\)\n") |
| 104 |
self.assertRegex(stdout, rb"\nnot ok 1 .*test\/example\/check-example TestExample.testFail \(1\.7GB\) # RETRY 2 \(be robust against unstable tests\)\n") |
| 105 |
self.assertRegex(stdout, rb"\nnot ok 1 .*test\/example\/check-example TestExample.testFail \(1\.7GB\)\n") |
| 106 |
self.assertNotRegex(stdout, b"RETRY 3") |
| 107 |
self.assertRegex(stdout, rb"\nok 2 .*test\/example\/check-example TestExample.testSkip \(1\.7GB\) # SKIP testSkip \(__main__\.TestExample") |
| 108 |
self.assertRegex(stdout, rb"# 1 TESTS FAILED \[\d*s on .*, 2 destructive tests, 0 nondestructive tests: \]") |
| 109 |
|
| 110 |
# Check retry logic for changed tests |
| 111 |
test_file = os.path.join(EXAMPLE_DIR, "check-retry") |
| 112 |
with open(test_file, 'r') as f: |
| 113 |
original_test = f.read() |
| 114 |
|
| 115 |
def write_file(file, content, mode=0o666): |
| 116 |
# test files need to be executable, respect umask |
| 117 |
content_bin = content.encode() |
| 118 |
fd = os.open(file, os.O_CREAT | os.O_TRUNC | os.O_WRONLY, mode=mode) |
| 119 |
try: |
| 120 |
written = os.write(fd, content_bin) |
| 121 |
assert written == len(content_bin) |
| 122 |
finally: |
| 123 |
os.close(fd) |
| 124 |
|
| 125 |
self.addCleanup(write_file, test_file, original_test) |
| 126 |
write_file(test_file, original_test.replace("class NoTest", "class Test")) |
| 127 |
|
| 128 |
process = subprocess.run([run_tests, "--test-dir", EXAMPLE_DIR, "TestRetryExample.testFail", "TestRetryExample.testBasic", |
| 129 |
"TestRetryExample.testNoRetry"], env=env, capture_output=True) |
| 130 |
stdout = process.stdout |
| 131 |
|
| 132 |
# Changed test need to succeed 3 times |
| 133 |
self.assertRegex(stdout, rb"\nok 1 .*test\/example\/check-retry TestRetryExample.testBasic \(1\.7GB\) # RETRY 1 \(test affected tests 3 times\)\n") |
| 134 |
self.assertRegex(stdout, rb"\nok 1 .*test\/example\/check-retry TestRetryExample.testBasic \(1\.7GB\) # RETRY 2 \(test affected tests 3 times\)\n") |
| 135 |
self.assertRegex(stdout, rb"\nok 1 .*test\/example\/check-retry TestRetryExample.testBasic \(1\.7GB\)\n") |
| 136 |
self.assertNotRegex(stdout, b"RETRY 3") |
| 137 |
|
| 138 |
# Changed test is never retried |
| 139 |
self.assertRegex(stdout, rb"\nnot ok 2 .*test\/example\/check-retry TestRetryExample.testFail \(1\.7GB\)\n") |
| 140 |
self.assertNotRegex(stdout, b"testFail # RETRY") |
| 141 |
|
| 142 |
# Using @no_retry_when_changed prevents this retry logic |
| 143 |
self.assertRegex(stdout, rb"\nok 3 .*test\/example\/check-retry TestRetryExample.testNoRetry \(1\.7GB\)\n") |
| 144 |
self.assertNotRegex(stdout, b"testNoRetry # RETRY") |
| 145 |
|
| 146 |
# Check retry logic for changed source |
| 147 |
shell = os.path.join("pkg", "shell", "hosts.tsx") |
| 148 |
self.assertTrue(os.path.exists(shell)) |
| 149 |
|
| 150 |
with open(shell, 'r') as f: |
| 151 |
original_source = f.read() |
| 152 |
|
| 153 |
self.addCleanup(write_file, shell, original_source) |
| 154 |
write_file(shell, original_source + "\n") |
| 155 |
|
| 156 |
# create affected test for shell changes |
| 157 |
affected_testfile = os.path.join(EXAMPLE_DIR, "check-shell-testlib") |
| 158 |
new_content = original_test.replace("class NoTest", "class NeinTest") |
| 159 |
write_file(affected_testfile, new_content.replace("class NeinTestRetryExample", "class TestShell"), mode=0o777) |
| 160 |
self.addCleanup(os.unlink, affected_testfile) |
| 161 |
|
| 162 |
process = subprocess.run([run_tests, "--test-dir", EXAMPLE_DIR, |
| 163 |
"TestShell.testBasic", "TestShell.testNoRetry"], |
| 164 |
env=env, capture_output=True) |
| 165 |
stdout = process.stdout |
| 166 |
self.assertRegex(stdout, rb"\nok .* TestShell.testBasic \(1\.7GB\) # RETRY 1 \(test affected tests 3 times\)\n") |
| 167 |
self.assertRegex(stdout, rb"\nok .* TestShell.testBasic \(1\.7GB\) # RETRY 2 \(test affected tests 3 times\)\n") |
| 168 |
self.assertRegex(stdout, rb"\nok .* TestShell.testBasic \(1\.7GB\)\n") |
| 169 |
self.assertNotRegex(stdout, b"RETRY 3") |
| 170 |
self.assertRegex(stdout, b"\nok .* TestShell.testNoRetry \\(1\\.7GB\\)\n") |
| 171 |
self.assertNotRegex(stdout, b"TestShell.testNoRetry # RETRY") |
| 172 |
|
| 173 |
# Check that just .css changes do no affect retry logic |
| 174 |
systemd = os.path.join("pkg", "systemd", "overview.scss") |
| 175 |
self.assertTrue(os.path.exists(systemd)) |
| 176 |
|
| 177 |
with open(systemd, 'r') as f: |
| 178 |
original_source = f.read() |
| 179 |
|
| 180 |
self.addCleanup(write_file, systemd, original_source) |
| 181 |
write_file(systemd, original_source + "\n") |
| 182 |
|
| 183 |
# create affected test for systemd changes |
| 184 |
affected_testfile = os.path.join(EXAMPLE_DIR, "check-system-testlib") |
| 185 |
write_file(affected_testfile, """#!/usr/bin/env python3 |
| 186 |
import unittest |
| 187 |
class TestSystemd(unittest.TestCase): |
| 188 |
def testBasic(self): |
| 189 |
self.assertTrue(True) |
| 190 |
""", mode=0o777) |
| 191 |
self.addCleanup(os.unlink, affected_testfile) |
| 192 |
|
| 193 |
process = subprocess.run([run_tests, "--test-dir", EXAMPLE_DIR, "TestSystemd.testBasic"], |
| 194 |
env=env, capture_output=True) |
| 195 |
stdout = process.stdout |
| 196 |
self.assertRegex(stdout, rb"\nok .* TestSystemd.testBasic \(1\.7GB\)\n") |
| 197 |
self.assertNotRegex(stdout, b"RETRY") |
| 198 |
|
| 199 |
@testlib.nondestructive |
| 200 |
def testTodo(self): |
| 201 |
env = os.environ.copy() |
| 202 |
try: |
| 203 |
del env["TEST_JOBS"] |
| 204 |
except KeyError: |
| 205 |
pass |
| 206 |
env["TEST_TODO"] = "1" |
| 207 |
|
| 208 |
process = subprocess.run([run_tests, "--test-dir", EXAMPLE_DIR, "TestTodo"], |
| 209 |
env=env, capture_output=True) |
| 210 |
stdout = process.stdout |
| 211 |
|
| 212 |
# A @todo test which fails should write 'not ok ... # TODO ...' |
| 213 |
self.assertRegex(stdout, rb"\nnot ok . .*test/example/check-example TestTodo.testTodoFail \(1\.7GB\) # TODO 2 is not yet sufficiently large\n") |
| 214 |
|
| 215 |
# A @todo test which passes should write 'not ok ... # expected failure ...' and hard fail |
| 216 |
self.assertRegex(stdout, rb"\nnot ok . .*test/example/check-example TestTodo.testTodoPass \(1\.7GB\) # expected failure: 2 is not yet sufficiently large\n") |
| 217 |
|
| 218 |
# There should have been 2 cases, one fail |
| 219 |
self.assertEqual(process.returncode, 1) |
| 220 |
|
| 221 |
|
| 222 |
@testlib.nondestructive |
| 223 |
class TestTestlib(testlib.MachineCase): |
| 224 |
def testRestoreAPI(self): |
| 225 |
m = self.machine |
| 226 |
|
| 227 |
self.assertEqual(m.execute("whoami").strip(), "root") |
| 228 |
# existing file |
| 229 |
m.execute("echo original > /etc/someconfig") |
| 230 |
self.restore_file("/etc/someconfig") |
| 231 |
m.execute("echo changed > /etc/someconfig") |
| 232 |
# nonexisting file |
| 233 |
self.restore_file("/var/lib/cockpittest.txt") |
| 234 |
m.execute("echo data > /var/lib/cockpittest.txt") |
| 235 |
|
| 236 |
# existing dir |
| 237 |
m.execute("mkdir -p /var/lib/existing_dir; echo hello > /var/lib/existing_dir/original") |
| 238 |
self.restore_dir("/var/lib/existing_dir") |
| 239 |
m.execute("rm /var/lib/existing_dir/original; echo pwned > /var/lib/existing_dir/new") |
| 240 |
# nonexisting dir |
| 241 |
self.restore_dir("/var/lib/cockpittestnew") |
| 242 |
m.execute("mkdir -p /var/lib/cockpittestnew; echo hello > /var/lib/cockpittestnew/cruft") |
| 243 |
|
| 244 |
# NSS is backed up by default |
| 245 |
m.execute("useradd cockpittest") |
| 246 |
|
| 247 |
# now pretend the test ends here |
| 248 |
self.doCleanups() |
| 249 |
|
| 250 |
# correctly restored existing file |
| 251 |
self.assertEqual("original", m.execute("cat /etc/someconfig").strip()) |
| 252 |
m.execute("rm /etc/someconfig") |
| 253 |
# correctly cleaned up nonexisting file |
| 254 |
m.execute("test ! -e /var/lib/cockpittest.txt") |
| 255 |
|
| 256 |
# correctly restored existing dir |
| 257 |
self.assertEqual("original", m.execute("ls /var/lib/existing_dir").strip()) |
| 258 |
self.assertEqual("hello\n", m.execute("cat /var/lib/existing_dir/original")) |
| 259 |
m.execute("rm -r /var/lib/existing_dir") |
| 260 |
# correctly removed nonexisting dir |
| 261 |
m.execute("test ! -e /var/lib/cockpittestnew") |
| 262 |
|
| 263 |
# NSS/home got restored |
| 264 |
self.assertNotIn("cockpittest", self.machine.execute("cat /etc/passwd")) |
| 265 |
self.machine.execute("test ! -e /home/cockpittest") |
| 266 |
|
| 267 |
@testlib.skipBeiboot("no cockpit-system package installed in beiboot mode") |
| 268 |
def testMiscAPI(self): |
| 269 |
assert self.system_before(1000) |
| 270 |
assert not self.system_before(100) |
| 271 |
|
| 272 |
|
| 273 |
if __name__ == '__main__': |
| 274 |
testlib.test_main() |