Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ usage: cog-safe-push [-h] [--config CONFIG] [--help-config]
[--fuzz-iterations FUZZ_ITERATIONS]
[--fuzz-prompt FUZZ_PROMPT] [--parallel PARALLEL]
[--ignore-schema-compatibility] [-v]
[--push-official-model]
[--push-official-model] [--validate-config]
[model]

Safely push a Cog model, with tests
Expand Down Expand Up @@ -175,6 +175,9 @@ options:
-v, --verbose Increase verbosity level (max 3)
--push-official-model
Push to the official model defined in the config
--validate-config Load and validate the config, then exit without
testing or pushing anything. Needs no
REPLICATE_API_TOKEN, cog, or network access.
```

### Using a configuration file
Expand Down Expand Up @@ -278,6 +281,10 @@ deployment:

The deployment will be created under the specified owner (or model owner if not specified) and will use the hardware configuration you provide.

`deployment.name` is the short name only. A name that includes the owner, such as `replicate/my-model-deployment`, is rejected when the config loads. Set `deployment.owner` instead.

To check a config in CI without testing or pushing, run `cog-safe-push --config cog-safe-push.yaml --validate-config`. It needs no `REPLICATE_API_TOKEN`, `cog`, or network access.

## Nota bene

* This is alpha software. If you find a bug, please open an issue!
13 changes: 12 additions & 1 deletion cog_safe_push/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import sys
from typing import Optional

from pydantic import BaseModel, ConfigDict, model_validator
from pydantic import BaseModel, ConfigDict, field_validator, model_validator

from .exceptions import ArgumentError

Expand Down Expand Up @@ -84,6 +84,17 @@ class DeploymentConfig(BaseModel):
name: str | None = None
hardware: str | None = None

@field_validator("name")
@classmethod
def name_must_not_include_owner(cls, name: str | None) -> str | None:
if name is not None and "/" in name:
owner, _, short_name = name.partition("/")
raise ValueError(
f'deployment.name must not include the owner ("{name}"); '
f"set deployment.owner: {owner} and deployment.name: {short_name}"
)
return name


class Config(BaseModel):
model_config = ConfigDict(extra="forbid")
Expand Down
9 changes: 9 additions & 0 deletions cog_safe_push/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ def parse_args_and_config() -> tuple[Config, bool, bool]:
help="Push to the official model defined in the config",
action="store_true",
)
parser.add_argument(
"--validate-config",
help="Load and validate the config, then exit without testing or pushing anything. Needs no REPLICATE_API_TOKEN, cog, or network access.",
action="store_true",
)
args = parser.parse_args()

if args.verbose > 3:
Expand Down Expand Up @@ -207,6 +212,10 @@ def parse_args_and_config() -> tuple[Config, bool, bool]:
if not config.test_model:
config.test_model = config.model + "-test"

if args.validate_config:
print(f"OK: {config_path or 'command line'} is a valid config")
sys.exit(0)

return config, args.no_push, args.push_official_model


Expand Down
76 changes: 76 additions & 0 deletions test/test_main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import pytest

from cog_safe_push import log
from cog_safe_push import main as main_module
from cog_safe_push.exceptions import ArgumentError, TestCaseFailedError
from cog_safe_push.main import (
main,
parse_args_and_config,
parse_input_value,
parse_inputs,
Expand Down Expand Up @@ -523,3 +525,77 @@ async def run(self):
assert "[3] Test case failed: Test case failed: Invalid input" in error_output
# Should not have prediction URL in the third error
assert "Invalid input; Prediction URL:" not in error_output


def write_config(tmp_path, deployment_name):
config_path = tmp_path / "cog-safe-push.yaml"
config_path.write_text(
f"""
model: user/model
deployment:
owner: replicate
name: {deployment_name}
"""
)
return config_path


def test_config_rejects_owner_prefixed_deployment_name(tmp_path, monkeypatch):
config_path = write_config(tmp_path, "replicate/foo")
monkeypatch.setattr("sys.argv", ["cog-safe-push", "--config", str(config_path)])
with pytest.raises(ArgumentError, match="deployment.name must not include"):
parse_args_and_config()


def test_config_accepts_plain_deployment_name(tmp_path, monkeypatch):
config_path = write_config(tmp_path, "foo")
monkeypatch.setattr("sys.argv", ["cog-safe-push", "--config", str(config_path)])
config, _, _ = parse_args_and_config()
assert config.deployment is not None
assert config.deployment.name == "foo"
assert config.deployment.owner == "replicate"


def test_validate_config_valid(tmp_path, monkeypatch, capsys):
config_path = write_config(tmp_path, "foo")
monkeypatch.delenv("REPLICATE_API_TOKEN", raising=False)
monkeypatch.setattr(
"sys.argv", ["cog-safe-push", "--config", str(config_path), "--validate-config"]
)
with pytest.raises(SystemExit) as exc_info:
parse_args_and_config()
assert exc_info.value.code == 0
assert "OK" in capsys.readouterr().out


def test_validate_config_invalid(tmp_path, monkeypatch, capsys):
config_path = write_config(tmp_path, "replicate/foo")
monkeypatch.delenv("REPLICATE_API_TOKEN", raising=False)
monkeypatch.setattr(
"sys.argv", ["cog-safe-push", "--config", str(config_path), "--validate-config"]
)
with pytest.raises(SystemExit) as exc_info:
main()
assert exc_info.value.code == 1
err = capsys.readouterr().err
assert "deployment.name must not include the owner" in err
assert "deployment.owner: replicate and deployment.name: foo" in err


def test_validate_config_makes_no_network_calls(tmp_path, monkeypatch):
import socket

def fail(*_args, **_kwargs):
raise AssertionError("network used")

monkeypatch.setattr(socket, "socket", fail)
monkeypatch.setattr(socket, "create_connection", fail)
monkeypatch.setattr(main_module, "run_config", fail)
config_path = write_config(tmp_path, "foo")
monkeypatch.delenv("REPLICATE_API_TOKEN", raising=False)
monkeypatch.setattr(
"sys.argv", ["cog-safe-push", "--config", str(config_path), "--validate-config"]
)
with pytest.raises(SystemExit) as exc_info:
main()
assert exc_info.value.code == 0
Loading