From b5b27df801eaeb7a01f8eb435be2c68e215f0a5c Mon Sep 17 00:00:00 2001 From: gchqdev227 <62302861+gchqdev227@users.noreply.github.com> Date: Wed, 15 Apr 2026 12:03:00 +0100 Subject: [PATCH 1/2] InOnlyTests in test_additional.py now uses a different fake resource that doesn't reach out to the internet --- tests/test_additional.py | 50 ++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/tests/test_additional.py b/tests/test_additional.py index ca88e05..dfa0980 100644 --- a/tests/test_additional.py +++ b/tests/test_additional.py @@ -1,13 +1,9 @@ # (C) Crown Copyright GCHQ from __future__ import annotations -from http.client import HTTPResponse import json from pathlib import Path -import ssl -from typing import cast from unittest import TestCase -import urllib.request from concoursetools import BuildMetadata, ConcourseResource from concoursetools.additional import (DatetimeVersion, InOnlyConcourseResource, MultiVersionConcourseResource, OutOnlyConcourseResource, @@ -203,24 +199,16 @@ def test_resource_download(self) -> None: self.assertDictEqual(directory_state.final_state, {"repos.json": expected_payload}) -class ImageDownloadResource(InOnlyConcourseResource): +class FileDownloadResource(InOnlyConcourseResource): - def __init__(self, image_url: str) -> None: - super().__init__() - self.image_url = image_url + def download_data(self, destination_dir: Path, build_metadata: BuildMetadata, name: str = "file.txt") -> Metadata: + file_path = destination_dir / name + file_path.write_text("Hello, world!") - def download_data(self, destination_dir: Path, build_metadata: BuildMetadata, name: str = "image") -> Metadata: - image_path = destination_dir / name - request = urllib.request.Request(url=self.image_url) - - ssl_context = ssl._create_unverified_context() - with urllib.request.urlopen(request, context=ssl_context) as response: - response = cast(HTTPResponse, response) - with open(image_path, "wb") as wf: - wf.write(response.read()) + print(f"Writing file {name!r} for job {build_metadata.BUILD_NAME}") metadata = { - "HTTP Status": str(response.status), + "HTTP Status": "200", } return metadata @@ -229,16 +217,34 @@ class InOnlyTests(TestCase): """ Tests for the InOnlyConcourseResource class. """ + def test_check_versions(self) -> None: + resource = FileDownloadResource() + wrapper = SimpleTestResourceWrapper(resource) + self.assertListEqual(wrapper.fetch_new_versions(), []) + self.assertListEqual(wrapper.fetch_new_versions(DatetimeVersion.now()), []) + def test_data_download(self) -> None: - resource = ImageDownloadResource(image_url="https://www.gchq.gov.uk/files/favicon.ico") + resource = FileDownloadResource() wrapper = SimpleTestResourceWrapper(resource) + version, publish_metadata = wrapper.publish_new_version() + self.assertDictEqual(publish_metadata, {}) + with wrapper.capture_debugging() as debugging: with wrapper.capture_directory_state() as directory_state: - wrapper.download_version(DatetimeVersion.now(), name="favicon.ico") + wrapper.download_version(version) - self.assertEqual(debugging, "") + self.assertEqual(debugging, "Writing file 'file.txt' for job 42\n") + self.assertDictEqual(directory_state.final_state, { + "file.txt": "Hello, world!", + }) + + with wrapper.capture_debugging() as debugging: + with wrapper.capture_directory_state() as directory_state: + wrapper.download_version(version, name="other.txt") + + self.assertEqual(debugging, "Writing file 'other.txt' for job 42\n") self.assertDictEqual(directory_state.final_state, { - "favicon.ico": b"\x00\x00\x01\x00\x01\x00\x00\x00\x00\x00\x01\x00 \x00w%", + "other.txt": "Hello, world!", }) From a79dd42429fa7e0ec432c8374c21f32d6fa940c9 Mon Sep 17 00:00:00 2001 From: gchqdev227 <62302861+gchqdev227@users.noreply.github.com> Date: Wed, 15 Apr 2026 17:17:01 +0100 Subject: [PATCH 2/2] Added extra assertion to InOnlyTests.test_data_download to check that the test BUILD_NAME is as we expect in the debugging output --- tests/test_additional.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_additional.py b/tests/test_additional.py index dfa0980..b970dcd 100644 --- a/tests/test_additional.py +++ b/tests/test_additional.py @@ -226,6 +226,8 @@ def test_check_versions(self) -> None: def test_data_download(self) -> None: resource = FileDownloadResource() wrapper = SimpleTestResourceWrapper(resource) + self.assertEqual(wrapper.mocked_build_metadata.BUILD_NAME, "42") + version, publish_metadata = wrapper.publish_new_version() self.assertDictEqual(publish_metadata, {})