Skip to content

Commit 23b93cc

Browse files
authored
Add integration test for 3.13 base images (#2485)
* Add integration test for 3.13 base images * Add fallback to binary base64 URL decode * Python 3.13 adds some mime types that dataurl cannot recognise. * To support this, we detect if an error occurs during decoding and attempt to fix the URL so it is a generic binary decode.
1 parent 67d6e43 commit 23b93cc

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

pkg/util/files/files.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import (
1616
"github.com/replicate/cog/pkg/util/mime"
1717
)
1818

19+
var (
20+
ErrorFailedToSplitDataURL = errors.New("Failed to split data URL into 2 parts")
21+
)
22+
1923
func Exists(path string) (bool, error) {
2024
if _, err := os.Stat(path); err == nil {
2125
return true, nil
@@ -96,7 +100,17 @@ func WriteDataURLToFile(url string, destination string) (string, error) {
96100
}
97101
dataurlObj, err := dataurl.DecodeString(url)
98102
if err != nil {
99-
return "", fmt.Errorf("Failed to decode data URL: %w", err)
103+
// Attempt to fallback to binary base64 file decode.
104+
parts := strings.SplitN(url, ",", 2)
105+
if len(parts) != 2 {
106+
return "", ErrorFailedToSplitDataURL
107+
}
108+
base64Data := parts[1]
109+
url = "data:;base64," + base64Data
110+
dataurlObj, err = dataurl.DecodeString(url)
111+
if err != nil {
112+
return "", fmt.Errorf("Failed to decode data URL: %w", err)
113+
}
100114
}
101115
output := dataurlObj.Data
102116

pkg/util/files/files_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,10 @@ func TestWriteBadlyFormattedBase64DataURI(t *testing.T) {
2525
_, err := WriteDataURLToFile("data:None;base64,SGVsbG8gVGhlcmU=", path)
2626
require.NoError(t, err)
2727
}
28+
29+
func TestWriteNotRecognisedBase64DataURL(t *testing.T) {
30+
dir := t.TempDir()
31+
path := filepath.Join(dir, "test-file")
32+
_, err := WriteDataURLToFile("data:None;model/gltf-binary,SGVsbG8gVGhlcmU=", path)
33+
require.NoError(t, err)
34+
}

test-integration/test_integration/test_build.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,14 @@ def test_torch_271_cuda_128_base_image(docker_image, cog_binary):
595595
capture_output=True,
596596
)
597597
assert build_process.returncode == 0
598+
599+
600+
def test_python_313_base_images(docker_image, cog_binary):
601+
project_dir = Path(__file__).parent / "fixtures/python-313"
602+
603+
build_process = subprocess.run(
604+
[cog_binary, "build", "-t", docker_image, "--use-cog-base-image"],
605+
cwd=project_dir,
606+
capture_output=True,
607+
)
608+
assert build_process.returncode == 0

0 commit comments

Comments
 (0)