@@ -1625,3 +1625,109 @@ def test_oss_reuse_preserves_user_tags(self):
16251625 assert user_tag in tags
16261626 source_tag = {"key" : "sagemaker.amazonaws.com/model-source" , "value" : "s3://bucket/path/" }
16271627 assert source_tag in tags
1628+
1629+
1630+ # ── _extract_tar_gz_to_s3 ───────────────────────────────────────────────────
1631+
1632+
1633+ def _tar_gz_bytes (members ):
1634+ """Build an in-memory tar.gz from (name, content) pairs."""
1635+ import io
1636+ import tarfile
1637+
1638+ buf = io .BytesIO ()
1639+ with tarfile .open (fileobj = buf , mode = "w:gz" ) as tar :
1640+ for name , content in members :
1641+ info = tarfile .TarInfo (name = name )
1642+ info .size = len (content )
1643+ tar .addfile (info , io .BytesIO (content ))
1644+ return buf .getvalue ()
1645+
1646+
1647+ def _extract_with_members (members ):
1648+ """Run _extract_tar_gz_to_s3 against a fake S3 client and return put_object keys."""
1649+ import io
1650+
1651+ s3_client = Mock ()
1652+ s3_client .list_objects_v2 .return_value = {"KeyCount" : 0 }
1653+ s3_client .get_object .return_value = {"Body" : io .BytesIO (_tar_gz_bytes (members ))}
1654+ b = object .__new__ (BedrockModelBuilder )
1655+ b .boto_session = Mock ()
1656+ b .boto_session .client .return_value = s3_client
1657+
1658+ uri = b ._extract_tar_gz_to_s3 ("s3://bucket/jobs/run/output/model.tar.gz" )
1659+
1660+ assert uri == "s3://bucket/jobs/run/output/extracted/"
1661+ return {c .kwargs ["Key" ]: c .kwargs ["Body" ] for c in s3_client .put_object .call_args_list }
1662+
1663+
1664+ class TestTarMemberRelativeKey :
1665+ @pytest .mark .parametrize (
1666+ "name,expected" ,
1667+ [
1668+ ("config.json" , "config.json" ),
1669+ ("checkpoints/hf/config.json" , "checkpoints/hf/config.json" ),
1670+ ("./config.json" , "config.json" ),
1671+ ("./checkpoints//hf/./config.json" , "checkpoints/hf/config.json" ),
1672+ ("model..safetensors" , "model..safetensors" ),
1673+ ],
1674+ )
1675+ def test_normalizes_relative_names (self , name , expected ):
1676+ from sagemaker .serve .bedrock_model_builder import _tar_member_relative_key
1677+
1678+ assert _tar_member_relative_key (name ) == expected
1679+
1680+ @pytest .mark .parametrize (
1681+ "name" ,
1682+ [
1683+ "" ,
1684+ "." ,
1685+ "./" ,
1686+ "/config.json" ,
1687+ "\\ config.json" ,
1688+ "C:/config.json" ,
1689+ "../config.json" ,
1690+ "a/../../config.json" ,
1691+ "a\\ ..\\ config.json" ,
1692+ ],
1693+ )
1694+ def test_rejects_non_relative_names (self , name ):
1695+ from sagemaker .serve .bedrock_model_builder import _tar_member_relative_key
1696+
1697+ assert _tar_member_relative_key (name ) is None
1698+
1699+
1700+ class TestExtractTarGzToS3 :
1701+ def test_uploads_members_under_extract_prefix (self ):
1702+ keys = _extract_with_members (
1703+ [("config.json" , b"{}" ), ("./checkpoints/hf/model.safetensors" , b"weights" )]
1704+ )
1705+
1706+ assert keys == {
1707+ "jobs/run/output/extracted/config.json" : b"{}" ,
1708+ "jobs/run/output/extracted/checkpoints/hf/model.safetensors" : b"weights" ,
1709+ }
1710+
1711+ def test_skips_members_with_unsupported_paths (self ):
1712+ keys = _extract_with_members (
1713+ [("config.json" , b"{}" ), ("../other/file.txt" , b"x" ), ("/abs/file.txt" , b"y" )]
1714+ )
1715+
1716+ assert keys == {"jobs/run/output/extracted/config.json" : b"{}" }
1717+
1718+ def test_raises_when_no_members_extracted (self ):
1719+ with pytest .raises (RuntimeError , match = "No files found" ):
1720+ _extract_with_members ([("../file.txt" , b"x" )])
1721+
1722+ def test_skips_extraction_when_prefix_exists (self ):
1723+ s3_client = Mock ()
1724+ s3_client .list_objects_v2 .return_value = {"KeyCount" : 1 }
1725+ b = object .__new__ (BedrockModelBuilder )
1726+ b .boto_session = Mock ()
1727+ b .boto_session .client .return_value = s3_client
1728+
1729+ uri = b ._extract_tar_gz_to_s3 ("s3://bucket/jobs/model.tar.gz" )
1730+
1731+ assert uri == "s3://bucket/jobs/extracted/"
1732+ s3_client .get_object .assert_not_called ()
1733+ s3_client .put_object .assert_not_called ()
0 commit comments