Skip to content

Commit 6e0191e

Browse files
committed
Add e2e pytest test for inspect command
- Migrate to e2e pytest the `test/system/100-inspect.bats` bat test. Signed-off-by: Roberto Majadas <[email protected]>
1 parent 022d799 commit 6e0191e

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

test/e2e/test_inspect.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import json
2+
import re
3+
import sys
4+
from subprocess import STDOUT, CalledProcessError
5+
from test.e2e.utils import RamalamaExecWorkspace
6+
7+
import pytest
8+
9+
GGUF_MODEL = "ollama://tinyllama"
10+
ST_MODEL = "https://huggingface.co/LiheYoung/depth-anything-small-hf/resolve/main/model.safetensors"
11+
12+
13+
@pytest.fixture(scope="module")
14+
def shared_ctx():
15+
with RamalamaExecWorkspace() as ctx:
16+
ctx.check_call(["ramalama", "-q", "pull", GGUF_MODEL])
17+
ctx.check_call(["ramalama", "-q", "pull", ST_MODEL])
18+
yield ctx
19+
20+
21+
@pytest.mark.e2e
22+
def test_inspect_non_existent_model(shared_ctx):
23+
ctx = shared_ctx
24+
model_name = "non_existent_model_for_inspect"
25+
with pytest.raises(CalledProcessError) as exc_info:
26+
ctx.check_output(["ramalama", "inspect", model_name], stderr=STDOUT)
27+
assert exc_info.value.returncode == 22
28+
assert f"Error: No ref file found for '{model_name}'. Please pull model." in exc_info.value.output.decode("utf-8")
29+
30+
31+
@pytest.mark.e2e
32+
@pytest.mark.parametrize(
33+
"model_name, use_all_flag, expected_key, expected_value",
34+
[
35+
# GGUF inspect (no --all)
36+
pytest.param(GGUF_MODEL, False, ["Name"], "tinyllama", id="gguf_inspect_name"),
37+
pytest.param(GGUF_MODEL, False, ["Registry"], "ollama", id="gguf_inspect_registry"),
38+
pytest.param(GGUF_MODEL, False, ["Format"], "GGUF", id="gguf_inspect_format"),
39+
pytest.param(GGUF_MODEL, False, ["Version"], "3", id="gguf_inspect_version"),
40+
pytest.param(
41+
GGUF_MODEL, False, ["Endianness"], "0" if sys.byteorder == "little" else "1", id="gguf_inspect_endianness"
42+
),
43+
pytest.param(GGUF_MODEL, False, ["Metadata"], "23", id="gguf_inspect_metadata_count"),
44+
pytest.param(GGUF_MODEL, False, ["Tensors"], "201", id="gguf_inspect_tensors_count"),
45+
pytest.param(
46+
GGUF_MODEL,
47+
False,
48+
["Path"],
49+
r".*store[\\/]+ollama[\\/]+library[\\/]+tinyllama.*",
50+
id="gguf_inspect_path",
51+
),
52+
# Safetensors inspect (no --all)
53+
pytest.param(ST_MODEL, False, ["Name"], "model.safetensors", id="safetensors_inspect_name"),
54+
pytest.param(ST_MODEL, False, ["Registry"], "https", id="safetensors_inspect_registry"),
55+
pytest.param(ST_MODEL, False, ["Metadata"], "288", id="safetensors_inspect_metadata_count"),
56+
# GGUF inspect --all
57+
pytest.param(GGUF_MODEL, True, ["Name"], "tinyllama", id="gguf_inspect_all_name"),
58+
pytest.param(GGUF_MODEL, True, ["Registry"], "ollama", id="gguf_inspect_all_registry"),
59+
pytest.param(GGUF_MODEL, True, ["Format"], "GGUF", id="gguf_inspect_all_format"),
60+
pytest.param(GGUF_MODEL, True, ["Version"], "3", id="gguf_inspect_all_version"),
61+
pytest.param(
62+
GGUF_MODEL,
63+
True,
64+
["Endianness"],
65+
"0" if sys.byteorder == "little" else "1",
66+
id="gguf_inspect_all_endianness",
67+
),
68+
pytest.param(
69+
GGUF_MODEL, True, ["Metadata", "data", "general.architecture"], "llama", id="gguf_inspect_all_meta_arch"
70+
),
71+
# Safetensors inspect --all
72+
pytest.param(ST_MODEL, True, ["Name"], "model.safetensors", id="safetensors_inspect_all_name"),
73+
pytest.param(ST_MODEL, True, ["Registry"], "https", id="safetensors_inspect_all_registry"),
74+
pytest.param(
75+
ST_MODEL, True, ["Header", "__metadata__", "format"], "pt", id="safetensors_inspect_all_header_format"
76+
),
77+
],
78+
)
79+
def test_inspect_model_json_output(shared_ctx, model_name, use_all_flag, expected_key, expected_value):
80+
ctx = shared_ctx
81+
result = ctx.check_output(["ramalama", "inspect", "--json"] + (["--all"] if use_all_flag else []) + [model_name])
82+
data = json.loads(result)
83+
84+
value = data
85+
for k in expected_key:
86+
value = value[k]
87+
88+
assert re.match(expected_value, str(value))
89+
90+
91+
@pytest.mark.e2e
92+
@pytest.mark.parametrize(
93+
"key, expected_value",
94+
[
95+
pytest.param("general.architecture", "llama", id="general.architecture"),
96+
pytest.param("general.file_type", "2", id="general.file_type"),
97+
pytest.param("general.name", "TinyLlama", id="general.name"),
98+
pytest.param("general.quantization_version", "2", id="general.quantization_version"),
99+
pytest.param("llama.attention.head_count", "32", id="llama.attention.head_count"),
100+
pytest.param("llama.attention.head_count_kv", "4", id="llama.attention.head_count_kv"),
101+
pytest.param(
102+
"llama.attention.layer_norm_rms_epsilon",
103+
"9.999999747378752e-06",
104+
id="llama.attention.layer_norm_rms_epsilon",
105+
),
106+
pytest.param("llama.block_count", "22", id="llama.block_count"),
107+
pytest.param("llama.context_length", "2048", id="llama.context_length"),
108+
pytest.param("llama.embedding_length", "2048", id="llama.embedding_length"),
109+
pytest.param("llama.feed_forward_length", "5632", id="llama.feed_forward_length"),
110+
pytest.param("llama.rope.dimension_count", "64", id="llama.rope.dimension_count"),
111+
pytest.param("llama.rope.freq_base", "10000.0", id="llama.rope.freq_base"),
112+
pytest.param("tokenizer.ggml.bos_token_id", "1", id="tokenizer.ggml.bos_token_id"),
113+
pytest.param("tokenizer.ggml.eos_token_id", "2", id="tokenizer.ggml.eos_token_id"),
114+
pytest.param("tokenizer.ggml.model", "llama", id="tokenizer.ggml.model"),
115+
pytest.param("tokenizer.ggml.padding_token_id", "2", id="tokenizer.ggml.padding_token_id"),
116+
pytest.param("tokenizer.ggml.unknown_token_id", "0", id="tokenizer.ggml.unknown_token_id"),
117+
],
118+
)
119+
def test_inspect_gguf_model_with_get(shared_ctx, key, expected_value):
120+
ctx = shared_ctx
121+
122+
output = ctx.check_output(["ramalama", "inspect", "--get", key, GGUF_MODEL])
123+
assert output.strip() == expected_value
124+
125+
output_all = ctx.check_output(["ramalama", "inspect", "--get", "all", GGUF_MODEL])
126+
assert f"{key}: {expected_value}" in output_all

0 commit comments

Comments
 (0)