|
1 | 1 | """Exercise release guards themselves, not only the eventual command exit code.""" |
2 | 2 | from pathlib import Path |
3 | 3 | import gzip |
| 4 | +import hashlib |
| 5 | +import os |
4 | 6 | import subprocess |
5 | 7 | import tempfile |
6 | 8 | import textwrap |
7 | 9 | import unittest |
8 | 10 |
|
9 | 11 | WORKFLOW = Path(__file__).resolve().parents[1] / '.github/workflows/publish-current-server.yml' |
| 12 | +JAVA_PATCH_GATE = WORKFLOW.parents[2] / 'scripts/check-server-java-patches-jdk25.sh' |
| 13 | +COMMONS_IO_SHA256 = '2b9a7b1f726fb86216dbd2c8321eabe0221dbd5b1be81c18e1cb53811b104758' |
| 14 | +COMMONS_IO_FIXTURE_BYTES = b'fixture Commons IO bytes; not a Java runtime acceptance result\n' |
10 | 15 |
|
11 | 16 |
|
12 | 17 | class PublisherSafetyGuards(unittest.TestCase): |
@@ -140,5 +145,115 @@ def test_collision_never_arms_cleanup_or_removes_existing_objects(self): |
140 | 145 | self.assertEqual(calls[2:], ['rm -f candidate', 'volume rm cattle mysql mysqllog']) |
141 | 146 |
|
142 | 147 |
|
| 148 | +class CandidateCommonsIoGuards(unittest.TestCase): |
| 149 | + """Exercise only dependency input routing; Java checks are not stubbed as PASS.""" |
| 150 | + |
| 151 | + def dependency_input(self, image='candidate:fixture', local_jdk=False, |
| 152 | + payload=None, docker_status=0, use_candidate=True): |
| 153 | + source = JAVA_PATCH_GATE.read_text() |
| 154 | + boundary = 'cat > "$tmp/src/io/github/ibuildthecloud/gdapi/model/Filter.java"' |
| 155 | + self.assertEqual(source.count(boundary), 1) |
| 156 | + self.assertEqual(source.count('commons_io_sha256=' + COMMONS_IO_SHA256), 1) |
| 157 | + self.assertIn('commons_io_version=2.22.0', source) |
| 158 | + prefix = source.split(boundary, 1)[0] |
| 159 | + # Tiny deterministic fixture bytes keep this test offline on a fresh CI runner. |
| 160 | + # The actual hardcoded release hash is asserted above; use real sha256sum below. |
| 161 | + # Local qualification can supply cached release bytes without changing the prefix. |
| 162 | + prefix = prefix.replace(COMMONS_IO_SHA256, hashlib.sha256(COMMONS_IO_FIXTURE_BYTES).hexdigest()) |
| 163 | + with tempfile.TemporaryDirectory() as temporary: |
| 164 | + root = Path(temporary) |
| 165 | + (root / 'scripts').mkdir() |
| 166 | + (root / 'server/patches').mkdir(parents=True) |
| 167 | + (root / 'server/patches/PatchV1GlobalSubscribe.java').write_text('class PatchV1GlobalSubscribe {}\n') |
| 168 | + for name in ('Dockerfile', 'Dockerfile.auth-hotfix'): |
| 169 | + (root / 'server' / name).write_text( |
| 170 | + 'javac -cp "/usr/share/cattle/war/WEB-INF/lib/*" PatchV1GlobalSubscribe.java\n') |
| 171 | + (root / 'payload').write_bytes(COMMONS_IO_FIXTURE_BYTES if payload is None else payload) |
| 172 | + jdk = root / 'jdk' |
| 173 | + (jdk / 'bin').mkdir(parents=True) |
| 174 | + for name in ('java', 'javac', 'javap'): |
| 175 | + path = jdk / 'bin' / name |
| 176 | + path.write_text('#!/bin/sh\nexit 97\n') |
| 177 | + path.chmod(0o755) |
| 178 | + environment = os.environ.copy() |
| 179 | + for name in ('RC16_JDK25_CHECK_IMAGE', 'RC16_JDK25_COMMONS_IO_IMAGE', 'RC16_JDK25_HOME'): |
| 180 | + environment.pop(name, None) |
| 181 | + environment.update(RC16_JDK25_CHECK_IMAGE='candidate:fixture', FIXTURE_ROOT=temporary, |
| 182 | + FIXTURE_DOCKER_STATUS=str(docker_status)) |
| 183 | + if use_candidate: |
| 184 | + environment['RC16_JDK25_COMMONS_IO_IMAGE'] = image |
| 185 | + if local_jdk: |
| 186 | + environment['RC16_JDK25_HOME'] = str(jdk) |
| 187 | + stubs = r'''docker() { |
| 188 | + printf 'docker' >>"$FIXTURE_ROOT/calls" |
| 189 | + printf '\t%s' "$@" >>"$FIXTURE_ROOT/calls" |
| 190 | + printf '\n' >>"$FIXTURE_ROOT/calls" |
| 191 | + if [ "$FIXTURE_DOCKER_STATUS" -ne 0 ]; then return "$FIXTURE_DOCKER_STATUS"; fi |
| 192 | + cat "$FIXTURE_ROOT/payload" |
| 193 | +} |
| 194 | +curl() { |
| 195 | + printf 'curl' >>"$FIXTURE_ROOT/calls" |
| 196 | + printf '\t%s' "$@" >>"$FIXTURE_ROOT/calls" |
| 197 | + printf '\n' >>"$FIXTURE_ROOT/calls" |
| 198 | + local output= |
| 199 | + while [ "$#" -gt 0 ]; do |
| 200 | + if [ "$1" = -o ]; then output=$2; shift; fi |
| 201 | + shift |
| 202 | + done |
| 203 | + test -n "$output" |
| 204 | + cat "$FIXTURE_ROOT/payload" >"$output" |
| 205 | +} |
| 206 | +''' |
| 207 | + result = subprocess.run(['bash', '-c', stubs + prefix + '\necho DEPENDENCY_INPUT_READY\n', |
| 208 | + str(root / 'scripts/check-server-java-patches-jdk25.sh')], |
| 209 | + env=environment, capture_output=True, text=True) |
| 210 | + calls = (root / 'calls').read_text().splitlines() if (root / 'calls').exists() else [] |
| 211 | + return result, [line.split('\t') for line in calls] |
| 212 | + |
| 213 | + def test_candidate_commons_io_uses_same_image_offline_and_checks_bytes(self): |
| 214 | + result, calls = self.dependency_input() |
| 215 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 216 | + self.assertIn('DEPENDENCY_INPUT_READY', result.stdout) |
| 217 | + self.assertEqual(calls, [['docker', 'run', '--rm', '--pull=never', '--network', 'none', |
| 218 | + '--entrypoint', '/bin/cat', 'candidate:fixture', |
| 219 | + '/usr/share/cattle/war/WEB-INF/lib/commons-io-2.22.0.jar']]) |
| 220 | + |
| 221 | + def test_candidate_commons_io_missing_or_wrong_bytes_never_fall_back(self): |
| 222 | + for payload, status in ((b'', 0), (b'wrong bytes', 0), (None, 1)): |
| 223 | + with self.subTest(payload=payload, status=status): |
| 224 | + result, calls = self.dependency_input(payload=payload, docker_status=status) |
| 225 | + self.assertNotEqual(result.returncode, 0) |
| 226 | + self.assertNotIn('DEPENDENCY_INPUT_READY', result.stdout) |
| 227 | + self.assertEqual(len(calls), 1) |
| 228 | + self.assertEqual(calls[0][0], 'docker') |
| 229 | + if status == 0: |
| 230 | + self.assertIn('FAILED', result.stdout) |
| 231 | + |
| 232 | + def test_candidate_commons_io_rejects_wrong_empty_or_local_jdk_image_before_io(self): |
| 233 | + for image, local_jdk in (('different:fixture', False), ('', False), ('candidate:fixture', True)): |
| 234 | + with self.subTest(image=image, local_jdk=local_jdk): |
| 235 | + result, calls = self.dependency_input(image=image, local_jdk=local_jdk) |
| 236 | + self.assertNotEqual(result.returncode, 0) |
| 237 | + self.assertIn('JDK25_COMMONS_IO_IMAGE_INVALID', result.stderr) |
| 238 | + self.assertEqual(calls, []) |
| 239 | + |
| 240 | + def test_candidate_commons_io_default_jdk_and_local_modes_keep_pinned_curl(self): |
| 241 | + for local_jdk in (False, True): |
| 242 | + with self.subTest(local_jdk=local_jdk): |
| 243 | + result, calls = self.dependency_input(local_jdk=local_jdk, use_candidate=False) |
| 244 | + self.assertEqual(result.returncode, 0, result.stderr) |
| 245 | + self.assertEqual(len(calls), 1) |
| 246 | + self.assertEqual(calls[0][0:2], ['curl', '-fsSL']) |
| 247 | + self.assertEqual(calls[0][-1], 'https://repo.maven.apache.org/maven2/commons-io/commons-io/2.22.0/commons-io-2.22.0.jar') |
| 248 | + self.assertIn('--retry-all-errors', calls[0]) |
| 249 | + self.assertIn('DEPENDENCY_INPUT_READY', result.stdout) |
| 250 | + |
| 251 | + def test_candidate_commons_io_publisher_binds_the_actual_check_image(self): |
| 252 | + source = WORKFLOW.read_text() |
| 253 | + binding = ('RC16_JDK25_COMMONS_IO_IMAGE="$CANDIDATE_IMAGE" \\\n' |
| 254 | + ' RC16_JDK25_CHECK_IMAGE="$CANDIDATE_IMAGE" bash scripts/check-server-java-patches-jdk25.sh') |
| 255 | + self.assertEqual(source.count(binding), 1) |
| 256 | + |
| 257 | + |
143 | 258 | if __name__ == '__main__': |
144 | 259 | unittest.main() |