Skip to content

Commit b872da9

Browse files
authored
feat(worker): auto pull images if not found (#200)
This commit provides users with the ability to pull the docker images if they are not found locally.
1 parent 28e0095 commit b872da9

File tree

7 files changed

+988
-46
lines changed

7 files changed

+988
-46
lines changed

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ require (
1010
github.com/getkin/kin-openapi v0.128.0
1111
github.com/go-chi/chi/v5 v5.1.0
1212
github.com/oapi-codegen/runtime v1.1.1
13+
github.com/opencontainers/image-spec v1.1.0
1314
github.com/stretchr/testify v1.9.0
1415
github.com/vincent-petithory/dataurl v1.0.0
1516
)
1617

1718
require (
19+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
1820
github.com/Microsoft/go-winio v0.6.2 // indirect
1921
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
2022
github.com/containerd/log v0.1.0 // indirect
@@ -36,7 +38,6 @@ require (
3638
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
3739
github.com/morikuni/aec v1.0.0 // indirect
3840
github.com/opencontainers/go-digest v1.0.0 // indirect
39-
github.com/opencontainers/image-spec v1.1.0 // indirect
4041
github.com/perimeterx/marshmallow v1.1.5 // indirect
4142
github.com/pkg/errors v0.9.1 // indirect
4243
github.com/pmezard/go-difflib v1.0.0 // indirect

go.sum

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
2-
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
1+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
2+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
33
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
44
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
55
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
@@ -10,6 +10,8 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3
1010
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
1111
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
1212
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
13+
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
14+
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
1315
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1416
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1517
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -134,6 +136,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
134136
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
135137
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
136138
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
139+
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
137140
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
138141
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
139142
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

worker/b64_test.go

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,75 @@ package worker
33
import (
44
"bytes"
55
"encoding/base64"
6+
"image"
7+
"image/color"
8+
"image/png"
69
"os"
710
"testing"
811

912
"github.com/stretchr/testify/require"
1013
)
1114

1215
func TestReadImageB64DataUrl(t *testing.T) {
13-
// Create a sample PNG image and encode it as a data URL
14-
imgData := []byte{
15-
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG header
16-
// ... (rest of the PNG data)
16+
tests := []struct {
17+
name string
18+
dataURL string
19+
expectError bool
20+
}{
21+
{
22+
name: "Valid PNG Image",
23+
dataURL: func() string {
24+
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
25+
img.Set(0, 0, color.RGBA{255, 0, 0, 255}) // Set a single red pixel
26+
var imgBuf bytes.Buffer
27+
err := png.Encode(&imgBuf, img)
28+
require.NoError(t, err)
29+
30+
return "data:image/png;base64," + base64.StdEncoding.EncodeToString(imgBuf.Bytes())
31+
}(),
32+
expectError: false,
33+
},
34+
{
35+
name: "Unsupported Image Format",
36+
dataURL: "data:image/bmp;base64," + base64.StdEncoding.EncodeToString([]byte{
37+
0x42, 0x4D, // BMP header
38+
// ... (rest of the BMP data)
39+
}),
40+
expectError: true,
41+
},
42+
{
43+
name: "Invalid Data URL",
44+
dataURL: "invalid-data-url",
45+
expectError: true,
46+
},
1747
}
18-
dataURL := "data:image/png;base64," + base64.StdEncoding.EncodeToString(imgData)
1948

20-
var buf bytes.Buffer
21-
err := ReadImageB64DataUrl(dataURL, &buf)
22-
require.NoError(t, err)
23-
require.NotEmpty(t, buf.Bytes())
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
var buf bytes.Buffer
52+
err := ReadImageB64DataUrl(tt.dataURL, &buf)
53+
if tt.expectError {
54+
require.Error(t, err)
55+
} else {
56+
require.NoError(t, err)
57+
require.NotEmpty(t, buf.Bytes())
58+
}
59+
})
60+
}
2461
}
2562

2663
func TestSaveImageB64DataUrl(t *testing.T) {
27-
// Create a sample PNG image and encode it as a data URL
28-
imgData := []byte{
29-
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG header
30-
// ... (rest of the PNG data)
31-
}
32-
dataURL := "data:image/png;base64," + base64.StdEncoding.EncodeToString(imgData)
64+
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
65+
img.Set(0, 0, color.RGBA{255, 0, 0, 255}) // Set a single red pixel
66+
var imgBuf bytes.Buffer
67+
err := png.Encode(&imgBuf, img)
68+
require.NoError(t, err)
69+
dataURL := "data:image/png;base64," + base64.StdEncoding.EncodeToString(imgBuf.Bytes())
3370

3471
outputPath := "test_output.png"
3572
defer os.Remove(outputPath)
3673

37-
err := SaveImageB64DataUrl(dataURL, outputPath)
74+
err = SaveImageB64DataUrl(dataURL, outputPath)
3875
require.NoError(t, err)
3976

4077
// Verify that the file was created and is not empty

worker/container.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ type RunnerContainerConfig struct {
4242
containerTimeout time.Duration
4343
}
4444

45+
// Create global references to functions to allow for mocking in tests.
46+
var runnerWaitUntilReadyFunc = runnerWaitUntilReady
47+
4548
func NewRunnerContainer(ctx context.Context, cfg RunnerContainerConfig, name string) (*RunnerContainer, error) {
4649
// Ensure that timeout is set to a non-zero value.
4750
timeout := cfg.containerTimeout
@@ -66,7 +69,7 @@ func NewRunnerContainer(ctx context.Context, cfg RunnerContainerConfig, name str
6669

6770
cctx, cancel := context.WithTimeout(ctx, cfg.containerTimeout)
6871
defer cancel()
69-
if err := runnerWaitUntilReady(cctx, client, pollingInterval); err != nil {
72+
if err := runnerWaitUntilReadyFunc(cctx, client, pollingInterval); err != nil {
7073
return nil, err
7174
}
7275

0 commit comments

Comments
 (0)