Skip to content

Commit

Permalink
Mock the registry to test skipping
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Caillé committed Aug 16, 2024
1 parent 3ca79b3 commit a02c2fa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
1 change: 1 addition & 0 deletions kraken-build/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ types-requests = "^2.28.0"
pytest-xdist = {version = "^3.5.0", extras = ["psutil"]}
mitmproxy = "^10.2.4"
types-networkx = "^3.2.1.20240703"
requests-mock = "^1.12.1"

# Slap configuration
# ------------------
Expand Down
4 changes: 2 additions & 2 deletions kraken-build/src/kraken/std/cargo/tasks/cargo_publish_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ def prepare(self) -> TaskStatus | None:
return self._check_package_existence(package_name, version, self.registry.get())
except Exception as e:
logger.warn(
f"An error happened while checking for {package_name} existence in {self.registry.get().alias}",
e,
f"An error happened while checking for {package_name} existence in {self.registry.get().alias}, {e}",
)
return TaskStatus.pending("Unable to verify package existence")

Expand Down Expand Up @@ -159,6 +158,7 @@ def _check_package_existence(cls, package_name: str, version: str, registry: Car
if not registry.index.startswith("sparse+"):
return TaskStatus.pending("Unable to verify package existence - Only sparse registries are supported")
index = registry.index.removeprefix("sparse+")
index = index.removesuffix("/")

# >> Index authentication
session = requests.sessions.Session()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,10 @@ class CargoRepositoryWithAuth:
creds: tuple[str, str] | None
token: str | None


def publish_lib_and_build_app(repository: CargoRepositoryWithAuth, tempdir: Path) -> None:
# Copy the Cargo project files to a temporary directory.
for item in ["cargo-hello-world-lib", "cargo-hello-world-app"]:
shutil.copytree(example_dir(item), tempdir / item)

app_dir = tempdir.joinpath("cargo-hello-world-app")
def skip_publish_lib(repository: CargoRepositoryWithAuth, tempdir: Path) -> None:
lib_dir = tempdir.joinpath("cargo-hello-world-lib")

shutil.copytree(example_dir("cargo-hello-world-lib"), lib_dir)
cargo_registry_id = "private-repo"
publish_version = ".".join(str(random.randint(0, 999)) for _ in range(3))
logger.info("==== Publish version is %s", publish_version)

with unittest.mock.patch.dict(os.environ, {"CARGO_HOME": str(tempdir)}):
# Build the library and publish it to the registry.
Expand All @@ -70,16 +62,38 @@ def publish_lib_and_build_app(repository: CargoRepositoryWithAuth, tempdir: Path
task = cargo_sync_config()
task.git_fetch_with_cli.set(True)
cargo_check_toolchain_version(minimal_version="1.60")
cargo_publish(
publish_task = cargo_publish(
cargo_registry_id,
version=publish_version,
version='0.1.0',
cargo_toml_file=project1.directory.joinpath("Cargo.toml"),
)
project1.context.execute(["fmt", "lint", "publish"])
graph = project1.context.execute(["publish"])
status = graph.get_status(publish_task)
assert status is not None and status.is_skipped()


def publish_lib_and_build_app(repository: CargoRepositoryWithAuth, tempdir: Path) -> None:
# Copy the Cargo project files to a temporary directory.
for item in ["cargo-hello-world-lib", "cargo-hello-world-app"]:
shutil.copytree(example_dir(item), tempdir / item)

app_dir = tempdir.joinpath("cargo-hello-world-app")
lib_dir = tempdir.joinpath("cargo-hello-world-lib")

# Try to publish the same version again - the task should be skipped and should not fail
with kraken_ctx() as ctx, kraken_project(ctx) as project2:
project2.directory = lib_dir
cargo_registry_id = "private-repo"
publish_version = ".".join(str(random.randint(0, 999)) for _ in range(3))
logger.info("==== Publish version is %s", publish_version)

with unittest.mock.patch.dict(os.environ, {"CARGO_HOME": str(tempdir)}):
# Build the library and publish it to the registry.
logger.info(
"Publishing cargo-hello-world-lib to Cargo repository %r (%r)",
repository.name,
repository.index_url,
)

with kraken_ctx() as ctx, kraken_project(ctx) as project1:
project1.directory = lib_dir
cargo_registry(
cargo_registry_id,
repository.index_url,
Expand All @@ -90,14 +104,12 @@ def publish_lib_and_build_app(repository: CargoRepositoryWithAuth, tempdir: Path
task = cargo_sync_config()
task.git_fetch_with_cli.set(True)
cargo_check_toolchain_version(minimal_version="1.60")
public_task = cargo_publish(
cargo_publish(
cargo_registry_id,
version=publish_version,
cargo_toml_file=project1.directory.joinpath("Cargo.toml"),
)
graph = project2.context.execute(["fmt", "lint", "publish"])
status = graph.get_status(public_task)
assert status is not None and status.is_skipped()
project1.context.execute(["fmt", "lint", "publish"])

num_tries = 3
for idx in range(num_tries):
Expand All @@ -108,9 +120,9 @@ def publish_lib_and_build_app(repository: CargoRepositoryWithAuth, tempdir: Path
repository.name,
repository.index_url,
)
with kraken_ctx() as ctx, kraken_project(ctx) as project3:
project3.directory = app_dir
cargo_toml = project3.directory / "Cargo.toml"
with kraken_ctx() as ctx, kraken_project(ctx) as project2:
project2.directory = app_dir
cargo_toml = project2.directory / "Cargo.toml"
cargo_toml.write_text(cargo_toml.read_text().replace("$VERSION", publish_version))
cargo_registry(
cargo_registry_id,
Expand All @@ -122,7 +134,7 @@ def publish_lib_and_build_app(repository: CargoRepositoryWithAuth, tempdir: Path
sync_task.git_fetch_with_cli.set(True)
build_task = cargo_build("debug")
build_task.from_project_dir = True
project3.context.execute(["fmt", "build"])
project2.context.execute(["fmt", "build"])

# Running the application should give "Hello from hello-world-lib!".
output = sp.check_output([app_dir / "target" / "debug" / "hello-world-app"]).decode()
Expand Down Expand Up @@ -173,9 +185,20 @@ def private_registry(docker_service_manager: DockerServiceManager) -> str:
time.sleep(5)
return index_url


def test__private_cargo_registry_publish_and_consume(tempdir: Path, private_registry: str) -> None:
repository = CargoRepositoryWithAuth(
"kraken-std-cargo-integration-test", private_registry, None, "xxxxxxxxxxxxxxxxxxxxxx"
)
publish_lib_and_build_app(repository, tempdir)

def test__mock_cargo_registry_skips_publish_if_exists(tempdir: Path, requests_mock) -> None:
registry_url = "http://0.0.0.0:35510"
index_url = f"sparse+{registry_url}/"

requests_mock.get(f"{registry_url}/config.json", text="{}")
requests_mock.get(f"{registry_url}/he/ll/hello-world-lib", text='{"vers": "0.1.0"}')

repository = CargoRepositoryWithAuth(
"kraken-std-cargo-integration-test", index_url, None, "xxxxxxxxxxxxxxxxxxxxxx"
)
skip_publish_lib(repository, tempdir)

0 comments on commit a02c2fa

Please sign in to comment.