Skip to content

Commit

Permalink
feat(worker): auto pull images if not found (#200)
Browse files Browse the repository at this point in the history
This commit provides users with the ability to pull the docker images if they are not found locally.
  • Loading branch information
rickstaa authored Nov 28, 2024
1 parent 28e0095 commit b872da9
Show file tree
Hide file tree
Showing 7 changed files with 988 additions and 46 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ require (
github.com/getkin/kin-openapi v0.128.0
github.com/go-chi/chi/v5 v5.1.0
github.com/oapi-codegen/runtime v1.1.1
github.com/opencontainers/image-spec v1.1.0
github.com/stretchr/testify v1.9.0
github.com/vincent-petithory/dataurl v1.0.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/containerd/log v0.1.0 // indirect
Expand All @@ -36,7 +38,6 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8=
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
Expand All @@ -10,6 +10,8 @@ github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK3
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY=
github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -134,6 +136,7 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
69 changes: 53 additions & 16 deletions worker/b64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,75 @@ package worker
import (
"bytes"
"encoding/base64"
"image"
"image/color"
"image/png"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestReadImageB64DataUrl(t *testing.T) {
// Create a sample PNG image and encode it as a data URL
imgData := []byte{
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, // PNG header
// ... (rest of the PNG data)
tests := []struct {
name string
dataURL string
expectError bool
}{
{
name: "Valid PNG Image",
dataURL: func() string {
img := image.NewRGBA(image.Rect(0, 0, 1, 1))
img.Set(0, 0, color.RGBA{255, 0, 0, 255}) // Set a single red pixel
var imgBuf bytes.Buffer
err := png.Encode(&imgBuf, img)
require.NoError(t, err)

return "data:image/png;base64," + base64.StdEncoding.EncodeToString(imgBuf.Bytes())
}(),
expectError: false,
},
{
name: "Unsupported Image Format",
dataURL: "data:image/bmp;base64," + base64.StdEncoding.EncodeToString([]byte{
0x42, 0x4D, // BMP header
// ... (rest of the BMP data)
}),
expectError: true,
},
{
name: "Invalid Data URL",
dataURL: "invalid-data-url",
expectError: true,
},
}
dataURL := "data:image/png;base64," + base64.StdEncoding.EncodeToString(imgData)

var buf bytes.Buffer
err := ReadImageB64DataUrl(dataURL, &buf)
require.NoError(t, err)
require.NotEmpty(t, buf.Bytes())
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
err := ReadImageB64DataUrl(tt.dataURL, &buf)
if tt.expectError {
require.Error(t, err)
} else {
require.NoError(t, err)
require.NotEmpty(t, buf.Bytes())
}
})
}
}

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

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

err := SaveImageB64DataUrl(dataURL, outputPath)
err = SaveImageB64DataUrl(dataURL, outputPath)
require.NoError(t, err)

// Verify that the file was created and is not empty
Expand Down
5 changes: 4 additions & 1 deletion worker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ type RunnerContainerConfig struct {
containerTimeout time.Duration
}

// Create global references to functions to allow for mocking in tests.
var runnerWaitUntilReadyFunc = runnerWaitUntilReady

func NewRunnerContainer(ctx context.Context, cfg RunnerContainerConfig, name string) (*RunnerContainer, error) {
// Ensure that timeout is set to a non-zero value.
timeout := cfg.containerTimeout
Expand All @@ -66,7 +69,7 @@ func NewRunnerContainer(ctx context.Context, cfg RunnerContainerConfig, name str

cctx, cancel := context.WithTimeout(ctx, cfg.containerTimeout)
defer cancel()
if err := runnerWaitUntilReady(cctx, client, pollingInterval); err != nil {
if err := runnerWaitUntilReadyFunc(cctx, client, pollingInterval); err != nil {
return nil, err
}

Expand Down
Loading

0 comments on commit b872da9

Please sign in to comment.